Fix some warnings reported by gcc -Wshadow ...
[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  * Wireshark - Network traffic analyzer
15  * By Gerald Combs <gerald@wireshark.org>
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/conversation.h>
42 #include <epan/packet.h>
43 #include <epan/strutil.h>
44 #include <epan/base64.h>
45 #include <epan/emem.h>
46 #include <epan/stats_tree.h>
47
48 #include <epan/req_resp_hdrs.h>
49 #include "packet-http.h"
50 #include "packet-tcp.h"
51 #include "packet-ssl.h"
52 #include <epan/prefs.h>
53 #include <epan/expert.h>
54
55 typedef enum _http_type {
56         HTTP_REQUEST,
57         HTTP_RESPONSE,
58         HTTP_NOTIFICATION,
59         HTTP_OTHERS
60 } http_type_t;
61
62 #include <epan/tap.h>
63
64
65 static int http_tap = -1;
66 static int http_eo_tap = -1;
67
68 static int proto_http = -1;
69 static int hf_http_notification = -1;
70 static int hf_http_response = -1;
71 static int hf_http_request = -1;
72 static int hf_http_basic = -1;
73 static int hf_http_request_method = -1;
74 static int hf_http_request_uri = -1;
75 static int hf_http_version = -1;
76 static int hf_http_response_code = -1;
77 static int hf_http_authorization = -1;
78 static int hf_http_proxy_authenticate = -1;
79 static int hf_http_proxy_authorization = -1;
80 static int hf_http_proxy_connect_host = -1;
81 static int hf_http_proxy_connect_port = -1;
82 static int hf_http_www_authenticate = -1;
83 static int hf_http_content_type = -1;
84 static int hf_http_content_length = -1;
85 static int hf_http_content_encoding = -1;
86 static int hf_http_transfer_encoding = -1;
87 static int hf_http_user_agent = -1;
88 static int hf_http_host = -1;
89 static int hf_http_connection = -1;
90 static int hf_http_cookie = -1;
91 static int hf_http_accept = -1;
92 static int hf_http_referer = -1;
93 static int hf_http_accept_language = -1;
94 static int hf_http_accept_encoding = -1;
95 static int hf_http_date = -1;
96 static int hf_http_cache_control = -1;
97 static int hf_http_server = -1;
98 static int hf_http_location = -1;
99 static int hf_http_set_cookie = -1;
100 static int hf_http_last_modified = -1;
101 static int hf_http_x_forwarded_for = -1;
102
103 static gint ett_http = -1;
104 static gint ett_http_ntlmssp = -1;
105 static gint ett_http_request = -1;
106 static gint ett_http_chunked_response = -1;
107 static gint ett_http_chunk_data = -1;
108 static gint ett_http_encoded_entity = -1;
109
110 static dissector_handle_t data_handle;
111 static dissector_handle_t media_handle;
112 static dissector_handle_t http_handle;
113
114 /*
115  * desegmentation of HTTP headers
116  * (when we are over TCP or another protocol providing the desegmentation API)
117  */
118 static gboolean http_desegment_headers = TRUE;
119
120 /*
121  * desegmentation of HTTP bodies
122  * (when we are over TCP or another protocol providing the desegmentation API)
123  * TODO let the user filter on content-type the bodies he wants desegmented
124  */
125 static gboolean http_desegment_body = TRUE;
126
127 /*
128  * De-chunking of content-encoding: chunk entity bodies.
129  */
130 static gboolean http_dechunk_body = TRUE;
131
132 /*
133  * Decompression of zlib encoded entities.
134  */
135 #ifdef HAVE_LIBZ
136 static gboolean http_decompress_body = TRUE;
137 #else
138 static gboolean http_decompress_body = FALSE;
139 #endif
140
141 #define TCP_PORT_HTTP                   80
142 #define TCP_PORT_PROXY_HTTP             3128
143 #define TCP_PORT_PROXY_ADMIN_HTTP       3132
144 #define TCP_ALT_PORT_HTTP               8080
145 #define TCP_RADAN_HTTP                  8088
146 #define TCP_PORT_HKP                    11371
147 #define TCP_PORT_DAAP                   3689
148
149 /*
150  * SSDP is implemented atop HTTP (yes, it really *does* run over UDP).
151  */
152 #define TCP_PORT_SSDP                   1900
153 #define UDP_PORT_SSDP                   1900
154
155 /*
156  * tcp and ssl ports
157  */
158
159 #define TCP_DEFAULT_RANGE "80,3128,3132,8080,8088,11371,3689,1900"
160 #define SSL_DEFAULT_RANGE "443"
161
162 static range_t *global_http_tcp_range = NULL;
163 static range_t *global_http_ssl_range = NULL;
164
165 static range_t *http_tcp_range = NULL;
166 static range_t *http_ssl_range = NULL;
167
168
169 /*
170  * Protocols implemented atop HTTP.
171  */
172 typedef enum {
173         PROTO_HTTP,             /* just HTTP */
174         PROTO_SSDP,             /* Simple Service Discovery Protocol */
175         PROTO_DAAP              /* Digital Audio Access Protocol */
176 } http_proto_t;
177
178 typedef void (*ReqRespDissector)(tvbuff_t*, proto_tree*, int, const guchar*,
179                                  const guchar*, http_conv_t *);
180
181 /*
182  * Structure holding information from headers needed by main
183  * HTTP dissector code.
184  */
185 typedef struct {
186         char    *content_type;
187         char    *content_type_parameters;
188         gboolean have_content_length;
189         long    content_length; /* XXX - make it 64-bit? */
190         char    *content_encoding;
191         char    *transfer_encoding;
192 } headers_t;
193
194 static int is_http_request_or_reply(const gchar *data, int linelen,
195                                     http_type_t *type, ReqRespDissector
196                                     *reqresp_dissector, http_conv_t *conv_data);
197 static int chunked_encoding_dissector(tvbuff_t **tvb_ptr, packet_info *pinfo,
198                 proto_tree *tree, int offset);
199 static void process_header(tvbuff_t *tvb, int offset, int next_offset,
200     const guchar *line, int linelen, int colon_offset, packet_info *pinfo,
201     proto_tree *tree, headers_t *eh_ptr, http_conv_t *conv_data);
202 static gint find_header_hf_value(tvbuff_t *tvb, int offset, guint header_len);
203 static gboolean check_auth_ntlmssp(proto_item *hdr_item, tvbuff_t *tvb,
204     packet_info *pinfo, gchar *value);
205 static gboolean check_auth_basic(proto_item *hdr_item, tvbuff_t *tvb,
206     gchar *value);
207
208 static dissector_table_t port_subdissector_table;
209 static dissector_table_t media_type_subdissector_table;
210 static heur_dissector_list_t heur_subdissector_list;
211
212 static dissector_handle_t ntlmssp_handle=NULL;
213 static dissector_handle_t gssapi_handle=NULL;
214
215 static const value_string vals_status_code[] = {
216         { 100, "Continue" },
217         { 101, "Switching Protocols" },
218         { 102, "Processing" },
219         { 199, "Informational - Others" },
220
221         { 200, "OK"},
222         { 201, "Created"},
223         { 202, "Accepted"},
224         { 203, "Non-authoritative Information"},
225         { 204, "No Content"},
226         { 205, "Reset Content"},
227         { 206, "Partial Content"},
228         { 207, "Multi-Status"},
229         { 299, "Success - Others"},
230
231         { 300, "Multiple Choices"},
232         { 301, "Moved Permanently"},
233         { 302, "Found"},
234         { 303, "See Other"},
235         { 304, "Not Modified"},
236         { 305, "Use Proxy"},
237         { 307, "Temporary Redirect"},
238         { 399, "Redirection - Others"},
239
240         { 400, "Bad Request"},
241         { 401, "Unauthorized"},
242         { 402, "Payment Required"},
243         { 403, "Forbidden"},
244         { 404, "Not Found"},
245         { 405, "Method Not Allowed"},
246         { 406, "Not Acceptable"},
247         { 407, "Proxy Authentication Required"},
248         { 408, "Request Time-out"},
249         { 409, "Conflict"},
250         { 410, "Gone"},
251         { 411, "Length Required"},
252         { 412, "Precondition Failed"},
253         { 413, "Request Entity Too Large"},
254         { 414, "Request-URI Too Long"},
255         { 415, "Unsupported Media Type"},
256         { 416, "Requested Range Not Satisfiable"},
257         { 417, "Expectation Failed"},
258         { 422, "Unprocessable Entity"},
259         { 423, "Locked"},
260         { 424, "Failed Dependency"},
261         { 499, "Client Error - Others"},
262
263         { 500, "Internal Server Error"},
264         { 501, "Not Implemented"},
265         { 502, "Bad Gateway"},
266         { 503, "Service Unavailable"},
267         { 504, "Gateway Time-out"},
268         { 505, "HTTP Version not supported"},
269         { 507, "Insufficient Storage"},
270         { 599, "Server Error - Others"},
271
272         { 0,    NULL}
273 };
274
275 static const gchar* st_str_reqs = "HTTP Requests by Server";
276 static const gchar* st_str_reqs_by_srv_addr = "HTTP Requests by Server Address";
277 static const gchar* st_str_reqs_by_http_host = "HTTP Requests by HTTP Host";
278 static const gchar* st_str_resps_by_srv_addr = "HTTP Responses by Server Address";
279
280 static int st_node_reqs = -1;
281 static int st_node_reqs_by_srv_addr = -1;
282 static int st_node_reqs_by_http_host = -1;
283 static int st_node_resps_by_srv_addr = -1;
284
285 /* HTTP/Load Distribution stats init function */
286 static void http_reqs_stats_tree_init(stats_tree* st) {
287         st_node_reqs = stats_tree_create_node(st, st_str_reqs, 0, TRUE);
288         st_node_reqs_by_srv_addr = stats_tree_create_node(st, st_str_reqs_by_srv_addr, st_node_reqs, TRUE);
289         st_node_reqs_by_http_host = stats_tree_create_node(st, st_str_reqs_by_http_host, st_node_reqs, TRUE);
290         st_node_resps_by_srv_addr = stats_tree_create_node(st, st_str_resps_by_srv_addr, 0, TRUE);
291 }
292
293 /* HTTP/Load Distribution stats packet function */
294 static int http_reqs_stats_tree_packet(stats_tree* st, packet_info* pinfo, epan_dissect_t* edt _U_, const void* p) {
295         const http_info_value_t* v = p;
296         int reqs_by_this_host;
297         int reqs_by_this_addr;
298         int resps_by_this_addr;
299         int i = v->response_code;
300         static gchar ip_str[256];
301
302
303         if (v->request_method) {
304                 g_snprintf(ip_str,sizeof(ip_str),"%s",address_to_str(&pinfo->dst));
305
306                 tick_stat_node(st, st_str_reqs, 0, FALSE);
307                 tick_stat_node(st, st_str_reqs_by_srv_addr, st_node_reqs, TRUE);
308                 tick_stat_node(st, st_str_reqs_by_http_host, st_node_reqs, TRUE);
309                 reqs_by_this_addr = tick_stat_node(st, ip_str, st_node_reqs_by_srv_addr, TRUE);
310
311                 if (v->http_host) {
312                         reqs_by_this_host = tick_stat_node(st, v->http_host, st_node_reqs_by_http_host, TRUE);
313                         tick_stat_node(st, ip_str, reqs_by_this_host, FALSE);
314
315                         tick_stat_node(st, v->http_host, reqs_by_this_addr, FALSE);
316                 }
317
318                 return 1;
319
320         } else if (i != 0) {
321                 g_snprintf(ip_str,sizeof(ip_str),"%s",address_to_str(&pinfo->src));
322
323                 tick_stat_node(st, st_str_resps_by_srv_addr, 0, FALSE);
324                 resps_by_this_addr = tick_stat_node(st, ip_str, st_node_resps_by_srv_addr, TRUE);
325
326                 if ( (i>100)&&(i<400) ) {
327                         tick_stat_node(st, "OK", resps_by_this_addr, FALSE);
328                 } else {
329                         tick_stat_node(st, "KO", resps_by_this_addr, FALSE);
330                 }
331
332                 return 1;
333         }
334
335         return 0;
336 }
337
338
339 static int st_node_requests_by_host = -1;
340 static const guint8* st_str_requests_by_host = "HTTP Requests by HTTP Host";
341
342 /* HTTP/Requests stats init function */
343 static void http_req_stats_tree_init(stats_tree* st) {
344         st_node_requests_by_host = stats_tree_create_node(st, st_str_requests_by_host, 0, TRUE);
345 }
346
347 /* HTTP/Requests stats packet function */
348 static int http_req_stats_tree_packet(stats_tree* st, packet_info* pinfo _U_, epan_dissect_t* edt _U_, const void* p) {
349         const http_info_value_t* v = p;
350         int reqs_by_this_host;
351
352         if (v->request_method) {
353                 tick_stat_node(st, st_str_requests_by_host, 0, FALSE);
354
355                 if (v->http_host) {
356                         reqs_by_this_host = tick_stat_node(st, v->http_host, st_node_requests_by_host, TRUE);
357
358                         if (v->request_uri) {
359                                 tick_stat_node(st, v->request_uri, reqs_by_this_host, TRUE);
360                         }
361                 }
362
363                 return 1;
364         }
365
366         return 0;
367 }
368
369 static const guint8* st_str_packets = "Total HTTP Packets";
370 static const guint8* st_str_requests = "HTTP Request Packets";
371 static const guint8* st_str_responses = "HTTP Response Packets";
372 static const guint8* st_str_resp_broken = "???: broken";
373 static const guint8* st_str_resp_100 = "1xx: Informational";
374 static const guint8* st_str_resp_200 = "2xx: Success";
375 static const guint8* st_str_resp_300 = "3xx: Redirection";
376 static const guint8* st_str_resp_400 = "4xx: Client Error";
377 static const guint8* st_str_resp_500 = "5xx: Server Error";
378 static const guint8* st_str_other = "Other HTTP Packets";
379
380 static int st_node_packets = -1;
381 static int st_node_requests = -1;
382 static int st_node_responses = -1;
383 static int st_node_resp_broken = -1;
384 static int st_node_resp_100 = -1;
385 static int st_node_resp_200 = -1;
386 static int st_node_resp_300 = -1;
387 static int st_node_resp_400 = -1;
388 static int st_node_resp_500 = -1;
389 static int st_node_other = -1;
390
391
392 /* HTTP/Packet Counter stats init function */
393 static void http_stats_tree_init(stats_tree* st) {
394         st_node_packets = stats_tree_create_node(st, st_str_packets, 0, TRUE);
395         st_node_requests = stats_tree_create_pivot(st, st_str_requests, st_node_packets);
396         st_node_responses = stats_tree_create_node(st, st_str_responses, st_node_packets, TRUE);
397         st_node_resp_broken = stats_tree_create_node(st, st_str_resp_broken, st_node_responses, TRUE);
398         st_node_resp_100    = stats_tree_create_node(st, st_str_resp_100,    st_node_responses, TRUE);
399         st_node_resp_200    = stats_tree_create_node(st, st_str_resp_200,    st_node_responses, TRUE);
400         st_node_resp_300    = stats_tree_create_node(st, st_str_resp_300,    st_node_responses, TRUE);
401         st_node_resp_400    = stats_tree_create_node(st, st_str_resp_400,    st_node_responses, TRUE);
402         st_node_resp_500    = stats_tree_create_node(st, st_str_resp_500,    st_node_responses, TRUE);
403         st_node_other = stats_tree_create_node(st, st_str_other, st_node_packets,FALSE);
404 }
405
406 /* HTTP/Packet Counter stats packet function */
407 static int http_stats_tree_packet(stats_tree* st, packet_info* pinfo _U_, epan_dissect_t* edt _U_, const void* p) {
408         const http_info_value_t* v = p;
409         guint i = v->response_code;
410         int resp_grp;
411         const guint8* resp_str;
412         static gchar str[64];
413
414         tick_stat_node(st, st_str_packets, 0, FALSE);
415
416         if (i) {
417                 tick_stat_node(st, st_str_responses, st_node_packets, FALSE);
418
419                 if ( (i<100)||(i>=600) ) {
420                         resp_grp = st_node_resp_broken;
421                         resp_str = st_str_resp_broken;
422                 } else if (i<200) {
423                         resp_grp = st_node_resp_100;
424                         resp_str = st_str_resp_100;
425                 } else if (i<300) {
426                         resp_grp = st_node_resp_200;
427                         resp_str = st_str_resp_200;
428                 } else if (i<400) {
429                         resp_grp = st_node_resp_300;
430                         resp_str = st_str_resp_300;
431                 } else if (i<500) {
432                         resp_grp = st_node_resp_400;
433                         resp_str = st_str_resp_400;
434                 } else {
435                         resp_grp = st_node_resp_500;
436                         resp_str = st_str_resp_500;
437                 }
438
439                 tick_stat_node(st, resp_str, st_node_responses, FALSE);
440
441                 g_snprintf(str, sizeof(str),"%u %s",i,val_to_str(i,vals_status_code, "Unknown (%d)"));
442                 tick_stat_node(st, str, resp_grp, FALSE);
443         } else if (v->request_method) {
444                 stats_tree_tick_pivot(st,st_node_requests,v->request_method);
445         } else {
446                 tick_stat_node(st, st_str_other, st_node_packets, FALSE);
447         }
448
449         return 1;
450 }
451
452 /* Return a tvb that contains the binary representation of a base64
453    string */
454
455 static tvbuff_t *
456 base64_to_tvb(const char *base64)
457 {
458         tvbuff_t *tvb;
459         char *data = g_strdup(base64);
460         size_t len;
461
462         len = epan_base64_decode(data);
463         tvb = tvb_new_real_data((const guint8 *)data, len, len);
464
465         tvb_set_free_cb(tvb, g_free);
466
467         return tvb;
468 }
469
470 static void
471 dissect_http_ntlmssp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
472     const char *line)
473 {
474         tvbuff_t *ntlmssp_tvb;
475
476         ntlmssp_tvb = base64_to_tvb(line);
477         tvb_set_child_real_data_tvbuff(tvb, ntlmssp_tvb);
478         add_new_data_source(pinfo, ntlmssp_tvb, "NTLMSSP / GSSAPI Data");
479         if (tvb_strneql(ntlmssp_tvb, 0, "NTLMSSP", 7) == 0)
480                 call_dissector(ntlmssp_handle, ntlmssp_tvb, pinfo, tree);
481         else
482                 call_dissector(gssapi_handle, ntlmssp_tvb, pinfo, tree);
483 }
484
485 static http_conv_t *
486 get_http_conversation_data(packet_info *pinfo)
487 {
488         conversation_t  *conversation;
489         http_conv_t     *conv_data;
490
491         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
492
493         if(!conversation) {  /* Conversation does not exist yet - create it */
494                 conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
495         }
496
497         /* Retrieve information from conversation
498          * or add it if it isn't there yet
499          */
500         conv_data = conversation_get_proto_data(conversation, proto_http);
501         if(!conv_data) {
502                 /* Setup the conversation structure itself */
503                 conv_data = se_alloc(sizeof(http_conv_t));
504
505                 conv_data->response_code = 0;
506                 conv_data->request_method = NULL;
507                 conv_data->request_uri = NULL;
508                 conv_data->startframe = 0;
509
510                 conversation_add_proto_data(conversation, proto_http,
511                                             conv_data);
512         }
513
514         return conv_data;
515 }
516
517 /*
518  * TODO: remove this ugly global variable.
519  * XXX: do we really want to have to pass this from one function to another?
520  */
521 static http_info_value_t        *stat_info;
522
523 static int
524 dissect_http_message(tvbuff_t *tvb, int offset, packet_info *pinfo,
525     proto_tree *tree, http_conv_t *conv_data)
526 {
527         http_proto_t    proto;
528         const char      *proto_tag;
529         proto_tree      *http_tree = NULL;
530         proto_item      *ti = NULL;
531         proto_item      *hidden_item;
532         const guchar    *line;
533         gint            next_offset;
534         const guchar    *linep, *lineend;
535         int             orig_offset;
536         int             first_linelen, linelen;
537         gboolean        is_request_or_reply;
538         gboolean        saw_req_resp_or_header;
539         guchar          c;
540         http_type_t     http_type;
541         proto_item      *hdr_item = NULL;
542         ReqRespDissector reqresp_dissector;
543         proto_tree      *req_tree;
544         int             colon_offset;
545         headers_t       headers;
546         int             datalen;
547         int             reported_datalen = -1;
548         dissector_handle_t handle;
549         gboolean        dissected;
550         /*guint         i;*/
551         /*http_info_value_t *si;*/
552         http_eo_t       *eo_info;
553
554         /*
555          * Is this a request or response?
556          *
557          * Note that "tvb_find_line_end()" will return a value that
558          * is not longer than what's in the buffer, so the
559          * "tvb_get_ptr()" call won't throw an exception.
560          */
561         first_linelen = tvb_find_line_end(tvb, offset,
562             tvb_ensure_length_remaining(tvb, offset), &next_offset,
563             FALSE);
564         /*
565          * Is the first line a request or response?
566          */
567         line = tvb_get_ptr(tvb, offset, first_linelen);
568         http_type = HTTP_OTHERS;        /* type not known yet */
569         is_request_or_reply = is_http_request_or_reply((const gchar *)line,
570             first_linelen, &http_type, NULL, conv_data);
571         if (is_request_or_reply) {
572                 /*
573                  * Yes, it's a request or response.
574                  * Do header desegmentation if we've been told to,
575                  * and do body desegmentation if we've been told to and
576                  * we find a Content-Length header.
577                  */
578                 if (!req_resp_hdrs_do_reassembly(tvb, offset, pinfo,
579                     http_desegment_headers, http_desegment_body)) {
580                         /*
581                          * More data needed for desegmentation.
582                          */
583                         return -1;
584                 }
585         }
586
587         stat_info = ep_alloc(sizeof(http_info_value_t));
588         stat_info->framenum = pinfo->fd->num;
589         stat_info->response_code = 0;
590         stat_info->request_method = NULL;
591         stat_info->request_uri = NULL;
592         stat_info->http_host = NULL;
593
594         switch (pinfo->match_port) {
595
596         case TCP_PORT_SSDP:     /* TCP_PORT_SSDP = UDP_PORT_SSDP */
597                 proto = PROTO_SSDP;
598                 proto_tag = "SSDP";
599                 break;
600
601         case TCP_PORT_DAAP:
602                 proto = PROTO_DAAP;
603                 proto_tag = "DAAP";
604                 break;
605
606         default:
607                 proto = PROTO_HTTP;
608                 proto_tag = "HTTP";
609                 break;
610         }
611
612         if (check_col(pinfo->cinfo, COL_PROTOCOL))
613                 col_set_str(pinfo->cinfo, COL_PROTOCOL, proto_tag);
614         if (check_col(pinfo->cinfo, COL_INFO)) {
615                 /*
616                  * Put the first line from the buffer into the summary
617                  * if it's an HTTP request or reply (but leave out the
618                  * line terminator).
619                  * Otherwise, just call it a continuation.
620                  *
621                  * Note that "tvb_find_line_end()" will return a value that
622                  * is not longer than what's in the buffer, so the
623                  * "tvb_get_ptr()" call won't throw an exception.
624                  */
625                 if (is_request_or_reply) {
626                     line = tvb_get_ptr(tvb, offset, first_linelen);
627                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", format_text(line, first_linelen));
628                 }
629                 else
630                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation or non-HTTP traffic");
631         }
632
633         orig_offset = offset;
634         if (tree) {
635                 ti = proto_tree_add_item(tree, proto_http, tvb, offset, -1,
636                     FALSE);
637                 http_tree = proto_item_add_subtree(ti, ett_http);
638         }
639
640         /*
641          * Process the packet data, a line at a time.
642          */
643         http_type = HTTP_OTHERS;        /* type not known yet */
644         headers.content_type = NULL;    /* content type not known yet */
645         headers.content_type_parameters = NULL; /* content type parameters too */
646         headers.have_content_length = FALSE;    /* content length not known yet */
647         headers.content_length = 0;             /* content length set to 0 (avoid a gcc warning) */
648         headers.content_encoding = NULL; /* content encoding not known yet */
649         headers.transfer_encoding = NULL; /* transfer encoding not known yet */
650         saw_req_resp_or_header = FALSE; /* haven't seen anything yet */
651         while (tvb_reported_length_remaining(tvb, offset) != 0) {
652                 /*
653                  * Find the end of the line.
654                  * XXX - what if we don't find it because the packet
655                  * is cut short by a snapshot length or the header is
656                  * split across TCP segments?  How much dissection should
657                  * we do on it?
658                  */
659                 linelen = tvb_find_line_end(tvb, offset,
660                     tvb_ensure_length_remaining(tvb, offset), &next_offset,
661                     FALSE);
662                 if (linelen < 0)
663                         return -1;
664
665                 /*
666                  * Get a buffer that refers to the line.
667                  */
668                 line = tvb_get_ptr(tvb, offset, linelen);
669                 lineend = line + linelen;
670                 colon_offset = -1;
671
672                 /*
673                  * OK, does it look like an HTTP request or response?
674                  */
675                 reqresp_dissector = NULL;
676                 is_request_or_reply =
677                     is_http_request_or_reply((const gchar *)line,
678                     linelen, &http_type, &reqresp_dissector, conv_data);
679                 if (is_request_or_reply)
680                         goto is_http;
681
682                 /*
683                  * No.  Does it look like a blank line (as would appear
684                  * at the end of an HTTP request)?
685                  */
686                 if (linelen == 0)
687                         goto is_http;   /* Yes. */
688
689                 /*
690                  * No.  Does it look like a header?
691                  */
692                 linep = line;
693                 colon_offset = offset;
694                 while (linep < lineend) {
695                         c = *linep++;
696
697                         /*
698                          * This must be a CHAR to be part of a token; that
699                          * means it must be ASCII.
700                          */
701                         if (!isascii(c))
702                                 break;  /* not ASCII, thus not a CHAR */
703
704                         /*
705                          * This mustn't be a CTL to be part of a token.
706                          *
707                          * XXX - what about leading LWS on continuation
708                          * lines of a header?
709                          */
710                         if (iscntrl(c))
711                                 break;  /* CTL, not part of a header */
712
713                         /*
714                          * This mustn't be a SEP to be part of a token;
715                          * a ':' ends the token, everything else is an
716                          * indication that this isn't a header.
717                          */
718                         switch (c) {
719
720                         case '(':
721                         case ')':
722                         case '<':
723                         case '>':
724                         case '@':
725                         case ',':
726                         case ';':
727                         case '\\':
728                         case '"':
729                         case '/':
730                         case '[':
731                         case ']':
732                         case '?':
733                         case '=':
734                         case '{':
735                         case '}':
736                         case ' ':
737                                 /*
738                                  * It's a separator, so it's not part of a
739                                  * token, so it's not a field name for the
740                                  * beginning of a header.
741                                  *
742                                  * (We don't have to check for HT; that's
743                                  * already been ruled out by "iscntrl()".)
744                                  */
745                                 goto not_http;
746
747                         case ':':
748                                 /*
749                                  * This ends the token; we consider this
750                                  * to be a header.
751                                  */
752                                 goto is_http;
753
754                         default:
755                                 colon_offset++;
756                                 break;
757                         }
758                 }
759
760                 /*
761                  * We haven't seen the colon, but everything else looks
762                  * OK for a header line.
763                  *
764                  * If we've already seen an HTTP request or response
765                  * line, or a header line, and we're at the end of
766                  * the tvbuff, we assume this is an incomplete header
767                  * line.  (We quit this loop after seeing a blank line,
768                  * so if we've seen a request or response line, or a
769                  * header line, this is probably more of the request
770                  * or response we're presumably seeing.  There is some
771                  * risk of false positives, but the same applies for
772                  * full request or response lines or header lines,
773                  * although that's less likely.)
774                  *
775                  * We throw an exception in that case, by checking for
776                  * the existence of the next byte after the last one
777                  * in the line.  If it exists, "tvb_ensure_bytes_exist()"
778                  * throws no exception, and we fall through to the
779                  * "not HTTP" case.  If it doesn't exist,
780                  * "tvb_ensure_bytes_exist()" will throw the appropriate
781                  * exception.
782                  */
783                 if (saw_req_resp_or_header)
784                         tvb_ensure_bytes_exist(tvb, offset, linelen + 1);
785
786         not_http:
787                 /*
788                  * We don't consider this part of an HTTP request or
789                  * reply, so we don't display it.
790                  * (Yeah, that means we don't display, say, a text/http
791                  * page, but you can get that from the data pane.)
792                  */
793                 break;
794
795         is_http:
796                 /*
797                  * Process this line.
798                  */
799                 if (linelen == 0) {
800                         /*
801                          * This is a blank line, which means that
802                          * whatever follows it isn't part of this
803                          * request or reply.
804                          */
805                         proto_tree_add_text(http_tree, tvb, offset,
806                             next_offset - offset, "%s",
807                             tvb_format_text(tvb, offset, next_offset - offset));
808                         offset = next_offset;
809                         break;
810                 }
811
812                 /*
813                  * Not a blank line - either a request, a reply, or a header
814                  * line.
815                  */
816                 saw_req_resp_or_header = TRUE;
817                 if (is_request_or_reply) {
818                         char *text = tvb_format_text(tvb, offset, next_offset - offset);
819                         if (tree) {
820                                 hdr_item = proto_tree_add_text(http_tree, tvb,
821                                     offset, next_offset - offset, "%s", text);
822                         }
823                         expert_add_info_format(pinfo, hdr_item, PI_SEQUENCE, PI_CHAT, "%s", text);
824                         if (reqresp_dissector) {
825                                 if (tree) req_tree = proto_item_add_subtree(hdr_item, ett_http_request);
826                                 else req_tree = NULL;
827
828                                 reqresp_dissector(tvb, req_tree, offset, line,
829                                                   lineend, conv_data);
830                         }
831
832                 } else {
833                         /*
834                          * Header.
835                          */
836                         process_header(tvb, offset, next_offset, line, linelen,
837                             colon_offset, pinfo, http_tree, &headers, conv_data);
838                 }
839                 offset = next_offset;
840         }
841
842         if (tree) {
843                 switch (http_type) {
844
845                 case HTTP_NOTIFICATION:
846                         hidden_item = proto_tree_add_boolean(http_tree,
847                                             hf_http_notification, tvb, 0, 0, 1);
848                         PROTO_ITEM_SET_HIDDEN(hidden_item);
849                         break;
850
851                 case HTTP_RESPONSE:
852                         hidden_item = proto_tree_add_boolean(http_tree,
853                                             hf_http_response, tvb, 0, 0, 1);
854                         PROTO_ITEM_SET_HIDDEN(hidden_item);
855                         break;
856
857                 case HTTP_REQUEST:
858                         hidden_item = proto_tree_add_boolean(http_tree,
859                                             hf_http_request, tvb, 0, 0, 1);
860                         PROTO_ITEM_SET_HIDDEN(hidden_item);
861                         break;
862
863                 case HTTP_OTHERS:
864                 default:
865                         break;
866                 }
867         }
868
869         /*
870          * If a content length was supplied, the amount of data to be
871          * processed as HTTP payload is the minimum of the content
872          * length and the amount of data remaining in the frame.
873          *
874          * If no content length was supplied (or if a bad content length
875          * was supplied), the amount of data to be processed is the amount
876          * of data remaining in the frame.
877          *
878          * If there was no Content-Length entity header, we should
879          * accumulate all data until the end of the connection.
880          * That'd require that the TCP dissector call subdissectors
881          * for all frames with FIN, even if they contain no data,
882          * which would require subdissectors to deal intelligently
883          * with empty segments.
884          *
885          * Acccording to RFC 2616, however, 1xx responses, 204 responses,
886          * and 304 responses MUST NOT include a message body; if no
887          * content length is specified for them, we don't attempt to
888          * dissect the body.
889          *
890          * XXX - it says the same about responses to HEAD requests;
891          * unless there's a way to determine from the response
892          * whether it's a response to a HEAD request, we have to
893          * keep information about the request and associate that with
894          * the response in order to handle that.
895          */
896         datalen = tvb_length_remaining(tvb, offset);
897         if (headers.have_content_length && headers.content_length != -1) {
898                 if (datalen > headers.content_length)
899                         datalen = headers.content_length;
900
901                 /*
902                  * XXX - limit the reported length in the tvbuff we'll
903                  * hand to a subdissector to be no greater than the
904                  * content length.
905                  *
906                  * We really need both unreassembled and "how long it'd
907                  * be if it were reassembled" lengths for tvbuffs, so
908                  * that we throw the appropriate exceptions for
909                  * "not enough data captured" (running past the length),
910                  * "packet needed reassembly" (within the length but
911                  * running past the unreassembled length), and
912                  * "packet is malformed" (running past the reassembled
913                  * length).
914                  */
915                 reported_datalen = tvb_reported_length_remaining(tvb, offset);
916                 if (reported_datalen > headers.content_length)
917                         reported_datalen = headers.content_length;
918         } else {
919                 switch (http_type) {
920
921                 case HTTP_REQUEST:
922                         /*
923                          * Requests have no content if there's no
924                          * Content-Length header and no Transfer-Encoding
925                          * header.
926                          */
927                         if (headers.transfer_encoding == NULL)
928                                 datalen = 0;
929                         else
930                                 reported_datalen = -1;
931                         break;
932
933                 case HTTP_RESPONSE:
934                         if ((stat_info->response_code/100) == 1 ||
935                             stat_info->response_code == 204 ||
936                             stat_info->response_code == 304)
937                                 datalen = 0;    /* no content! */
938                         else {
939                                 /*
940                                  * XXX - responses to HEAD requests,
941                                  * and possibly other responses,
942                                  * "MUST NOT" include a
943                                  * message-body.
944                                  */
945                                 reported_datalen = -1;
946                         }
947                         break;
948
949                 default:
950                         /*
951                          * XXX - what about HTTP_NOTIFICATION?
952                          */
953                         reported_datalen = -1;
954                         break;
955                 }
956         }
957
958         if (datalen > 0) {
959                 /*
960                  * There's stuff left over; process it.
961                  */
962                 tvbuff_t *next_tvb;
963                 void *save_private_data = NULL;
964                 gint chunks_decoded = 0;
965
966                 /*
967                  * Create a tvbuff for the payload.
968                  *
969                  * The amount of data to be processed that's
970                  * available in the tvbuff is "datalen", which
971                  * is the minimum of the amount of data left in
972                  * the tvbuff and any specified content length.
973                  *
974                  * The amount of data to be processed that's in
975                  * this frame, regardless of whether it was
976                  * captured or not, is "reported_datalen",
977                  * which, if no content length was specified,
978                  * is -1, i.e. "to the end of the frame.
979                  */
980                 next_tvb = tvb_new_subset(tvb, offset, datalen,
981                     reported_datalen);
982                 /*
983                  * BEWARE - next_tvb is a subset of another tvb,
984                  * so we MUST NOT attempt tvb_free(next_tvb);
985                  */
986
987                 /*
988                  * Handle *transfer* encodings other than "identity".
989                  */
990                 if (headers.transfer_encoding != NULL &&
991                     g_ascii_strcasecmp(headers.transfer_encoding, "identity") != 0) {
992                         if (http_dechunk_body &&
993                             (g_ascii_strncasecmp(headers.transfer_encoding, "chunked", 7)
994                             == 0)) {
995
996                                 chunks_decoded = chunked_encoding_dissector(
997                                     &next_tvb, pinfo, http_tree, 0);
998
999                                 if (chunks_decoded <= 0) {
1000                                         /*
1001                                          * The chunks weren't reassembled,
1002                                          * or there was a single zero
1003                                          * length chunk.
1004                                          */
1005                                         goto body_dissected;
1006                                 } else {
1007                                         /*
1008                                          * Add a new data source for the
1009                                          * de-chunked data.
1010                                          */
1011                                         tvb_set_child_real_data_tvbuff(tvb,
1012                                                 next_tvb);
1013                                         add_new_data_source(pinfo, next_tvb,
1014                                                 "De-chunked entity body");
1015                                 }
1016                         } else {
1017                                 /*
1018                                  * We currently can't handle, for example,
1019                                  * "gzip", "compress", or "deflate" as
1020                                  * *transfer* encodings; just handle them
1021                                  * as data for now.
1022                                  */
1023                                 call_dissector(data_handle, next_tvb, pinfo,
1024                                     http_tree);
1025                                 goto body_dissected;
1026                         }
1027                 }
1028                 /*
1029                  * At this point, any chunked *transfer* coding has been removed
1030                  * (the entity body has been dechunked) so it can be presented
1031                  * for the following operation (*content* encoding), or it has
1032                  * been been handed off to the data dissector.
1033                  *
1034                  * Handle *content* encodings other than "identity" (which
1035                  * shouldn't appear in a Content-Encoding header, but
1036                  * we handle it in any case).
1037                  */
1038                 if (headers.content_encoding != NULL &&
1039                     g_ascii_strcasecmp(headers.content_encoding, "identity") != 0) {
1040                         /*
1041                          * We currently can't handle, for example, "compress";
1042                          * just handle them as data for now.
1043                          *
1044                          * After July 7, 2004 the LZW patent expires, so support
1045                          * might be added then.  However, I don't think that
1046                          * anybody ever really implemented "compress", due to
1047                          * the aforementioned patent.
1048                          */
1049                         tvbuff_t *uncomp_tvb = NULL;
1050                         proto_item *e_ti = NULL;
1051                         proto_tree *e_tree = NULL;
1052
1053                         if (http_decompress_body &&
1054                             (g_ascii_strcasecmp(headers.content_encoding, "gzip") == 0 ||
1055                             g_ascii_strcasecmp(headers.content_encoding, "deflate")
1056                             == 0)) {
1057
1058                                 uncomp_tvb = tvb_uncompress(next_tvb, 0,
1059                                     tvb_length(next_tvb));
1060                         }
1061
1062                         /*
1063                          * Add the encoded entity to the protocol tree
1064                          */
1065                         e_ti = proto_tree_add_text(http_tree, next_tvb,
1066                                         0, tvb_length(next_tvb),
1067                                         "Content-encoded entity body (%s): %u bytes",
1068                                         headers.content_encoding,
1069                     tvb_length(next_tvb));
1070                         e_tree = proto_item_add_subtree(e_ti,
1071                                         ett_http_encoded_entity);
1072
1073                         if (uncomp_tvb != NULL) {
1074                                 /*
1075                                  * Decompression worked
1076                                  */
1077
1078                                 /* XXX - Don't free this, since it's possible
1079                                  * that the data was only partially
1080                                  * decompressed, such as when desegmentation
1081                                  * isn't enabled.
1082                                  *
1083                                 tvb_free(next_tvb);
1084                                 */
1085                 proto_item_append_text(e_ti, " -> %u bytes", tvb_length(uncomp_tvb));
1086                                 next_tvb = uncomp_tvb;
1087                                 tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1088                                 add_new_data_source(pinfo, next_tvb,
1089                                     "Uncompressed entity body");
1090                         } else {
1091                                 call_dissector(data_handle, next_tvb, pinfo,
1092                                     e_tree);
1093
1094                                 goto body_dissected;
1095                         }
1096                 }
1097                 /*
1098                  * Note that a new data source is added for the entity body
1099                  * only if it was content-encoded and/or transfer-encoded.
1100                  */
1101
1102                 /* Save values for the Export Object GUI feature if we have
1103                  * an active listener to process it (which happens when
1104                  * the export object window is open). */
1105                 if(have_tap_listener(http_eo_tap)) {
1106                         eo_info = ep_alloc(sizeof(http_eo_t));
1107
1108                         eo_info->hostname = conv_data->http_host;
1109                         eo_info->filename = conv_data->request_uri;
1110                         eo_info->content_type = headers.content_type;
1111                         eo_info->payload_len = next_tvb->length;
1112                         eo_info->payload_data = next_tvb->real_data;
1113
1114                         tap_queue_packet(http_eo_tap, pinfo, eo_info);
1115                 }
1116
1117                 /*
1118                  * Do subdissector checks.
1119                  *
1120                  * First, check whether some subdissector asked that they
1121                  * be called if something was on some particular port.
1122                  */
1123
1124                 handle = dissector_get_port_handle(port_subdissector_table,
1125                     pinfo->match_port);
1126                 if (handle == NULL && headers.content_type != NULL) {
1127                         /*
1128                          * We didn't find any subdissector that
1129                          * registered for the port, and we have a
1130                          * Content-Type value.  Is there any subdissector
1131                          * for that content type?
1132                          */
1133                         save_private_data = pinfo->private_data;
1134
1135                         if (headers.content_type_parameters)
1136                                 pinfo->private_data = ep_strdup(headers.content_type_parameters);
1137                         else
1138                                 pinfo->private_data = NULL;
1139                         /*
1140                          * Calling the string handle for the media type
1141                          * dissector table will set pinfo->match_string
1142                          * to headers.content_type for us.
1143                          */
1144                         pinfo->match_string = headers.content_type;
1145                         handle = dissector_get_string_handle(
1146                             media_type_subdissector_table,
1147                             headers.content_type);
1148                 }
1149                 if (handle != NULL) {
1150                         /*
1151                          * We have a subdissector - call it.
1152                          */
1153                         dissected = call_dissector(handle, next_tvb, pinfo,
1154                             tree);
1155                 } else {
1156                         /*
1157                          * We don't have a subdissector - try the heuristic
1158                          * subdissectors.
1159                          */
1160                         dissected = dissector_try_heuristic(
1161                             heur_subdissector_list, next_tvb, pinfo, tree);
1162                 }
1163                 if (dissected) {
1164                         /*
1165                          * The subdissector dissected the body.
1166                          * Fix up the top-level item so that it doesn't
1167                          * include the stuff for that protocol.
1168                          */
1169                         if (ti != NULL)
1170                                 proto_item_set_len(ti, offset);
1171                 } else {
1172                         if (headers.content_type != NULL) {
1173                                 /*
1174                                  * Calling the default media handle if there is a content-type that
1175                                  * wasn't handled above.
1176                                  */
1177                                 call_dissector(media_handle, next_tvb, pinfo, tree);
1178                         } else {
1179                                 /* Call the default data dissector */
1180                                 call_dissector(data_handle, next_tvb, pinfo, http_tree);
1181                         }
1182                 }
1183
1184         body_dissected:
1185                 /*
1186                  * Do *not* attempt at freeing the private data;
1187                  * it may be in use by subdissectors.
1188                  */
1189                 if (save_private_data)
1190                         pinfo->private_data = save_private_data;
1191                 /*
1192                  * We've processed "datalen" bytes worth of data
1193                  * (which may be no data at all); advance the
1194                  * offset past whatever data we've processed.
1195                  */
1196                 offset += datalen;
1197         }
1198
1199         tap_queue_packet(http_tap, pinfo, stat_info);
1200
1201         return offset - orig_offset;
1202 }
1203
1204 /* This can be used to dissect an HTTP request until such time
1205  * that a more complete dissector is written for that HTTP request.
1206  * This simple dissector only puts the request method, URI, and
1207  * protocol version into a sub-tree.
1208  */
1209 static void
1210 basic_request_dissector(tvbuff_t *tvb, proto_tree *tree, int offset,
1211                         const guchar *line, const guchar *lineend,
1212                         http_conv_t *conv_data)
1213 {
1214         const guchar *next_token;
1215         gchar *request_uri;
1216         int tokenlen;
1217
1218         /* The first token is the method. */
1219         tokenlen = get_token_len(line, lineend, &next_token);
1220         if (tokenlen == 0)
1221                 return;
1222         proto_tree_add_item(tree, hf_http_request_method, tvb, offset, tokenlen,
1223             FALSE);
1224         offset += next_token - line;
1225         line = next_token;
1226
1227         /* The next token is the URI. */
1228         tokenlen = get_token_len(line, lineend, &next_token);
1229         if (tokenlen == 0)
1230                 return;
1231
1232         /* Save the request URI for various later uses */
1233         request_uri = (gchar *)tvb_get_ephemeral_string(tvb, offset, tokenlen);
1234         stat_info->request_uri = ep_strdup(request_uri);
1235         conv_data->request_uri = se_strdup(request_uri);
1236
1237         proto_tree_add_string(tree, hf_http_request_uri, tvb, offset, tokenlen,
1238                               request_uri);
1239         offset += next_token - line;
1240         line = next_token;
1241
1242         /* Everything to the end of the line is the version. */
1243         tokenlen = lineend - line;
1244         if (tokenlen == 0)
1245                 return;
1246         proto_tree_add_item(tree, hf_http_version, tvb, offset, tokenlen,
1247             FALSE);
1248 }
1249
1250 static void
1251 basic_response_dissector(tvbuff_t *tvb, proto_tree *tree, int offset,
1252                          const guchar *line, const guchar *lineend,
1253                          http_conv_t *conv_data _U_)
1254 {
1255         const guchar *next_token;
1256         int tokenlen;
1257         gchar response_chars[4];
1258
1259         /* The first token is the version. */
1260         tokenlen = get_token_len(line, lineend, &next_token);
1261         if (tokenlen == 0)
1262                 return;
1263         proto_tree_add_item(tree, hf_http_version, tvb, offset, tokenlen,
1264             FALSE);
1265         offset += next_token - line;
1266         line = next_token;
1267
1268         /* The next token is the status code. */
1269         tokenlen = get_token_len(line, lineend, &next_token);
1270         if (tokenlen < 3)
1271                 return;
1272         memcpy(response_chars, line, 3);
1273         response_chars[3] = '\0';
1274
1275         stat_info->response_code = conv_data->response_code =
1276                 strtoul(response_chars, NULL, 10);
1277
1278         proto_tree_add_uint(tree, hf_http_response_code, tvb, offset, 3,
1279             stat_info->response_code);
1280 }
1281
1282 /*
1283  * Dissect the http data chunks and add them to the tree.
1284  */
1285 static int
1286 chunked_encoding_dissector(tvbuff_t **tvb_ptr, packet_info *pinfo,
1287     proto_tree *tree, int offset)
1288 {
1289         guint8 *chunk_string = NULL;
1290         guint32 chunk_size = 0;
1291         gint chunk_offset = 0;
1292         guint32 datalen = 0;
1293         gint linelen = 0;
1294         gint chunks_decoded = 0;
1295         tvbuff_t *tvb = NULL;
1296         tvbuff_t *new_tvb = NULL;
1297         gint chunked_data_size = 0;
1298         proto_tree *subtree = NULL;
1299         proto_item *ti = NULL;
1300
1301         if (tvb_ptr == NULL || *tvb_ptr == NULL) {
1302                 return 0;
1303         }
1304
1305         tvb = *tvb_ptr;
1306
1307         datalen = tvb_reported_length_remaining(tvb, offset);
1308
1309         if (tree) {
1310                 ti = proto_tree_add_text(tree, tvb, offset, datalen,
1311                     "HTTP chunked response");
1312                 subtree = proto_item_add_subtree(ti, ett_http_chunked_response);
1313         }
1314
1315
1316         while (datalen != 0) {
1317                 proto_item *chunk_ti = NULL;
1318                 proto_tree *chunk_subtree = NULL;
1319                 tvbuff_t *data_tvb = NULL;
1320                 gchar *c = NULL;
1321                 guint8 *raw_data;
1322                 gint raw_len = 0;
1323
1324                 linelen = tvb_find_line_end(tvb, offset, -1, &chunk_offset, TRUE);
1325
1326                 if (linelen <= 0) {
1327                         /* Can't get the chunk size line */
1328                         break;
1329                 }
1330
1331                 chunk_string = tvb_get_ephemeral_string(tvb, offset, linelen);
1332
1333                 if (chunk_string == NULL) {
1334                         /* Can't get the chunk size line */
1335                         break;
1336                 }
1337
1338                 c = (gchar*) chunk_string;
1339
1340                 /*
1341                  * We don't care about the extensions.
1342                  */
1343                 if ((c = strchr(c, ';'))) {
1344                         *c = '\0';
1345                 }
1346
1347                 chunk_size = strtol((gchar*)chunk_string, NULL, 16);
1348
1349                 if (chunk_size > datalen) {
1350                         /*
1351                          * The chunk size is more than what's in the tvbuff,
1352                          * so either the user hasn't enabled decoding, or all
1353                          * of the segments weren't captured.
1354                          */
1355                         chunk_size = datalen;
1356                 }
1357 #if 0
1358                   else if (new_tvb == NULL) {
1359                         new_tvb = tvb_new_composite();
1360                 }
1361
1362
1363
1364                 if (new_tvb != NULL && chunk_size != 0) {
1365                         tvbuff_t *chunk_tvb = NULL;
1366
1367                         chunk_tvb = tvb_new_subset(tvb, chunk_offset,
1368                             chunk_size, datalen);
1369
1370                         tvb_composite_append(new_tvb, chunk_tvb);
1371
1372                 }
1373 #endif
1374
1375                 chunked_data_size += chunk_size;
1376
1377                 raw_data = g_malloc(chunked_data_size);
1378                 raw_len = 0;
1379
1380                 if (new_tvb != NULL) {
1381                         raw_len = tvb_length_remaining(new_tvb, 0);
1382                         tvb_memcpy(new_tvb, raw_data, 0, raw_len);
1383
1384                         tvb_free(new_tvb);
1385                 }
1386
1387                 tvb_memcpy(tvb, (guint8 *)(raw_data + raw_len),
1388                             chunk_offset, chunk_size);
1389
1390                 /* Don't create a new tvb if we have a single chunk with
1391                  * a size of zero (meaning it is the end of the chunks). */
1392                 if(chunked_data_size > 0) {
1393                         new_tvb = tvb_new_real_data(raw_data,
1394                               chunked_data_size, chunked_data_size);
1395                         tvb_set_free_cb(new_tvb, g_free);
1396                 }
1397
1398
1399                 if (subtree) {
1400                         if(chunk_size == 0) {
1401                                 chunk_ti = proto_tree_add_text(subtree, tvb,
1402                                             offset,
1403                                             chunk_offset - offset + chunk_size + 2,
1404                                             "End of chunked encoding");
1405                         } else {
1406                                 chunk_ti = proto_tree_add_text(subtree, tvb,
1407                                             offset,
1408                                             chunk_offset - offset + chunk_size + 2,
1409                                             "Data chunk (%u octets)", chunk_size);
1410                         }
1411
1412                         chunk_subtree = proto_item_add_subtree(chunk_ti,
1413                             ett_http_chunk_data);
1414
1415                         proto_tree_add_text(chunk_subtree, tvb, offset,
1416                             chunk_offset - offset, "Chunk size: %u octets",
1417                             chunk_size);
1418
1419                         data_tvb = tvb_new_subset(tvb, chunk_offset, chunk_size,
1420                             datalen);
1421
1422
1423                         /*
1424                          * XXX - just use "proto_tree_add_text()"?
1425                          * This means that, in TShark, you get
1426                          * the entire chunk dumped out in hex,
1427                          * in addition to whatever dissection is
1428                          * done on the reassembled data.
1429                          */
1430                         call_dissector(data_handle, data_tvb, pinfo,
1431                                     chunk_subtree);
1432
1433                         proto_tree_add_text(chunk_subtree, tvb, chunk_offset +
1434                             chunk_size, 2, "Chunk boundary");
1435                 }
1436
1437                 chunks_decoded++;
1438                 offset = chunk_offset + chunk_size + 2;
1439                 datalen = tvb_reported_length_remaining(tvb, offset);
1440         }
1441
1442         if (new_tvb != NULL) {
1443
1444                 /* Placeholder for the day that composite tvbuffer's will work.
1445                 tvb_composite_finalize(new_tvb);
1446                 / * tvb_set_reported_length(new_tvb, chunked_data_size); * /
1447                 */
1448
1449                 /*
1450                  * XXX - Don't free this, since the tvbuffer that was passed
1451                  * may be used if the data spans multiple frames and reassembly
1452                  * isn't enabled.
1453                  *
1454                 tvb_free(*tvb_ptr);
1455                  */
1456                 *tvb_ptr = new_tvb;
1457
1458         } else {
1459                 /*
1460                  * We didn't create a new tvb, so don't allow sub dissectors
1461                  * try to decode the non-existant entity body.
1462                  */
1463                 chunks_decoded = -1;
1464         }
1465
1466         return chunks_decoded;
1467
1468 }
1469
1470 /* Call a subdissector to handle HTTP CONNECT's traffic */
1471 static void
1472 http_payload_subdissector(tvbuff_t *tvb, proto_tree *tree,
1473                           packet_info *pinfo, http_conv_t *conv_data)
1474 {
1475         guint32 *ptr = NULL;
1476         guint32 dissect_as, saved_port;
1477         gchar **strings; /* An array for splitting the request URI into hostname and port */
1478         proto_item *item;
1479         proto_tree *proxy_tree;
1480
1481         /* Grab the destination port number from the request URI to find the right subdissector */
1482         strings = g_strsplit(conv_data->request_uri, ":", 2);
1483
1484         if(strings[0] != NULL && strings[1] != NULL) {
1485                 /*
1486                  * The string was successfuly split in two
1487                  * Create a proxy-connect subtree
1488                  */
1489                 if(tree) {
1490                         item = proto_tree_add_item(tree, proto_http, tvb, 0, -1, FALSE);
1491                         proxy_tree = proto_item_add_subtree(item, ett_http);
1492
1493                         item = proto_tree_add_string(proxy_tree, hf_http_proxy_connect_host,
1494                             tvb, 0, 0, strings[0]);
1495                         PROTO_ITEM_SET_GENERATED(item);
1496
1497                         item = proto_tree_add_uint(proxy_tree, hf_http_proxy_connect_port,
1498                             tvb, 0, 0, strtol(strings[1], NULL, 10) );
1499                         PROTO_ITEM_SET_GENERATED(item);
1500                 }
1501
1502                 /* We're going to get stuck in a loop if we let process_tcp_payload call
1503                    us, so call the data dissector instead for proxy connections to http ports. */
1504                 dissect_as = (int)strtol(strings[1], NULL, 10); /* Convert string to a base-10 integer */
1505
1506                 if (value_is_in_range(http_tcp_range, dissect_as)) {
1507                         call_dissector(data_handle, tvb, pinfo, tree);
1508                 } else {
1509                         /* set pinfo->{src/dst port} and call the TCP sub-dissector lookup */
1510                         if ( !ptr && value_is_in_range(http_tcp_range, pinfo->destport) )
1511                                 ptr = &pinfo->destport;
1512                         else
1513                                 ptr = &pinfo->srcport;
1514
1515                         /* Increase pinfo->can_desegment because we are traversing
1516                          * http and want to preserve desegmentation functionality for
1517                          * the proxied protocol
1518                          */
1519                         if( pinfo->can_desegment>0 )
1520                                 pinfo->can_desegment++;
1521
1522                         saved_port = *ptr;
1523                         *ptr = dissect_as;
1524                         decode_tcp_ports(tvb, 0, pinfo, tree,
1525                                 pinfo->srcport, pinfo->destport, NULL);
1526                         *ptr = saved_port;
1527                 }
1528         }
1529         g_strfreev(strings); /* Free the result of g_strsplit() above */
1530 }
1531
1532
1533
1534 /*
1535  * XXX - this won't handle HTTP 0.9 replies, but they're all data
1536  * anyway.
1537  */
1538 static int
1539 is_http_request_or_reply(const gchar *data, int linelen, http_type_t *type,
1540                          ReqRespDissector *reqresp_dissector,
1541                          http_conv_t *conv_data)
1542 {
1543         int isHttpRequestOrReply = FALSE;
1544         int prefix_len = 0;
1545
1546         /*
1547          * From RFC 2774 - An HTTP Extension Framework
1548          *
1549          * Support the command prefix that identifies the presence of
1550          * a "mandatory" header.
1551          */
1552         if (linelen >= 2 && strncmp(data, "M-", 2) == 0) {
1553                 data += 2;
1554                 linelen -= 2;
1555                 prefix_len = 2;
1556         }
1557
1558         /*
1559          * From draft-cohen-gena-client-01.txt, available from the uPnP forum:
1560          *      NOTIFY, SUBSCRIBE, UNSUBSCRIBE
1561          *
1562          * From draft-ietf-dasl-protocol-00.txt, a now vanished Microsoft draft:
1563          *      SEARCH
1564          */
1565         if (linelen >= 5 && strncmp(data, "HTTP/", 5) == 0) {
1566                 *type = HTTP_RESPONSE;
1567                 isHttpRequestOrReply = TRUE;    /* response */
1568                 if (reqresp_dissector)
1569                         *reqresp_dissector = basic_response_dissector;
1570         } else {
1571                 const guchar * ptr = (const guchar *)data;
1572                 int              indx = 0;
1573
1574                 /* Look for the space following the Method */
1575                 while (indx < linelen) {
1576                         if (*ptr == ' ')
1577                                 break;
1578                         else {
1579                                 ptr++;
1580                                 indx++;
1581                         }
1582                 }
1583
1584                 /* Check the methods that have same length */
1585                 switch (indx) {
1586
1587                 case 3:
1588                         if (strncmp(data, "GET", indx) == 0 ||
1589                             strncmp(data, "PUT", indx) == 0) {
1590                                 *type = HTTP_REQUEST;
1591                                 isHttpRequestOrReply = TRUE;
1592                         }
1593                         else if (strncmp(data, "ICY", indx) == 0) {
1594                                 *type = HTTP_RESPONSE;
1595                                 isHttpRequestOrReply = TRUE;
1596                         }
1597                         break;
1598
1599                 case 4:
1600                         if (strncmp(data, "COPY", indx) == 0 ||
1601                             strncmp(data, "HEAD", indx) == 0 ||
1602                             strncmp(data, "LOCK", indx) == 0 ||
1603                             strncmp(data, "MOVE", indx) == 0 ||
1604                             strncmp(data, "POLL", indx) == 0 ||
1605                             strncmp(data, "POST", indx) == 0) {
1606                                 *type = HTTP_REQUEST;
1607                                 isHttpRequestOrReply = TRUE;
1608                         }
1609                         break;
1610
1611                 case 5:
1612                         if (strncmp(data, "BCOPY", indx) == 0 ||
1613                                 strncmp(data, "BMOVE", indx) == 0 ||
1614                                 strncmp(data, "MKCOL", indx) == 0 ||
1615                                 strncmp(data, "TRACE", indx) == 0 ||
1616                                 strncmp(data, "LABEL", indx) == 0 ||  /* RFC 3253 8.2 */
1617                                 strncmp(data, "MERGE", indx) == 0) {  /* RFC 3253 11.2 */
1618                                 *type = HTTP_REQUEST;
1619                                 isHttpRequestOrReply = TRUE;
1620                         }
1621                         break;
1622
1623                 case 6:
1624                         if (strncmp(data, "DELETE", indx) == 0 ||
1625                                 strncmp(data, "SEARCH", indx) == 0 ||
1626                                 strncmp(data, "UNLOCK", indx) == 0 ||
1627                                 strncmp(data, "REPORT", indx) == 0 ||  /* RFC 3253 3.6 */
1628                                 strncmp(data, "UPDATE", indx) == 0) {  /* RFC 3253 7.1 */
1629                                 *type = HTTP_REQUEST;
1630                                 isHttpRequestOrReply = TRUE;
1631                         }
1632                         else if (strncmp(data, "NOTIFY", indx) == 0) {
1633                                 *type = HTTP_NOTIFICATION;
1634                                 isHttpRequestOrReply = TRUE;
1635                         }
1636                         break;
1637
1638                 case 7:
1639                         if (strncmp(data, "BDELETE", indx) == 0 ||
1640                             strncmp(data, "CONNECT", indx) == 0 ||
1641                             strncmp(data, "OPTIONS", indx) == 0 ||
1642                             strncmp(data, "CHECKIN", indx) == 0) {  /* RFC 3253 4.4, 9.4 */
1643                                 *type = HTTP_REQUEST;
1644                                 isHttpRequestOrReply = TRUE;
1645                         }
1646                         break;
1647
1648                 case 8:
1649                         if (strncmp(data, "PROPFIND", indx) == 0 ||
1650                             strncmp(data, "CHECKOUT", indx) == 0 || /* RFC 3253 4.3, 9.3 */
1651                             strncmp(data, "CCM_POST", indx) == 0) {
1652                                 *type = HTTP_REQUEST;
1653                                 isHttpRequestOrReply = TRUE;
1654                         }
1655                         break;
1656
1657                 case 9:
1658                         if (strncmp(data, "SUBSCRIBE", indx) == 0) {
1659                                 *type = HTTP_NOTIFICATION;
1660                                 isHttpRequestOrReply = TRUE;
1661                         } else if (strncmp(data, "PROPPATCH", indx) == 0 ||
1662                             strncmp(data, "BPROPFIND", indx) == 0) {
1663                                 *type = HTTP_REQUEST;
1664                                 isHttpRequestOrReply = TRUE;
1665                         }
1666                         break;
1667
1668                 case 10:
1669                         if (strncmp(data, "BPROPPATCH", indx) == 0 ||
1670                                 strncmp(data, "UNCHECKOUT", indx) == 0 ||  /* RFC 3253 4.5 */
1671                                 strncmp(data, "MKACTIVITY", indx) == 0) {  /* RFC 3253 13.5 */
1672                                 *type = HTTP_REQUEST;
1673                                 isHttpRequestOrReply = TRUE;
1674                         }
1675                         break;
1676
1677                 case 11:
1678                         if (strncmp(data, "MKWORKSPACE", indx) == 0) {  /* RFC 3253 6.3 */
1679                                 *type = HTTP_REQUEST;
1680                                 isHttpRequestOrReply = TRUE;
1681                         } else if (strncmp(data, "UNSUBSCRIBE", indx) == 0) {
1682                                 *type = HTTP_NOTIFICATION;
1683                                 isHttpRequestOrReply = TRUE;
1684                         } else if (strncmp(data, "RPC_CONNECT", indx) == 0) {
1685                                 *type = HTTP_REQUEST;
1686                                 isHttpRequestOrReply = TRUE;
1687                         }
1688                         break;
1689
1690                 case 15:
1691                         if (strncmp(data, "VERSION-CONTROL", indx) == 0) {  /* RFC 3253 3.5 */
1692                                 *type = HTTP_REQUEST;
1693                                 isHttpRequestOrReply = TRUE;
1694                         }
1695                         break;
1696
1697                 case 16:
1698                         if (strncmp(data, "BASELINE-CONTROL", indx) == 0) {  /* RFC 3253 12.6 */
1699                                 *type = HTTP_REQUEST;
1700                                 isHttpRequestOrReply = TRUE;
1701                         }
1702                         break;
1703
1704                 default:
1705                         break;
1706                 }
1707
1708                 if (isHttpRequestOrReply && reqresp_dissector) {
1709                         *reqresp_dissector = basic_request_dissector;
1710
1711                         stat_info->request_method = ep_strndup(data, indx+1);
1712                         conv_data->request_method = se_strndup(data, indx+1);
1713                 }
1714
1715
1716
1717         }
1718
1719         return isHttpRequestOrReply;
1720 }
1721
1722 /*
1723  * Process headers.
1724  */
1725 typedef struct {
1726         const char      *name;
1727         gint            *hf;
1728         int             special;
1729 } header_info;
1730
1731 #define HDR_NO_SPECIAL          0
1732 #define HDR_AUTHORIZATION       1
1733 #define HDR_AUTHENTICATE        2
1734 #define HDR_CONTENT_TYPE        3
1735 #define HDR_CONTENT_LENGTH      4
1736 #define HDR_CONTENT_ENCODING    5
1737 #define HDR_TRANSFER_ENCODING   6
1738 #define HDR_HOST  7
1739
1740 static const header_info headers[] = {
1741         { "Authorization", &hf_http_authorization, HDR_AUTHORIZATION },
1742         { "Proxy-Authorization", &hf_http_proxy_authorization, HDR_AUTHORIZATION },
1743         { "Proxy-Authenticate", &hf_http_proxy_authenticate, HDR_AUTHENTICATE },
1744         { "WWW-Authenticate", &hf_http_www_authenticate, HDR_AUTHENTICATE },
1745         { "Content-Type", &hf_http_content_type, HDR_CONTENT_TYPE },
1746         { "Content-Length", &hf_http_content_length, HDR_CONTENT_LENGTH },
1747         { "Content-Encoding", &hf_http_content_encoding, HDR_CONTENT_ENCODING },
1748         { "Transfer-Encoding", &hf_http_transfer_encoding, HDR_TRANSFER_ENCODING },
1749         { "User-Agent", &hf_http_user_agent, HDR_NO_SPECIAL },
1750         { "Host", &hf_http_host, HDR_HOST },
1751         { "Connection", &hf_http_connection, HDR_NO_SPECIAL },
1752         { "Cookie", &hf_http_cookie, HDR_NO_SPECIAL },
1753         { "Accept", &hf_http_accept, HDR_NO_SPECIAL },
1754         { "Referer", &hf_http_referer, HDR_NO_SPECIAL },
1755         { "Accept-Language", &hf_http_accept_language, HDR_NO_SPECIAL },
1756         { "Accept-Encoding", &hf_http_accept_encoding, HDR_NO_SPECIAL },
1757         { "Date", &hf_http_date, HDR_NO_SPECIAL },
1758         { "Cache-Control", &hf_http_cache_control, HDR_NO_SPECIAL },
1759         { "Server", &hf_http_server, HDR_NO_SPECIAL },
1760         { "Location", &hf_http_location, HDR_NO_SPECIAL },
1761         { "Set-Cookie", &hf_http_set_cookie, HDR_NO_SPECIAL },
1762         { "Last-Modified", &hf_http_last_modified, HDR_NO_SPECIAL },
1763         { "X-Forwarded-For", &hf_http_x_forwarded_for, HDR_NO_SPECIAL },
1764 };
1765
1766 static void
1767 process_header(tvbuff_t *tvb, int offset, int next_offset,
1768     const guchar *line, int linelen, int colon_offset,
1769     packet_info *pinfo, proto_tree *tree, headers_t *eh_ptr,
1770     http_conv_t *conv_data)
1771 {
1772         int len;
1773         int line_end_offset;
1774         int header_len;
1775         gint hf_index;
1776         guchar c;
1777         int value_offset;
1778         int value_len;
1779         char *value;
1780         char *p;
1781         guchar *up;
1782         proto_item *hdr_item;
1783         int i;
1784
1785         len = next_offset - offset;
1786         line_end_offset = offset + linelen;
1787         header_len = colon_offset - offset;
1788         hf_index = find_header_hf_value(tvb, offset, header_len);
1789
1790         if (hf_index == -1) {
1791                 /*
1792                  * Not a header we know anything about.  Just put it into
1793                  * the tree as text.
1794                  */
1795                 if (tree) {
1796                         proto_tree_add_text(tree, tvb, offset, len,
1797                             "%s", format_text(line, len));
1798                 }
1799         } else {
1800                 /*
1801                  * Skip whitespace after the colon.
1802                  */
1803                 value_offset = colon_offset + 1;
1804                 while (value_offset < line_end_offset
1805                     && ((c = line[value_offset - offset]) == ' ' || c == '\t'))
1806                         value_offset++;
1807
1808                 /*
1809                  * Fetch the value.
1810                  */
1811                 value_len = line_end_offset - value_offset;
1812                 value = ep_strndup(&line[value_offset - offset], value_len);
1813
1814                 /*
1815                  * Add it to the protocol tree as a particular field,
1816                  * but display the line as is.
1817                  */
1818                 if (tree) {
1819                         header_field_info *hfinfo;
1820                         guint32 tmp;
1821
1822                         hfinfo = proto_registrar_get_nth(*headers[hf_index].hf);
1823                         switch(hfinfo->type){
1824                         case FT_UINT8:
1825                         case FT_UINT16:
1826                         case FT_UINT24:
1827                         case FT_UINT32:
1828                         case FT_INT8:
1829                         case FT_INT16:
1830                         case FT_INT24:
1831                         case FT_INT32:
1832                                 tmp=strtol(value, NULL, 10);
1833                                 hdr_item = proto_tree_add_uint(tree, *headers[hf_index].hf, tvb, offset, len, tmp);
1834                                 break;
1835                         default:
1836                                 hdr_item = proto_tree_add_string_format(tree,
1837                                     *headers[hf_index].hf, tvb, offset, len,
1838                                     value, "%s", format_text(line, len));
1839                         }
1840                 } else
1841                         hdr_item = NULL;
1842
1843                 /*
1844                  * Do any special processing that particular headers
1845                  * require.
1846                  */
1847                 switch (headers[hf_index].special) {
1848
1849                 case HDR_AUTHORIZATION:
1850                         if (check_auth_ntlmssp(hdr_item, tvb, pinfo, value))
1851                                 break;  /* dissected NTLMSSP */
1852                         check_auth_basic(hdr_item, tvb, value);
1853                         break;
1854
1855                 case HDR_AUTHENTICATE:
1856                         check_auth_ntlmssp(hdr_item, tvb, pinfo, value);
1857                         break;
1858
1859                 case HDR_CONTENT_TYPE:
1860                         eh_ptr->content_type = (gchar*) ep_memdup((guint8*)value,value_len + 1);
1861
1862                         for (i = 0; i < value_len; i++) {
1863                                 c = value[i];
1864                                 if (c == ';' || isspace(c)) {
1865                                         /*
1866                                          * End of subtype - either
1867                                          * white space or a ";"
1868                                          * separating the subtype from
1869                                          * a parameter.
1870                                          */
1871                                         break;
1872                                 }
1873
1874                                 /*
1875                                  * Map the character to lower case;
1876                                  * content types are case-insensitive.
1877                                  */
1878                                 eh_ptr->content_type[i] = tolower(eh_ptr->content_type[i]);
1879                         }
1880                         eh_ptr->content_type[i] = '\0';
1881                         /*
1882                          * Now find the start of the optional parameters;
1883                          * skip the optional white space and the semicolon
1884                          * if this has not been done before.
1885                          */
1886                         i++;
1887                         while (i < value_len) {
1888                                 c = eh_ptr->content_type[i];
1889                                 if (c == ';' || isspace(c))
1890                                         /* Skip till start of parameters */
1891                                         i++;
1892                                 else
1893                                         break;
1894                         }
1895                         if (i < value_len)
1896                                 eh_ptr->content_type_parameters = eh_ptr->content_type + i;
1897                         else
1898                                 eh_ptr->content_type_parameters = NULL;
1899                         break;
1900
1901                 case HDR_CONTENT_LENGTH:
1902                         eh_ptr->content_length = strtol(value, &p, 10);
1903                         up = (guchar *)p;
1904                         if (eh_ptr->content_length < 0 || p == value ||
1905                             (*up != '\0' && !isspace(*up))) {
1906                                 /*
1907                                  * Content length not valid; pretend
1908                                  * we don't have it.
1909                                  */
1910                                 eh_ptr->have_content_length = FALSE;
1911                         } else {
1912                                 /*
1913                                  * We do have a valid content length.
1914                                  */
1915                                 eh_ptr->have_content_length = TRUE;
1916                         }
1917                         break;
1918
1919                 case HDR_CONTENT_ENCODING:
1920                         eh_ptr->content_encoding = ep_strndup(value, value_len);
1921                         break;
1922
1923                 case HDR_TRANSFER_ENCODING:
1924                         eh_ptr->transfer_encoding = ep_strndup(value, value_len);
1925                         break;
1926
1927                 case HDR_HOST:
1928                         stat_info->http_host = ep_strndup(value, value_len);
1929                         conv_data->http_host = se_strndup(value, value_len);
1930                         break;
1931
1932                 }
1933         }
1934 }
1935
1936 /* Returns index of header tag in headers */
1937 static gint
1938 find_header_hf_value(tvbuff_t *tvb, int offset, guint header_len)
1939 {
1940         guint i;
1941
1942         for (i = 0; i < array_length(headers); i++) {
1943                 if (header_len == strlen(headers[i].name) &&
1944                     tvb_strncaseeql(tvb, offset,
1945                                                 headers[i].name, header_len) == 0)
1946                         return i;
1947         }
1948
1949         return -1;
1950 }
1951
1952 /*
1953  * Dissect Microsoft's abomination called NTLMSSP over HTTP.
1954  */
1955 static gboolean
1956 check_auth_ntlmssp(proto_item *hdr_item, tvbuff_t *tvb, packet_info *pinfo,
1957     gchar *value)
1958 {
1959         static const char *ntlm_headers[] = {
1960                 "NTLM ",
1961                 "Negotiate ",
1962                 NULL
1963         };
1964         const char **header;
1965         size_t hdrlen;
1966         proto_tree *hdr_tree;
1967
1968         /*
1969          * Check for NTLM credentials and challenge; those can
1970          * occur with WWW-Authenticate.
1971          */
1972         for (header = &ntlm_headers[0]; *header != NULL; header++) {
1973                 hdrlen = strlen(*header);
1974                 if (strncmp(value, *header, hdrlen) == 0) {
1975                         if (hdr_item != NULL) {
1976                                 hdr_tree = proto_item_add_subtree(hdr_item,
1977                                     ett_http_ntlmssp);
1978                         } else
1979                                 hdr_tree = NULL;
1980                         value += hdrlen;
1981                         dissect_http_ntlmssp(tvb, pinfo, hdr_tree, value);
1982                         return TRUE;
1983                 }
1984         }
1985         return FALSE;
1986 }
1987
1988 /*
1989  * Dissect HTTP Basic authorization.
1990  */
1991 static gboolean
1992 check_auth_basic(proto_item *hdr_item, tvbuff_t *tvb, gchar *value)
1993 {
1994         static const char *basic_headers[] = {
1995                 "Basic ",
1996                 NULL
1997         };
1998         const char **header;
1999         size_t hdrlen;
2000         proto_tree *hdr_tree;
2001         size_t len;
2002
2003         for (header = &basic_headers[0]; *header != NULL; header++) {
2004                 hdrlen = strlen(*header);
2005                 if (strncmp(value, *header, hdrlen) == 0) {
2006                         if (hdr_item != NULL) {
2007                                 hdr_tree = proto_item_add_subtree(hdr_item,
2008                                     ett_http_ntlmssp);
2009                         } else
2010                                 hdr_tree = NULL;
2011                         value += hdrlen;
2012
2013                         len = epan_base64_decode(value);
2014                         value[len] = '\0';
2015                         proto_tree_add_string(hdr_tree, hf_http_basic, tvb,
2016                             0, 0, value);
2017
2018                         return TRUE;
2019                 }
2020         }
2021         return FALSE;
2022 }
2023
2024 static void
2025 dissect_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2026 {
2027         http_conv_t     *conv_data;
2028         int             offset = 0;
2029         int             len;
2030
2031         /*
2032          * Check if this is proxied connection and if so, hand of dissection to the
2033          * payload-dissector.
2034          * Response code 200 means "OK" and strncmp() == 0 means the strings match exactly */
2035         conv_data = get_http_conversation_data(pinfo);
2036         if(pinfo->fd->num >= conv_data->startframe &&
2037            conv_data->response_code == 200 &&
2038            conv_data->request_method &&
2039            strncmp(conv_data->request_method, "CONNECT", 7) == 0 &&
2040            conv_data->request_uri) {
2041                 if(conv_data->startframe == 0 && !pinfo->fd->flags.visited)
2042                         conv_data->startframe = pinfo->fd->num;
2043                 http_payload_subdissector(tvb, tree, pinfo, conv_data);
2044         } else {
2045                 while (tvb_reported_length_remaining(tvb, offset) != 0) {
2046                         len = dissect_http_message(tvb, offset, pinfo, tree, conv_data);
2047                         if (len == -1)
2048                                 break;
2049                         offset += len;
2050
2051                         /*
2052                          * OK, we've set the Protocol and Info columns for the
2053                          * first HTTP message; set a fence so that subsequent
2054                          * HTTP messages don't overwrite the Info column.
2055                          */
2056                         if (check_col(pinfo->cinfo, COL_INFO))
2057                                 col_set_fence(pinfo->cinfo, COL_INFO);
2058                 }
2059         }
2060 }
2061
2062 static void
2063 dissect_http_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2064 {
2065         http_conv_t     *conv_data;
2066
2067         conv_data = get_http_conversation_data(pinfo);
2068         dissect_http_message(tvb, 0, pinfo, tree, conv_data);
2069 }
2070
2071 static void range_delete_http_tcp_callback(guint32 port) {
2072   dissector_delete("tcp.port", port, http_handle);
2073 }
2074
2075 static void range_add_http_tcp_callback(guint32 port) {
2076   dissector_add("tcp.port", port, http_handle);
2077 }
2078
2079 static void range_delete_http_ssl_callback(guint32 port) {
2080   ssl_dissector_delete(port, "http", TRUE);
2081 }
2082
2083 static void range_add_http_ssl_callback(guint32 port) {
2084   ssl_dissector_add(port, "http", TRUE);
2085 }
2086
2087 static void reinit_http(void) {
2088         range_foreach(http_tcp_range, range_delete_http_tcp_callback);
2089         g_free(http_tcp_range);
2090         http_tcp_range = range_copy(global_http_tcp_range);
2091         range_foreach(http_tcp_range, range_add_http_tcp_callback);
2092
2093         range_foreach(http_ssl_range, range_delete_http_ssl_callback);
2094         g_free(http_ssl_range);
2095         http_ssl_range = range_copy(global_http_ssl_range);
2096         range_foreach(http_ssl_range, range_add_http_ssl_callback);
2097 }
2098
2099 void
2100 proto_register_http(void)
2101 {
2102         static hf_register_info hf[] = {
2103             { &hf_http_notification,
2104               { "Notification",         "http.notification",
2105                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2106                 "TRUE if HTTP notification", HFILL }},
2107             { &hf_http_response,
2108               { "Response",             "http.response",
2109                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2110                 "TRUE if HTTP response", HFILL }},
2111             { &hf_http_request,
2112               { "Request",              "http.request",
2113                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2114                 "TRUE if HTTP request", HFILL }},
2115             { &hf_http_basic,
2116               { "Credentials",          "http.authbasic",
2117                 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }},
2118             { &hf_http_request_method,
2119               { "Request Method",       "http.request.method",
2120                 FT_STRING, BASE_NONE, NULL, 0x0,
2121                 "HTTP Request Method", HFILL }},
2122             { &hf_http_request_uri,
2123               { "Request URI",  "http.request.uri",
2124                 FT_STRING, BASE_NONE, NULL, 0x0,
2125                 "HTTP Request-URI", HFILL }},
2126             { &hf_http_version,
2127               { "Request Version",      "http.request.version",
2128                 FT_STRING, BASE_NONE, NULL, 0x0,
2129                 "HTTP Request HTTP-Version", HFILL }},
2130             { &hf_http_response_code,
2131               { "Response Code",        "http.response.code",
2132                 FT_UINT16, BASE_DEC, NULL, 0x0,
2133                 "HTTP Response Code", HFILL }},
2134             { &hf_http_authorization,
2135               { "Authorization",        "http.authorization",
2136                 FT_STRING, BASE_NONE, NULL, 0x0,
2137                 "HTTP Authorization header", HFILL }},
2138             { &hf_http_proxy_authenticate,
2139               { "Proxy-Authenticate",   "http.proxy_authenticate",
2140                 FT_STRING, BASE_NONE, NULL, 0x0,
2141                 "HTTP Proxy-Authenticate header", HFILL }},
2142             { &hf_http_proxy_authorization,
2143               { "Proxy-Authorization",  "http.proxy_authorization",
2144                 FT_STRING, BASE_NONE, NULL, 0x0,
2145                 "HTTP Proxy-Authorization header", HFILL }},
2146             { &hf_http_proxy_connect_host,
2147               { "Proxy-Connect-Hostname", "http.proxy_connect_host",
2148                 FT_STRING, BASE_NONE, NULL, 0x0,
2149                 "HTTP Proxy Connect Hostname", HFILL }},
2150             { &hf_http_proxy_connect_port,
2151               { "Proxy-Connect-Port",   "http.proxy_connect_port",
2152                 FT_UINT16, BASE_DEC, NULL, 0x0,
2153                 "HTTP Proxy Connect Port", HFILL }},
2154             { &hf_http_www_authenticate,
2155               { "WWW-Authenticate",     "http.www_authenticate",
2156                 FT_STRING, BASE_NONE, NULL, 0x0,
2157                 "HTTP WWW-Authenticate header", HFILL }},
2158             { &hf_http_content_type,
2159               { "Content-Type", "http.content_type",
2160                 FT_STRING, BASE_NONE, NULL, 0x0,
2161                 "HTTP Content-Type header", HFILL }},
2162             { &hf_http_content_length,
2163               { "Content-Length",       "http.content_length",
2164                 FT_UINT32, BASE_DEC, NULL, 0x0,
2165                 "HTTP Content-Length header", HFILL }},
2166             { &hf_http_content_encoding,
2167               { "Content-Encoding",     "http.content_encoding",
2168                 FT_STRING, BASE_NONE, NULL, 0x0,
2169                 "HTTP Content-Encoding header", HFILL }},
2170             { &hf_http_transfer_encoding,
2171               { "Transfer-Encoding",    "http.transfer_encoding",
2172                 FT_STRING, BASE_NONE, NULL, 0x0,
2173                 "HTTP Transfer-Encoding header", HFILL }},
2174             { &hf_http_user_agent,
2175               { "User-Agent",   "http.user_agent",
2176                 FT_STRING, BASE_NONE, NULL, 0x0,
2177                 "HTTP User-Agent header", HFILL }},
2178             { &hf_http_host,
2179               { "Host", "http.host",
2180                 FT_STRING, BASE_NONE, NULL, 0x0,
2181                 "HTTP Host", HFILL }},
2182             { &hf_http_connection,
2183               { "Connection",   "http.connection",
2184                 FT_STRING, BASE_NONE, NULL, 0x0,
2185                 "HTTP Connection", HFILL }},
2186             { &hf_http_cookie,
2187               { "Cookie",       "http.cookie",
2188                 FT_STRING, BASE_NONE, NULL, 0x0,
2189                 "HTTP Cookie", HFILL }},
2190             { &hf_http_accept,
2191               { "Accept",       "http.accept",
2192                 FT_STRING, BASE_NONE, NULL, 0x0,
2193                 "HTTP Accept", HFILL }},
2194             { &hf_http_referer,
2195               { "Referer",      "http.referer",
2196                 FT_STRING, BASE_NONE, NULL, 0x0,
2197                 "HTTP Referer", HFILL }},
2198             { &hf_http_accept_language,
2199               { "Accept-Language",      "http.accept_language",
2200                 FT_STRING, BASE_NONE, NULL, 0x0,
2201             "HTTP Accept Language", HFILL }},
2202             { &hf_http_accept_encoding,
2203               { "Accept Encoding",      "http.accept_encoding",
2204                 FT_STRING, BASE_NONE, NULL, 0x0,
2205                 "HTTP Accept Encoding", HFILL }},
2206             { &hf_http_date,
2207               { "Date", "http.date",
2208                 FT_STRING, BASE_NONE, NULL, 0x0,
2209                 "HTTP Date", HFILL }},
2210             { &hf_http_cache_control,
2211               { "Cache-Control",        "http.cache_control",
2212                 FT_STRING, BASE_NONE, NULL, 0x0,
2213                 "HTTP Cache Control", HFILL }},
2214             { &hf_http_server,
2215               { "Server",       "http.server",
2216                 FT_STRING, BASE_NONE, NULL, 0x0,
2217                 "HTTP Server", HFILL }},
2218             { &hf_http_location,
2219               { "Location",     "http.location",
2220                 FT_STRING, BASE_NONE, NULL, 0x0,
2221                 "HTTP Location", HFILL }},
2222             { &hf_http_set_cookie,
2223               { "Set-Cookie",   "http.set_cookie",
2224                 FT_STRING, BASE_NONE, NULL, 0x0,
2225                 "HTTP Set Cookie", HFILL }},
2226             { &hf_http_last_modified,
2227               { "Last-Modified",        "http.last_modified",
2228                 FT_STRING, BASE_NONE, NULL, 0x0,
2229                 "HTTP Last Modified", HFILL }},
2230             { &hf_http_x_forwarded_for,
2231               { "X-Forwarded-For",      "http.x_forwarded_for",
2232                 FT_STRING, BASE_NONE, NULL, 0x0,
2233                 "HTTP X-Forwarded-For", HFILL }},
2234         };
2235         static gint *ett[] = {
2236                 &ett_http,
2237                 &ett_http_ntlmssp,
2238                 &ett_http_request,
2239                 &ett_http_chunked_response,
2240                 &ett_http_chunk_data,
2241                 &ett_http_encoded_entity,
2242         };
2243         module_t *http_module;
2244
2245         proto_http = proto_register_protocol("Hypertext Transfer Protocol",
2246             "HTTP", "http");
2247         proto_register_field_array(proto_http, hf, array_length(hf));
2248         proto_register_subtree_array(ett, array_length(ett));
2249         register_dissector("http", dissect_http, proto_http);
2250         http_module = prefs_register_protocol(proto_http, reinit_http);
2251         prefs_register_bool_preference(http_module, "desegment_headers",
2252             "Reassemble HTTP headers spanning multiple TCP segments",
2253             "Whether the HTTP dissector should reassemble headers "
2254             "of a request spanning multiple TCP segments. "
2255                 "To use this option, you must also enable "
2256         "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
2257             &http_desegment_headers);
2258         prefs_register_bool_preference(http_module, "desegment_body",
2259             "Reassemble HTTP bodies spanning multiple TCP segments",
2260             "Whether the HTTP dissector should use the "
2261             "\"Content-length:\" value, if present, to reassemble "
2262             "the body of a request spanning multiple TCP segments, "
2263             "and reassemble chunked data spanning multiple TCP segments. "
2264                 "To use this option, you must also enable "
2265         "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
2266             &http_desegment_body);
2267         prefs_register_bool_preference(http_module, "dechunk_body",
2268             "Reassemble chunked transfer-coded bodies",
2269             "Whether to reassemble bodies of entities that are transfered "
2270             "using the \"Transfer-Encoding: chunked\" method",
2271             &http_dechunk_body);
2272 #ifdef HAVE_LIBZ
2273         prefs_register_bool_preference(http_module, "decompress_body",
2274             "Uncompress entity bodies",
2275             "Whether to uncompress entity bodies that are compressed "
2276             "using \"Content-Encoding: \"",
2277             &http_decompress_body);
2278 #endif
2279         prefs_register_obsolete_preference(http_module, "tcp_alternate_port");
2280
2281         range_convert_str(&global_http_tcp_range, TCP_DEFAULT_RANGE, 65535);
2282         http_tcp_range = range_empty();
2283         prefs_register_range_preference(http_module, "tcp.port", "TCP Ports",
2284                                                                         "TCP Ports range",
2285                                                                         &global_http_tcp_range, 65535);
2286
2287         range_convert_str(&global_http_ssl_range, SSL_DEFAULT_RANGE, 65535);
2288         http_ssl_range = range_empty();
2289         prefs_register_range_preference(http_module, "ssl.port", "SSL/TLS Ports",
2290                                                                         "SSL/TLS Ports range",
2291                                                                         &global_http_ssl_range, 65535);
2292
2293         http_handle = create_dissector_handle(dissect_http, proto_http);
2294
2295         /*
2296          * Dissectors shouldn't register themselves in this table;
2297          * instead, they should call "http_dissector_add()", and
2298          * we'll register the port number they specify as a port
2299          * for HTTP, and register them in our subdissector table.
2300          *
2301          * This only works for protocols such as IPP that run over
2302          * HTTP on a specific non-HTTP port.
2303          */
2304         port_subdissector_table = register_dissector_table("http.port",
2305             "TCP port for protocols using HTTP", FT_UINT16, BASE_DEC);
2306
2307         /*
2308          * Dissectors can register themselves in this table.
2309          * It's just "media_type", not "http.content_type", because
2310          * it's an Internet media type, usable by other protocols as well.
2311          */
2312         media_type_subdissector_table =
2313             register_dissector_table("media_type",
2314                 "Internet media type", FT_STRING, BASE_NONE);
2315
2316         /*
2317          * Heuristic dissectors SHOULD register themselves in
2318          * this table using the standard heur_dissector_add()
2319          * function.
2320          */
2321         register_heur_dissector_list("http", &heur_subdissector_list);
2322
2323         /*
2324          * Register for tapping
2325          */
2326         http_tap = register_tap("http"); /* HTTP statistics tap */
2327         http_eo_tap = register_tap("http_eo"); /* HTTP Export Object tap */
2328 }
2329
2330 /*
2331  * Called by dissectors for protocols that run atop HTTP/TCP.
2332  */
2333 void
2334 http_dissector_add(guint32 port, dissector_handle_t handle)
2335 {
2336         /*
2337          * Register ourselves as the handler for that port number
2338          * over TCP.
2339          */
2340         dissector_add("tcp.port", port, http_handle);
2341
2342         /*
2343          * And register them in *our* table for that port.
2344          */
2345         dissector_add("http.port", port, handle);
2346 }
2347
2348 void
2349 proto_reg_handoff_http(void)
2350 {
2351         dissector_handle_t http_udp_handle;
2352
2353         data_handle = find_dissector("data");
2354         media_handle = find_dissector("media");
2355
2356         /*
2357          * XXX - is there anything to dissect in the body of an SSDP
2358          * request or reply?  I.e., should there be an SSDP dissector?
2359          */
2360         http_udp_handle = create_dissector_handle(dissect_http_udp, proto_http);
2361         dissector_add("udp.port", UDP_PORT_SSDP, http_udp_handle);
2362
2363         ntlmssp_handle = find_dissector("ntlmssp");
2364         gssapi_handle = find_dissector("gssapi");
2365
2366         stats_tree_register("http","http","HTTP/Packet Counter", http_stats_tree_packet, http_stats_tree_init, NULL );
2367         stats_tree_register("http","http_req","HTTP/Requests", http_req_stats_tree_packet, http_req_stats_tree_init, NULL );
2368         stats_tree_register("http","http_srv","HTTP/Load Distribution",http_reqs_stats_tree_packet,http_reqs_stats_tree_init, NULL );
2369
2370 }
2371
2372 /*
2373  * Content-Type: message/http
2374  */
2375
2376 static gint proto_message_http = -1;
2377 static gint ett_message_http = -1;
2378 static dissector_handle_t message_http_handle;
2379
2380 static void
2381 dissect_message_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2382 {
2383         proto_tree      *subtree;
2384         proto_item      *ti;
2385         gint            offset = 0, next_offset;
2386         gint            len;
2387
2388         if (check_col(pinfo->cinfo, COL_INFO))
2389                 col_append_str(pinfo->cinfo, COL_INFO, " (message/http)");
2390         if (tree) {
2391                 ti = proto_tree_add_item(tree, proto_message_http,
2392                                 tvb, 0, -1, FALSE);
2393                 subtree = proto_item_add_subtree(ti, ett_message_http);
2394                 while (tvb_reported_length_remaining(tvb, offset) != 0) {
2395                         len = tvb_find_line_end(tvb, offset,
2396                                         tvb_ensure_length_remaining(tvb, offset),
2397                                         &next_offset, FALSE);
2398                         if (len == -1)
2399                                 break;
2400                         proto_tree_add_text(subtree, tvb, offset, next_offset - offset,
2401                                         "%s", tvb_format_text(tvb, offset, len));
2402                         offset = next_offset;
2403                 }
2404         }
2405 }
2406
2407 void
2408 proto_register_message_http(void)
2409 {
2410         static gint *ett[] = {
2411                 &ett_message_http,
2412         };
2413
2414         proto_message_http = proto_register_protocol(
2415                         "Media Type: message/http",
2416                         "message/http",
2417                         "message-http"
2418         );
2419         proto_register_subtree_array(ett, array_length(ett));
2420 }
2421
2422 void
2423 proto_reg_handoff_message_http(void)
2424 {
2425         message_http_handle = create_dissector_handle(dissect_message_http,
2426                         proto_message_http);
2427
2428         dissector_add_string("media_type", "message/http", message_http_handle);
2429
2430         reinit_http();
2431 }