2 * Routines for HTTP packet disassembly
4 * Guy Harris <guy@alum.mit.edu>
6 * $Id: packet-http.c,v 1.19 2000/05/31 05:07:06 guy Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@zing.org>
10 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
43 #include "packet-ipp.h"
45 typedef enum _http_type {
51 static int proto_http = -1;
52 static int hf_http_response = -1;
53 static int hf_http_request = -1;
55 static gint ett_http = -1;
57 #define TCP_PORT_HTTP 80
58 #define TCP_PORT_PROXY_HTTP 3128
59 #define TCP_PORT_PROXY_ADMIN_HTTP 3132
60 #define TCP_ALT_PORT_HTTP 8080
62 static int is_http_request_or_reply(const u_char *data, int linelen, http_type_t *type);
64 void dissect_http(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
66 gboolean is_ipp = (pi.srcport == 631 || pi.destport == 631);
68 proto_tree *http_tree;
69 const u_char *data, *dataend;
70 const u_char *linep, *lineend, *eol;
73 http_type_t http_type = HTTP_OTHERS;
76 dataend = data + END_OF_FRAME;
78 if (check_col(fd, COL_PROTOCOL))
79 col_add_str(fd, COL_PROTOCOL, is_ipp ? "IPP" : "HTTP");
80 if (check_col(fd, COL_INFO)) {
82 * Put the first line from the buffer into the summary,
83 * if it's an HTTP request or reply.
84 * Otherwise, just call it a continuation.
86 lineend = find_line_end(data, dataend, &eol);
87 linelen = lineend - data;
88 if (is_http_request_or_reply(data, linelen, &http_type))
89 col_add_str(fd, COL_INFO, format_text(data, linelen));
91 col_add_str(fd, COL_INFO, "Continuation");
95 ti = proto_tree_add_item(tree, proto_http, NullTVB, offset, END_OF_FRAME, FALSE);
96 http_tree = proto_item_add_subtree(ti, ett_http);
98 while (data < dataend) {
100 * Find the end of the line.
102 lineend = find_line_end(data, dataend, &eol);
103 linelen = lineend - data;
106 * OK, does it look like an HTTP request or
109 if (is_http_request_or_reply(data, linelen, &http_type))
113 * No. Does it look like a blank line (as would
114 * appear at the end of an HTTP request)?
121 if (strncmp(data, "\r\n", 2) == 0 ||
122 strncmp(data, "\n\r", 2) == 0)
127 * No. Does it look like a MIME header?
130 while (linep < lineend) {
133 break; /* not printable, not a MIME header */
153 * It's a tspecial, so it's not
154 * part of a token, so it's not
155 * a field name for the beginning
162 * This ends the token; we consider
163 * this to be a MIME header.
171 * We don't consider this part of an HTTP request or
172 * reply, so we don't display it.
173 * (Yeah, that means we don't display, say, a
174 * text/http page, but you can get that from the
183 proto_tree_add_text(http_tree, NullTVB, offset, linelen, "%s",
184 format_text(data, linelen));
192 proto_tree_add_boolean_hidden(http_tree,
193 hf_http_response, NullTVB, 0, 0, 1);
197 proto_tree_add_boolean_hidden(http_tree,
198 hf_http_request, NullTVB, 0, 0, 1);
206 if (data < dataend) {
208 dissect_ipp(pd, offset, fd, tree);
210 dissect_data(&pd[offset], offset, fd, http_tree);
216 * XXX - this won't handle HTTP 0.9 replies, but they're all data
220 is_http_request_or_reply(const u_char *data, int linelen, http_type_t *type)
223 if (strncasecmp(data, "GET", 3) == 0 ||
224 strncasecmp(data, "PUT", 3) == 0) {
225 if (*type == HTTP_OTHERS)
226 *type = HTTP_REQUEST;
231 if (strncasecmp(data, "HEAD", 4) == 0 ||
232 strncasecmp(data, "POST", 4) == 0) {
233 if (*type == HTTP_OTHERS)
234 *type = HTTP_REQUEST;
239 if (strncasecmp(data, "TRACE", 5) == 0)
241 if (strncasecmp(data, "HTTP/", 5) == 0) {
242 if (*type == HTTP_OTHERS)
243 *type = HTTP_RESPONSE;
244 return TRUE; /* response */
248 if (strncasecmp(data, "DELETE", 6) == 0) {
249 if (*type == HTTP_OTHERS)
250 *type = HTTP_REQUEST;
255 if (strncasecmp(data, "OPTIONS", 7) == 0) {
256 if (*type == HTTP_OTHERS)
257 *type = HTTP_REQUEST;
262 if (strncasecmp(data, "CONNECT", 7) == 0) {
263 if (*type == HTTP_OTHERS)
264 *type = HTTP_REQUEST;
272 proto_register_http(void)
275 static hf_register_info hf[] = {
277 { "Response", "http.response",
278 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
279 "TRUE if HTTP response" }},
281 { "Request", "http.request",
282 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
283 "TRUE if HTTP request" }},
285 static gint *ett[] = {
289 proto_http = proto_register_protocol("Hypertext Transfer Protocol", "http");
290 proto_register_field_array(proto_http, hf, array_length(hf));
291 proto_register_subtree_array(ett, array_length(ett));
295 proto_reg_handoff_http(void)
297 dissector_add("tcp.port", TCP_PORT_HTTP, dissect_http);
298 dissector_add("tcp.port", TCP_ALT_PORT_HTTP, dissect_http);
299 dissector_add("tcp.port", TCP_PORT_PROXY_HTTP, dissect_http);
300 dissector_add("tcp.port", TCP_PORT_PROXY_ADMIN_HTTP, dissect_http);