From: Harper Mann Date: Thu, 20 Jan 2005 22:50:09 +0000 (+0000) Subject: Tracker 1099682 fix. Now getting the page size from Content-Length in the header... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0ebaa6c29736d1c0c52a4c66d9f593bda32fe943;p=nagiosplug.git Tracker 1099682 fix. Now getting the page size from Content-Length in the header. Added max value to the -m switch so can do "-m min:max". Retained "-m min" convention. Renamed long arg name to --minmax git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1086 f882894a-f735-0410-b71e-b25c423dba1c --- diff --git a/plugins/check_http.c b/plugins/check_http.c index 6e76706..5328475 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c @@ -114,6 +114,7 @@ int use_ssl = FALSE; int verbose = FALSE; int sd; int min_page_len = 0; +int max_page_len = 0; int redir_depth = 0; int max_depth = 15; char *http_method; @@ -222,7 +223,7 @@ process_arguments (int argc, char **argv) {"no-body", no_argument, 0, 'N'}, {"max-age", required_argument, 0, 'M'}, {"content-type", required_argument, 0, 'T'}, - {"min", required_argument, 0, 'm'}, + {"minmax", required_argument, 0, 'm'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0} @@ -408,8 +409,27 @@ process_arguments (int argc, char **argv) verbose = TRUE; break; case 'm': /* min_page_length */ - min_page_len = atoi (optarg); + { + char *tmp; + if (strchr(optarg, ':') != (char *)NULL) { + /* range, so get two values, min:max */ + tmp = strtok(optarg, ":"); + if (tmp == NULL) { + printf("Bad format: try \"-m min:max\"\n"); + exit (STATE_WARNING); + } else + min_page_len = atoi(tmp); + + tmp = strtok(NULL, ":"); + if (tmp == NULL) { + printf("Bad format: try \"-m min:max\"\n"); + exit (STATE_WARNING); + } else + max_page_len = atoi(tmp); + } else + min_page_len = atoi (optarg); break; + } case 'N': /* no-body */ no_body = TRUE; break; @@ -427,7 +447,7 @@ process_arguments (int argc, char **argv) maximum_age = atoi (optarg); else { fprintf (stderr, "unparsable max-age: %s\n", optarg); - exit (1); + exit (STATE_WARNING); } } break; @@ -700,7 +720,55 @@ check_document_dates (const char *headers) } } +int +get_content_length (const char *headers) +{ + const char *s; + int content_length = 0; + + s = headers; + while (*s) { + const char *field = s; + const char *value = 0; + + /* Find the end of the header field */ + while (*s && !isspace(*s) && *s != ':') + s++; + + /* Remember the header value, if any. */ + if (*s == ':') + value = ++s; + + /* Skip to the end of the header, including continuation lines. */ + while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t'))) + s++; + s++; + + /* Process this header. */ + if (value && value > field+2) { + char *ff = (char *) malloc (value-field); + char *ss = ff; + while (field < value-1) + *ss++ = tolower(*field++); + *ss++ = 0; + if (!strcmp (ff, "content-length")) { + const char *e; + while (*value && isspace (*value)) + value++; + for (e = value; *e && *e != '\r' && *e != '\n'; e++) + ; + ss = (char *) malloc (e - value + 1); + strncpy (ss, value, e - value); + ss[e - value] = 0; + content_length = atoi(ss); + free (ss); + } + free (ff); + } + } + return (content_length); +} int check_http (void) @@ -774,6 +842,7 @@ check_http (void) } else { asprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf); } + asprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, strlen (http_post_data)); asprintf (&buf, "%s%s%s", buf, http_post_data, CRLF); } @@ -1005,8 +1074,13 @@ check_http (void) #endif /* make sure the page is of an appropriate size */ - page_len = strlen (page); - if ((min_page_len > 0) && (page_len < min_page_len)) { + /* page_len = get_content_length(header); */ + page_len = pagesize; + if ((max_page_len > 0) && (page_len > max_page_len)) { + printf (_("HTTP WARNING: page size %d too large%s|%s\n"), + page_len, (display_html ? "" : ""), perfd_size (page_len) ); + exit (STATE_WARNING); + } else if ((min_page_len > 0) && (page_len < min_page_len)) { printf (_("HTTP WARNING: page size %d too small%s|%s\n"), page_len, (display_html ? "" : ""), perfd_size (page_len) ); exit (STATE_WARNING); @@ -1442,8 +1516,8 @@ certificate expiration times.\n\n")); Wrap output in HTML link (obsoleted by urlize)\n\ -f, --onredirect=\n\ How to handle redirected pages\n\ - -m, --min=INTEGER\n\ - Minimum page size required (bytes)\n")); + -m, --minmax=INTEGER<:INTEGER>\n\ + Minimum page size required (bytes) : Maximum page size required (bytes)\n")); printf (_(UT_WARN_CRIT)); @@ -1489,10 +1563,10 @@ void print_usage (void) { printf ("\ -Usage: %s -H | -I ) [-u ] [-p ]\n\ +Usage: %s -H | -I [-u ] [-p ]\n\ [-w ] [-c ] [-t ] [-L]\n\ [-a auth] [-f ] [-e ]\n\ [-s string] [-l] [-r | -R ]\n\ - [-P string] [-m min_pg_size] [-4|-6] [-N] [-M ]\n\ - [-A string] [-k string]\n", progname); + [-P string] [-m :] [-4|-6] [-N] \n\ + [-M ] [-A string] [-k string]\n", progname); }