Skip to content

Instantly share code, notes, and snippets.

@glensc
Created March 3, 2016 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glensc/f2f0dfa36d2e369a6493 to your computer and use it in GitHub Desktop.
Save glensc/f2f0dfa36d2e369a6493 to your computer and use it in GitHub Desktop.
check_http chunked
--- nagios-plugins-2.1.1/plugins/check_http.c 2015-07-31 00:40:06.000000000 +0300
+++ monitoring-plugins-2.1.2/plugins/check_http.c 2015-10-16 12:06:18.000000000 +0300
@@ -1,9 +1,9 @@
/*****************************************************************************
*
-* Nagios check_http plugin
+* Monitoring check_http plugin
*
* License: GPL
-* Copyright (c) 1999-2014 Nagios Plugins Development Team
+* Copyright (c) 1999-2013 Monitoring Plugins Development Team
*
* Description:
*
@@ -34,8 +34,8 @@
/* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */
const char *progname = "check_http";
-const char *copyright = "1999-2014";
-const char *email = "devel@nagios-plugins.org";
+const char *copyright = "1999-2013";
+const char *email = "devel@monitoring-plugins.org";
#include "common.h"
#include "netutils.h"
@@ -157,7 +157,7 @@
/* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */
server_url = strdup(HTTP_URL);
server_url_length = strlen(server_url);
- xasprintf (&user_agent, "User-Agent: check_http/v%s (nagios-plugins %s)",
+ xasprintf (&user_agent, "User-Agent: check_http/v%s (monitoring-plugins %s)",
NP_VERSION, VERSION);
/* Parse extra opts if any */
@@ -173,7 +173,7 @@
/* initialize alarm signal handling, set socket timeout, start timer */
(void) signal (SIGALRM, socket_timeout_alarm_handler);
- (void) alarm (timeout_interval);
+ (void) alarm (socket_timeout);
gettimeofday (&tv, NULL);
result = check_http ();
@@ -274,7 +274,10 @@
exit (STATE_OK);
break;
case 't': /* timeout period */
- timeout_interval = parse_timeout_string(optarg);
+ if (!is_intnonneg (optarg))
+ usage2 (_("Timeout interval must be a positive integer"), optarg);
+ else
+ socket_timeout = atoi (optarg);
break;
case 'c': /* critical time threshold */
critical_thresholds = optarg;
@@ -530,8 +533,8 @@
set_thresholds(&thlds, warning_thresholds, critical_thresholds);
- if (critical_thresholds && thlds->critical->end>(double)timeout_interval)
- timeout_interval = (int)thlds->critical->end + 1;
+ if (critical_thresholds && thlds->critical->end>(double)socket_timeout)
+ socket_timeout = (int)thlds->critical->end + 1;
if (http_method == NULL)
http_method = strdup ("GET");
@@ -673,115 +676,6 @@
return result;
}
-char *
-decode_chunked_page (const char *raw, char *dst)
-{
- unsigned long int chunksize;
- char *raw_pos = (char *)raw;
- char *dst_pos = (char *)dst;
- const char *raw_end = raw + strlen(raw);
-
- while (chunksize = strtoul(raw_pos, NULL, 16)) {
- if (chunksize <= 0) {
- die (STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse chunked body, invalid chunk size\n"));
- }
-
- // soak up the optional chunk params (which we will ignore)
- while (*raw_pos && *raw_pos != '\r' && *raw_pos != '\n') raw_pos++;
-
- // soak up the leading CRLF
- if (*raw_pos && *raw_pos == '\r' && *(++raw_pos) && *raw_pos == '\n') {
- raw_pos++;
- }
- else {
- die (STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse chunked body, invalid format\n"));
- }
-
- // move chunk from raw into dst, only if we can fit within the buffer
- if (*raw_pos && *dst_pos && (raw_pos + chunksize) < raw_end ) {
- strncpy(dst_pos, raw_pos, chunksize);
- }
- else {
- die (STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse chunked body, too large for destination\n"));
- }
-
- raw_pos += chunksize;
- dst_pos += chunksize;
-
- // soak up the ending CRLF
- if (*raw_pos && *raw_pos == '\r' && *(++raw_pos) && *raw_pos == '\n') {
- raw_pos++;
- }
- else {
- die (STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse chunked body, invalid format\n"));
- }
- }
-
- if (*dst_pos) *dst_pos = '\0';
- else {
- die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
- }
-
- return dst;
-}
-
-static char *
-header_value (const char *headers, const char *header)
-{
- char *s;
- char *value;
- const char *value_end;
- int value_size;
-
- if (!(s = strcasestr(headers, header))) {
- return NULL;
- }
-
- s += strlen(header);
-
- while (*s && (isspace(*s) || *s == ':')) s++;
- while (*s && isspace(*s)) s++;
-
- value_end = strchr(s, '\r');
- if (!value_end) {
- die (STATE_UNKNOWN, _("HTTP_UNKNOWN - Failed to parse response headers\n"));
- }
-
- value_size = value_end - s;
-
- value = malloc(value_size + 1);
- if (!value) {
- die (STATE_UNKNOWN, _("HTTP_UNKNOWN - Memory allocation error\n"));
- }
-
- if (!strncpy(value, s, value_size)) {
- die(STATE_UNKNOWN, _("HTTP_UNKNOWN - Memory copy failure\n"));
- }
- value[value_size] = '\0';
-
- return value;
-}
-
-static int
-chunked_transfer_encoding (const char *headers)
-{
- int result;
- char *encoding = header_value(headers, "Transfer-Encoding");
- if (!encoding) {
- return 0;
- }
-
- if (! strncmp(encoding, "chunked", sizeof("chunked"))) {
- result = 1;
- }
- else {
- result = 0;
- }
-
- free(encoding);
- return result;
-}
-
static int
check_document_dates (const char *headers, char **msg)
{
@@ -975,7 +869,6 @@
double elapsed_time_transfer = 0.0;
int page_len = 0;
int result = STATE_OK;
- char *force_host_header = NULL;
/* try to connect to the host at the given port number */
gettimeofday (&tv_temp, NULL);
@@ -1005,46 +898,24 @@
/* tell HTTP/1.1 servers not to keep the connection alive */
xasprintf (&buf, "%sConnection: close\r\n", buf);
- /* check if Host header is explicitly set in options */
- if (http_opt_headers_count) {
- for (i = 0; i < http_opt_headers_count ; i++) {
- if (strncmp(http_opt_headers[i], "Host:", 5) == 0) {
- force_host_header = http_opt_headers[i];
- }
- }
- }
-
/* optionally send the host header info */
if (host_name) {
- if (force_host_header) {
- xasprintf (&buf, "%s%s\r\n", buf, force_host_header);
- }
- else {
- /*
- * Specify the port only if we're using a non-default port (see RFC 2616,
- * 14.23). Some server applications/configurations cause trouble if the
- * (default) port is explicitly specified in the "Host:" header line.
- */
- if ((use_ssl == FALSE && server_port == HTTP_PORT) ||
- (use_ssl == TRUE && server_port == HTTPS_PORT))
- xasprintf (&buf, "%sHost: %s\r\n", buf, host_name);
- else
- xasprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
- }
+ /*
+ * Specify the port only if we're using a non-default port (see RFC 2616,
+ * 14.23). Some server applications/configurations cause trouble if the
+ * (default) port is explicitly specified in the "Host:" header line.
+ */
+ if ((use_ssl == FALSE && server_port == HTTP_PORT) ||
+ (use_ssl == TRUE && server_port == HTTPS_PORT))
+ xasprintf (&buf, "%sHost: %s\r\n", buf, host_name);
+ else
+ xasprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
}
- /* Inform server we accept any MIME type response
- * TODO: Take an arguement to determine what type(s) to accept,
- * so that we can alert if a response is of an invalid type.
- */
- xasprintf(&buf, "%sAccept: */*\r\n", buf);
-
/* optionally send any other header tag */
if (http_opt_headers_count) {
for (i = 0; i < http_opt_headers_count ; i++) {
- if (force_host_header != http_opt_headers[i]) {
- xasprintf (&buf, "%s%s\r\n", buf, http_opt_headers[i]);
- }
+ xasprintf (&buf, "%s%s\r\n", buf, http_opt_headers[i]);
}
/* This cannot be free'd here because a redirection will then try to access this and segfault */
/* Covered in a testcase in tests/check_http.t */
@@ -1167,21 +1038,13 @@
page += (size_t) strcspn (page, "\r\n");
pos = page;
if ((strspn (page, "\r") == 1 && strspn (page, "\r\n") >= 2) ||
- (strspn (page, "\n") == 1 && strspn (page, "\r\n") >= 2)) {
+ (strspn (page, "\n") == 1 && strspn (page, "\r\n") >= 2))
page += (size_t) 2;
- pos += (size_t) 2;
- }
- else {
+ else
page += (size_t) 1;
- pos += (size_t) 1;
- }
}
page += (size_t) strspn (page, "\r\n");
header[pos - header] = 0;
-
- if (chunked_transfer_encoding(header) && *page)
- page = decode_chunked_page(page, page);
-
if (verbose)
printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header,
(no_body ? " [[ skipped ]]" : page));
@@ -1256,7 +1119,6 @@
result = max_state_alt(check_document_dates(header, &msg), result);
}
-
/* Page and Header content checks go here */
if (strlen (header_expect)) {
if (!strstr (header, header_expect)) {
@@ -1269,6 +1131,7 @@
}
}
+
if (strlen (string_expect)) {
if (!strstr (page, string_expect)) {
strncpy(&output_string_search[0],string_expect,sizeof(output_string_search));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment