1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
24 #ifndef CURL_DISABLE_HTTP
25 /* -- WIN32 approved -- */
32 #include "urldata.h" /* it includes http_chunks.h */
33 #include "sendf.h" /* for the client write stuff */
35 #include "content_encoding.h"
37 #include "curl_memory.h"
38 #include "non-ascii.h" /* for Curl_convert_to_network prototype */
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
43 /* The last #include file should be: */
47 * Chunk format (simplified):
49 * <HEX SIZE>[ chunk extension ] CRLF
52 * Highlights from RFC2616 section 3.6 say:
54 The chunked encoding modifies the body of a message in order to
55 transfer it as a series of chunks, each with its own size indicator,
56 followed by an OPTIONAL trailer containing entity-header fields. This
57 allows dynamically produced content to be transferred along with the
58 information necessary for the recipient to verify that it has
59 received the full message.
66 chunk = chunk-size [ chunk-extension ] CRLF
69 last-chunk = 1*("0") [ chunk-extension ] CRLF
71 chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
72 chunk-ext-name = token
73 chunk-ext-val = token | quoted-string
74 chunk-data = chunk-size(OCTET)
75 trailer = *(entity-header CRLF)
77 The chunk-size field is a string of hex digits indicating the size of
78 the chunk. The chunked encoding is ended by any chunk whose size is
79 zero, followed by the trailer, which is terminated by an empty line.
83 /* Check for an ASCII hex digit.
84 We avoid the use of isxdigit to accommodate non-ASCII hosts. */
85 static bool Curl_isxdigit(char digit)
87 return (bool)( (digit >= 0x30 && digit <= 0x39) /* 0-9 */
88 || (digit >= 0x41 && digit <= 0x46) /* A-F */
89 || (digit >= 0x61 && digit <= 0x66) ); /* a-f */
92 void Curl_httpchunk_init(struct connectdata *conn)
94 struct Curl_chunker *chunk = &conn->chunk;
95 chunk->hexindex=0; /* start at 0 */
96 chunk->dataleft=0; /* no data left yet! */
97 chunk->state = CHUNK_HEX; /* we get hex first! */
101 * chunk_read() returns a OK for normal operations, or a positive return code
102 * for errors. STOP means this sequence of chunks is complete. The 'wrote'
103 * argument is set to tell the caller how many bytes we actually passed to the
104 * client (for byte-counting and whatever).
106 * The states and the state-machine is further explained in the header file.
108 * This function always uses ASCII hex values to accommodate non-ASCII hosts.
109 * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
111 CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
116 CURLcode result=CURLE_OK;
117 struct SessionHandle *data = conn->data;
118 struct Curl_chunker *ch = &conn->chunk;
119 struct SingleRequest *k = &data->req;
121 size_t length = (size_t)datalen;
122 size_t *wrote = (size_t *)wrotep;
124 *wrote = 0; /* nothing's written yet */
126 /* the original data is written to the client, but we go on with the
127 chunk read process, to properly calculate the content length*/
128 if(data->set.http_te_skip && !k->ignorebody) {
129 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
131 return CHUNKE_WRITE_ERROR;
137 if(Curl_isxdigit(*datap)) {
138 if(ch->hexindex < MAXNUM_SIZE) {
139 ch->hexbuffer[ch->hexindex] = *datap;
145 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
149 if(0 == ch->hexindex) {
150 /* This is illegal data, we received junk where we expected
151 a hexadecimal digit. */
152 return CHUNKE_ILLEGAL_HEX;
154 /* length and datap are unmodified */
155 ch->hexbuffer[ch->hexindex]=0;
157 /* convert to host encoding before calling strtoul */
158 result = Curl_convert_from_network(conn->data, ch->hexbuffer,
161 /* Curl_convert_from_network calls failf if unsuccessful */
162 /* Treat it as a bad hex character */
163 return(CHUNKE_ILLEGAL_HEX);
166 ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
167 ch->state = CHUNK_POSTHEX;
172 /* In this state, we're waiting for CRLF to arrive. We support
173 this to allow so called chunk-extensions to show up here
174 before the CRLF comes. */
176 ch->state = CHUNK_CR;
182 /* waiting for the LF */
184 /* we're now expecting data to come, unless size was zero! */
185 if(0 == ch->datasize) {
186 ch->state = CHUNK_TRAILER; /* now check for trailers */
190 ch->state = CHUNK_DATA;
194 /* previously we got a fake CR, go back to CR waiting! */
195 ch->state = CHUNK_CR;
201 /* we get pure and fine data
203 We expect another 'datasize' of data. We have 'length' right now,
204 it can be more or less than 'datasize'. Get the smallest piece.
206 piece = (ch->datasize >= length)?length:ch->datasize;
208 /* Write the data portion available */
210 switch (conn->data->set.http_ce_skip?
211 IDENTITY : data->req.auto_decoding) {
215 if( !data->set.http_te_skip )
216 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
225 /* update data->req.keep.str to point to the chunk data. */
226 data->req.str = datap;
227 result = Curl_unencode_deflate_write(conn, &data->req,
232 /* update data->req.keep.str to point to the chunk data. */
233 data->req.str = datap;
234 result = Curl_unencode_gzip_write(conn, &data->req,
241 "Unrecognized content encoding type. "
242 "libcurl understands `identity', `deflate' and `gzip' "
243 "content encodings.");
244 return CHUNKE_BAD_ENCODING;
249 return CHUNKE_WRITE_ERROR;
253 ch->datasize -= piece; /* decrease amount left to expect */
254 datap += piece; /* move read pointer forward */
255 length -= piece; /* decrease space left in this round */
257 if(0 == ch->datasize)
258 /* end of data this round, we now expect a trailing CRLF */
259 ch->state = CHUNK_POSTCR;
264 ch->state = CHUNK_POSTLF;
269 return CHUNKE_BAD_CHUNK;
276 * The last one before we go back to hex state and start all
279 Curl_httpchunk_init(conn);
284 return CHUNKE_BAD_CHUNK;
290 /* this is the end of a trailer, but if the trailer was zero bytes
291 there was no trailer and we move on */
294 /* we allocate trailer with 3 bytes extra room to fit this */
295 conn->trailer[conn->trlPos++]=0x0d;
296 conn->trailer[conn->trlPos++]=0x0a;
297 conn->trailer[conn->trlPos]=0;
299 /* Convert to host encoding before calling Curl_client_write */
300 result = Curl_convert_from_network(conn->data, conn->trailer,
303 /* Curl_convert_from_network calls failf if unsuccessful */
304 /* Treat it as a bad chunk */
305 return CHUNKE_BAD_CHUNK;
307 if(!data->set.http_te_skip) {
308 result = Curl_client_write(conn, CLIENTWRITE_HEADER,
309 conn->trailer, conn->trlPos);
311 return CHUNKE_WRITE_ERROR;
314 ch->state = CHUNK_TRAILER_CR;
317 /* no trailer, we're on the final CRLF pair */
318 ch->state = CHUNK_TRAILER_POSTCR;
319 break; /* don't advance the pointer */
323 /* conn->trailer is assumed to be freed in url.c on a
325 if(conn->trlPos >= conn->trlMax) {
326 /* we always allocate three extra bytes, just because when the full
327 header has been received we append CRLF\0 */
331 ptr = realloc(conn->trailer, conn->trlMax + 3);
335 ptr = malloc(conn->trlMax + 3);
338 return CHUNKE_OUT_OF_MEMORY;
341 conn->trailer[conn->trlPos++]=*datap;
347 case CHUNK_TRAILER_CR:
349 ch->state = CHUNK_TRAILER_POSTCR;
354 return CHUNKE_BAD_CHUNK;
357 case CHUNK_TRAILER_POSTCR:
358 /* We enter this state when a CR should arrive so we expect to
359 have to first pass a CR before we wait for LF */
361 /* not a CR then it must be another header in the trailer */
362 ch->state = CHUNK_TRAILER;
367 /* now wait for the final LF */
368 ch->state = CHUNK_STOP;
372 /* Read the final CRLF that ends all chunk bodies */
375 ch->state = CHUNK_STOP;
380 return CHUNKE_BAD_CHUNK;
387 /* Record the length of any data left in the end of the buffer
388 even if there's no more chunks to read */
390 ch->dataleft = length;
391 return CHUNKE_STOP; /* return stop */
394 return CHUNKE_BAD_CHUNK;
397 return CHUNKE_STATE_ERROR;
402 #endif /* CURL_DISABLE_HTTP */