Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / http_chunks.c
blob6b4643ec8ea85184061c0a2ba51b1ae25f39bf14
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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 ***************************************************************************/
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->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,
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 = &data->reqdata.proto.http->chunk;
112 struct Curl_transfer_keeper *k = &data->reqdata.keep;
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 while(length) {
120 switch(ch->state) {
121 case CHUNK_HEX:
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;
129 datap++;
130 length--;
131 ch->hexindex++;
133 else {
134 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
137 else {
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,
148 ch->hexbuffer,
149 ch->hexindex);
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;
159 break;
161 case 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. */
165 if(*datap == 0x0d)
166 ch->state = CHUNK_CR;
167 length--;
168 datap++;
169 break;
171 case CHUNK_CR:
172 /* waiting for the LF */
173 if(*datap == 0x0a) {
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;
179 if (1 == length) {
180 /* This is the final byte, return right now */
181 return CHUNKE_STOP;
184 else {
185 ch->state = CHUNK_TRAILER; /* attempt to read trailers */
186 conn->trlPos=0;
189 else
190 ch->state = CHUNK_DATA;
192 else
193 /* previously we got a fake CR, go back to CR waiting! */
194 ch->state = CHUNK_CR;
195 datap++;
196 length--;
197 break;
199 case CHUNK_DATA:
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 */
208 #ifdef HAVE_LIBZ
209 switch (data->reqdata.keep.content_encoding) {
210 case IDENTITY:
211 #endif
212 if(!k->ignorebody)
213 result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
214 piece);
215 #ifdef HAVE_LIBZ
216 break;
218 case DEFLATE:
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,
222 (ssize_t)piece);
223 break;
225 case GZIP:
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,
229 (ssize_t)piece);
230 break;
232 case COMPRESS:
233 default:
234 failf (conn->data,
235 "Unrecognized content encoding type. "
236 "libcurl understands `identity', `deflate' and `gzip' "
237 "content encodings.");
238 return CHUNKE_BAD_ENCODING;
240 #endif
242 if(result)
243 return CHUNKE_WRITE_ERROR;
245 *wrote += piece;
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;
254 break;
256 case CHUNK_POSTCR:
257 if(*datap == 0x0d) {
258 ch->state = CHUNK_POSTLF;
259 datap++;
260 length--;
262 else
263 return CHUNKE_BAD_CHUNK;
264 break;
266 case CHUNK_POSTLF:
267 if(*datap == 0x0a) {
269 * The last one before we go back to hex state and start all
270 * over.
272 Curl_httpchunk_init(conn);
273 datap++;
274 length--;
276 else
277 return CHUNKE_BAD_CHUNK;
278 break;
280 case CHUNK_TRAILER:
281 /* conn->trailer is assumed to be freed in url.c on a
282 connection basis */
283 if (conn->trlPos >= conn->trlMax) {
284 char *ptr;
285 if(conn->trlMax) {
286 conn->trlMax *= 2;
287 ptr = (char*)realloc(conn->trailer,conn->trlMax);
289 else {
290 conn->trlMax=128;
291 ptr = (char*)malloc(conn->trlMax);
293 if(!ptr)
294 return CHUNKE_OUT_OF_MEMORY;
295 conn->trailer = ptr;
297 conn->trailer[conn->trlPos++]=*datap;
299 if(*datap == 0x0d)
300 ch->state = CHUNK_TRAILER_CR;
301 else {
302 datap++;
303 length--;
305 break;
307 case CHUNK_TRAILER_CR:
308 if(*datap == 0x0d) {
309 ch->state = CHUNK_TRAILER_POSTCR;
310 datap++;
311 length--;
313 else
314 return CHUNKE_BAD_CHUNK;
315 break;
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;
323 return CHUNKE_STOP;
325 else {
326 #ifdef CURL_DOES_CONVERSIONS
327 /* Convert to host encoding before calling Curl_client_write */
328 result = Curl_convert_from_network(conn->data,
329 conn->trailer,
330 conn->trlPos);
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;
341 conn->trlPos=0;
342 datap++;
343 length--;
345 else
346 return CHUNKE_BAD_CHUNK;
347 break;
349 case CHUNK_STOP:
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 */
354 default:
355 return CHUNKE_STATE_ERROR;
358 return CHUNKE_OK;
360 #endif /* CURL_DISABLE_HTTP */