summaryrefslogtreecommitdiffstats
path: root/web/attachments/296709-check_http.patch
blob: 04d83f115c7b24bfa9a2d071a445a102ef0375ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Index: check_http.c
===================================================================
--- check_http.c	(revision 2059)
+++ check_http.c	(working copy)
@@ -187,6 +187,7 @@
     {"nohtml", no_argument, 0, 'n'},
     {"ssl", no_argument, 0, 'S'},
     {"post", required_argument, 0, 'P'},
+    {"method", required_argument, 0, 'j'},
     {"IP-address", required_argument, 0, 'I'},
     {"url", required_argument, 0, 'u'},
     {"port", required_argument, 0, 'p'},
@@ -228,7 +229,7 @@
   }
 
   while (1) {
-    c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:T:I:a:e:p:s:R:r:u:f:C:nlLSm:M:N", longopts, &option);
+    c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:e:p:s:R:r:u:f:C:nlLSm:M:N", longopts, &option);
     if (c == -1 || c == EOF)
       break;
 
@@ -344,11 +345,19 @@
       strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1);
       user_auth[MAX_INPUT_BUFFER - 1] = 0;
       break;
-    case 'P': /* HTTP POST data in URL encoded format */
-      if (http_method || http_post_data) break;
+    case 'P': /* HTTP POST data in URL encoded format; only sets data when method already set*/
+      if (http_post_data) break;
+      if (http_method)
+      {
+          http_post_data = strdup (optarg);
+          break;
+      }
       http_method = strdup("POST");
       http_post_data = strdup (optarg);
       break;
+    case 'j': /* SET HTTP METHOD */
+      http_method = strdup (optarg);
+      break;
     case 's': /* string or substring */
       strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1);
       string_expect[MAX_INPUT_BUFFER - 1] = 0;
@@ -817,7 +826,7 @@
     asprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth);
   }
 
-  /* either send http POST data */
+  /* either send http POST data (any data, not only POST)*/
   if (http_post_data) {
     if (http_content_type) {
       asprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type);
@@ -1312,7 +1321,9 @@
   printf (" %s\n", "-u, --url=PATH");
   printf ("    %s\n", _("URL to GET or POST (default: /)"));
   printf (" %s\n", "-P, --post=STRING");
-  printf ("    %s\n", _("URL encoded http POST data"));
+  printf ("    %s\n", _("URL encoded http POST data. When used with -j only sets data."));
+  printf (" %s\n", "-j, --method=STRING  (ex. OPTIONS, TRACE, PUT, DELETE)");
+  printf ("    %s\n", _("Set HTTP method."));
   printf (" %s\n", "-N, --no-body");
   printf ("    %s\n", _("Don't wait for document body: stop reading after headers."));
   printf ("    %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)"));
@@ -1396,5 +1407,5 @@
   printf ("       [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n");
   printf ("       [-s string] [-l] [-r <regex> | -R <case-insensitive regex>] [-P string]\n");
   printf ("       [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>] [-A string]\n");
-  printf ("       [-k string] [-S] [-C <age>] [-T <content-type>]\n");
+  printf ("       [-k string] [-S] [-C <age>] [-T <content-type>] [-j string]\n");
 }
Index: tests/check_http.t
===================================================================
--- tests/check_http.t	(revision 2059)
+++ tests/check_http.t	(working copy)
@@ -33,9 +33,7 @@
 	print "Please contact me at: <URL:", $d->url, ">\n";
 	while (my $c = $d->accept ) {
 		while (my $r = $c->get_request) {
-			if ($r->method eq "GET" and $r->url->path eq "/xyzzy") {
-				$c->send_file_response("/etc/passwd");
-			} elsif ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
+			if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
 				$c->send_basic_header($1);
 				$c->send_crlf;
 			} elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
@@ -46,8 +44,15 @@
 				$c->send_basic_header;
 				$c->send_crlf;
 				sleep 1;
-				$c->send_response("slow");
+			} elsif ($r->url->path eq "/method") {
+				if ($r->method eq "DELETE") {
+					$c->send_error(RC_METHOD_NOT_ALLOWED);
+				} elsif ($r->method eq "foo") {
+					$c->send_error(RC_NOT_IMPLEMENTED);
 			} else {
+					$c->send_status_line(200, $r->method);
+				}
+			} else {
 				$c->send_error(RC_FORBIDDEN);
 			}
 			$c->close;
@@ -63,7 +68,7 @@
 }
 
 if (-x "./check_http") {
-	plan tests => 19;
+	plan tests => 33;
 } else {
 	plan skip_all => "No check_http compiled";
 }
@@ -84,7 +89,7 @@
 
 $result = NPTest->testCmd( "$command -u /slow" );
 is( $result->return_code, 0, "/file/root");
-like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" );
+like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct" );
 $result->output =~ /in ([\d\.]+) seconds/;
 cmp_ok( $1, ">", 1, "Time is > 1 second" );
 
@@ -119,3 +124,37 @@
 is( $result->return_code, 2, $cmd);
 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output );
 
+$cmd = "$command -j HEAD -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
+
+$cmd = "$command -j POST -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
+
+$cmd = "$command -j GET -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
+
+$cmd = "$command -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
+
+$cmd = "$command -P foo -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );
+
+$cmd = "$command -j DELETE -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 1, $cmd);
+like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
+
+$cmd = "$command -j foo -u /method";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 2, $cmd);
+like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );