summaryrefslogtreecommitdiffstats
path: root/web/attachments/410030-checkhttpgzipdeflate.patch
blob: 0850a7047411916227d2cbcea6629782594229b0 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
diff --git a/plugins/check_http.c b/plugins/check_http.c
index 433c28e..a71cfe6 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -41,7 +41,9 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 #include "netutils.h"
 #include "utils.h"
 #include "base64.h"
+#include "zlib.h"
 #include <ctype.h>
+#include <stdlib.h>
 
 #define INPUT_DELIMITER ";"
 #define STICKY_NONE 0
@@ -114,6 +116,8 @@ int followsticky = STICKY_NONE;
 int use_ssl = FALSE;
 int use_sni = FALSE;
 int verbose = FALSE;
+int decompress = FALSE;
+int chunked = FALSE;
 int sd;
 int min_page_len = 0;
 int max_page_len = 0;
@@ -134,6 +138,14 @@ char *perfd_size (int page_len);
 void print_help (void);
 void print_usage (void);
 
+int page_content_decompress(const char *in_buf,int in_size,
+		char **data,int *out_size,int type);
+int find_header_info(const char* header,
+		char* content,const char* keyword);
+int get_content_encoding(const char* header); 
+int get_transfer_encoding(const char* header);
+char* decodechunked(char * chunked, unsigned int *inputlen);
+	
 int
 main (int argc, char **argv)
 {
@@ -214,6 +226,8 @@ process_arguments (int argc, char **argv)
     {"invert-regex", no_argument, NULL, INVERT_REGEX},
     {"use-ipv4", no_argument, 0, '4'},
     {"use-ipv6", no_argument, 0, '6'},
+    {"decompress",no_argument,0,'d'},
+    {"chunked",no_argument,0,'X'},
     {0, 0, 0, 0}
   };
 
@@ -234,7 +248,7 @@ process_arguments (int argc, char **argv)
   }
 
   while (1) {
-    c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b: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:b:e:p:s:R:r:u:f:C:nlLSm:M:NdX", longopts, &option);
     if (c == -1 || c == EOF)
       break;
 
@@ -406,6 +420,12 @@ process_arguments (int argc, char **argv)
     case 'v': /* verbose */
       verbose = TRUE;
       break;
+    case 'd': /* decompress */
+      decompress = TRUE;
+      break;
+    case 'X': /*chunked*/
+      chunked = TRUE;
+      break;
     case 'm': /* min_page_length */
       {
       char *tmp;
@@ -793,6 +813,9 @@ check_http (void)
   int page_len = 0;
   int result = STATE_OK;
 
+  char *uncompress_page;
+  int uncompress_page_size;
+
   /* try to connect to the host at the given port number */
   if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
     die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n"));
@@ -870,18 +893,15 @@ check_http (void)
   my_send (buf, strlen (buf));
 
   /* fetch the page */
-  full_page = strdup("");
+  full_page =(char*)malloc(sizeof(char)*4096); 
   while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) {
-    buffer[i] = '\0';
-    asprintf (&full_page_new, "%s%s", full_page, buffer);
-    free (full_page);
-    full_page = full_page_new;
+    full_page = realloc(full_page, pagesize + i);
+    memcpy(full_page + pagesize, buffer, i);
     pagesize += i;
-
-                if (no_body && document_headers_done (full_page)) {
-                  i = 0;
-                  break;
-                }
+    if (no_body && document_headers_done (full_page)) {
+      i = 0;
+      break;
+    }
   }
 
   if (i < 0 && errno != ECONNRESET) {
@@ -951,6 +971,28 @@ check_http (void)
   }
   page += (size_t) strspn (page, "\r\n");
   header[pos - header] = 0;
+
+  /*deal chunked data*/
+  if(get_transfer_encoding(header)==1 && chunked) {
+    pagesize = pagesize - (page-header);
+    page = decodechunked(page,&pagesize);
+  }
+  else {
+    pagesize = get_content_length(header);
+  }
+
+  /*decompress the page content*/
+  int content_encoding = get_content_encoding(header);
+  if(decompress && (content_encoding == 1 || content_encoding == 2)) {
+    result = page_content_decompress(page,pagesize,&uncompress_page,
+		&uncompress_page_size,content_encoding);
+  
+    if(result == 0) {
+      page = uncompress_page;
+      pagesize = uncompress_page_size;
+    }
+  }
+
   if (verbose)
     printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header,
                 (no_body ? "  [[ skipped ]]" : page));
@@ -1384,6 +1426,8 @@ print_help (void)
   printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
 
   printf (UT_VERBOSE);
+  printf (UT_DECOMPRESS);
+  printf (UT_CHUNKED);
 
   printf ("\n");
   printf ("%s\n", _("Notes:"));
@@ -1432,3 +1476,167 @@ print_usage (void)
   printf ("       [-A string] [-k string] [-S] [--sni] [-C <age>] [-T <content-type>]\n");
   printf ("       [-j method]\n");
 }
+
+/* HTTP gzip or deflate decompress;
+ * type = 1 represent gzip format
+ * type = 2 represent deflate format */
+int page_content_decompress(const char *in_buf,int in_size,                 
+        char **out_buf_ptr, int *out_size,int type)
+{
+  z_stream stream = {0}; /* decompression stream */
+  char *out_buf = NULL;
+  int out_buf_bytes = 0;
+  char tmp_buf[4096];
+  int result;
+  int new_bytes;
+
+  stream.zalloc = (alloc_func)0;
+  stream.zfree = (free_func)0;
+  stream.opaque = (voidpf)0;
+  stream.next_in  = (void*) in_buf;
+  stream.avail_in = in_size;
+  stream.next_out = tmp_buf;
+  stream.avail_out = sizeof tmp_buf;
+
+  switch (type) {
+    case 1:  /*gzip*/
+      if(inflateInit2(&stream,MAX_WBITS+32) != Z_OK)
+	return -1;
+      break;
+    case 2:  /*deflate*/
+      if(inflateInit2(&stream,-MAX_WBITS) != Z_OK) 
+        return -1;
+  }
+  
+  do {
+    result = inflate(&stream,Z_NO_FLUSH);
+    switch (result) {
+      case Z_BUF_ERROR:
+        if(stream.avail_in == 0)
+          goto DONE; /*zlib bug */
+      case Z_ERRNO:
+      case Z_NEED_DICT:
+      case Z_MEM_ERROR:
+      case Z_DATA_ERROR:
+        inflateEnd(&stream);
+        free(out_buf);
+        return -1;
+    }
+    if(stream.avail_out < sizeof tmp_buf) {
+      new_bytes = sizeof tmp_buf - stream.avail_out;
+      out_buf = realloc(out_buf,out_buf_bytes + new_bytes);
+      memcpy (out_buf + out_buf_bytes,tmp_buf,new_bytes);
+      out_buf_bytes += new_bytes;
+      stream.next_out = tmp_buf;
+      stream.avail_out = sizeof tmp_buf;
+    }
+    else {
+      inflateEnd(&stream);
+      free(out_buf);
+      return -1;
+    }
+  }
+  while(result != Z_STREAM_END);
+
+DONE:
+
+  if(inflateEnd(&stream) != Z_OK) {
+    free(out_buf);
+    return -1;
+  }
+  
+  *out_size = out_buf_bytes;
+  out_buf = realloc(out_buf,out_buf_bytes + 1);
+  out_buf[out_buf_bytes] = 0;
+  *out_buf_ptr = out_buf;
+
+  return 0;
+}
+
+/*find content from header data by keyword*/
+int find_header_info(const char* header,char* content,const char* keyword) {
+  char* start  = strstr(header,keyword);
+  if(start != 0) {
+    start = start + strcspn(start,": ");
+    start += strspn(start,": ");
+    char* end = start + strcspn(start," \r\n");
+    if( content != NULL) {
+      memcpy(content,start,end-start);
+    }
+    return 1;
+  }
+  else{
+    return 0;
+  }
+}
+
+/*find Content-Encoding from the header info;
+ *if Content-Encoding equal 'gzip' return 1,or return 0.
+ */
+int get_content_encoding(const char* header) {
+  int result = 0;
+  int ret = 0;
+  char* content_encoding = (char*)malloc(sizeof(char)*10);
+  result = find_header_info(header,content_encoding,"Content-Encoding");
+
+  if(result && strncmp(content_encoding,"gzip",4)==0) {
+    ret = 1;
+  }
+  else if(result && strncmp(content_encoding,"deflate",7)==0) {
+    ret = 2;
+  }
+  else {
+    ret = 0;
+  }
+  if(content_encoding != NULL) {
+    free(content_encoding);
+  }
+  return ret;
+}
+
+/*find Transfer-Encoding from the header info;
+ * if Content-Encoding equal 'chunked' return 1,or return 0.
+ */
+int get_transfer_encoding(const char* header) {
+  int result = 0;
+  int ret = 0;
+  char *transfer_encoding = (char*)malloc(sizeof(char)*10);
+  result = find_header_info(header,transfer_encoding,"Transfer-Encoding");
+
+  if(result && strncmp(transfer_encoding,"chunked",7)==0){
+    ret = 1;
+  }
+  else {
+    ret = 0;
+  }
+	
+  if(transfer_encoding != NULL) {
+    free(transfer_encoding);
+  }
+  return ret;
+}
+
+/* Returns NULL on invalid input */
+char* decodechunked(char * chunked, unsigned int *inputlen) {
+  char *orig = chunked, *dest = chunked;
+  unsigned long chunklen;
+  while((chunklen = strtoul(orig, &orig, 16))) {
+    /* process one more chunk: */
+    /* skip chunk-extension part */
+    while(*orig && (*orig != '\r'))
+      orig++;
+      /* skip '\r\n' after chunk length */
+    orig += 2;
+    if(( chunklen > (chunked + *inputlen - orig)))
+      /* insane chunk length. Well... */
+      return NULL;
+    memmove(dest, orig, chunklen);
+    dest += chunklen;
+    orig += chunklen;
+    /* and go to the next chunk */
+  }
+  *dest = '\0';
+  *inputlen = dest - chunked;
+	
+  return chunked;
+}
diff --git a/plugins/utils.h b/plugins/utils.h
index 3c3f189..78a74aa 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -204,4 +204,12 @@ The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\
 copies of the plugins under the terms of the GNU General Public License.\n\
 For more information about these matters, see the file named COPYING.\n")
 
+#define UT_DECOMPRESS _("\
+ -d, --decompress\n\
+    Decompress the contents of page which were compressed by gzip or deflate.\n")
+
+#define UT_CHUNKED _("\
+ -X, --chunked\n\
+    Deal with the data which was transferred by chunked encoding.")
+
 #endif /* NP_UTILS_H */