--- nagios-plugins-1.3.1/plugins/check_http.c 2003-06-30 04:56:08.000000000 -0700 +++ nagios-plugins-1.3.1.DS/plugins/check_http.c 2003-07-27 15:26:14.792132000 -0700 @@ -44,7 +44,7 @@ (-H | -I ) [-u ] [-p ]\n\ [-w ] [-c ] [-t ] [-L]\n\ [-a auth] [-f ] [-e ]\n\ - [-s string] [-l] [-r | -R ]\n\ + [-s string] [-l] [-k] [-r | -R ]\n\ [-P string]" #define LONGOPTIONS "\ @@ -75,6 +75,8 @@ Wrap output in HTML link (obsoleted by urlize)\n\ -f, --onredirect=\n\ How to handle redirected pages\n%s%s\ + -k, --keepalive\n\ + Use Keep-Alive header in every request request\n\ -v, --verbose\n\ Show details for command-line debugging (do not use with nagios server)\n\ -h, --help\n\ @@ -186,6 +188,10 @@ #define URI_HOST "%[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" #define URI_PORT ":%[0123456789]" #define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]" +#define HDR_SETCOOKIE "%*[Ss]%*[Ee]%*[Tt]%*[-]%*[Cc]%*[Oo]%*[Kk]%*[Ii]%*[Ee]: " +#define COOKIE_BODY "%[/a-zA-Z0-9._-=@,: ]" +#define COOKIE_PATH ";%[/a-zA-Z0-9._-=@,: ]" +#define COOKIE_DATE ";%[/a-zA-Z0-9._-=@,: ]" enum { MAX_IPV4_HOSTLENGTH = 255, HTTP_PORT = 80, @@ -206,6 +212,7 @@ char *server_url = ""; int server_url_length; int server_expect_yn = 0; +char *cookies=NULL; char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; char string_expect[MAX_INPUT_BUFFER] = ""; double warning_time = 0; @@ -216,6 +223,7 @@ int display_html = FALSE; int onredirect = STATE_OK; int use_ssl = FALSE; +int keepalive = FALSE; int verbose = FALSE; int sd; char *http_method = "GET"; @@ -309,6 +317,7 @@ {"ereg", required_argument, 0, 'r'}, {"eregi", required_argument, 0, 'R'}, {"linespan", no_argument, 0, 'l'}, + {"keepalive", no_argument, 0, 'k'}, {"onredirect", required_argument, 0, 'f'}, {"certificate", required_argument, 0, 'C'}, {0, 0, 0, 0} @@ -331,7 +340,7 @@ strcpy (argv[c], "-n"); } -#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLS" +#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlkLS" while (1) { #ifdef HAVE_GETOPT_H @@ -466,6 +475,9 @@ } break; #endif + case 'k': /* Keep-Alive */ + keepalive = TRUE; + break; case 'v': /* verbose */ verbose = TRUE; break; @@ -550,6 +562,12 @@ char *x = NULL; char *orig_url = NULL; double elapsed_time; + char *cpos = ""; + char cookie_body[255]; + char cookie_path[255]; + char cookie_date[255]; + struct tm tm; + char date[255],rest[128]; #ifdef HAVE_SSL int sslerr; #endif @@ -589,6 +607,16 @@ asprintf (&buf, "%sUser-Agent: check_http/%s (nagios-plugins %s)\r\n", buf, clean_revstring (REVISION), PACKAGE_VERSION); + /* send Collected Cookies */ + if(cookies) + { + asprintf (&buf, "%s%s\r\n", buf,cookies); + } + + /* send "Connection: Keep-Alive" if need */ + if(keepalive) + asprintf (&buf, "%sConnection: Keep-Alive\r\n", buf); + /* optionally send the authentication info */ if (strcmp (user_auth, "")) { auth = base64 (user_auth, strlen (user_auth)); @@ -615,6 +643,9 @@ } else { #endif + if (verbose) + printf ("**** REQUEST ****\n%s\n", buf); + send (sd, buf, strlen (buf), 0); #ifdef HAVE_SSL } @@ -737,6 +768,44 @@ if (onredirect == STATE_DEPENDENT) { asprintf (&orig_url, "%s", server_url); + cpos = header; + cookies=NULL; + while (*cpos) { + cookie_date[0]='\0'; + if (sscanf(cpos, HDR_SETCOOKIE COOKIE_BODY COOKIE_PATH COOKIE_DATE, cookie_body, cookie_path, cookie_date)) { + if(verbose) + printf("Cookie parameters: body=%s path=%s date=%s\n", cookie_body, cookie_path, cookie_date); + + if(strlen(cookie_date)>1) + { + sscanf(cookie_date, "%*[Ee]%*[Xx]%*[Pp]%*[Ii]%*[Rr]%*[Ee]%*[Ss]=%*s %s %s %*s", date, rest); + strcat(date, " "); + strcat(date, rest); + strptime(date,"%d-%b-%Y %T",&tm); + + if(verbose) + printf("Cookie Date %d-%d-%d Time %d:%d:%d\n", tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec); + if(mktime(&tm) < time(0)) + { + if(verbose) + printf("Ignoring cookie since expired\n"); + + cpos += (size_t) strcspn (cpos, "\r\n"); + cpos += (size_t) strspn (cpos, "\r\n"); + continue; + } + } + + if(cookies) + asprintf (&cookies, "%s;%s", cookies, cookie_body); + else + asprintf (&cookies, "Cookie: %s", cookie_body); + } + + cpos += (size_t) strcspn (cpos, "\r\n"); + cpos += (size_t) strspn (cpos, "\r\n"); + } /* end while (cpos) */ + pos = header; while (pos) { server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH + 1);