Move the base-64 routines to "epan/base64.c".
[obnox/wireshark/wip.git] / epan / dissectors / packet-http.c
1 /* packet-http.c
2  * Routines for HTTP packet disassembly
3  * RFC 1945 (HTTP/1.0)
4  * RFC 2616 (HTTP/1.1)
5  *
6  * Guy Harris <guy@alum.mit.edu>
7  *
8  * Copyright 2004, Jerry Talkington <jtalkington@users.sourceforge.net>
9  * Copyright 2002, Tim Potter <tpot@samba.org>
10  * Copyright 1999, Andrew Tridgell <tridge@samba.org>
11  *
12  * $Id$
13  *
14  * Ethereal - Network traffic analyzer
15  * By Gerald Combs <gerald@ethereal.com>
16  * Copyright 1998 Gerald Combs
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <string.h>
38 #include <ctype.h>
39
40 #include <glib.h>
41 #include <epan/packet.h>
42 #include <epan/strutil.h>
43 #include <epan/base64.h>
44
45 #include "req_resp_hdrs.h"
46 #include "packet-http.h"
47 #include "prefs.h"
48
49 typedef enum _http_type {
50         HTTP_REQUEST,
51         HTTP_RESPONSE,
52         HTTP_NOTIFICATION,
53         HTTP_OTHERS
54 } http_type_t;
55
56 #include "tap.h"
57
58 static int http_tap = -1;
59
60 static int proto_http = -1;
61 static int hf_http_notification = -1;
62 static int hf_http_response = -1;
63 static int hf_http_request = -1;
64 static int hf_http_basic = -1;
65 static int hf_http_request_method = -1;
66 static int hf_http_response_code = -1;
67 static int hf_http_authorization = -1;
68 static int hf_http_proxy_authenticate = -1;
69 static int hf_http_proxy_authorization = -1;
70 static int hf_http_www_authenticate = -1;
71 static int hf_http_content_type = -1;
72 static int hf_http_content_length = -1;
73 static int hf_http_content_encoding = -1;
74 static int hf_http_transfer_encoding = -1;
75
76 static gint ett_http = -1;
77 static gint ett_http_ntlmssp = -1;
78 static gint ett_http_request = -1;
79 static gint ett_http_chunked_response = -1;
80 static gint ett_http_chunk_data = -1;
81 static gint ett_http_encoded_entity = -1;
82
83 static dissector_handle_t data_handle;
84 static dissector_handle_t media_handle;
85 static dissector_handle_t http_handle;
86
87 /*
88  * desegmentation of HTTP headers
89  * (when we are over TCP or another protocol providing the desegmentation API)
90  */
91 static gboolean http_desegment_headers = FALSE;
92
93 /*
94  * desegmentation of HTTP bodies
95  * (when we are over TCP or another protocol providing the desegmentation API)
96  * TODO let the user filter on content-type the bodies he wants desegmented
97  */
98 static gboolean http_desegment_body = FALSE;
99
100 /*
101  * De-chunking of content-encoding: chunk entity bodies.
102  */
103 static gboolean http_dechunk_body = TRUE;
104
105 /*
106  * Decompression of zlib encoded entities.
107  */
108 #ifdef HAVE_LIBZ
109 static gboolean http_decompress_body = TRUE;
110 #else
111 static gboolean http_decompress_body = FALSE;
112 #endif
113
114
115 #define TCP_PORT_HTTP                   80
116 #define TCP_PORT_PROXY_HTTP             3128
117 #define TCP_PORT_PROXY_ADMIN_HTTP       3132
118 #define TCP_ALT_PORT_HTTP               8080
119 #define TCP_PORT_HKP                    11371
120 #define TCP_PORT_DAAP                   3689
121 /*
122  * SSDP is implemented atop HTTP (yes, it really *does* run over UDP).
123  */
124 #define TCP_PORT_SSDP                   1900
125 #define UDP_PORT_SSDP                   1900
126
127 /*
128  * Protocols implemented atop HTTP.
129  */
130 typedef enum {
131         PROTO_HTTP,             /* just HTTP */
132         PROTO_SSDP,             /* Simple Service Discovery Protocol */
133         PROTO_DAAP              /* Digital Audio Access Protocol */
134 } http_proto_t;
135
136 typedef void (*RequestDissector)(tvbuff_t*, proto_tree*, int);
137
138 /*
139  * Structure holding information from headers needed by main
140  * HTTP dissector code.
141  */
142 typedef struct {
143         char    *content_type;
144         char    *content_type_parameters;
145         long    content_length; /* XXX - make it 64-bit? */
146         char    *content_encoding;
147         char    *transfer_encoding;
148 } headers_t;
149
150 static int is_http_request_or_reply(const gchar *data, int linelen, http_type_t *type,
151                 RequestDissector *req_dissector, int *req_strlen);
152 static int chunked_encoding_dissector(tvbuff_t **tvb_ptr, packet_info *pinfo,
153                 proto_tree *tree, int offset);
154 static void process_header(tvbuff_t *tvb, int offset, int next_offset,
155     const guchar *line, int linelen, int colon_offset, packet_info *pinfo,
156     proto_tree *tree, headers_t *eh_ptr);
157 static gint find_header_hf_value(tvbuff_t *tvb, int offset, guint header_len);
158 static gboolean check_auth_ntlmssp(proto_item *hdr_item, tvbuff_t *tvb,
159     packet_info *pinfo, gchar *value);
160 static gboolean check_auth_basic(proto_item *hdr_item, tvbuff_t *tvb,
161     gchar *value);
162
163 static dissector_table_t port_subdissector_table;
164 static dissector_table_t media_type_subdissector_table;
165 static heur_dissector_list_t heur_subdissector_list;
166
167 static dissector_handle_t ntlmssp_handle=NULL;
168
169
170 /* Return a tvb that contains the binary representation of a base64
171    string */
172
173 static tvbuff_t *
174 base64_to_tvb(const char *base64)
175 {
176         tvbuff_t *tvb;
177         char *data = g_strdup(base64);
178         size_t len;
179
180         len = epan_base64_decode(data);
181         tvb = tvb_new_real_data((const guint8 *)data, len, len);
182
183         tvb_set_free_cb(tvb, g_free);
184
185         return tvb;
186 }
187
188 static void
189 dissect_http_ntlmssp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
190     const char *line)
191 {
192         tvbuff_t *ntlmssp_tvb;
193
194         ntlmssp_tvb = base64_to_tvb(line);
195         tvb_set_child_real_data_tvbuff(tvb, ntlmssp_tvb);
196         add_new_data_source(pinfo, ntlmssp_tvb, "NTLMSSP Data");
197
198         call_dissector(ntlmssp_handle, ntlmssp_tvb, pinfo, tree);
199 }
200
201 static void
202 cleanup_headers(void *arg)
203 {
204         headers_t *headers = arg;
205
206         if (headers->content_type != NULL)
207                 g_free(headers->content_type);
208         /*
209          * The content_type_parameters field actually points into the
210          * content_type headers, so don't free it, as that'll double-free
211          * some memory.
212          */
213         if (headers->content_encoding != NULL)
214                 g_free(headers->content_encoding);
215         if (headers->transfer_encoding != NULL)
216                 g_free(headers->transfer_encoding);
217 }
218
219 /*
220  * TODO: remove this ugly global variable.
221  *
222  * XXX - we leak "http_info_value_t" structures.
223  * XXX - this gets overwritten if there's more than one HTTP request or
224  * reply in the tvbuff.
225  */
226 static http_info_value_t        *stat_info;
227
228 static int
229 dissect_http_message(tvbuff_t *tvb, int offset, packet_info *pinfo,
230     proto_tree *tree)
231 {
232         http_proto_t    proto;
233         char            *proto_tag;
234         proto_tree      *http_tree = NULL;
235         proto_item      *ti = NULL;
236         const guchar    *line;
237         gint            next_offset;
238         const guchar    *linep, *lineend;
239         int             orig_offset;
240         int             first_linelen, linelen;
241         gboolean        is_request_or_reply;
242         gboolean        saw_req_resp_or_header;
243         guchar          c;
244         http_type_t     http_type;
245         proto_item      *hdr_item;
246         RequestDissector req_dissector;
247         int             req_strlen;
248         proto_tree      *req_tree;
249         int             colon_offset;
250         headers_t       headers;
251         int             datalen;
252         int             reported_datalen;
253         dissector_handle_t handle;
254         gboolean        dissected;
255
256         /*
257          * Is this a request or response?
258          *
259          * Note that "tvb_find_line_end()" will return a value that
260          * is not longer than what's in the buffer, so the
261          * "tvb_get_ptr()" call won't throw an exception.
262          */
263         first_linelen = tvb_find_line_end(tvb, offset,
264             tvb_ensure_length_remaining(tvb, offset), &next_offset,
265             FALSE);
266         /*
267          * Is the first line a request or response?
268          */
269         line = tvb_get_ptr(tvb, offset, first_linelen);
270         http_type = HTTP_OTHERS;        /* type not known yet */
271         is_request_or_reply = is_http_request_or_reply((const gchar *)line,
272                         first_linelen, &http_type, NULL, NULL);
273         if (is_request_or_reply) {
274                 /*
275                  * Yes, it's a request or response.
276                  * Do header desegmentation if we've been told to,
277                  * and do body desegmentation if we've been told to and
278                  * we find a Content-Length header.
279                  */
280                 if (!req_resp_hdrs_do_reassembly(tvb, pinfo,
281                     http_desegment_headers, http_desegment_body)) {
282                         /*
283                          * More data needed for desegmentation.
284                          */
285                         return -1;
286                 }
287         }
288
289         stat_info = g_malloc(sizeof(http_info_value_t));
290         stat_info->response_code = 0;
291         stat_info->request_method = NULL;
292
293         switch (pinfo->match_port) {
294
295         case TCP_PORT_SSDP:     /* TCP_PORT_SSDP = UDP_PORT_SSDP */
296                 proto = PROTO_SSDP;
297                 proto_tag = "SSDP";
298                 break;
299
300         case TCP_PORT_DAAP:     
301                 proto = PROTO_DAAP;
302                 proto_tag = "DAAP";
303                 break;
304
305         default:
306                 proto = PROTO_HTTP;
307                 proto_tag = "HTTP";
308                 break;
309         }
310
311         if (check_col(pinfo->cinfo, COL_PROTOCOL))
312                 col_set_str(pinfo->cinfo, COL_PROTOCOL, proto_tag);
313         if (check_col(pinfo->cinfo, COL_INFO)) {
314                 /*
315                  * Put the first line from the buffer into the summary
316                  * if it's an HTTP request or reply (but leave out the
317                  * line terminator).
318                  * Otherwise, just call it a continuation.
319                  *
320                  * Note that "tvb_find_line_end()" will return a value that
321                  * is not longer than what's in the buffer, so the
322                  * "tvb_get_ptr()" call won't throw an exception.
323                  */
324                 line = tvb_get_ptr(tvb, offset, first_linelen);
325                 if (is_request_or_reply)
326                         col_add_str(pinfo->cinfo, COL_INFO,
327                             format_text(line, first_linelen));
328                 else
329                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
330         }
331
332         orig_offset = offset;
333         if (tree) {
334                 ti = proto_tree_add_item(tree, proto_http, tvb, offset, -1,
335                     FALSE);
336                 http_tree = proto_item_add_subtree(ti, ett_http);
337         }
338
339         /*
340          * Process the packet data, a line at a time.
341          */
342         http_type = HTTP_OTHERS;        /* type not known yet */
343         headers.content_type = NULL;    /* content type not known yet */
344         headers.content_type_parameters = NULL; /* content type parameters too */
345         headers.content_length = -1;    /* content length not known yet */
346         headers.content_encoding = NULL; /* content encoding not known yet */
347         headers.transfer_encoding = NULL; /* transfer encoding not known yet */
348         saw_req_resp_or_header = FALSE; /* haven't seen anything yet */
349         CLEANUP_PUSH(cleanup_headers, &headers);
350         while (tvb_reported_length_remaining(tvb, offset) != 0) {
351                 /*
352                  * Find the end of the line.
353                  */
354                 linelen = tvb_find_line_end(tvb, offset,
355                     tvb_ensure_length_remaining(tvb, offset), &next_offset,
356                     FALSE);
357                 if (linelen < 0)
358                         return -1;
359
360                 /*
361                  * Get a buffer that refers to the line.
362                  */
363                 line = tvb_get_ptr(tvb, offset, linelen);
364                 lineend = line + linelen;
365                 colon_offset = -1;
366
367                 /*
368                  * OK, does it look like an HTTP request or response?
369                  */
370                 req_dissector = NULL;
371                 is_request_or_reply = is_http_request_or_reply((const gchar *)line,
372                                 linelen, &http_type, &req_dissector, &req_strlen);
373                 if (is_request_or_reply)
374                         goto is_http;
375
376                 /*
377                  * No.  Does it look like a blank line (as would appear
378                  * at the end of an HTTP request)?
379                  */
380                 if (linelen == 0)
381                         goto is_http;   /* Yes. */
382
383                 /*
384                  * No.  Does it look like a header?
385                  */
386                 linep = line;
387                 colon_offset = offset;
388                 while (linep < lineend) {
389                         c = *linep++;
390
391                         /*
392                          * This must be a CHAR to be part of a token; that
393                          * means it must be ASCII.
394                          */
395                         if (!isascii(c))
396                                 break;  /* not ASCII, thus not a CHAR */
397
398                         /*
399                          * This mustn't be a CTL to be part of a token.
400                          *
401                          * XXX - what about leading LWS on continuation
402                          * lines of a header?
403                          */
404                         if (iscntrl(c))
405                                 break;  /* CTL, not part of a header */
406
407                         /*
408                          * This mustn't be a SEP to be part of a token;
409                          * a ':' ends the token, everything else is an
410                          * indication that this isn't a header.
411                          */
412                         switch (c) {
413
414                         case '(':
415                         case ')':
416                         case '<':
417                         case '>':
418                         case '@':
419                         case ',':
420                         case ';':
421                         case '\\':
422                         case '"':
423                         case '/':
424                         case '[':
425                         case ']':
426                         case '?':
427                         case '=':
428                         case '{':
429                         case '}':
430                         case ' ':
431                                 /*
432                                  * It's a separator, so it's not part of a
433                                  * token, so it's not a field name for the
434                                  * beginning of a header.
435                                  *
436                                  * (We don't have to check for HT; that's
437                                  * already been ruled out by "iscntrl()".)
438                                  */
439                                 goto not_http;
440
441                         case ':':
442                                 /*
443                                  * This ends the token; we consider this
444                                  * to be a header.
445                                  */
446                                 goto is_http;
447
448                         default:
449                                 colon_offset++;
450                                 break;
451                         }
452                 }
453
454                 /*
455                  * We haven't seen the colon, but everything else looks
456                  * OK for a header line.
457                  *
458                  * If we've already seen an HTTP request or response
459                  * line, or a header line, and we're at the end of
460                  * the tvbuff, we assume this is an incomplete header
461                  * line.  (We quit this loop after seeing a blank line,
462                  * so if we've seen a request or response line, or a
463                  * header line, this is probably more of the request
464                  * or response we're presumably seeing.  There is some
465                  * risk of false positives, but the same applies for
466                  * full request or response lines or header lines,
467                  * although that's less likely.)
468                  *
469                  * We throw an exception in that case, by checking for
470                  * the existence of the next byte after the last one
471                  * in the line.  If it exists, "tvb_ensure_bytes_exist()"
472                  * throws no exception, and we fall through to the
473                  * "not HTTP" case.  If it doesn't exist,
474                  * "tvb_ensure_bytes_exist()" will throw the appropriate
475                  * exception.
476                  */
477                 if (saw_req_resp_or_header)
478                         tvb_ensure_bytes_exist(tvb, offset, linelen + 1);
479
480         not_http:
481                 /*
482                  * We don't consider this part of an HTTP request or
483                  * reply, so we don't display it.
484                  * (Yeah, that means we don't display, say, a text/http
485                  * page, but you can get that from the data pane.)
486                  */
487                 break;
488
489         is_http:
490                 /*
491                  * Process this line.
492                  */
493                 if (linelen == 0) {
494                         /*
495                          * This is a blank line, which means that
496                          * whatever follows it isn't part of this
497                          * request or reply.
498                          */
499                         proto_tree_add_text(http_tree, tvb, offset,
500                             next_offset - offset, "%s",
501                             tvb_format_text(tvb, offset, next_offset - offset));
502                         offset = next_offset;
503                         break;
504                 }
505
506                 /*
507                  * Not a blank line - either a request, a reply, or a header
508                  * line.
509                  */
510                 saw_req_resp_or_header = TRUE;
511                 if (is_request_or_reply) {
512                         if (tree) {
513                                 hdr_item = proto_tree_add_text(http_tree, tvb,
514                                     offset, next_offset - offset, "%s",
515                                     tvb_format_text(tvb, offset,
516                                       next_offset - offset));
517                                 if (req_dissector) {
518                                         req_tree = proto_item_add_subtree(
519                                             hdr_item, ett_http_request);
520                                         req_dissector(tvb, req_tree,
521                                             req_strlen);
522                                 }
523                         }
524                 } else {
525                         /*
526                          * Header.
527                          */
528                         process_header(tvb, offset, next_offset, line, linelen,
529                             colon_offset, pinfo, http_tree, &headers);
530                 }
531                 offset = next_offset;
532         }
533
534         if (tree) {
535                 switch (http_type) {
536
537                 case HTTP_NOTIFICATION:
538                         proto_tree_add_boolean_hidden(http_tree,
539                             hf_http_notification, tvb, 0, 0, 1);
540                         break;
541
542                 case HTTP_RESPONSE:
543                         proto_tree_add_boolean_hidden(http_tree,
544                             hf_http_response, tvb, 0, 0, 1);
545                         break;
546
547                 case HTTP_REQUEST:
548                         proto_tree_add_boolean_hidden(http_tree,
549                             hf_http_request, tvb, 0, 0, 1);
550                         break;
551
552                 case HTTP_OTHERS:
553                 default:
554                         break;
555                 }
556         }
557
558         /*
559          * If a content length was supplied, the amount of data to be
560          * processed as HTTP payload is the minimum of the content
561          * length and the amount of data remaining in the frame.
562          *
563          * If no content length was supplied (or if a bad content length
564          * was supplied), the amount of data to be processed is the amount
565          * of data remaining in the frame.
566          *
567          * If there was no Content-Length entity header, we should
568          * accumulate all data until the end of the connection.
569          * That'd require that the TCP dissector call subdissectors
570          * for all frames with FIN, even if they contain no data,
571          * which would require subdissectors to deal intelligently
572          * with empty segments.
573          *
574          * Acccording to RFC 2616, however, 1xx responses, 204 responses,
575          * and 304 responses MUST NOT include a message body; if no
576          * content length is specified for them, we don't attempt to
577          * dissect the body.
578          *
579          * XXX - it says the same about responses to HEAD requests;
580          * unless there's a way to determine from the response
581          * whether it's a response to a HEAD request, we have to
582          * keep information about the request and associate that with
583          * the response in order to handle that.
584          */
585         datalen = tvb_length_remaining(tvb, offset);
586         if (headers.content_length != -1) {
587                 if (datalen > headers.content_length)
588                         datalen = headers.content_length;
589
590                 /*
591                  * XXX - limit the reported length in the tvbuff we'll
592                  * hand to a subdissector to be no greater than the
593                  * content length.
594                  *
595                  * We really need both unreassembled and "how long it'd
596                  * be if it were reassembled" lengths for tvbuffs, so
597                  * that we throw the appropriate exceptions for
598                  * "not enough data captured" (running past the length),
599                  * "packet needed reassembly" (within the length but
600                  * running past the unreassembled length), and
601                  * "packet is malformed" (running past the reassembled
602                  * length).
603                  */
604                 reported_datalen = tvb_reported_length_remaining(tvb, offset);
605                 if (reported_datalen > headers.content_length)
606                         reported_datalen = headers.content_length;
607         } else {
608                 if ((stat_info->response_code/100) == 1 ||
609                     stat_info->response_code == 204 ||
610                     stat_info->response_code == 304)
611                         datalen = 0;    /* no content! */
612                 else
613                         reported_datalen = -1;
614         }
615
616         if (datalen > 0) {
617                 /*
618                  * There's stuff left over; process it.
619                  */
620                 tvbuff_t *next_tvb;
621                 void *save_private_data = NULL;
622                 gint chunks_decoded = 0;
623
624                 /*
625                  * Create a tvbuff for the payload.
626                  *
627                  * The amount of data to be processed that's
628                  * available in the tvbuff is "datalen", which
629                  * is the minimum of the amount of data left in
630                  * the tvbuff and any specified content length.
631                  *
632                  * The amount of data to be processed that's in
633                  * this frame, regardless of whether it was
634                  * captured or not, is "reported_datalen",
635                  * which, if no content length was specified,
636                  * is -1, i.e. "to the end of the frame.
637                  */
638                 next_tvb = tvb_new_subset(tvb, offset, datalen,
639                     reported_datalen);
640                 /*
641                  * BEWARE - next_tvb is a subset of another tvb,
642                  * so we MUST NOT attempt tvb_free(next_tvb);
643                  */
644
645                 /*
646                  * Handle *transfer* encodings other than "identity".
647                  */
648                 if (headers.transfer_encoding != NULL &&
649                     strcasecmp(headers.transfer_encoding, "identity") != 0) {
650                         if (http_dechunk_body &&
651                             (strcasecmp(headers.transfer_encoding, "chunked")
652                             == 0)) {
653
654                                 chunks_decoded = chunked_encoding_dissector(
655                                     &next_tvb, pinfo, http_tree, 0);
656
657                                 if (chunks_decoded <= 0) {
658                                         /*
659                                          * The chunks weren't reassembled,
660                                          * or there was a single zero
661                                          * length chunk.
662                                          */
663                                         goto body_dissected;
664                                 } else {
665                                         /*
666                                          * Add a new data source for the
667                                          * de-chunked data.
668                                          */
669                                         tvb_set_child_real_data_tvbuff(tvb,
670                                                 next_tvb);
671                                         add_new_data_source(pinfo, next_tvb,
672                                                 "De-chunked entity body");
673                                 }
674                         } else {
675                                 /*
676                                  * We currently can't handle, for example,
677                                  * "gzip", "compress", or "deflate" as
678                                  * *transfer* encodings; just handle them
679                                  * as data for now.
680                                  */
681                                 call_dissector(data_handle, next_tvb, pinfo,
682                                     http_tree);
683                                 goto body_dissected;
684                         }
685                 }
686                 /*
687                  * At this point, any chunked *transfer* coding has been removed
688                  * (the entity body has been dechunked) so it can be presented
689                  * for the following operation (*content* encoding), or it has
690                  * been been handed off to the data dissector.
691                  *
692                  * Handle *content* encodings other than "identity" (which
693                  * shouldn't appear in a Content-Encoding header, but
694                  * we handle it in any case).
695                  */
696                 if (headers.content_encoding != NULL &&
697                     strcasecmp(headers.content_encoding, "identity") != 0) {
698                         /*
699                          * We currently can't handle, for example, "compress";
700                          * just handle them as data for now.
701                          *
702                          * After July 7, 2004 the LZW patent expires, so support
703                          * might be added then.  However, I don't think that
704                          * anybody ever really implemented "compress", due to
705                          * the aforementioned patent.
706                          */
707                         tvbuff_t *uncomp_tvb = NULL;
708                         proto_item *e_ti = NULL;
709                         proto_tree *e_tree = NULL;
710
711                         if (http_decompress_body &&
712                             (strcasecmp(headers.content_encoding, "gzip") == 0 ||
713                             strcasecmp(headers.content_encoding, "deflate")
714                             == 0)) {
715
716                                 uncomp_tvb = tvb_uncompress(next_tvb, 0,
717                                     tvb_length(next_tvb));
718                         }
719
720                         /*
721                          * Add the encoded entity to the protocol tree
722                          */
723                         e_ti = proto_tree_add_text(http_tree, next_tvb,
724                                         0, tvb_length(next_tvb),
725                                         "Content-encoded entity body (%s)",
726                                         headers.content_encoding);
727                         e_tree = proto_item_add_subtree(e_ti,
728                                         ett_http_encoded_entity);
729
730                         if (uncomp_tvb != NULL) {
731                                 /*
732                                  * Decompression worked
733                                  */
734
735                                 /* XXX - Don't free this, since it's possible
736                                  * that the data was only partially
737                                  * decompressed, such as when desegmentation
738                                  * isn't enabled.
739                                  *
740                                 tvb_free(next_tvb);
741                                 */
742                                 next_tvb = uncomp_tvb;
743                                 tvb_set_child_real_data_tvbuff(tvb, next_tvb);
744                                 add_new_data_source(pinfo, next_tvb,
745                                     "Uncompressed entity body");
746                         } else {
747                                 if (chunks_decoded > 1) {
748                                         tvb_set_child_real_data_tvbuff(tvb,
749                                             next_tvb);
750                                         add_new_data_source(pinfo, next_tvb,
751                                             "Compressed entity body");
752                                 }
753                                 call_dissector(data_handle, next_tvb, pinfo,
754                                     e_tree);
755
756                                 goto body_dissected;
757                         }
758                 }
759                 /*
760                  * Note that a new data source is added for the entity body
761                  * only if it was content-encoded and/or transfer-encoded.
762                  */
763
764                 /*
765                  * Do subdissector checks.
766                  *
767                  * First, check whether some subdissector asked that they
768                  * be called if something was on some particular port.
769                  */
770                 handle = dissector_get_port_handle(port_subdissector_table,
771                     pinfo->match_port);
772                 if (handle == NULL && headers.content_type != NULL) {
773                         /*
774                          * We didn't find any subdissector that
775                          * registered for the port, and we have a
776                          * Content-Type value.  Is there any subdissector
777                          * for that content type?
778                          */
779                         save_private_data = pinfo->private_data;
780                         /*
781                          * XXX - this won't get freed if the subdissector
782                          * throws an exception.  Do we really need to
783                          * strdup it?
784                          */
785                         if (headers.content_type_parameters)
786                                 pinfo->private_data = g_strdup(headers.content_type_parameters);
787                         else
788                                 pinfo->private_data = NULL;
789                         /*
790                          * Calling the string handle for the media type
791                          * dissector table will set pinfo->match_string
792                          * to headers.content_type for us.
793                          */
794                         pinfo->match_string = headers.content_type;
795                         handle = dissector_get_string_handle(
796                             media_type_subdissector_table,
797                             headers.content_type);
798                         /*
799                          * Calling the default media handle otherwise
800                          */
801                         if (handle == NULL) {
802                             handle = media_handle;
803                         }
804                 }
805                 if (handle != NULL) {
806                         /*
807                          * We have a subdissector - call it.
808                          */
809                         dissected = call_dissector(handle, next_tvb, pinfo,
810                             tree);
811                 } else {
812                         /*
813                          * We don't have a subdissector - try the heuristic
814                          * subdissectors.
815                          */
816                         dissected = dissector_try_heuristic(
817                             heur_subdissector_list, next_tvb, pinfo, tree);
818                 }
819                 if (dissected) {
820                         /*
821                          * The subdissector dissected the body.
822                          * Fix up the top-level item so that it doesn't
823                          * include the stuff for that protocol.
824                          */
825                         if (ti != NULL)
826                                 proto_item_set_len(ti, offset);
827                 } else {
828                         call_dissector(data_handle, next_tvb, pinfo,
829                             http_tree);
830                 }
831
832         body_dissected:
833                 /*
834                  * Do *not* attempt at freeing the private data;
835                  * it may be in use by subdissectors.
836                  */
837                 if (save_private_data)
838                         pinfo->private_data = save_private_data;
839                 /*
840                  * We've processed "datalen" bytes worth of data
841                  * (which may be no data at all); advance the
842                  * offset past whatever data we've processed.
843                  */
844                 offset += datalen;
845         }
846
847         /*
848          * Clean up any header stuff, by calling and popping the cleanup
849          * handler.
850          */
851         CLEANUP_CALL_AND_POP;
852
853         tap_queue_packet(http_tap, pinfo, stat_info);
854
855         return offset - orig_offset;
856 }
857
858 /* This can be used to dissect an HTTP request until such time
859  * that a more complete dissector is written for that HTTP request.
860  * This simple dissectory only puts http.request_method into a sub-tree.
861  */
862 static void
863 basic_request_dissector(tvbuff_t *tvb, proto_tree *tree, int req_strlen)
864 {
865         proto_tree_add_item(tree, hf_http_request_method, tvb, 0, req_strlen, FALSE);
866 }
867
868 static void
869 basic_response_dissector(tvbuff_t *tvb, proto_tree *tree, int resp_strlen)
870 {
871         gchar *data;
872         int minor, major, status_code;
873
874         /* BEWARE - sscanf() only operates on C strings.
875          * The pointer returned by tvb_get_ptr points into the real data,
876          * which is not necessarily NULL terminated. For this reason,
877          * the sscanf() call is only applied to a buffer guaranteed to
878          * only contain a NULL terminated string. */
879         data = g_strndup((const gchar *)tvb_get_ptr(tvb, 5, resp_strlen), resp_strlen);
880         if (sscanf((const gchar *)data, "%d.%d %d", &minor, &major, &status_code) == 3) {
881                 proto_tree_add_uint(tree, hf_http_response_code, tvb, 9, 3, status_code);
882                 stat_info->response_code = status_code;
883         }
884         g_free(data);
885 }
886
887 /*
888  * Dissect the http data chunks and add them to the tree.
889  */
890 static int
891 chunked_encoding_dissector(tvbuff_t **tvb_ptr, packet_info *pinfo,
892     proto_tree *tree, int offset)
893 {
894         guint8 *chunk_string = NULL;
895         gint chunk_size = 0;
896         gint chunk_offset = 0;
897         gint datalen = 0;
898         gint linelen = 0;
899         gint chunks_decoded = 0;
900         tvbuff_t *tvb = NULL;
901         tvbuff_t *new_tvb = NULL;
902         gint chunked_data_size = 0;
903         proto_tree *subtree = NULL;
904         proto_item *ti = NULL;
905
906         if (tvb_ptr == NULL || *tvb_ptr == NULL) {
907                 return 0;
908         }
909
910         tvb = *tvb_ptr;
911
912         datalen = tvb_reported_length_remaining(tvb, offset);
913
914         if (tree) {
915                 ti = proto_tree_add_text(tree, tvb, offset, datalen,
916                     "HTTP chunked response");
917                 subtree = proto_item_add_subtree(ti, ett_http_chunked_response);
918         }
919
920
921         while (datalen != 0) {
922                 proto_item *chunk_ti = NULL;
923                 proto_tree *chunk_subtree = NULL;
924                 tvbuff_t *data_tvb = NULL;
925                 gchar *c = NULL;
926
927                 linelen = tvb_find_line_end(tvb, offset, -1, &chunk_offset, TRUE);
928
929                 if (linelen <= 0) {
930                         /* Can't get the chunk size line */
931                         break;
932                 }
933
934                 chunk_string = tvb_get_string(tvb, offset, linelen);
935
936                 if (chunk_string == NULL) {
937                         /* Can't get the chunk size line */
938                         break;
939                 }
940
941                 c = chunk_string;
942
943                 /*
944                  * We don't care about the extensions.
945                  */
946                 if ((c = strchr(c, ';'))) {
947                         *c = '\0';
948                 }
949
950                 if (sscanf(chunk_string, "%x", &chunk_size) != 1) {
951                         g_free(chunk_string);
952                         break;
953                 }
954
955                 g_free(chunk_string);
956
957
958                 if (chunk_size > datalen) {
959                         /*
960                          * The chunk size is more than what's in the tvbuff,
961                          * so either the user hasn't enabled decoding, or all
962                          * of the segments weren't captured.
963                          */
964                         chunk_size = datalen;
965                 }/* else if (new_tvb == NULL) {
966                         new_tvb = tvb_new_composite();
967                 }
968
969
970
971                 if (new_tvb != NULL && chunk_size != 0) {
972                         tvbuff_t *chunk_tvb = NULL;
973
974                         chunk_tvb = tvb_new_subset(tvb, chunk_offset,
975                             chunk_size, datalen);
976
977                         tvb_composite_append(new_tvb, chunk_tvb);
978
979                 }
980                 */
981
982                 chunked_data_size += chunk_size;
983
984                 if (chunk_size != 0) {
985                         guint8 *raw_data = g_malloc(chunked_data_size);
986                         gint raw_len = 0;
987
988                         if (new_tvb != NULL) {
989                                 raw_len = tvb_length_remaining(new_tvb, 0);
990                                 tvb_memcpy(new_tvb, raw_data, 0, raw_len);
991
992                                 tvb_free(new_tvb);
993                         }
994
995                         tvb_memcpy(tvb, (guint8 *)(raw_data + raw_len),
996                             chunk_offset, chunk_size);
997
998                         new_tvb = tvb_new_real_data(raw_data,
999                             chunked_data_size, chunked_data_size);
1000                         tvb_set_free_cb(new_tvb, g_free);
1001
1002                 }
1003
1004                 if (subtree) {
1005                         if (chunk_size == 0) {
1006                                 chunk_ti = proto_tree_add_text(subtree, tvb,
1007                                     offset,
1008                                     chunk_offset - offset + chunk_size + 2,
1009                                     "Data chunk (last chunk)");
1010                         } else {
1011                                 chunk_ti = proto_tree_add_text(subtree, tvb,
1012                                     offset,
1013                                     chunk_offset - offset + chunk_size + 2,
1014                                     "Data chunk (%u octets)", chunk_size);
1015                         }
1016
1017                         chunk_subtree = proto_item_add_subtree(chunk_ti,
1018                             ett_http_chunk_data);
1019
1020                         proto_tree_add_text(chunk_subtree, tvb, offset,
1021                             chunk_offset - offset, "Chunk size: %u octets",
1022                             chunk_size);
1023
1024                         data_tvb = tvb_new_subset(tvb, chunk_offset, chunk_size,
1025                             datalen);
1026
1027
1028                         if (chunk_size > 0) {
1029                                 call_dissector(data_handle, data_tvb, pinfo,
1030                                     chunk_subtree);
1031                         }
1032
1033                         proto_tree_add_text(chunk_subtree, tvb, chunk_offset +
1034                             chunk_size, 2, "Chunk boundary");
1035                 }
1036
1037                 chunks_decoded++;
1038                 offset = chunk_offset + chunk_size + 2;
1039                 datalen = tvb_reported_length_remaining(tvb, offset);
1040         }
1041
1042         if (new_tvb != NULL) {
1043
1044                 /* Placeholder for the day that composite tvbuffer's will work.
1045                 tvb_composite_finalize(new_tvb);
1046                 / * tvb_set_reported_length(new_tvb, chunked_data_size); * /
1047                 */
1048
1049                 /*
1050                  * XXX - Don't free this, since the tvbuffer that was passed
1051                  * may be used if the data spans multiple frames and reassembly
1052                  * isn't enabled.
1053                  *
1054                 tvb_free(*tvb_ptr);
1055                  */
1056                 *tvb_ptr = new_tvb;
1057
1058         } else {
1059                 /*
1060                  * We didn't create a new tvb, so don't allow sub dissectors
1061                  * try to decode the non-existant entity body.
1062                  */
1063                 chunks_decoded = -1;
1064         }
1065
1066         return chunks_decoded;
1067
1068 }
1069
1070
1071 /*
1072  * XXX - this won't handle HTTP 0.9 replies, but they're all data
1073  * anyway.
1074  */
1075 static int
1076 is_http_request_or_reply(const gchar *data, int linelen, http_type_t *type,
1077                 RequestDissector *req_dissector, int *req_strlen)
1078 {
1079         int isHttpRequestOrReply = FALSE;
1080         int prefix_len = 0;
1081
1082         /*
1083          * From RFC 2774 - An HTTP Extension Framework
1084          *
1085          * Support the command prefix that identifies the presence of
1086          * a "mandatory" header.
1087          */
1088         if (linelen >= 2 && strncmp(data, "M-", 2) == 0) {
1089                 data += 2;
1090                 linelen -= 2;
1091                 prefix_len = 2;
1092         }
1093
1094         /*
1095          * From draft-cohen-gena-client-01.txt, available from the uPnP forum:
1096          *      NOTIFY, SUBSCRIBE, UNSUBSCRIBE
1097          *
1098          * From draft-ietf-dasl-protocol-00.txt, a now vanished Microsoft draft:
1099          *      SEARCH
1100          */
1101         if (linelen >= 5 && strncmp(data, "HTTP/", 5) == 0) {
1102                 *type = HTTP_RESPONSE;
1103                 isHttpRequestOrReply = TRUE;    /* response */
1104                 if (req_dissector) {
1105                         *req_dissector = basic_response_dissector;
1106                         *req_strlen = linelen - 5;
1107                 }
1108         } else {
1109                 const guchar * ptr = (const guchar *)data;
1110                 int              index = 0;
1111
1112                 /* Look for the space following the Method */
1113                 while (index < linelen) {
1114                         if (*ptr == ' ')
1115                                 break;
1116                         else {
1117                                 ptr++;
1118                                 index++;
1119                         }
1120                 }
1121
1122                 /* Check the methods that have same length */
1123                 switch (index) {
1124
1125                 case 3:
1126                         if (strncmp(data, "GET", index) == 0 ||
1127                             strncmp(data, "PUT", index) == 0) {
1128                                 *type = HTTP_REQUEST;
1129                                 isHttpRequestOrReply = TRUE;
1130                         }
1131                         else if (strncmp(data, "ICY", index) == 0) {
1132                                 *type = HTTP_RESPONSE;
1133                                 isHttpRequestOrReply = TRUE;
1134                         }
1135                         break;
1136
1137                 case 4:
1138                         if (strncmp(data, "COPY", index) == 0 ||
1139                             strncmp(data, "HEAD", index) == 0 ||
1140                             strncmp(data, "LOCK", index) == 0 ||
1141                             strncmp(data, "MOVE", index) == 0 ||
1142                             strncmp(data, "POLL", index) == 0 ||
1143                             strncmp(data, "POST", index) == 0) {
1144                                 *type = HTTP_REQUEST;
1145                                 isHttpRequestOrReply = TRUE;
1146                         }
1147                         break;
1148
1149                 case 5:
1150                         if (strncmp(data, "BCOPY", index) == 0 ||
1151                                 strncmp(data, "BMOVE", index) == 0 ||
1152                                 strncmp(data, "MKCOL", index) == 0 ||
1153                                 strncmp(data, "TRACE", index) == 0 ||
1154                                 strncmp(data, "LABEL", index) == 0 ||  /* RFC 3253 8.2 */
1155                                 strncmp(data, "MERGE", index) == 0) {  /* RFC 3253 11.2 */
1156                                 *type = HTTP_REQUEST;
1157                                 isHttpRequestOrReply = TRUE;
1158                         }
1159                         break;
1160
1161                 case 6:
1162                         if (strncmp(data, "DELETE", index) == 0 ||
1163                                 strncmp(data, "SEARCH", index) == 0 ||
1164                                 strncmp(data, "UNLOCK", index) == 0 ||
1165                                 strncmp(data, "REPORT", index) == 0 ||  /* RFC 3253 3.6 */
1166                                 strncmp(data, "UPDATE", index) == 0) {  /* RFC 3253 7.1 */
1167                                 *type = HTTP_REQUEST;
1168                                 isHttpRequestOrReply = TRUE;
1169                         }
1170                         else if (strncmp(data, "NOTIFY", index) == 0) {
1171                                 *type = HTTP_NOTIFICATION;
1172                                 isHttpRequestOrReply = TRUE;
1173                         }
1174                         break;
1175
1176                 case 7:
1177                         if (strncmp(data, "BDELETE", index) == 0 ||
1178                             strncmp(data, "CONNECT", index) == 0 ||
1179                             strncmp(data, "OPTIONS", index) == 0 ||
1180                                 strncmp(data, "CHECKIN", index) == 0) {  /* RFC 3253 4.4, 9.4 */
1181                                 *type = HTTP_REQUEST;
1182                                 isHttpRequestOrReply = TRUE;
1183                         }
1184                         break;
1185
1186                 case 8:
1187                         if (strncmp(data, "PROPFIND", index) == 0 ||
1188                                 strncmp(data, "CHECKOUT", index) == 0) {  /* RFC 3253 4.3, 9.3 */
1189                                 *type = HTTP_REQUEST;
1190                                 isHttpRequestOrReply = TRUE;
1191                         }
1192                         break;
1193
1194                 case 9:
1195                         if (strncmp(data, "SUBSCRIBE", index) == 0) {
1196                                 *type = HTTP_NOTIFICATION;
1197                                 isHttpRequestOrReply = TRUE;
1198                         } else if (strncmp(data, "PROPPATCH", index) == 0 ||
1199                             strncmp(data, "BPROPFIND", index) == 0) {
1200                                 *type = HTTP_REQUEST;
1201                                 isHttpRequestOrReply = TRUE;
1202                         }
1203                         break;
1204
1205                 case 10:
1206                         if (strncmp(data, "BPROPPATCH", index) == 0 ||
1207                                 strncmp(data, "UNCHECKOUT", index) == 0 ||  /* RFC 3253 4.5 */
1208                                 strncmp(data, "MKACTIVITY", index) == 0) {  /* RFC 3253 13.5 */
1209                                 *type = HTTP_REQUEST;
1210                                 isHttpRequestOrReply = TRUE;
1211                         }
1212                         break;
1213
1214                 case 11:
1215                         if (strncmp(data, "MKWORKSPACE", index) == 0) {  /* RFC 3253 6.3 */
1216                                 *type = HTTP_REQUEST;
1217                                 isHttpRequestOrReply = TRUE;
1218                         } else if (strncmp(data, "UNSUBSCRIBE", index) == 0) {
1219                                 *type = HTTP_NOTIFICATION;
1220                                 isHttpRequestOrReply = TRUE;
1221                         }
1222                         break;
1223
1224                 case 15:
1225                         if (strncmp(data, "VERSION-CONTROL", index) == 0) {  /* RFC 3253 3.5 */
1226                                 *type = HTTP_REQUEST;
1227                                 isHttpRequestOrReply = TRUE;
1228                         }
1229                         break;
1230
1231                 case 16:
1232                         if (strncmp(data, "BASELINE-CONTROL", index) == 0) {  /* RFC 3253 12.6 */
1233                                 *type = HTTP_REQUEST;
1234                                 isHttpRequestOrReply = TRUE;
1235                         }
1236                         break;
1237
1238                 default:
1239                         break;
1240                 }
1241
1242                 if (isHttpRequestOrReply && req_dissector) {
1243                         *req_dissector = basic_request_dissector;
1244                         *req_strlen = index + prefix_len;
1245                 }
1246                 if (isHttpRequestOrReply && req_dissector) {
1247                         if (!stat_info->request_method)
1248                                 stat_info->request_method = g_malloc( index+1 );
1249                                 strncpy( stat_info->request_method, data, index);
1250                                 stat_info->request_method[index] = '\0';
1251                 }
1252         }
1253
1254         return isHttpRequestOrReply;
1255 }
1256
1257 /*
1258  * Process headers.
1259  */
1260 typedef struct {
1261         char    *name;
1262         gint    *hf;
1263         int     special;
1264 } header_info;
1265
1266 #define HDR_NO_SPECIAL          0
1267 #define HDR_AUTHORIZATION       1
1268 #define HDR_AUTHENTICATE        2
1269 #define HDR_CONTENT_TYPE        3
1270 #define HDR_CONTENT_LENGTH      4
1271 #define HDR_CONTENT_ENCODING    5
1272 #define HDR_TRANSFER_ENCODING   6
1273
1274 static const header_info headers[] = {
1275         { "Authorization", &hf_http_authorization, HDR_AUTHORIZATION },
1276         { "Proxy-Authorization", &hf_http_proxy_authorization, HDR_AUTHORIZATION },
1277         { "Proxy-Authenticate", &hf_http_proxy_authenticate, HDR_AUTHENTICATE },
1278         { "WWW-Authenticate", &hf_http_www_authenticate, HDR_AUTHENTICATE },
1279         { "Content-Type", &hf_http_content_type, HDR_CONTENT_TYPE },
1280         { "Content-Length", &hf_http_content_length, HDR_CONTENT_LENGTH },
1281         { "Content-Encoding", &hf_http_content_encoding, HDR_CONTENT_ENCODING },
1282         { "Transfer-Encoding", &hf_http_transfer_encoding, HDR_TRANSFER_ENCODING },
1283 };
1284
1285 static void
1286 process_header(tvbuff_t *tvb, int offset, int next_offset,
1287     const guchar *line, int linelen, int colon_offset,
1288     packet_info *pinfo, proto_tree *tree, headers_t *eh_ptr)
1289 {
1290         int len;
1291         int line_end_offset;
1292         int header_len;
1293         gint hf_index;
1294         guchar c;
1295         int value_offset;
1296         int value_len;
1297         char *value;
1298         char *p;
1299         guchar *up;
1300         proto_item *hdr_item;
1301         int i;
1302
1303         len = next_offset - offset;
1304         line_end_offset = offset + linelen;
1305         header_len = colon_offset - offset;
1306         hf_index = find_header_hf_value(tvb, offset, header_len);
1307
1308         if (hf_index == -1) {
1309                 /*
1310                  * Not a header we know anything about.  Just put it into
1311                  * the tree as text.
1312                  */
1313                 if (tree) {
1314                         proto_tree_add_text(tree, tvb, offset, len,
1315                             "%s", format_text(line, len));
1316                 }
1317         } else {
1318                 /*
1319                  * Skip whitespace after the colon.
1320                  */
1321                 value_offset = colon_offset + 1;
1322                 while (value_offset < line_end_offset
1323                     && ((c = line[value_offset - offset]) == ' ' || c == '\t'))
1324                         value_offset++;
1325
1326                 /*
1327                  * Fetch the value.
1328                  */
1329                 value_len = line_end_offset - value_offset;
1330                 value = g_malloc(value_len + 1);
1331                 memcpy(value, &line[value_offset - offset], value_len);
1332                 value[value_len] = '\0';
1333                 CLEANUP_PUSH(g_free, value);
1334
1335                 /*
1336                  * Add it to the protocol tree as a particular field,
1337                  * but display the line as is.
1338                  */
1339                 if (tree) {
1340                         hdr_item = proto_tree_add_string_format(tree,
1341                             *headers[hf_index].hf, tvb, offset, len,
1342                             value, "%s", format_text(line, len));
1343                 } else
1344                         hdr_item = NULL;
1345
1346                 /*
1347                  * Do any special processing that particular headers
1348                  * require.
1349                  */
1350                 switch (headers[hf_index].special) {
1351
1352                 case HDR_AUTHORIZATION:
1353                         if (check_auth_ntlmssp(hdr_item, tvb, pinfo, value))
1354                                 break;  /* dissected NTLMSSP */
1355                         check_auth_basic(hdr_item, tvb, value);
1356                         break;
1357
1358                 case HDR_AUTHENTICATE:
1359                         check_auth_ntlmssp(hdr_item, tvb, pinfo, value);
1360                         break;
1361
1362                 case HDR_CONTENT_TYPE:
1363                         if (eh_ptr->content_type != NULL)
1364                                 g_free(eh_ptr->content_type);
1365                         eh_ptr->content_type = g_malloc(value_len + 1);
1366                         for (i = 0; i < value_len; i++) {
1367                                 c = value[i];
1368                                 if (c == ';' || isspace(c)) {
1369                                         /*
1370                                          * End of subtype - either
1371                                          * white space or a ";"
1372                                          * separating the subtype from
1373                                          * a parameter.
1374                                          */
1375                                         break;
1376                                 }
1377
1378                                 /*
1379                                  * Map the character to lower case;
1380                                  * content types are case-insensitive.
1381                                  */
1382                                 eh_ptr->content_type[i] = tolower(c);
1383                         }
1384                         eh_ptr->content_type[i] = '\0';
1385                         /*
1386                          * Now find the start of the optional parameters;
1387                          * skip the optional white space and the semicolon
1388                          * if this has not been done before.
1389                          */
1390                         i++;
1391                         while (i < value_len) {
1392                                 c = value[i];
1393                                 if (c == ';' || isspace(c))
1394                                         /* Skip till start of parameters */
1395                                         i++;
1396                                 else
1397                                         break;
1398                         }
1399                         if (i < value_len)
1400                                 eh_ptr->content_type_parameters = value + i;
1401                         else
1402                                 eh_ptr->content_type_parameters = NULL;
1403                         break;
1404
1405                 case HDR_CONTENT_LENGTH:
1406                         eh_ptr->content_length = strtol(value, &p, 10);
1407                         up = (guchar *)p;
1408                         if (eh_ptr->content_length < 0 || p == value ||
1409                             (*up != '\0' && !isspace(*up)))
1410                                 eh_ptr->content_length = -1;    /* not valid */
1411                         break;
1412
1413                 case HDR_CONTENT_ENCODING:
1414                         if (eh_ptr->content_encoding != NULL)
1415                                 g_free(eh_ptr->content_encoding);
1416                         eh_ptr->content_encoding = g_malloc(value_len + 1);
1417                         memcpy(eh_ptr->content_encoding, value, value_len);
1418                         eh_ptr->content_encoding[value_len] = '\0';
1419                         break;
1420
1421                 case HDR_TRANSFER_ENCODING:
1422                         if (eh_ptr->transfer_encoding != NULL)
1423                                 g_free(eh_ptr->transfer_encoding);
1424                         eh_ptr->transfer_encoding = g_malloc(value_len + 1);
1425                         memcpy(eh_ptr->transfer_encoding, value, value_len);
1426                         eh_ptr->transfer_encoding[value_len] = '\0';
1427                         break;
1428                 }
1429
1430                 /*
1431                  * Free the value, by calling and popping the cleanup
1432                  * handler for it.
1433                  */
1434                 CLEANUP_CALL_AND_POP;
1435         }
1436 }
1437
1438 /* Returns index of header tag in headers */
1439 static gint
1440 find_header_hf_value(tvbuff_t *tvb, int offset, guint header_len)
1441 {
1442         guint i;
1443
1444         for (i = 0; i < array_length(headers); i++) {
1445                 if (header_len == strlen(headers[i].name) &&
1446                     tvb_strncaseeql(tvb, offset,
1447                                                 headers[i].name, header_len) == 0)
1448                         return i;
1449         }
1450
1451         return -1;
1452 }
1453
1454 /*
1455  * Dissect Microsoft's abomination called NTLMSSP over HTTP.
1456  */
1457 static gboolean
1458 check_auth_ntlmssp(proto_item *hdr_item, tvbuff_t *tvb, packet_info *pinfo,
1459     gchar *value)
1460 {
1461         static const char *ntlm_headers[] = {
1462                 "NTLM ",
1463                 "Negotiate ",
1464                 NULL
1465         };
1466         const char **header;
1467         size_t hdrlen;
1468         proto_tree *hdr_tree;
1469
1470         /*
1471          * Check for NTLM credentials and challenge; those can
1472          * occur with WWW-Authenticate.
1473          */
1474         for (header = &ntlm_headers[0]; *header != NULL; header++) {
1475                 hdrlen = strlen(*header);
1476                 if (strncmp(value, *header, hdrlen) == 0) {
1477                         if (hdr_item != NULL) {
1478                                 hdr_tree = proto_item_add_subtree(hdr_item,
1479                                     ett_http_ntlmssp);
1480                         } else
1481                                 hdr_tree = NULL;
1482                         value += hdrlen;
1483                         dissect_http_ntlmssp(tvb, pinfo, hdr_tree, value);
1484                         return TRUE;
1485                 }
1486         }
1487         return FALSE;
1488 }
1489
1490 /*
1491  * Dissect HTTP Basic authorization.
1492  */
1493 static gboolean
1494 check_auth_basic(proto_item *hdr_item, tvbuff_t *tvb, gchar *value)
1495 {
1496         static const char *basic_headers[] = {
1497                 "Basic ",
1498                 NULL
1499         };
1500         const char **header;
1501         size_t hdrlen;
1502         proto_tree *hdr_tree;
1503         size_t len;
1504
1505         for (header = &basic_headers[0]; *header != NULL; header++) {
1506                 hdrlen = strlen(*header);
1507                 if (strncmp(value, *header, hdrlen) == 0) {
1508                         if (hdr_item != NULL) {
1509                                 hdr_tree = proto_item_add_subtree(hdr_item,
1510                                     ett_http_ntlmssp);
1511                         } else
1512                                 hdr_tree = NULL;
1513                         value += hdrlen;
1514
1515                         len = epan_base64_decode(value);
1516                         value[len] = '\0';
1517                         proto_tree_add_string(hdr_tree, hf_http_basic, tvb,
1518                             0, 0, value);
1519
1520                         return TRUE;
1521                 }
1522         }
1523         return FALSE;
1524 }
1525
1526 static void
1527 dissect_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1528 {
1529         int             offset = 0;
1530         int             len;
1531
1532         while (tvb_reported_length_remaining(tvb, offset) != 0) {
1533                 len = dissect_http_message(tvb, offset, pinfo, tree);
1534                 if (len == -1)
1535                         break;
1536                 offset += len;
1537
1538                 /*
1539                  * OK, we've set the Protocol and Info columns for the
1540                  * first HTTP message; make the columns non-writable,
1541                  * so that we don't change it for subsequent HTTP messages.
1542                  */
1543                 col_set_writable(pinfo->cinfo, FALSE);
1544         }
1545 }
1546
1547 static void
1548 dissect_http_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1549 {
1550         dissect_http_message(tvb, 0, pinfo, tree);
1551 }
1552
1553 void
1554 proto_register_http(void)
1555 {
1556         static hf_register_info hf[] = {
1557             { &hf_http_notification,
1558               { "Notification",         "http.notification",
1559                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1560                 "TRUE if HTTP notification", HFILL }},
1561             { &hf_http_response,
1562               { "Response",             "http.response",
1563                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1564                 "TRUE if HTTP response", HFILL }},
1565             { &hf_http_request,
1566               { "Request",              "http.request",
1567                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1568                 "TRUE if HTTP request", HFILL }},
1569             { &hf_http_basic,
1570               { "Credentials",          "http.authbasic",
1571                 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }},
1572             { &hf_http_request_method,
1573               { "Request Method",       "http.request.method",
1574                 FT_STRING, BASE_NONE, NULL, 0x0,
1575                 "HTTP Request Method", HFILL }},
1576             { &hf_http_response_code,
1577               { "Response Code",        "http.response.code",
1578                 FT_UINT16, BASE_DEC, NULL, 0x0,
1579                 "HTTP Response Code", HFILL }},
1580             { &hf_http_authorization,
1581               { "Authorization",        "http.authorization",
1582                 FT_STRING, BASE_NONE, NULL, 0x0,
1583                 "HTTP Authorization header", HFILL }},
1584             { &hf_http_proxy_authenticate,
1585               { "Proxy-Authenticate",   "http.proxy_authenticate",
1586                 FT_STRING, BASE_NONE, NULL, 0x0,
1587                 "HTTP Proxy-Authenticate header", HFILL }},
1588             { &hf_http_proxy_authorization,
1589               { "Proxy-Authorization",  "http.proxy_authorization",
1590                 FT_STRING, BASE_NONE, NULL, 0x0,
1591                 "HTTP Proxy-Authorization header", HFILL }},
1592             { &hf_http_www_authenticate,
1593               { "WWW-Authenticate",     "http.www_authenticate",
1594                 FT_STRING, BASE_NONE, NULL, 0x0,
1595                 "HTTP WWW-Authenticate header", HFILL }},
1596             { &hf_http_content_type,
1597               { "Content-Type", "http.content_type",
1598                 FT_STRING, BASE_NONE, NULL, 0x0,
1599                 "HTTP Content-Type header", HFILL }},
1600             { &hf_http_content_length,
1601               { "Content-Length",       "http.content_length",
1602                 FT_STRING, BASE_NONE, NULL, 0x0,
1603                 "HTTP Content-Length header", HFILL }},
1604             { &hf_http_content_encoding,
1605               { "Content-Encoding",     "http.content_encoding",
1606                 FT_STRING, BASE_NONE, NULL, 0x0,
1607                 "HTTP Content-Encoding header", HFILL }},
1608             { &hf_http_transfer_encoding,
1609               { "Transfer-Encoding",    "http.transfer_encoding",
1610                 FT_STRING, BASE_NONE, NULL, 0x0,
1611                 "HTTP Transfer-Encoding header", HFILL }},
1612         };
1613         static gint *ett[] = {
1614                 &ett_http,
1615                 &ett_http_ntlmssp,
1616                 &ett_http_request,
1617                 &ett_http_chunked_response,
1618                 &ett_http_chunk_data,
1619                 &ett_http_encoded_entity,
1620         };
1621         module_t *http_module;
1622
1623         proto_http = proto_register_protocol("Hypertext Transfer Protocol",
1624             "HTTP", "http");
1625         proto_register_field_array(proto_http, hf, array_length(hf));
1626         proto_register_subtree_array(ett, array_length(ett));
1627         http_module = prefs_register_protocol(proto_http, NULL);
1628         prefs_register_bool_preference(http_module, "desegment_headers",
1629             "Reassemble HTTP headers spanning multiple TCP segments",
1630             "Whether the HTTP dissector should reassemble headers "
1631             "of a request spanning multiple TCP segments. "
1632                 "To use this option, you must also enable "
1633         "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
1634             &http_desegment_headers);
1635         prefs_register_bool_preference(http_module, "desegment_body",
1636             "Reassemble HTTP bodies spanning multiple TCP segments",
1637             "Whether the HTTP dissector should use the "
1638             "\"Content-length:\" value, if present, to reassemble "
1639             "the body of a request spanning multiple TCP segments, "
1640             "and reassemble chunked data spanning multiple TCP segments. "
1641                 "To use this option, you must also enable "
1642         "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
1643             &http_desegment_body);
1644         prefs_register_bool_preference(http_module, "dechunk_body",
1645             "Reassemble chunked transfer-coded bodies",
1646             "Whether to reassemble bodies of entities that are transfered "
1647             "using the \"Transfer-Encoding: chunked\" method",
1648             &http_dechunk_body);
1649 #ifdef HAVE_LIBZ
1650         prefs_register_bool_preference(http_module, "decompress_body",
1651             "Uncompress entity bodies",
1652             "Whether to uncompress entity bodies that are compressed "
1653             "using \"Content-Encoding: \"",
1654             &http_decompress_body);
1655 #endif
1656
1657         http_handle = create_dissector_handle(dissect_http, proto_http);
1658
1659         /*
1660          * Dissectors shouldn't register themselves in this table;
1661          * instead, they should call "http_dissector_add()", and
1662          * we'll register the port number they specify as a port
1663          * for HTTP, and register them in our subdissector table.
1664          *
1665          * This only works for protocols such as IPP that run over
1666          * HTTP on a specific non-HTTP port.
1667          */
1668         port_subdissector_table = register_dissector_table("http.port",
1669             "TCP port for protocols using HTTP", FT_UINT16, BASE_DEC);
1670
1671         /*
1672          * Dissectors can register themselves in this table.
1673          * It's just "media_type", not "http.content_type", because
1674          * it's an Internet media type, usable by other protocols as well.
1675          */
1676         media_type_subdissector_table =
1677             register_dissector_table("media_type",
1678                 "Internet media type", FT_STRING, BASE_NONE);
1679
1680         /*
1681          * Heuristic dissectors SHOULD register themselves in
1682          * this table using the standard heur_dissector_add()
1683          * function.
1684          */
1685         register_heur_dissector_list("http", &heur_subdissector_list);
1686
1687         /*
1688          * Register for tapping
1689          */
1690         http_tap = register_tap("http");
1691 }
1692
1693 /*
1694  * Called by dissectors for protocols that run atop HTTP/TCP.
1695  */
1696 void
1697 http_dissector_add(guint32 port, dissector_handle_t handle)
1698 {
1699         /*
1700          * Register ourselves as the handler for that port number
1701          * over TCP.
1702          */
1703         dissector_add("tcp.port", port, http_handle);
1704
1705         /*
1706          * And register them in *our* table for that port.
1707          */
1708         dissector_add("http.port", port, handle);
1709 }
1710
1711 void
1712 proto_reg_handoff_http(void)
1713 {
1714         dissector_handle_t http_udp_handle;
1715
1716         data_handle = find_dissector("data");
1717         media_handle = find_dissector("media");
1718
1719         dissector_add("tcp.port", TCP_PORT_HTTP, http_handle);
1720         dissector_add("tcp.port", TCP_ALT_PORT_HTTP, http_handle);
1721         dissector_add("tcp.port", TCP_PORT_PROXY_HTTP, http_handle);
1722         dissector_add("tcp.port", TCP_PORT_PROXY_ADMIN_HTTP, http_handle);
1723         dissector_add("tcp.port", TCP_PORT_HKP, http_handle);
1724
1725         /*
1726          * XXX - is there anything to dissect in the body of an SSDP
1727          * request or reply?  I.e., should there be an SSDP dissector?
1728          */
1729         dissector_add("tcp.port", TCP_PORT_SSDP, http_handle);
1730         http_udp_handle = create_dissector_handle(dissect_http_udp, proto_http);
1731         dissector_add("udp.port", UDP_PORT_SSDP, http_udp_handle);
1732
1733         ntlmssp_handle = find_dissector("ntlmssp");
1734 }
1735
1736 /*
1737  * Content-Type: message/http
1738  */
1739
1740 static gint proto_message_http = -1;
1741 static gint ett_message_http = -1;
1742 static dissector_handle_t message_http_handle;
1743
1744 static void
1745 dissect_message_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1746 {
1747         proto_tree      *subtree;
1748         proto_item      *ti;
1749         gint            offset = 0, next_offset;
1750         gint            len;
1751
1752         if (check_col(pinfo->cinfo, COL_INFO))
1753                 col_append_str(pinfo->cinfo, COL_INFO, " (message/http)");
1754         if (tree) {
1755                 ti = proto_tree_add_item(tree, proto_message_http,
1756                                 tvb, 0, -1, FALSE);
1757                 subtree = proto_item_add_subtree(ti, ett_message_http);
1758                 while (tvb_reported_length_remaining(tvb, offset) != 0) {
1759                         len = tvb_find_line_end(tvb, offset,
1760                                         tvb_ensure_length_remaining(tvb, offset),
1761                                         &next_offset, FALSE);
1762                         if (len == -1)
1763                                 break;
1764                         proto_tree_add_text(subtree, tvb, offset, next_offset - offset,
1765                                         "%s", tvb_format_text(tvb, offset, len));
1766                         offset = next_offset;
1767                 }
1768         }
1769 }
1770
1771 void
1772 proto_register_message_http(void)
1773 {
1774         static gint *ett[] = {
1775                 &ett_message_http,
1776         };
1777
1778         proto_message_http = proto_register_protocol(
1779                         "Media Type: message/http",
1780                         "message/http",
1781                         "message-http"
1782         );
1783         proto_register_subtree_array(ett, array_length(ett));
1784         message_http_handle = create_dissector_handle(dissect_message_http,
1785                         proto_message_http);
1786 }
1787
1788 void
1789 proto_reg_handoff_message_http(void)
1790 {
1791         message_http_handle = create_dissector_handle(dissect_message_http,
1792                         proto_message_http);
1793
1794         dissector_add_string("media_type", "message/http", message_http_handle);
1795 }