1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.
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"
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.
84 void Curl_httpchunk_init(struct connectdata *conn)
86 struct Curl_chunker *chunk = &conn->data->reqdata.proto.http->chunk;
87 chunk->hexindex=0; /* start at 0 */
88 chunk->dataleft=0; /* no data left yet! */
89 chunk->state = CHUNK_HEX; /* we get hex first! */
93 * chunk_read() returns a OK for normal operations, or a positive return code
94 * for errors. STOP means this sequence of chunks is complete. The 'wrote'
95 * argument is set to tell the caller how many bytes we actually passed to the
96 * client (for byte-counting and whatever).
98 * The states and the state-machine is further explained in the header file.
100 CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
105 CURLcode result=CURLE_OK;
106 struct SessionHandle *data = conn->data;
107 struct Curl_chunker *ch = &data->reqdata.proto.http->chunk;
108 struct Curl_transfer_keeper *k = &data->reqdata.keep;
110 size_t length = (size_t)datalen;
111 size_t *wrote = (size_t *)wrotep;
113 *wrote = 0; /* nothing's written yet */
118 if(ISXDIGIT(*datap)) {
119 if(ch->hexindex < MAXNUM_SIZE) {
120 ch->hexbuffer[ch->hexindex] = *datap;
126 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
130 if(0 == ch->hexindex) {
131 /* This is illegal data, we received junk where we expected
132 a hexadecimal digit. */
133 return CHUNKE_ILLEGAL_HEX;
135 /* length and datap are unmodified */
136 ch->hexbuffer[ch->hexindex]=0;
137 ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
138 ch->state = CHUNK_POSTHEX;
143 /* In this state, we're waiting for CRLF to arrive. We support
144 this to allow so called chunk-extensions to show up here
145 before the CRLF comes. */
147 ch->state = CHUNK_CR;
153 /* waiting for the LF */
155 /* we're now expecting data to come, unless size was zero! */
156 if(0 == ch->datasize) {
157 if (conn->bits.trailerHdrPresent!=TRUE) {
158 /* No Trailer: header found - revert to original Curl processing */
159 ch->state = CHUNK_STOP;
161 /* This is the final byte, return right now */
166 ch->state = CHUNK_TRAILER; /* attempt to read trailers */
171 ch->state = CHUNK_DATA;
174 /* previously we got a fake CR, go back to CR waiting! */
175 ch->state = CHUNK_CR;
181 /* we get pure and fine data
183 We expect another 'datasize' of data. We have 'length' right now,
184 it can be more or less than 'datasize'. Get the smallest piece.
186 piece = (ch->datasize >= length)?length:ch->datasize;
188 /* Write the data portion available */
190 switch (data->reqdata.keep.content_encoding) {
194 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
200 /* update data->reqdata.keep.str to point to the chunk data. */
201 data->reqdata.keep.str = datap;
202 result = Curl_unencode_deflate_write(conn, &data->reqdata.keep,
207 /* update data->reqdata.keep.str to point to the chunk data. */
208 data->reqdata.keep.str = datap;
209 result = Curl_unencode_gzip_write(conn, &data->reqdata.keep,
216 "Unrecognized content encoding type. "
217 "libcurl understands `identity', `deflate' and `gzip' "
218 "content encodings.");
219 return CHUNKE_BAD_ENCODING;
224 return CHUNKE_WRITE_ERROR;
228 ch->datasize -= piece; /* decrease amount left to expect */
229 datap += piece; /* move read pointer forward */
230 length -= piece; /* decrease space left in this round */
232 if(0 == ch->datasize)
233 /* end of data this round, we now expect a trailing CRLF */
234 ch->state = CHUNK_POSTCR;
239 ch->state = CHUNK_POSTLF;
244 return CHUNKE_BAD_CHUNK;
250 * The last one before we go back to hex state and start all
253 Curl_httpchunk_init(conn);
258 return CHUNKE_BAD_CHUNK;
262 /* conn->trailer is assumed to be freed in url.c on a
264 if (conn->trlPos >= conn->trlMax) {
268 ptr = (char*)realloc(conn->trailer,conn->trlMax);
272 ptr = (char*)malloc(conn->trlMax);
275 return CHUNKE_OUT_OF_MEMORY;
278 conn->trailer[conn->trlPos++]=*datap;
281 ch->state = CHUNK_TRAILER_CR;
288 case CHUNK_TRAILER_CR:
290 ch->state = CHUNK_TRAILER_POSTCR;
295 return CHUNKE_BAD_CHUNK;
298 case CHUNK_TRAILER_POSTCR:
299 if (*datap == '\n') {
300 conn->trailer[conn->trlPos++]='\n';
301 conn->trailer[conn->trlPos]=0;
302 if (conn->trlPos==2) {
303 ch->state = CHUNK_STOP;
307 Curl_client_write(conn, CLIENTWRITE_HEADER,
308 conn->trailer, conn->trlPos);
310 ch->state = CHUNK_TRAILER;
316 return CHUNKE_BAD_CHUNK;
320 /* If we arrive here, there is data left in the end of the buffer
321 even if there's no more chunks to read */
322 ch->dataleft = length;
323 return CHUNKE_STOP; /* return stop */
325 return CHUNKE_STATE_ERROR;
330 #endif /* CURL_DISABLE_HTTP */