2 * Routines handling protocols with a request/response line, headers,
3 * a blank line, and an optional body.
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <epan/packet.h>
32 #include <epan/strutil.h>
35 #include <epan/req_resp_hdrs.h>
38 * Optionally do reassembly of the request/response line, headers, and body.
41 req_resp_hdrs_do_reassembly(tvbuff_t *tvb, int offset, packet_info *pinfo,
42 gboolean desegment_headers, gboolean desegment_body)
46 gint length_remaining, reported_length_remaining;
49 long int content_length;
50 gboolean content_length_found = FALSE;
51 gboolean content_type_found = FALSE;
52 gboolean chunked_encoding = FALSE;
53 gboolean keepalive_found = FALSE;
56 * Do header desegmentation if we've been told to.
58 * RFC 2616 defines HTTP messages as being either of the
59 * Request or the Response type
60 * (HTTP-message = Request | Response).
61 * Request and Response are defined as:
62 * Request = Request-Line
65 * | entity-header ) CRLF)
68 * Response = Status-Line
71 * | entity-header ) CRLF)
74 * that's why we can always assume two consecutive line
75 * endings (we allow CR, LF, or CRLF, as some clients
76 * or servers might not use a full CRLF) to mark the end
77 * of the headers. The worst thing that would happen
78 * otherwise would be the packet not being desegmented
79 * or being interpreted as only headers.
81 * RFC 2326 says RTSP works the same way; RFC 3261 says SIP
86 * If header desegmentation is activated, check that all
87 * headers are in this tvbuff (search for an empty line
88 * marking end of headers) or request one more byte (we
89 * don't know how many bytes we'll need, so we just ask
92 if (desegment_headers && pinfo->can_desegment) {
95 next_offset_sav = next_offset;
97 length_remaining = tvb_length_remaining(tvb,
99 reported_length_remaining =
100 tvb_reported_length_remaining(tvb, next_offset);
103 * Request one more byte if there're no
104 * bytes left in the reported data (if there're
105 * bytes left in the reported data, but not in
106 * the available data, requesting more bytes
107 * won't help, as those bytes weren't captured).
109 if (reported_length_remaining < 1) {
110 pinfo->desegment_offset = offset;
111 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
116 * Request one more byte if we cannot find a
117 * header (i.e. a line end).
119 linelen = tvb_find_line_end(tvb, next_offset,
120 -1, &next_offset, TRUE);
122 length_remaining >= reported_length_remaining) {
124 * Not enough data; ask for one more
127 pinfo->desegment_offset = offset;
128 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
130 } else if (linelen == 0) {
132 * We found the end of the headers.
138 * Is this a Content-Length or Transfer-Encoding
139 * header? If not, it either means that we are in
140 * a different header line, or that we are
141 * at the end of the headers, or that there
142 * isn't enough data; the two latter cases
143 * have already been handled above.
145 if (desegment_body) {
147 * Check if we've found Content-Length.
149 if (tvb_strncaseeql(tvb, next_offset_sav,
150 "Content-Length:", 15) == 0) {
151 header_val = tvb_get_ephemeral_string(tvb,
152 next_offset_sav + 15,
154 if (sscanf(header_val,
155 "%li", &content_length)
157 content_length_found = TRUE;
158 } else if (tvb_strncaseeql(tvb, next_offset_sav,
159 "Content-Type:", 13) == 0) {
160 content_type_found = TRUE;
161 } else if (tvb_strncaseeql(tvb, next_offset_sav,
162 "Connection:", 11) == 0) {
163 /* Check for keep-alive */
164 header_val = tvb_get_ephemeral_string(tvb,
165 next_offset_sav + 11,
168 while(*header_val==' '){
171 if(!strncasecmp(header_val, "Keep-Alive", 10)){
172 keepalive_found = TRUE;
175 } else if (tvb_strncaseeql(tvb,
177 "Transfer-Encoding:", 18) == 0) {
179 * Find out if this Transfer-Encoding is
180 * chunked. It should be, since there
181 * really aren't any other types, but
182 * RFC 2616 allows for them.
187 header_val = tvb_get_ephemeral_string(tvb,
188 next_offset_sav + 18, linelen - 18);
190 len = strlen(header_val);
191 /* Skip white space */
192 while (p < header_val + len &&
193 (*p == ' ' || *p == '\t'))
195 if (p <= header_val + len) {
196 if (strncasecmp(p, "chunked", 7)
199 * Don't bother looking
206 chunked_encoding = TRUE;
215 * The above loop ends when we reached the end of the headers, so
216 * there should be content_length bytes after the 4 terminating bytes
217 * and next_offset points to after the end of the headers.
219 if (desegment_body) {
220 if (content_length_found) {
221 /* next_offset has been set to the end of the headers */
222 if (!tvb_bytes_exist(tvb, next_offset, content_length)) {
223 length_remaining = tvb_length_remaining(tvb,
225 reported_length_remaining =
226 tvb_reported_length_remaining(tvb, next_offset);
227 if (length_remaining < reported_length_remaining) {
229 * It's a waste of time asking for more
230 * data, because that data wasn't captured.
234 if (length_remaining == -1)
235 length_remaining = 0;
236 pinfo->desegment_offset = offset;
237 pinfo->desegment_len =
238 content_length - length_remaining;
241 } else if (chunked_encoding) {
243 * This data is chunked, so we need to keep pulling
244 * data until we reach the end of the stream, or a
248 * This doesn't bother with trailing headers; I don't
249 * think they are really used, and we'd have to use
250 * is_http_request_or_reply() to determine if it was
251 * a trailing header, or the start of a new response.
253 gboolean done_chunking = FALSE;
255 while (!done_chunking) {
257 gint chunk_offset = 0;
258 gchar *chunk_string = NULL;
261 length_remaining = tvb_length_remaining(tvb,
263 reported_length_remaining =
264 tvb_reported_length_remaining(tvb,
267 if (reported_length_remaining < 1) {
268 pinfo->desegment_offset = offset;
269 pinfo->desegment_len = 1;
273 linelen = tvb_find_line_end(tvb, next_offset,
274 -1, &chunk_offset, TRUE);
278 reported_length_remaining) {
279 pinfo->desegment_offset = offset;
280 pinfo->desegment_len = 2;
284 /* We have a line with the chunk size in it.*/
285 chunk_string = tvb_get_ephemeral_string(tvb, next_offset,
290 * We don't care about the extensions.
292 if ((c = strchr(c, ';'))) {
296 if ((sscanf(chunk_string, "%x",
297 &chunk_size) < 0) || chunk_size < 0) {
298 /* We couldn't get the chunk size,
304 if (chunk_size == 0) {
306 * This is the last chunk. Let's pull in the
309 linelen = tvb_find_line_end(tvb,
310 chunk_offset, -1, &chunk_offset, TRUE);
314 reported_length_remaining) {
315 pinfo->desegment_offset = offset;
316 pinfo->desegment_len = 1;
320 pinfo->desegment_offset = chunk_offset;
321 pinfo->desegment_len = 0;
322 done_chunking = TRUE;
325 * Skip to the next chunk if we
328 if (reported_length_remaining >
331 next_offset = chunk_offset
335 * Fetch this chunk, plus the
338 pinfo->desegment_offset = offset;
339 pinfo->desegment_len =
341 reported_length_remaining;
347 } else if (content_type_found && pinfo->can_desegment) {
348 /* We found a content-type but no content-length.
349 * This is probably a HTTP header for a session with
350 * only one HTTP PDU and where the content spans
351 * until the end of the tcp session, unless there
352 * is a keepalive header present in which case we
353 * assume there is no message body at all and thus
354 * we wont do any reassembly.
355 * Set up tcp reassembly until the end of this session.
357 length_remaining = tvb_length_remaining(tvb, next_offset);
358 reported_length_remaining = tvb_reported_length_remaining(tvb, next_offset);
359 if (length_remaining < reported_length_remaining) {
361 * It's a waste of time asking for more
362 * data, because that data wasn't captured.
367 if (keepalive_found) {
368 /* We have a keep-alive but no content-length.
369 * Assume there is no message body and dont
375 pinfo->desegment_offset = offset;
376 pinfo->desegment_len = DESEGMENT_UNTIL_FIN;
384 * No further desegmentation needed.