1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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.2 2007-03-15 19:22:13 andy 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
->data
->reqdata
.proto
.http
->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
= &data
->reqdata
.proto
.http
->chunk
;
112 struct Curl_transfer_keeper
*k
= &data
->reqdata
.keep
;
114 size_t length
= (size_t)datalen
;
115 size_t *wrote
= (size_t *)wrotep
;
117 *wrote
= 0; /* nothing's written yet */
122 /* Check for an ASCII hex digit.
123 We avoid the use of isxdigit to accommodate non-ASCII hosts. */
124 if((*datap
>= 0x30 && *datap
<= 0x39) /* 0-9 */
125 || (*datap
>= 0x41 && *datap
<= 0x46) /* A-F */
126 || (*datap
>= 0x61 && *datap
<= 0x66)) { /* a-f */
127 if(ch
->hexindex
< MAXNUM_SIZE
) {
128 ch
->hexbuffer
[ch
->hexindex
] = *datap
;
134 return CHUNKE_TOO_LONG_HEX
; /* longer hex than we support */
138 if(0 == ch
->hexindex
) {
139 /* This is illegal data, we received junk where we expected
140 a hexadecimal digit. */
141 return CHUNKE_ILLEGAL_HEX
;
143 /* length and datap are unmodified */
144 ch
->hexbuffer
[ch
->hexindex
]=0;
145 #ifdef CURL_DOES_CONVERSIONS
146 /* convert to host encoding before calling strtoul */
147 result
= Curl_convert_from_network(conn
->data
,
150 if(result
!= CURLE_OK
) {
151 /* Curl_convert_from_network calls failf if unsuccessful */
152 /* Treat it as a bad hex character */
153 return(CHUNKE_ILLEGAL_HEX
);
155 #endif /* CURL_DOES_CONVERSIONS */
156 ch
->datasize
=strtoul(ch
->hexbuffer
, NULL
, 16);
157 ch
->state
= CHUNK_POSTHEX
;
162 /* In this state, we're waiting for CRLF to arrive. We support
163 this to allow so called chunk-extensions to show up here
164 before the CRLF comes. */
166 ch
->state
= CHUNK_CR
;
172 /* waiting for the LF */
174 /* we're now expecting data to come, unless size was zero! */
175 if(0 == ch
->datasize
) {
176 if (conn
->bits
.trailerHdrPresent
!=TRUE
) {
177 /* No Trailer: header found - revert to original Curl processing */
178 ch
->state
= CHUNK_STOP
;
180 /* This is the final byte, return right now */
185 ch
->state
= CHUNK_TRAILER
; /* attempt to read trailers */
190 ch
->state
= CHUNK_DATA
;
193 /* previously we got a fake CR, go back to CR waiting! */
194 ch
->state
= CHUNK_CR
;
200 /* we get pure and fine data
202 We expect another 'datasize' of data. We have 'length' right now,
203 it can be more or less than 'datasize'. Get the smallest piece.
205 piece
= (ch
->datasize
>= length
)?length
:ch
->datasize
;
207 /* Write the data portion available */
209 switch (data
->reqdata
.keep
.content_encoding
) {
213 result
= Curl_client_write(conn
, CLIENTWRITE_BODY
, datap
,
219 /* update data->reqdata.keep.str to point to the chunk data. */
220 data
->reqdata
.keep
.str
= datap
;
221 result
= Curl_unencode_deflate_write(conn
, &data
->reqdata
.keep
,
226 /* update data->reqdata.keep.str to point to the chunk data. */
227 data
->reqdata
.keep
.str
= datap
;
228 result
= Curl_unencode_gzip_write(conn
, &data
->reqdata
.keep
,
235 "Unrecognized content encoding type. "
236 "libcurl understands `identity', `deflate' and `gzip' "
237 "content encodings.");
238 return CHUNKE_BAD_ENCODING
;
243 return CHUNKE_WRITE_ERROR
;
247 ch
->datasize
-= piece
; /* decrease amount left to expect */
248 datap
+= piece
; /* move read pointer forward */
249 length
-= piece
; /* decrease space left in this round */
251 if(0 == ch
->datasize
)
252 /* end of data this round, we now expect a trailing CRLF */
253 ch
->state
= CHUNK_POSTCR
;
258 ch
->state
= CHUNK_POSTLF
;
263 return CHUNKE_BAD_CHUNK
;
269 * The last one before we go back to hex state and start all
272 Curl_httpchunk_init(conn
);
277 return CHUNKE_BAD_CHUNK
;
281 /* conn->trailer is assumed to be freed in url.c on a
283 if (conn
->trlPos
>= conn
->trlMax
) {
287 ptr
= (char*)realloc(conn
->trailer
,conn
->trlMax
);
291 ptr
= (char*)malloc(conn
->trlMax
);
294 return CHUNKE_OUT_OF_MEMORY
;
297 conn
->trailer
[conn
->trlPos
++]=*datap
;
300 ch
->state
= CHUNK_TRAILER_CR
;
307 case CHUNK_TRAILER_CR
:
309 ch
->state
= CHUNK_TRAILER_POSTCR
;
314 return CHUNKE_BAD_CHUNK
;
317 case CHUNK_TRAILER_POSTCR
:
318 if (*datap
== 0x0a) {
319 conn
->trailer
[conn
->trlPos
++]=0x0a;
320 conn
->trailer
[conn
->trlPos
]=0;
321 if (conn
->trlPos
==2) {
322 ch
->state
= CHUNK_STOP
;
326 #ifdef CURL_DOES_CONVERSIONS
327 /* Convert to host encoding before calling Curl_client_write */
328 result
= Curl_convert_from_network(conn
->data
,
331 if(result
!= CURLE_OK
) {
332 /* Curl_convert_from_network calls failf if unsuccessful */
333 /* Treat it as a bad chunk */
334 return(CHUNKE_BAD_CHUNK
);
336 #endif /* CURL_DOES_CONVERSIONS */
337 Curl_client_write(conn
, CLIENTWRITE_HEADER
,
338 conn
->trailer
, conn
->trlPos
);
340 ch
->state
= CHUNK_TRAILER
;
346 return CHUNKE_BAD_CHUNK
;
350 /* If we arrive here, there is data left in the end of the buffer
351 even if there's no more chunks to read */
352 ch
->dataleft
= length
;
353 return CHUNKE_STOP
; /* return stop */
355 return CHUNKE_STATE_ERROR
;
360 #endif /* CURL_DISABLE_HTTP */