summaryrefslogtreecommitdiffstats
path: root/plugins/check_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_http.c')
-rw-r--r--plugins/check_http.c173
1 files changed, 72 insertions, 101 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c
index b371cd64..3b93795d 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -113,7 +113,6 @@ char **http_opt_headers;
113int http_opt_headers_count = 0; 113int http_opt_headers_count = 0;
114int onredirect = STATE_OK; 114int onredirect = STATE_OK;
115int use_ssl = FALSE; 115int use_ssl = FALSE;
116int verbose = FALSE;
117int sd; 116int sd;
118int min_page_len = 0; 117int min_page_len = 0;
119int max_page_len = 0; 118int max_page_len = 0;
@@ -140,6 +139,8 @@ main (int argc, char **argv)
140{ 139{
141 int result = STATE_UNKNOWN; 140 int result = STATE_UNKNOWN;
142 141
142 np_set_mynames(argv[0], "HTTP");
143
143 /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */ 144 /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */
144 server_url = strdup(HTTP_URL); 145 server_url = strdup(HTTP_URL);
145 server_url_length = strlen(server_url); 146 server_url_length = strlen(server_url);
@@ -307,8 +308,7 @@ process_arguments (int argc, char **argv)
307 onredirect = STATE_WARNING; 308 onredirect = STATE_WARNING;
308 if (!strcmp (optarg, "critical")) 309 if (!strcmp (optarg, "critical"))
309 onredirect = STATE_CRITICAL; 310 onredirect = STATE_CRITICAL;
310 if (verbose) 311 np_verbose (_("option f:%d"), onredirect);
311 printf(_("option f:%d \n"), onredirect);
312 break; 312 break;
313 /* Note: H, I, and u must be malloc'd or will fail on redirects */ 313 /* Note: H, I, and u must be malloc'd or will fail on redirects */
314 case 'H': /* Host Name (virtual host) */ 314 case 'H': /* Host Name (virtual host) */
@@ -381,7 +381,7 @@ process_arguments (int argc, char **argv)
381#endif 381#endif
382 break; 382 break;
383 case 'v': /* verbose */ 383 case 'v': /* verbose */
384 verbose = TRUE; 384 np_increase_verbosity(1);
385 break; 385 break;
386 case 'm': /* min_page_length */ 386 case 'm': /* min_page_length */
387 { 387 {
@@ -389,17 +389,15 @@ process_arguments (int argc, char **argv)
389 if (strchr(optarg, ':') != (char *)NULL) { 389 if (strchr(optarg, ':') != (char *)NULL) {
390 /* range, so get two values, min:max */ 390 /* range, so get two values, min:max */
391 tmp = strtok(optarg, ":"); 391 tmp = strtok(optarg, ":");
392 if (tmp == NULL) { 392 if (tmp == NULL)
393 printf("Bad format: try \"-m min:max\"\n"); 393 np_die (STATE_UNKNOWN, "Bad format: try \"-m min:max\"");
394 exit (STATE_WARNING); 394 else
395 } else
396 min_page_len = atoi(tmp); 395 min_page_len = atoi(tmp);
397 396
398 tmp = strtok(NULL, ":"); 397 tmp = strtok(NULL, ":");
399 if (tmp == NULL) { 398 if (tmp == NULL)
400 printf("Bad format: try \"-m min:max\"\n"); 399 np_die (STATE_UNKNOWN, "Bad format: try \"-m min:max\"");
401 exit (STATE_WARNING); 400 else
402 } else
403 max_page_len = atoi(tmp); 401 max_page_len = atoi(tmp);
404 } else 402 } else
405 min_page_len = atoi (optarg); 403 min_page_len = atoi (optarg);
@@ -420,10 +418,8 @@ process_arguments (int argc, char **argv)
420 else if (L && (optarg[L-1] == 's' || 418 else if (L && (optarg[L-1] == 's' ||
421 isdigit (optarg[L-1]))) 419 isdigit (optarg[L-1])))
422 maximum_age = atoi (optarg); 420 maximum_age = atoi (optarg);
423 else { 421 else
424 fprintf (stderr, "unparsable max-age: %s\n", optarg); 422 np_die (STATE_UNKNOWN, _("Unparsable max-age: %s"), optarg);
425 exit (STATE_WARNING);
426 }
427 } 423 }
428 break; 424 break;
429 } 425 }
@@ -593,7 +589,7 @@ parse_time_string (const char *string)
593 t = mktime (&tm); 589 t = mktime (&tm);
594 if (t == (time_t) -1) t = 0; 590 if (t == (time_t) -1) t = 0;
595 591
596 if (verbose) { 592 if (np_get_verbosity() > 0) {
597 const char *s = string; 593 const char *s = string;
598 while (*s && *s != '\r' && *s != '\n') 594 while (*s && *s != '\r' && *s != '\n')
599 fputc (*s++, stdout); 595 fputc (*s++, stdout);
@@ -665,28 +661,27 @@ check_document_dates (const char *headers)
665 661
666 /* Done parsing the body. Now check the dates we (hopefully) parsed. */ 662 /* Done parsing the body. Now check the dates we (hopefully) parsed. */
667 if (!server_date || !*server_date) { 663 if (!server_date || !*server_date) {
668 die (STATE_UNKNOWN, _("HTTP UNKNOWN - Server date unknown\n")); 664 np_die (STATE_UNKNOWN, _("Server date unknown"));
669 } else if (!document_date || !*document_date) { 665 } else if (!document_date || !*document_date) {
670 die (STATE_CRITICAL, _("HTTP CRITICAL - Document modification date unknown\n")); 666 np_die (STATE_CRITICAL, _("Document modification date unknown"));
671 } else { 667 } else {
672 time_t srv_data = parse_time_string (server_date); 668 time_t srv_data = parse_time_string (server_date);
673 time_t doc_data = parse_time_string (document_date); 669 time_t doc_data = parse_time_string (document_date);
674 670
675 if (srv_data <= 0) { 671 if (srv_data <= 0) {
676 die (STATE_CRITICAL, _("HTTP CRITICAL - Server date \"%100s\" unparsable"), server_date); 672 np_die (STATE_CRITICAL, _("Server date \"%100s\" unparsable"), server_date);
677 } else if (doc_data <= 0) { 673 } else if (doc_data <= 0) {
678 die (STATE_CRITICAL, _("HTTP CRITICAL - Document date \"%100s\" unparsable"), document_date); 674 np_die (STATE_CRITICAL, _("Document date \"%100s\" unparsable"), document_date);
679 } else if (doc_data > srv_data + 30) { 675 } else if (doc_data > srv_data + 30) {
680 die (STATE_CRITICAL, _("HTTP CRITICAL - Document is %d seconds in the future\n"), (int)doc_data - (int)srv_data); 676 np_die (STATE_CRITICAL, _("Document is %d seconds in the future"),
677 (int)doc_data - (int)srv_data);
681 } else if (doc_data < srv_data - maximum_age) { 678 } else if (doc_data < srv_data - maximum_age) {
682 int n = (srv_data - doc_data); 679 int n = (srv_data - doc_data);
683 if (n > (60 * 60 * 24 * 2)) 680 if (n > (60 * 60 * 24 * 2))
684 die (STATE_CRITICAL, 681 np_die (STATE_CRITICAL, _("Last modified %.1f days ago"),
685 _("HTTP CRITICAL - Last modified %.1f days ago\n"),
686 ((float) n) / (60 * 60 * 24)); 682 ((float) n) / (60 * 60 * 24));
687 else 683 else
688 die (STATE_CRITICAL, 684 np_die (STATE_CRITICAL, _("Last modified %d:%02d:%02d ago"),
689 _("HTTP CRITICAL - Last modified %d:%02d:%02d ago\n"),
690 n / (60 * 60), (n / 60) % 60, n % 60); 685 n / (60 * 60), (n / 60) % 60, n % 60);
691 } 686 }
692 687
@@ -767,7 +762,7 @@ check_http (void)
767 762
768 /* try to connect to the host at the given port number */ 763 /* try to connect to the host at the given port number */
769 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) 764 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
770 die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n")); 765 np_die (STATE_CRITICAL, _("Unable to open TCP socket"));
771#ifdef HAVE_SSL 766#ifdef HAVE_SSL
772 if (use_ssl == TRUE) { 767 if (use_ssl == TRUE) {
773 np_net_ssl_init(sd); 768 np_net_ssl_init(sd);
@@ -820,7 +815,7 @@ check_http (void)
820 asprintf (&buf, "%s%s", buf, CRLF); 815 asprintf (&buf, "%s%s", buf, CRLF);
821 } 816 }
822 817
823 if (verbose) printf ("%s\n", buf); 818 np_verbatim (buf);
824 my_send (buf, strlen (buf)); 819 my_send (buf, strlen (buf));
825 820
826 /* fetch the page */ 821 /* fetch the page */
@@ -842,15 +837,15 @@ check_http (void)
842 if (use_ssl) { 837 if (use_ssl) {
843 sslerr=SSL_get_error(ssl, i); 838 sslerr=SSL_get_error(ssl, i);
844 if ( sslerr == SSL_ERROR_SSL ) { 839 if ( sslerr == SSL_ERROR_SSL ) {
845 die (STATE_WARNING, _("HTTP WARNING - Client Certificate Required\n")); 840 np_die (STATE_WARNING, _("Client Certificate Required"));
846 } else { 841 } else {
847 die (STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n")); 842 np_die (STATE_CRITICAL, _("Error on receive"));
848 } 843 }
849 } 844 }
850 else { 845 else {
851 */ 846 */
852#endif 847#endif
853 die (STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n")); 848 np_die (STATE_CRITICAL, _("HTTP CRITICAL - Error on receive"));
854#ifdef HAVE_SSL 849#ifdef HAVE_SSL
855 /* XXX 850 /* XXX
856 } 851 }
@@ -860,7 +855,7 @@ check_http (void)
860 855
861 /* return a CRITICAL status if we couldn't read any data */ 856 /* return a CRITICAL status if we couldn't read any data */
862 if (pagesize == (size_t) 0) 857 if (pagesize == (size_t) 0)
863 die (STATE_CRITICAL, _("HTTP CRITICAL - No data received from host\n")); 858 np_die (STATE_CRITICAL, _("HTTP CRITICAL - No data received from host"));
864 859
865 /* close the connection */ 860 /* close the connection */
866#ifdef HAVE_SSL 861#ifdef HAVE_SSL
@@ -874,8 +869,7 @@ check_http (void)
874 /* leave full_page untouched so we can free it later */ 869 /* leave full_page untouched so we can free it later */
875 page = full_page; 870 page = full_page;
876 871
877 if (verbose) 872 np_verbose ("%s://%s:%d%s is %d characters",
878 printf ("%s://%s:%d%s is %d characters\n",
879 use_ssl ? "https" : "http", server_address, 873 use_ssl ? "https" : "http", server_address,
880 server_port, server_url, (int)pagesize); 874 server_port, server_url, (int)pagesize);
881 875
@@ -886,8 +880,7 @@ check_http (void)
886 page += (size_t) strspn (page, "\r\n"); 880 page += (size_t) strspn (page, "\r\n");
887 status_line[strcspn(status_line, "\r\n")] = 0; 881 status_line[strcspn(status_line, "\r\n")] = 0;
888 strip (status_line); 882 strip (status_line);
889 if (verbose) 883 np_verbose ("STATUS: %s", status_line);
890 printf ("STATUS: %s\n", status_line);
891 884
892 /* find header info and null-terminate it */ 885 /* find header info and null-terminate it */
893 header = page; 886 header = page;
@@ -902,29 +895,27 @@ check_http (void)
902 } 895 }
903 page += (size_t) strspn (page, "\r\n"); 896 page += (size_t) strspn (page, "\r\n");
904 header[pos - header] = 0; 897 header[pos - header] = 0;
905 if (verbose) 898 np_verbose ("**** HEADER ****\n%s\n**** CONTENT ****\n%s", header,
906 printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header,
907 (no_body ? " [[ skipped ]]" : page)); 899 (no_body ? " [[ skipped ]]" : page));
908 900
909 /* make sure the status line matches the response we are looking for */ 901 /* make sure the status line matches the response we are looking for */
910 if (!strstr (status_line, server_expect)) { 902 if (!strstr (status_line, server_expect)) {
911 if (server_port == HTTP_PORT) 903 if (server_port == HTTP_PORT)
912 asprintf (&msg, 904 asprintf (&msg,
913 _("Invalid HTTP response received from host\n")); 905 _("Invalid HTTP response received from host"));
914 else 906 else
915 asprintf (&msg, 907 asprintf (&msg,
916 _("Invalid HTTP response received from host on port %d\n"), 908 _("Invalid HTTP response received from host on port %d"),
917 server_port); 909 server_port);
918 die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); 910 np_die (STATE_CRITICAL, "%s", msg);
919 } 911 }
920 912
921 /* Exit here if server_expect was set by user and not default */ 913 /* Exit here if server_expect was set by user and not default */
922 if ( server_expect_yn ) { 914 if ( server_expect_yn ) {
923 asprintf (&msg, 915 asprintf (&msg,
924 _("HTTP OK: Status line output matched \"%s\"\n"), 916 _("Status line output matched \"%s\""),
925 server_expect); 917 server_expect);
926 if (verbose) 918 np_verbatim (msg);
927 printf ("%s\n",msg);
928 } 919 }
929 else { 920 else {
930 /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ 921 /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
@@ -933,40 +924,32 @@ check_http (void)
933 924
934 status_code = strchr (status_line, ' ') + sizeof (char); 925 status_code = strchr (status_line, ' ') + sizeof (char);
935 if (strspn (status_code, "1234567890") != 3) 926 if (strspn (status_code, "1234567890") != 3)
936 die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line); 927 np_die (STATE_CRITICAL, _("Invalid Status Line (%s)"), status_line);
937 928
938 http_status = atoi (status_code); 929 http_status = atoi (status_code);
939 930
940 /* check the return code */ 931 /* check the return code */
941 932
942 if (http_status >= 600 || http_status < 100) 933 if (http_status >= 600 || http_status < 100)
943 die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line); 934 np_die (STATE_CRITICAL, _("Invalid Status (%s)"), status_line);
944 935
945 /* server errors result in a critical state */ 936 /* server errors result in a critical state */
946 else if (http_status >= 500) 937 else if (http_status >= 500)
947 die (STATE_CRITICAL, _("HTTP CRITICAL: %s\n"), status_line); 938 np_die (STATE_CRITICAL, _("%s"), status_line);
948 939
949 /* client errors result in a warning state */ 940 /* client errors result in a warning state */
950 else if (http_status >= 400) 941 else if (http_status >= 400)
951 die (STATE_WARNING, _("HTTP WARNING: %s\n"), status_line); 942 np_die (STATE_WARNING, _("%s"), status_line);
952 943
953 /* check redirected page if specified */ 944 /* check redirected page if specified */
954 else if (http_status >= 300) { 945 else if (http_status >= 300) {
955 946
956 if (onredirect == STATE_DEPENDENT) 947 if (onredirect == STATE_DEPENDENT)
957 redir (header, status_line); 948 redir (header, status_line);
958 else if (onredirect == STATE_UNKNOWN)
959 printf (_("HTTP UNKNOWN"));
960 else if (onredirect == STATE_OK)
961 printf (_("HTTP OK"));
962 else if (onredirect == STATE_WARNING)
963 printf (_("HTTP WARNING"));
964 else if (onredirect == STATE_CRITICAL)
965 printf (_("HTTP CRITICAL"));
966 microsec = deltime (tv); 949 microsec = deltime (tv);
967 elapsed_time = (double)microsec / 1.0e6; 950 elapsed_time = (double)microsec / 1.0e6;
968 die (onredirect, 951 np_die (onredirect,
969 _(" - %s - %.3f second response time %s|%s %s\n"), 952 _("%s - %.3f second response time %s|%s %s"),
970 status_line, elapsed_time, 953 status_line, elapsed_time,
971 (display_html ? "</A>" : ""), 954 (display_html ? "</A>" : ""),
972 perfd_time (elapsed_time), perfd_size (pagesize)); 955 perfd_time (elapsed_time), perfd_size (pagesize));
@@ -982,59 +965,50 @@ check_http (void)
982 microsec = deltime (tv); 965 microsec = deltime (tv);
983 elapsed_time = (double)microsec / 1.0e6; 966 elapsed_time = (double)microsec / 1.0e6;
984 asprintf (&msg, 967 asprintf (&msg,
985 _("HTTP WARNING: %s - %.3f second response time %s|%s %s\n"), 968 _("%s - %.3f second response time %s|%s %s"),
986 status_line, elapsed_time, 969 status_line, elapsed_time,
987 (display_html ? "</A>" : ""), 970 (display_html ? "</A>" : ""),
988 perfd_time (elapsed_time), perfd_size (pagesize)); 971 perfd_time (elapsed_time), perfd_size (pagesize));
989 if (check_critical_time == TRUE && elapsed_time > critical_time) 972 if (check_critical_time == TRUE && elapsed_time > critical_time)
990 die (STATE_CRITICAL, "%s", msg); 973 np_die (STATE_CRITICAL, "%s", msg);
991 if (check_warning_time == TRUE && elapsed_time > warning_time) 974 if (check_warning_time == TRUE && elapsed_time > warning_time)
992 die (STATE_WARNING, "%s", msg); 975 np_die (STATE_WARNING, "%s", msg);
993 976
994 /* Page and Header content checks go here */ 977 /* Page and Header content checks go here */
995 /* these checks should be last */ 978 /* these checks should be last */
996 979
997 if (strlen (string_expect)) { 980 if (strlen (string_expect)) {
998 if (strstr (page, string_expect)) { 981 if (strstr (page, string_expect))
999 printf (_("HTTP OK %s - %.3f second response time %s|%s %s\n"), 982 np_die (STATE_OK, _("%s - %.3f second response time %s|%s %s"),
1000 status_line, elapsed_time, 983 status_line, elapsed_time,
1001 (display_html ? "</A>" : ""), 984 (display_html ? "</A>" : ""),
1002 perfd_time (elapsed_time), perfd_size (pagesize)); 985 perfd_time (elapsed_time), perfd_size (pagesize));
1003 exit (STATE_OK); 986 else
1004 } 987 np_die (STATE_CRITICAL, _("string not found%s|%s %s"),
1005 else {
1006 printf (_("HTTP CRITICAL - string not found%s|%s %s\n"),
1007 (display_html ? "</A>" : ""), 988 (display_html ? "</A>" : ""),
1008 perfd_time (elapsed_time), perfd_size (pagesize)); 989 perfd_time (elapsed_time), perfd_size (pagesize));
1009 exit (STATE_CRITICAL);
1010 }
1011 } 990 }
1012 991
1013 if (strlen (regexp)) { 992 if (strlen (regexp)) {
1014 errcode = regexec (&preg, page, REGS, pmatch, 0); 993 errcode = regexec (&preg, page, REGS, pmatch, 0);
1015 if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) { 994 if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1))
1016 printf (_("HTTP OK %s - %.3f second response time %s|%s %s\n"), 995 np_die (STATE_OK, _("%s - %.3f second response time %s|%s %s"),
1017 status_line, elapsed_time, 996 status_line, elapsed_time,
1018 (display_html ? "</A>" : ""), 997 (display_html ? "</A>" : ""),
1019 perfd_time (elapsed_time), perfd_size (pagesize)); 998 perfd_time (elapsed_time), perfd_size (pagesize));
1020 exit (STATE_OK);
1021 }
1022 else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { 999 else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) {
1023 if (invert_regex == 0) 1000 if (invert_regex == 0)
1024 msg = strdup(_("pattern not found")); 1001 msg = strdup(_("pattern not found"));
1025 else 1002 else
1026 msg = strdup(_("pattern found")); 1003 msg = strdup(_("pattern found"));
1027 printf (("%s - %s%s|%s %s\n"), 1004 np_die (STATE_CRITICAL, "%s%s|%s %s",
1028 _("HTTP CRITICAL"),
1029 msg, 1005 msg,
1030 (display_html ? "</A>" : ""), 1006 (display_html ? "</A>" : ""),
1031 perfd_time (elapsed_time), perfd_size (pagesize)); 1007 perfd_time (elapsed_time), perfd_size (pagesize));
1032 exit (STATE_CRITICAL);
1033 } 1008 }
1034 else { 1009 else {
1035 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); 1010 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
1036 printf (_("HTTP CRITICAL - Execute Error: %s\n"), errbuf); 1011 np_die (STATE_CRITICAL, _("Execute Error: %s"), errbuf);
1037 exit (STATE_CRITICAL);
1038 } 1012 }
1039 } 1013 }
1040 1014
@@ -1042,20 +1016,18 @@ check_http (void)
1042 /* page_len = get_content_length(header); */ 1016 /* page_len = get_content_length(header); */
1043 page_len = pagesize; 1017 page_len = pagesize;
1044 if ((max_page_len > 0) && (page_len > max_page_len)) { 1018 if ((max_page_len > 0) && (page_len > max_page_len)) {
1045 printf (_("HTTP WARNING: page size %d too large%s|%s\n"), 1019 np_die (STATE_WARNING, _("page size %d too large%s|%s"),
1046 page_len, (display_html ? "</A>" : ""), perfd_size (page_len) ); 1020 page_len, (display_html ? "</A>" : ""), perfd_size (page_len) );
1047 exit (STATE_WARNING);
1048 } else if ((min_page_len > 0) && (page_len < min_page_len)) { 1021 } else if ((min_page_len > 0) && (page_len < min_page_len)) {
1049 printf (_("HTTP WARNING: page size %d too small%s|%s\n"), 1022 np_die (STATE_WARNING, _("page size %d too small%s|%s"),
1050 page_len, (display_html ? "</A>" : ""), perfd_size (page_len) ); 1023 page_len, (display_html ? "</A>" : ""), perfd_size (page_len) );
1051 exit (STATE_WARNING);
1052 } 1024 }
1053 /* We only get here if all tests have been passed */ 1025 /* We only get here if all tests have been passed */
1054 asprintf (&msg, _("HTTP OK %s - %d bytes in %.3f seconds %s|%s %s\n"), 1026 asprintf (&msg, _("%s - %d bytes in %.3f seconds %s|%s %s"),
1055 status_line, page_len, elapsed_time, 1027 status_line, page_len, elapsed_time,
1056 (display_html ? "</A>" : ""), 1028 (display_html ? "</A>" : ""),
1057 perfd_time (elapsed_time), perfd_size (page_len)); 1029 perfd_time (elapsed_time), perfd_size (page_len));
1058 die (STATE_OK, "%s", msg); 1030 np_die (STATE_OK, "%s", msg);
1059 return STATE_UNKNOWN; 1031 return STATE_UNKNOWN;
1060} 1032}
1061 1033
@@ -1085,11 +1057,11 @@ redir (char *pos, char *status_line)
1085 1057
1086 addr = malloc (MAX_IPV4_HOSTLENGTH + 1); 1058 addr = malloc (MAX_IPV4_HOSTLENGTH + 1);
1087 if (addr == NULL) 1059 if (addr == NULL)
1088 die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate addr\n")); 1060 np_die (STATE_UNKNOWN, _("Could not allocate addr"));
1089 1061
1090 url = malloc (strcspn (pos, "\r\n")); 1062 url = malloc (strcspn (pos, "\r\n"));
1091 if (url == NULL) 1063 if (url == NULL)
1092 die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate url\n")); 1064 np_die (STATE_UNKNOWN, _("Could not allocate url"));
1093 1065
1094 while (pos) { 1066 while (pos) {
1095 sscanf (pos, "%[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]:%n", xx, &i); 1067 sscanf (pos, "%[Ll]%*[Oo]%*[Cc]%*[Aa]%*[Tt]%*[Ii]%*[Oo]%*[Nn]:%n", xx, &i);
@@ -1097,8 +1069,8 @@ redir (char *pos, char *status_line)
1097 pos += (size_t) strcspn (pos, "\r\n"); 1069 pos += (size_t) strcspn (pos, "\r\n");
1098 pos += (size_t) strspn (pos, "\r\n"); 1070 pos += (size_t) strspn (pos, "\r\n");
1099 if (strlen(pos) == 0) 1071 if (strlen(pos) == 0)
1100 die (STATE_UNKNOWN, 1072 np_die (STATE_UNKNOWN,
1101 _("HTTP UNKNOWN - Could not find redirect location - %s%s\n"), 1073 _("Could not find redirect location - %s%s"),
1102 status_line, (display_html ? "</A>" : "")); 1074 status_line, (display_html ? "</A>" : ""));
1103 continue; 1075 continue;
1104 } 1076 }
@@ -1113,14 +1085,14 @@ redir (char *pos, char *status_line)
1113 for (; (i = strspn (pos, "\r\n")); pos += i) { 1085 for (; (i = strspn (pos, "\r\n")); pos += i) {
1114 pos += i; 1086 pos += i;
1115 if (!(i = strspn (pos, " \t"))) { 1087 if (!(i = strspn (pos, " \t"))) {
1116 die (STATE_UNKNOWN, _("HTTP UNKNOWN - Empty redirect location%s\n"), 1088 np_die (STATE_UNKNOWN, _("Empty redirect location%s"),
1117 display_html ? "</A>" : ""); 1089 display_html ? "</A>" : "");
1118 } 1090 }
1119 } 1091 }
1120 1092
1121 url = realloc (url, strcspn (pos, "\r\n") + 1); 1093 url = realloc (url, strcspn (pos, "\r\n") + 1);
1122 if (url == NULL) 1094 if (url == NULL)
1123 die (STATE_UNKNOWN, _("HTTP UNKNOWN - could not allocate url\n")); 1095 np_die (STATE_UNKNOWN, _("could not allocate url"));
1124 1096
1125 /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */ 1097 /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */
1126 if (sscanf (pos, HD1, type, addr, &i, url) == 4) 1098 if (sscanf (pos, HD1, type, addr, &i, url) == 4)
@@ -1159,8 +1131,8 @@ redir (char *pos, char *status_line)
1159 } 1131 }
1160 1132
1161 else { 1133 else {
1162 die (STATE_UNKNOWN, 1134 np_die (STATE_UNKNOWN,
1163 _("HTTP UNKNOWN - Could not parse redirect location - %s%s\n"), 1135 _("Could not parse redirect location - %s%s"),
1164 pos, (display_html ? "</A>" : "")); 1136 pos, (display_html ? "</A>" : ""));
1165 } 1137 }
1166 1138
@@ -1169,16 +1141,16 @@ redir (char *pos, char *status_line)
1169 } /* end while (pos) */ 1141 } /* end while (pos) */
1170 1142
1171 if (++redir_depth > max_depth) 1143 if (++redir_depth > max_depth)
1172 die (STATE_WARNING, 1144 np_die (STATE_WARNING,
1173 _("HTTP WARNING - maximum redirection depth %d exceeded - %s://%s:%d%s%s\n"), 1145 _("maximum redirection depth %d exceeded - %s://%s:%d%s%s"),
1174 max_depth, type, addr, i, url, (display_html ? "</A>" : "")); 1146 max_depth, type, addr, i, url, (display_html ? "</A>" : ""));
1175 1147
1176 if (server_port==i && 1148 if (server_port==i &&
1177 !strcmp(server_address, addr) && 1149 !strcmp(server_address, addr) &&
1178 (host_name && !strcmp(host_name, addr)) && 1150 (host_name && !strcmp(host_name, addr)) &&
1179 !strcmp(server_url, url)) 1151 !strcmp(server_url, url))
1180 die (STATE_WARNING, 1152 np_die (STATE_WARNING,
1181 _("HTTP WARNING - redirection creates an infinite loop - %s://%s:%d%s%s\n"), 1153 _("redirection creates an infinite loop - %s://%s:%d%s%s"),
1182 type, addr, i, url, (display_html ? "</A>" : "")); 1154 type, addr, i, url, (display_html ? "</A>" : ""));
1183 1155
1184 strcpy (server_type, type); 1156 strcpy (server_type, type);
@@ -1193,18 +1165,17 @@ redir (char *pos, char *status_line)
1193 if ((url[0] == '/')) 1165 if ((url[0] == '/'))
1194 server_url = strdup (url); 1166 server_url = strdup (url);
1195 else if (asprintf(&server_url, "/%s", url) == -1) 1167 else if (asprintf(&server_url, "/%s", url) == -1)
1196 die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate server_url%s\n"), 1168 np_die (STATE_UNKNOWN, _("Could not allocate server_url%s"),
1197 display_html ? "</A>" : ""); 1169 display_html ? "</A>" : "");
1198 free(url); 1170 free(url);
1199 1171
1200 if ((server_port = i) > MAX_PORT) 1172 if ((server_port = i) > MAX_PORT)
1201 die (STATE_UNKNOWN, 1173 np_die (STATE_UNKNOWN,
1202 _("HTTP UNKNOWN - Redirection to port above %d - %s://%s:%d%s%s\n"), 1174 _("Redirection to port above %d - %s://%s:%d%s%s"),
1203 MAX_PORT, server_type, server_address, server_port, server_url, 1175 MAX_PORT, server_type, server_address, server_port, server_url,
1204 display_html ? "</A>" : ""); 1176 display_html ? "</A>" : "");
1205 1177
1206 if (verbose) 1178 np_verbose (_("Redirection to %s://%s:%d%s"), server_type, server_address,
1207 printf (_("Redirection to %s://%s:%d%s\n"), server_type, server_address,
1208 server_port, server_url); 1179 server_port, server_url);
1209 1180
1210 check_http (); 1181 check_http ();