1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2001, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * In order to be useful for every potential user, curl and libcurl are
11 * dual-licensed under the MPL and the MIT/X-derivate licenses.
13 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
14 * copies of the Software, and permit persons to whom the Software is
15 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
16 * licenses. You may pick one of these licenses.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 *****************************************************************************/
25 /* -- WIN32 approved -- */
32 #include "urldata.h" /* it includes http_chunks.h */
33 #include "sendf.h" /* for the client write stuff */
35 #define _MPRINTF_REPLACE /* use our functions only */
36 #include <curl/mprintf.h>
38 /* The last #include file should be: */
44 * Chunk format (simplified):
46 * <HEX SIZE>[ chunk extension ] CRLF
49 * Highlights from RFC2616 section 3.6 say:
51 The chunked encoding modifies the body of a message in order to
52 transfer it as a series of chunks, each with its own size indicator,
53 followed by an OPTIONAL trailer containing entity-header fields. This
54 allows dynamically produced content to be transferred along with the
55 information necessary for the recipient to verify that it has
56 received the full message.
63 chunk = chunk-size [ chunk-extension ] CRLF
66 last-chunk = 1*("0") [ chunk-extension ] CRLF
68 chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
69 chunk-ext-name = token
70 chunk-ext-val = token | quoted-string
71 chunk-data = chunk-size(OCTET)
72 trailer = *(entity-header CRLF)
74 The chunk-size field is a string of hex digits indicating the size of
75 the chunk. The chunked encoding is ended by any chunk whose size is
76 zero, followed by the trailer, which is terminated by an empty line.
81 void Curl_httpchunk_init(struct connectdata *conn)
83 struct Curl_chunker *chunk = &conn->proto.http->chunk;
84 chunk->hexindex=0; /* start at 0 */
85 chunk->state = CHUNK_HEX; /* we get hex first! */
89 * chunk_read() returns a 0 for normal operations, or a positive return code
90 * for errors. A negative number means this sequence of chunks is complete,
91 * and that many ~bytes were NOT used at the end of the buffer passed in.
92 * The 'wrote' argument is set to tell the caller how many bytes we actually
93 * passed to the client (for byte-counting and whatever).
95 * The states and the state-machine is further explained in the header file.
97 CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
103 struct Curl_chunker *ch = &conn->proto.http->chunk;
105 *wrote = 0; /* nothing yet */
110 if(isxdigit((int)*datap)) {
111 if(ch->hexindex < MAXNUM_SIZE) {
112 ch->hexbuffer[ch->hexindex] = *datap;
118 return 1; /* longer hex than we support */
122 /* length and datap are unmodified */
123 ch->hexbuffer[ch->hexindex]=0;
124 ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
125 ch->state = CHUNK_POSTHEX;
130 /* just a lame state waiting for CRLF to arrive */
132 ch->state = CHUNK_CR;
138 /* waiting for the LF */
140 /* we're now expecting data to come, unless size was zero! */
141 if(0 == ch->datasize) {
142 ch->state = CHUNK_STOP; /* stop reading! */
144 /* This was the final byte, return right now */
149 ch->state = CHUNK_DATA;
152 /* previously we got a fake CR, go back to CR waiting! */
153 ch->state = CHUNK_CR;
159 /* we get pure and fine data
161 We expect another 'datasize' of data. We have 'length' right now,
162 it can be more or less than 'datasize'. Get the smallest piece.
164 piece = (ch->datasize >= length)?length:ch->datasize;
166 /* Write the data portion available */
167 result = Curl_client_write(conn->data, CLIENTWRITE_BODY, datap, piece);
169 return CHUNKE_WRITE_ERROR;
172 ch->datasize -= piece; /* decrease amount left to expect */
173 datap += piece; /* move read pointer forward */
174 length -= piece; /* decrease space left in this round */
176 if(0 == ch->datasize)
177 /* end of data this round, go back to get a new size */
178 Curl_httpchunk_init(conn);
182 return ~length; /* return the data size left */
184 return CHUNKE_STATE_ERROR;