1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: http_chunks.c,v 1.43 2008-01-31 12:04:33 bagder Exp $
22 ***************************************************************************/
25 #ifndef CURL_DISABLE_HTTP
26 /* -- WIN32 approved -- */
33 #include "urldata.h" /* it includes http_chunks.h */
34 #include "sendf.h" /* for the client write stuff */
36 #include "content_encoding.h"
39 #include "easyif.h" /* for Curl_convert_to_network prototype */
41 #define _MPRINTF_REPLACE /* use our functions only */
42 #include <curl/mprintf.h>
44 /* The last #include file should be: */
48 * Chunk format (simplified):
50 * <HEX SIZE>[ chunk extension ] CRLF
53 * Highlights from RFC2616 section 3.6 say:
55 The chunked encoding modifies the body of a message in order to
56 transfer it as a series of chunks, each with its own size indicator,
57 followed by an OPTIONAL trailer containing entity-header fields. This
58 allows dynamically produced content to be transferred along with the
59 information necessary for the recipient to verify that it has
60 received the full message.
67 chunk = chunk-size [ chunk-extension ] CRLF
70 last-chunk = 1*("0") [ chunk-extension ] CRLF
72 chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
73 chunk-ext-name = token
74 chunk-ext-val = token | quoted-string
75 chunk-data = chunk-size(OCTET)
76 trailer = *(entity-header CRLF)
78 The chunk-size field is a string of hex digits indicating the size of
79 the chunk. The chunked encoding is ended by any chunk whose size is
80 zero, followed by the trailer, which is terminated by an empty line.
85 void Curl_httpchunk_init(struct connectdata
*conn
)
87 struct Curl_chunker
*chunk
= &conn
->chunk
;
88 chunk
->hexindex
=0; /* start at 0 */
89 chunk
->dataleft
=0; /* no data left yet! */
90 chunk
->state
= CHUNK_HEX
; /* we get hex first! */
94 * chunk_read() returns a OK for normal operations, or a positive return code
95 * for errors. STOP means this sequence of chunks is complete. The 'wrote'
96 * argument is set to tell the caller how many bytes we actually passed to the
97 * client (for byte-counting and whatever).
99 * The states and the state-machine is further explained in the header file.
101 * This function always uses ASCII hex values to accommodate non-ASCII hosts.
102 * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
104 CHUNKcode
Curl_httpchunk_read(struct connectdata
*conn
,
109 CURLcode result
=CURLE_OK
;
110 struct SessionHandle
*data
= conn
->data
;
111 struct Curl_chunker
*ch
= &conn
->chunk
;
112 struct SingleRequest
*k
= &data
->req
;
114 size_t length
= (size_t)datalen
;
115 size_t *wrote
= (size_t *)wrotep
;
117 *wrote
= 0; /* nothing's written yet */
119 /* the original data is written to the client, but we go on with the
120 chunk read process, to properly calculate the content length*/
121 if(data
->set
.http_te_skip
&& !k
->ignorebody
) {
122 result
= Curl_client_write(conn
, CLIENTWRITE_BODY
, datap
, datalen
);
124 return CHUNKE_WRITE_ERROR
;
130 /* Check for an ASCII hex digit.
131 We avoid the use of isxdigit to accommodate non-ASCII hosts. */
132 if((*datap
>= 0x30 && *datap
<= 0x39) /* 0-9 */
133 || (*datap
>= 0x41 && *datap
<= 0x46) /* A-F */
134 || (*datap
>= 0x61 && *datap
<= 0x66)) { /* a-f */
135 if(ch
->hexindex
< MAXNUM_SIZE
) {
136 ch
->hexbuffer
[ch
->hexindex
] = *datap
;
142 return CHUNKE_TOO_LONG_HEX
; /* longer hex than we support */
146 if(0 == ch
->hexindex
) {
147 /* This is illegal data, we received junk where we expected
148 a hexadecimal digit. */
149 return CHUNKE_ILLEGAL_HEX
;
151 /* length and datap are unmodified */
152 ch
->hexbuffer
[ch
->hexindex
]=0;
153 #ifdef CURL_DOES_CONVERSIONS
154 /* convert to host encoding before calling strtoul */
155 result
= Curl_convert_from_network(conn
->data
,
158 if(result
!= CURLE_OK
) {
159 /* Curl_convert_from_network calls failf if unsuccessful */
160 /* Treat it as a bad hex character */
161 return(CHUNKE_ILLEGAL_HEX
);
163 #endif /* CURL_DOES_CONVERSIONS */
164 ch
->datasize
=strtoul(ch
->hexbuffer
, NULL
, 16);
165 ch
->state
= CHUNK_POSTHEX
;
170 /* In this state, we're waiting for CRLF to arrive. We support
171 this to allow so called chunk-extensions to show up here
172 before the CRLF comes. */
174 ch
->state
= CHUNK_CR
;
180 /* waiting for the LF */
182 /* we're now expecting data to come, unless size was zero! */
183 if(0 == ch
->datasize
) {
184 if(k
->trailerhdrpresent
!=TRUE
) {
185 /* No Trailer: header found - revert to original Curl processing */
186 ch
->state
= CHUNK_STOPCR
;
188 /* We need to increment the datap here since we bypass the
189 increment below with the immediate break */
193 /* This is the final byte, continue to read the final CRLF */
197 ch
->state
= CHUNK_TRAILER
; /* attempt to read trailers */
202 ch
->state
= CHUNK_DATA
;
206 /* previously we got a fake CR, go back to CR waiting! */
207 ch
->state
= CHUNK_CR
;
213 /* we get pure and fine data
215 We expect another 'datasize' of data. We have 'length' right now,
216 it can be more or less than 'datasize'. Get the smallest piece.
218 piece
= (ch
->datasize
>= length
)?length
:ch
->datasize
;
220 /* Write the data portion available */
222 switch (conn
->data
->set
.http_ce_skip
?
223 IDENTITY
: data
->req
.content_encoding
) {
227 if( !data
->set
.http_te_skip
)
228 result
= Curl_client_write(conn
, CLIENTWRITE_BODY
, datap
,
237 /* update data->req.keep.str to point to the chunk data. */
238 data
->req
.str
= datap
;
239 result
= Curl_unencode_deflate_write(conn
, &data
->req
,
244 /* update data->req.keep.str to point to the chunk data. */
245 data
->req
.str
= datap
;
246 result
= Curl_unencode_gzip_write(conn
, &data
->req
,
253 "Unrecognized content encoding type. "
254 "libcurl understands `identity', `deflate' and `gzip' "
255 "content encodings.");
256 return CHUNKE_BAD_ENCODING
;
261 return CHUNKE_WRITE_ERROR
;
265 ch
->datasize
-= piece
; /* decrease amount left to expect */
266 datap
+= piece
; /* move read pointer forward */
267 length
-= piece
; /* decrease space left in this round */
269 if(0 == ch
->datasize
)
270 /* end of data this round, we now expect a trailing CRLF */
271 ch
->state
= CHUNK_POSTCR
;
276 ch
->state
= CHUNK_POSTLF
;
281 return CHUNKE_BAD_CHUNK
;
288 * The last one before we go back to hex state and start all
291 Curl_httpchunk_init(conn
);
296 return CHUNKE_BAD_CHUNK
;
302 /* conn->trailer is assumed to be freed in url.c on a
304 if(conn
->trlPos
>= conn
->trlMax
) {
308 ptr
= (char*)realloc(conn
->trailer
,conn
->trlMax
);
312 ptr
= (char*)malloc(conn
->trlMax
);
315 return CHUNKE_OUT_OF_MEMORY
;
318 conn
->trailer
[conn
->trlPos
++]=*datap
;
321 ch
->state
= CHUNK_TRAILER_CR
;
328 case CHUNK_TRAILER_CR
:
330 ch
->state
= CHUNK_TRAILER_POSTCR
;
335 return CHUNKE_BAD_CHUNK
;
338 case CHUNK_TRAILER_POSTCR
:
340 conn
->trailer
[conn
->trlPos
++]=0x0a;
341 conn
->trailer
[conn
->trlPos
]=0;
342 if(conn
->trlPos
==2) {
343 ch
->state
= CHUNK_STOP
;
348 * Note that this case skips over the final STOP states since we've
349 * already read the final CRLF and need to return
352 ch
->dataleft
= length
;
354 return CHUNKE_STOP
; /* return stop */
357 #ifdef CURL_DOES_CONVERSIONS
358 /* Convert to host encoding before calling Curl_client_write */
359 result
= Curl_convert_from_network(conn
->data
,
362 if(result
!= CURLE_OK
) {
363 /* Curl_convert_from_network calls failf if unsuccessful */
364 /* Treat it as a bad chunk */
365 return(CHUNKE_BAD_CHUNK
);
367 #endif /* CURL_DOES_CONVERSIONS */
368 if(!data
->set
.http_te_skip
) {
369 result
= Curl_client_write(conn
, CLIENTWRITE_HEADER
,
370 conn
->trailer
, conn
->trlPos
);
372 return CHUNKE_WRITE_ERROR
;
375 ch
->state
= CHUNK_TRAILER
;
381 return CHUNKE_BAD_CHUNK
;
385 /* Read the final CRLF that ends all chunk bodies */
388 ch
->state
= CHUNK_STOP
;
393 return CHUNKE_BAD_CHUNK
;
402 /* Record the length of any data left in the end of the buffer
403 even if there's no more chunks to read */
405 ch
->dataleft
= length
;
406 return CHUNKE_STOP
; /* return stop */
409 return CHUNKE_BAD_CHUNK
;
413 return CHUNKE_STATE_ERROR
;
418 #endif /* CURL_DISABLE_HTTP */