Initial commit of visual studio 9 git build superproject.
[git-build-vc9.git] / curl / lib / http_chunks.c
blobaabd611469d54a7eeaf46500d84e2bdc4c4f8791
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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 ***************************************************************************/
23 #include "setup.h"
25 #ifndef CURL_DISABLE_HTTP
26 /* -- WIN32 approved -- */
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <ctype.h>
33 #include "urldata.h" /* it includes http_chunks.h */
34 #include "sendf.h" /* for the client write stuff */
36 #include "content_encoding.h"
37 #include "http.h"
38 #include "memory.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: */
45 #include "memdebug.h"
48 * Chunk format (simplified):
50 * <HEX SIZE>[ chunk extension ] CRLF
51 * <DATA> 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.
62 Chunked-Body = *chunk
63 last-chunk
64 trailer
65 CRLF
67 chunk = chunk-size [ chunk-extension ] CRLF
68 chunk-data CRLF
69 chunk-size = 1*HEX
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,
105 char *datap,
106 ssize_t datalen,
107 ssize_t *wrotep)
109 CURLcode result=CURLE_OK;
110 struct SessionHandle *data = conn->data;
111 struct Curl_chunker *ch = &conn->chunk;
112 struct SingleRequest *k = &data->req;
113 size_t piece;
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);
123 if(result)
124 return CHUNKE_WRITE_ERROR;
127 while(length) {
128 switch(ch->state) {
129 case CHUNK_HEX:
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;
137 datap++;
138 length--;
139 ch->hexindex++;
141 else {
142 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
145 else {
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,
156 ch->hexbuffer,
157 ch->hexindex);
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;
167 break;
169 case 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. */
173 if(*datap == 0x0d)
174 ch->state = CHUNK_CR;
175 length--;
176 datap++;
177 break;
179 case CHUNK_CR:
180 /* waiting for the LF */
181 if(*datap == 0x0a) {
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 */
190 length--;
191 datap++;
193 /* This is the final byte, continue to read the final CRLF */
194 break;
196 else {
197 ch->state = CHUNK_TRAILER; /* attempt to read trailers */
198 conn->trlPos=0;
201 else {
202 ch->state = CHUNK_DATA;
205 else
206 /* previously we got a fake CR, go back to CR waiting! */
207 ch->state = CHUNK_CR;
208 datap++;
209 length--;
210 break;
212 case CHUNK_DATA:
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 */
221 #ifdef HAVE_LIBZ
222 switch (conn->data->set.http_ce_skip?
223 IDENTITY : data->req.content_encoding) {
224 case IDENTITY:
225 #endif
226 if(!k->ignorebody) {
227 if( !data->set.http_te_skip )
228 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
229 piece);
230 else
231 result = CURLE_OK;
233 #ifdef HAVE_LIBZ
234 break;
236 case DEFLATE:
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,
240 (ssize_t)piece);
241 break;
243 case GZIP:
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,
247 (ssize_t)piece);
248 break;
250 case COMPRESS:
251 default:
252 failf (conn->data,
253 "Unrecognized content encoding type. "
254 "libcurl understands `identity', `deflate' and `gzip' "
255 "content encodings.");
256 return CHUNKE_BAD_ENCODING;
258 #endif
260 if(result)
261 return CHUNKE_WRITE_ERROR;
263 *wrote += piece;
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;
272 break;
274 case CHUNK_POSTCR:
275 if(*datap == 0x0d) {
276 ch->state = CHUNK_POSTLF;
277 datap++;
278 length--;
280 else {
281 return CHUNKE_BAD_CHUNK;
283 break;
285 case CHUNK_POSTLF:
286 if(*datap == 0x0a) {
288 * The last one before we go back to hex state and start all
289 * over.
291 Curl_httpchunk_init(conn);
292 datap++;
293 length--;
295 else {
296 return CHUNKE_BAD_CHUNK;
299 break;
301 case CHUNK_TRAILER:
302 /* conn->trailer is assumed to be freed in url.c on a
303 connection basis */
304 if(conn->trlPos >= conn->trlMax) {
305 char *ptr;
306 if(conn->trlMax) {
307 conn->trlMax *= 2;
308 ptr = (char*)realloc(conn->trailer,conn->trlMax);
310 else {
311 conn->trlMax=128;
312 ptr = (char*)malloc(conn->trlMax);
314 if(!ptr)
315 return CHUNKE_OUT_OF_MEMORY;
316 conn->trailer = ptr;
318 conn->trailer[conn->trlPos++]=*datap;
320 if(*datap == 0x0d)
321 ch->state = CHUNK_TRAILER_CR;
322 else {
323 datap++;
324 length--;
326 break;
328 case CHUNK_TRAILER_CR:
329 if(*datap == 0x0d) {
330 ch->state = CHUNK_TRAILER_POSTCR;
331 datap++;
332 length--;
334 else
335 return CHUNKE_BAD_CHUNK;
336 break;
338 case CHUNK_TRAILER_POSTCR:
339 if(*datap == 0x0a) {
340 conn->trailer[conn->trlPos++]=0x0a;
341 conn->trailer[conn->trlPos]=0;
342 if(conn->trlPos==2) {
343 ch->state = CHUNK_STOP;
344 datap++;
345 length--;
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 */
356 else {
357 #ifdef CURL_DOES_CONVERSIONS
358 /* Convert to host encoding before calling Curl_client_write */
359 result = Curl_convert_from_network(conn->data,
360 conn->trailer,
361 conn->trlPos);
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);
371 if(result)
372 return CHUNKE_WRITE_ERROR;
375 ch->state = CHUNK_TRAILER;
376 conn->trlPos=0;
377 datap++;
378 length--;
380 else
381 return CHUNKE_BAD_CHUNK;
382 break;
384 case CHUNK_STOPCR:
385 /* Read the final CRLF that ends all chunk bodies */
387 if(*datap == 0x0d) {
388 ch->state = CHUNK_STOP;
389 datap++;
390 length--;
392 else {
393 return CHUNKE_BAD_CHUNK;
395 break;
397 case CHUNK_STOP:
398 if(*datap == 0x0a) {
399 datap++;
400 length--;
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 */
408 else {
409 return CHUNKE_BAD_CHUNK;
412 default:
413 return CHUNKE_STATE_ERROR;
416 return CHUNKE_OK;
418 #endif /* CURL_DISABLE_HTTP */