1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_HTTP
27 #include "urldata.h" /* it includes http_chunks.h */
28 #include "sendf.h" /* for the client write stuff */
30 #include "content_encoding.h"
32 #include "non-ascii.h" /* for Curl_convert_to_network prototype */
33 #include "strtoofft.h"
36 /* The last #include files should be: */
37 #include "curl_memory.h"
41 * Chunk format (simplified):
43 * <HEX SIZE>[ chunk extension ] CRLF
46 * Highlights from RFC2616 section 3.6 say:
48 The chunked encoding modifies the body of a message in order to
49 transfer it as a series of chunks, each with its own size indicator,
50 followed by an OPTIONAL trailer containing entity-header fields. This
51 allows dynamically produced content to be transferred along with the
52 information necessary for the recipient to verify that it has
53 received the full message.
60 chunk = chunk-size [ chunk-extension ] CRLF
63 last-chunk = 1*("0") [ chunk-extension ] CRLF
65 chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
66 chunk-ext-name = token
67 chunk-ext-val = token | quoted-string
68 chunk-data = chunk-size(OCTET)
69 trailer = *(entity-header CRLF)
71 The chunk-size field is a string of hex digits indicating the size of
72 the chunk. The chunked encoding is ended by any chunk whose size is
73 zero, followed by the trailer, which is terminated by an empty line.
77 /* Check for an ASCII hex digit.
78 We avoid the use of isxdigit to accommodate non-ASCII hosts. */
79 static bool Curl_isxdigit(char digit)
81 return ( (digit >= 0x30 && digit <= 0x39) /* 0-9 */
82 || (digit >= 0x41 && digit <= 0x46) /* A-F */
83 || (digit >= 0x61 && digit <= 0x66) /* a-f */) ? TRUE : FALSE;
86 void Curl_httpchunk_init(struct connectdata *conn)
88 struct Curl_chunker *chunk = &conn->chunk;
89 chunk->hexindex=0; /* start at 0 */
90 chunk->dataleft=0; /* no data left yet! */
91 chunk->state = CHUNK_HEX; /* we get hex first! */
95 * chunk_read() returns a OK for normal operations, or a positive return code
96 * for errors. STOP means this sequence of chunks is complete. The 'wrote'
97 * argument is set to tell the caller how many bytes we actually passed to the
98 * client (for byte-counting and whatever).
100 * The states and the state-machine is further explained in the header file.
102 * This function always uses ASCII hex values to accommodate non-ASCII hosts.
103 * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
105 CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
110 CURLcode result=CURLE_OK;
111 struct Curl_easy *data = conn->data;
112 struct Curl_chunker *ch = &conn->chunk;
113 struct SingleRequest *k = &data->req;
115 curl_off_t length = (curl_off_t)datalen;
116 size_t *wrote = (size_t *)wrotep;
118 *wrote = 0; /* nothing's written yet */
120 /* the original data is written to the client, but we go on with the
121 chunk read process, to properly calculate the content length*/
122 if(data->set.http_te_skip && !k->ignorebody) {
123 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
125 return CHUNKE_WRITE_ERROR;
131 if(Curl_isxdigit(*datap)) {
132 if(ch->hexindex < MAXNUM_SIZE) {
133 ch->hexbuffer[ch->hexindex] = *datap;
139 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
144 if(0 == ch->hexindex)
145 /* This is illegal data, we received junk where we expected
146 a hexadecimal digit. */
147 return CHUNKE_ILLEGAL_HEX;
149 /* length and datap are unmodified */
150 ch->hexbuffer[ch->hexindex]=0;
152 /* convert to host encoding before calling strtoul */
153 result = Curl_convert_from_network(conn->data, ch->hexbuffer,
156 /* Curl_convert_from_network calls failf if unsuccessful */
157 /* Treat it as a bad hex character */
158 return CHUNKE_ILLEGAL_HEX;
161 ch->datasize=curlx_strtoofft(ch->hexbuffer, &endptr, 16);
162 if((ch->datasize == CURL_OFF_T_MAX) && (errno == ERANGE))
163 /* overflow is an error */
164 return CHUNKE_ILLEGAL_HEX;
165 ch->state = CHUNK_LF; /* now wait for the CRLF */
170 /* waiting for the LF after a chunk size */
172 /* we're now expecting data to come, unless size was zero! */
173 if(0 == ch->datasize) {
174 ch->state = CHUNK_TRAILER; /* now check for trailers */
178 ch->state = CHUNK_DATA;
186 /* We expect 'datasize' of data. We have 'length' right now, it can be
187 more or less than 'datasize'. Get the smallest piece.
189 piece = curlx_sotouz((ch->datasize >= length)?length:ch->datasize);
191 /* Write the data portion available */
193 switch (conn->data->set.http_ce_skip?
194 IDENTITY : data->req.auto_decoding) {
198 if(!data->set.http_te_skip)
199 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
208 /* update data->req.keep.str to point to the chunk data. */
209 data->req.str = datap;
210 result = Curl_unencode_deflate_write(conn, &data->req,
215 /* update data->req.keep.str to point to the chunk data. */
216 data->req.str = datap;
217 result = Curl_unencode_gzip_write(conn, &data->req,
223 "Unrecognized content encoding type. "
224 "libcurl understands `identity', `deflate' and `gzip' "
225 "content encodings.");
226 return CHUNKE_BAD_ENCODING;
231 return CHUNKE_WRITE_ERROR;
235 ch->datasize -= piece; /* decrease amount left to expect */
236 datap += piece; /* move read pointer forward */
237 length -= piece; /* decrease space left in this round */
239 if(0 == ch->datasize)
240 /* end of data this round, we now expect a trailing CRLF */
241 ch->state = CHUNK_POSTLF;
246 /* The last one before we go back to hex state and start all over. */
247 Curl_httpchunk_init(conn); /* sets state back to CHUNK_HEX */
249 else if(*datap != 0x0d)
250 return CHUNKE_BAD_CHUNK;
256 if((*datap == 0x0d) || (*datap == 0x0a)) {
257 /* this is the end of a trailer, but if the trailer was zero bytes
258 there was no trailer and we move on */
261 /* we allocate trailer with 3 bytes extra room to fit this */
262 conn->trailer[conn->trlPos++]=0x0d;
263 conn->trailer[conn->trlPos++]=0x0a;
264 conn->trailer[conn->trlPos]=0;
266 /* Convert to host encoding before calling Curl_client_write */
267 result = Curl_convert_from_network(conn->data, conn->trailer,
270 /* Curl_convert_from_network calls failf if unsuccessful */
271 /* Treat it as a bad chunk */
272 return CHUNKE_BAD_CHUNK;
274 if(!data->set.http_te_skip) {
275 result = Curl_client_write(conn, CLIENTWRITE_HEADER,
276 conn->trailer, conn->trlPos);
278 return CHUNKE_WRITE_ERROR;
281 ch->state = CHUNK_TRAILER_CR;
283 /* already on the LF */
287 /* no trailer, we're on the final CRLF pair */
288 ch->state = CHUNK_TRAILER_POSTCR;
289 break; /* don't advance the pointer */
293 /* conn->trailer is assumed to be freed in url.c on a
295 if(conn->trlPos >= conn->trlMax) {
296 /* we always allocate three extra bytes, just because when the full
297 header has been received we append CRLF\0 */
301 ptr = realloc(conn->trailer, conn->trlMax + 3);
305 ptr = malloc(conn->trlMax + 3);
308 return CHUNKE_OUT_OF_MEMORY;
311 conn->trailer[conn->trlPos++]=*datap;
317 case CHUNK_TRAILER_CR:
319 ch->state = CHUNK_TRAILER_POSTCR;
324 return CHUNKE_BAD_CHUNK;
327 case CHUNK_TRAILER_POSTCR:
328 /* We enter this state when a CR should arrive so we expect to
329 have to first pass a CR before we wait for LF */
330 if((*datap != 0x0d) && (*datap != 0x0a)) {
331 /* not a CR then it must be another header in the trailer */
332 ch->state = CHUNK_TRAILER;
340 /* now wait for the final LF */
341 ch->state = CHUNK_STOP;
348 /* Record the length of any data left in the end of the buffer
349 even if there's no more chunks to read */
350 ch->dataleft = curlx_sotouz(length);
352 return CHUNKE_STOP; /* return stop */
355 return CHUNKE_BAD_CHUNK;
361 const char *Curl_chunked_strerror(CHUNKcode code)
366 case CHUNKE_TOO_LONG_HEX:
367 return "Too long hexadecimal number";
368 case CHUNKE_ILLEGAL_HEX:
369 return "Illegal or missing hexadecimal sequence";
370 case CHUNKE_BAD_CHUNK:
371 return "Malformed encoding found";
372 case CHUNKE_WRITE_ERROR:
373 return "Write error";
374 case CHUNKE_BAD_ENCODING:
375 return "Bad content-encoding found";
376 case CHUNKE_OUT_OF_MEMORY:
377 return "Out of memory";
381 #endif /* CURL_DISABLE_HTTP */