VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/http_chunks.c@ 97138

最後變更 在這個檔案從97138是 95312,由 vboxsync 提交於 3 年 前

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 9.8 KB
 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#ifndef CURL_DISABLE_HTTP
26
27#include "urldata.h" /* it includes http_chunks.h */
28#include "sendf.h" /* for the client write stuff */
29#include "dynbuf.h"
30#include "content_encoding.h"
31#include "http.h"
32#include "strtoofft.h"
33#include "warnless.h"
34
35/* The last #include files should be: */
36#include "curl_memory.h"
37#include "memdebug.h"
38
39/*
40 * Chunk format (simplified):
41 *
42 * <HEX SIZE>[ chunk extension ] CRLF
43 * <DATA> CRLF
44 *
45 * Highlights from RFC2616 section 3.6 say:
46
47 The chunked encoding modifies the body of a message in order to
48 transfer it as a series of chunks, each with its own size indicator,
49 followed by an OPTIONAL trailer containing entity-header fields. This
50 allows dynamically produced content to be transferred along with the
51 information necessary for the recipient to verify that it has
52 received the full message.
53
54 Chunked-Body = *chunk
55 last-chunk
56 trailer
57 CRLF
58
59 chunk = chunk-size [ chunk-extension ] CRLF
60 chunk-data CRLF
61 chunk-size = 1*HEX
62 last-chunk = 1*("0") [ chunk-extension ] CRLF
63
64 chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
65 chunk-ext-name = token
66 chunk-ext-val = token | quoted-string
67 chunk-data = chunk-size(OCTET)
68 trailer = *(entity-header CRLF)
69
70 The chunk-size field is a string of hex digits indicating the size of
71 the chunk. The chunked encoding is ended by any chunk whose size is
72 zero, followed by the trailer, which is terminated by an empty line.
73
74 */
75
76#define isxdigit_ascii(x) Curl_isxdigit(x)
77
78void Curl_httpchunk_init(struct Curl_easy *data)
79{
80 struct connectdata *conn = data->conn;
81 struct Curl_chunker *chunk = &conn->chunk;
82 chunk->hexindex = 0; /* start at 0 */
83 chunk->state = CHUNK_HEX; /* we get hex first! */
84 Curl_dyn_init(&conn->trailer, DYN_H1_TRAILER);
85}
86
87/*
88 * chunk_read() returns a OK for normal operations, or a positive return code
89 * for errors. STOP means this sequence of chunks is complete. The 'wrote'
90 * argument is set to tell the caller how many bytes we actually passed to the
91 * client (for byte-counting and whatever).
92 *
93 * The states and the state-machine is further explained in the header file.
94 *
95 * This function always uses ASCII hex values to accommodate non-ASCII hosts.
96 * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
97 */
98CHUNKcode Curl_httpchunk_read(struct Curl_easy *data,
99 char *datap,
100 ssize_t datalen,
101 ssize_t *wrotep,
102 CURLcode *extrap)
103{
104 CURLcode result = CURLE_OK;
105 struct connectdata *conn = data->conn;
106 struct Curl_chunker *ch = &conn->chunk;
107 struct SingleRequest *k = &data->req;
108 size_t piece;
109 curl_off_t length = (curl_off_t)datalen;
110 size_t *wrote = (size_t *)wrotep;
111
112 *wrote = 0; /* nothing's written yet */
113
114 /* the original data is written to the client, but we go on with the
115 chunk read process, to properly calculate the content length*/
116 if(data->set.http_te_skip && !k->ignorebody) {
117 result = Curl_client_write(data, CLIENTWRITE_BODY, datap, datalen);
118 if(result) {
119 *extrap = result;
120 return CHUNKE_PASSTHRU_ERROR;
121 }
122 }
123
124 while(length) {
125 switch(ch->state) {
126 case CHUNK_HEX:
127 if(isxdigit_ascii(*datap)) {
128 if(ch->hexindex < CHUNK_MAXNUM_LEN) {
129 ch->hexbuffer[ch->hexindex] = *datap;
130 datap++;
131 length--;
132 ch->hexindex++;
133 }
134 else {
135 return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
136 }
137 }
138 else {
139 char *endptr;
140 if(0 == ch->hexindex)
141 /* This is illegal data, we received junk where we expected
142 a hexadecimal digit. */
143 return CHUNKE_ILLEGAL_HEX;
144
145 /* length and datap are unmodified */
146 ch->hexbuffer[ch->hexindex] = 0;
147
148 if(curlx_strtoofft(ch->hexbuffer, &endptr, 16, &ch->datasize))
149 return CHUNKE_ILLEGAL_HEX;
150 ch->state = CHUNK_LF; /* now wait for the CRLF */
151 }
152 break;
153
154 case CHUNK_LF:
155 /* waiting for the LF after a chunk size */
156 if(*datap == 0x0a) {
157 /* we're now expecting data to come, unless size was zero! */
158 if(0 == ch->datasize) {
159 ch->state = CHUNK_TRAILER; /* now check for trailers */
160 }
161 else
162 ch->state = CHUNK_DATA;
163 }
164
165 datap++;
166 length--;
167 break;
168
169 case CHUNK_DATA:
170 /* We expect 'datasize' of data. We have 'length' right now, it can be
171 more or less than 'datasize'. Get the smallest piece.
172 */
173 piece = curlx_sotouz((ch->datasize >= length)?length:ch->datasize);
174
175 /* Write the data portion available */
176 if(!data->set.http_te_skip && !k->ignorebody) {
177 if(!data->set.http_ce_skip && k->writer_stack)
178 result = Curl_unencode_write(data, k->writer_stack, datap, piece);
179 else
180 result = Curl_client_write(data, CLIENTWRITE_BODY, datap, piece);
181
182 if(result) {
183 *extrap = result;
184 return CHUNKE_PASSTHRU_ERROR;
185 }
186 }
187
188 *wrote += piece;
189 ch->datasize -= piece; /* decrease amount left to expect */
190 datap += piece; /* move read pointer forward */
191 length -= piece; /* decrease space left in this round */
192
193 if(0 == ch->datasize)
194 /* end of data this round, we now expect a trailing CRLF */
195 ch->state = CHUNK_POSTLF;
196 break;
197
198 case CHUNK_POSTLF:
199 if(*datap == 0x0a) {
200 /* The last one before we go back to hex state and start all over. */
201 Curl_httpchunk_init(data); /* sets state back to CHUNK_HEX */
202 }
203 else if(*datap != 0x0d)
204 return CHUNKE_BAD_CHUNK;
205 datap++;
206 length--;
207 break;
208
209 case CHUNK_TRAILER:
210 if((*datap == 0x0d) || (*datap == 0x0a)) {
211 char *tr = Curl_dyn_ptr(&conn->trailer);
212 /* this is the end of a trailer, but if the trailer was zero bytes
213 there was no trailer and we move on */
214
215 if(tr) {
216 size_t trlen;
217 result = Curl_dyn_addn(&conn->trailer, (char *)STRCONST("\x0d\x0a"));
218 if(result)
219 return CHUNKE_OUT_OF_MEMORY;
220
221 tr = Curl_dyn_ptr(&conn->trailer);
222 trlen = Curl_dyn_len(&conn->trailer);
223 if(!data->set.http_te_skip) {
224 result = Curl_client_write(data,
225 CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER,
226 tr, trlen);
227 if(result) {
228 *extrap = result;
229 return CHUNKE_PASSTHRU_ERROR;
230 }
231 }
232 Curl_dyn_reset(&conn->trailer);
233 ch->state = CHUNK_TRAILER_CR;
234 if(*datap == 0x0a)
235 /* already on the LF */
236 break;
237 }
238 else {
239 /* no trailer, we're on the final CRLF pair */
240 ch->state = CHUNK_TRAILER_POSTCR;
241 break; /* don't advance the pointer */
242 }
243 }
244 else {
245 result = Curl_dyn_addn(&conn->trailer, datap, 1);
246 if(result)
247 return CHUNKE_OUT_OF_MEMORY;
248 }
249 datap++;
250 length--;
251 break;
252
253 case CHUNK_TRAILER_CR:
254 if(*datap == 0x0a) {
255 ch->state = CHUNK_TRAILER_POSTCR;
256 datap++;
257 length--;
258 }
259 else
260 return CHUNKE_BAD_CHUNK;
261 break;
262
263 case CHUNK_TRAILER_POSTCR:
264 /* We enter this state when a CR should arrive so we expect to
265 have to first pass a CR before we wait for LF */
266 if((*datap != 0x0d) && (*datap != 0x0a)) {
267 /* not a CR then it must be another header in the trailer */
268 ch->state = CHUNK_TRAILER;
269 break;
270 }
271 if(*datap == 0x0d) {
272 /* skip if CR */
273 datap++;
274 length--;
275 }
276 /* now wait for the final LF */
277 ch->state = CHUNK_STOP;
278 break;
279
280 case CHUNK_STOP:
281 if(*datap == 0x0a) {
282 length--;
283
284 /* Record the length of any data left in the end of the buffer
285 even if there's no more chunks to read */
286 ch->datasize = curlx_sotouz(length);
287
288 return CHUNKE_STOP; /* return stop */
289 }
290 else
291 return CHUNKE_BAD_CHUNK;
292 }
293 }
294 return CHUNKE_OK;
295}
296
297const char *Curl_chunked_strerror(CHUNKcode code)
298{
299 switch(code) {
300 default:
301 return "OK";
302 case CHUNKE_TOO_LONG_HEX:
303 return "Too long hexadecimal number";
304 case CHUNKE_ILLEGAL_HEX:
305 return "Illegal or missing hexadecimal sequence";
306 case CHUNKE_BAD_CHUNK:
307 return "Malformed encoding found";
308 case CHUNKE_PASSTHRU_ERROR:
309 DEBUGASSERT(0); /* never used */
310 return "";
311 case CHUNKE_BAD_ENCODING:
312 return "Bad content-encoding found";
313 case CHUNKE_OUT_OF_MEMORY:
314 return "Out of memory";
315 }
316}
317
318#endif /* CURL_DISABLE_HTTP */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette