curl/lib/http_chunks.c

386 строки
12 KiB
C
Исходник Обычный вид История

2002-09-03 15:52:59 +04:00
/***************************************************************************
2005-03-04 03:14:45 +03:00
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
2001-03-08 02:28:22 +03:00
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
2001-03-08 02:28:22 +03:00
*
2002-09-03 15:52:59 +04:00
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
2005-03-04 03:14:45 +03:00
*
2001-03-08 02:28:22 +03:00
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
2002-09-03 15:52:59 +04:00
* furnished to do so, under the terms of the COPYING file.
2001-03-08 02:28:22 +03:00
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
2002-09-03 15:52:59 +04:00
***************************************************************************/
build: fix circular header inclusion with other packages This commit renames lib/setup.h to lib/curl_setup.h and renames lib/setup_once.h to lib/curl_setup_once.h. Removes the need and usage of a header inclusion guard foreign to libcurl. [1] Removes the need and presence of an alarming notice we carried in old setup_once.h [2] ---------------------------------------- 1 - lib/setup_once.h used __SETUP_ONCE_H macro as header inclusion guard up to commit ec691ca3 which changed this to HEADER_CURL_SETUP_ONCE_H, this single inclusion guard is enough to ensure that inclusion of lib/setup_once.h done from lib/setup.h is only done once. Additionally lib/setup.h has always used __SETUP_ONCE_H macro to protect inclusion of setup_once.h even after commit ec691ca3, this was to avoid a circular header inclusion triggered when building a c-ares enabled version with c-ares sources available which also has a setup_once.h header. Commit ec691ca3 exposes the real nature of __SETUP_ONCE_H usage in lib/setup.h, it is a header inclusion guard foreign to libcurl belonging to c-ares's setup_once.h The renaming this commit does, fixes the circular header inclusion, and as such removes the need and usage of a header inclusion guard foreign to libcurl. Macro __SETUP_ONCE_H no longer used in libcurl. 2 - Due to the circular interdependency of old lib/setup_once.h and the c-ares setup_once.h header, old file lib/setup_once.h has carried back from 2006 up to now days an alarming and prominent notice about the need of keeping libcurl's and c-ares's setup_once.h in sync. Given that this commit fixes the circular interdependency, the need and presence of mentioned notice is removed. All mentioned interdependencies come back from now old days when the c-ares project lived inside a curl subdirectory. This commit removes last traces of such fact.
2013-01-06 22:06:49 +04:00
#include "curl_setup.h"
2001-03-08 02:28:22 +03:00
#ifndef CURL_DISABLE_HTTP
2001-03-08 02:28:22 +03:00
#include "urldata.h" /* it includes http_chunks.h */
#include "sendf.h" /* for the client write stuff */
2001-03-08 02:28:22 +03:00
#include "content_encoding.h"
#include "http.h"
#include "curl_memory.h"
#include "non-ascii.h" /* for Curl_convert_to_network prototype */
#include "strtoofft.h"
#include "warnless.h"
2001-03-08 02:28:22 +03:00
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
/* The last #include file should be: */
#include "memdebug.h"
2001-03-08 02:28:22 +03:00
2005-03-04 03:14:45 +03:00
/*
2001-03-08 02:28:22 +03:00
* Chunk format (simplified):
*
* <HEX SIZE>[ chunk extension ] CRLF
* <DATA> CRLF
2001-03-08 02:28:22 +03:00
*
* Highlights from RFC2616 section 3.6 say:
The chunked encoding modifies the body of a message in order to
transfer it as a series of chunks, each with its own size indicator,
followed by an OPTIONAL trailer containing entity-header fields. This
allows dynamically produced content to be transferred along with the
information necessary for the recipient to verify that it has
received the full message.
Chunked-Body = *chunk
last-chunk
trailer
CRLF
chunk = chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size = 1*HEX
last-chunk = 1*("0") [ chunk-extension ] CRLF
chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
chunk-data = chunk-size(OCTET)
trailer = *(entity-header CRLF)
The chunk-size field is a string of hex digits indicating the size of
the chunk. The chunked encoding is ended by any chunk whose size is
zero, followed by the trailer, which is terminated by an empty line.
*/
2008-10-08 03:20:06 +04:00
/* Check for an ASCII hex digit.
We avoid the use of isxdigit to accommodate non-ASCII hosts. */
static bool Curl_isxdigit(char digit)
{
return ( (digit >= 0x30 && digit <= 0x39) /* 0-9 */
|| (digit >= 0x41 && digit <= 0x46) /* A-F */
|| (digit >= 0x61 && digit <= 0x66) /* a-f */ ) ? TRUE : FALSE;
2008-10-08 03:20:06 +04:00
}
2001-03-08 02:28:22 +03:00
void Curl_httpchunk_init(struct connectdata *conn)
{
struct Curl_chunker *chunk = &conn->chunk;
chunk->hexindex=0; /* start at 0 */
chunk->dataleft=0; /* no data left yet! */
2001-03-08 02:28:22 +03:00
chunk->state = CHUNK_HEX; /* we get hex first! */
}
/*
* chunk_read() returns a OK for normal operations, or a positive return code
* for errors. STOP means this sequence of chunks is complete. The 'wrote'
* argument is set to tell the caller how many bytes we actually passed to the
* client (for byte-counting and whatever).
2001-03-08 02:28:22 +03:00
*
* The states and the state-machine is further explained in the header file.
*
* This function always uses ASCII hex values to accommodate non-ASCII hosts.
* For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
2001-03-08 02:28:22 +03:00
*/
CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
char *datap,
ssize_t datalen,
ssize_t *wrotep)
2001-03-08 02:28:22 +03:00
{
CURLcode result=CURLE_OK;
struct SessionHandle *data = conn->data;
struct Curl_chunker *ch = &conn->chunk;
struct SingleRequest *k = &data->req;
size_t piece;
curl_off_t length = (curl_off_t)datalen;
size_t *wrote = (size_t *)wrotep;
*wrote = 0; /* nothing's written yet */
2001-03-08 02:28:22 +03:00
/* the original data is written to the client, but we go on with the
chunk read process, to properly calculate the content length*/
if(data->set.http_te_skip && !k->ignorebody) {
result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
if(result)
return CHUNKE_WRITE_ERROR;
}
2001-03-08 02:28:22 +03:00
while(length) {
switch(ch->state) {
case CHUNK_HEX:
2008-10-08 03:20:06 +04:00
if(Curl_isxdigit(*datap)) {
2001-03-08 02:28:22 +03:00
if(ch->hexindex < MAXNUM_SIZE) {
ch->hexbuffer[ch->hexindex] = *datap;
datap++;
length--;
ch->hexindex++;
}
else {
return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
2001-03-08 02:28:22 +03:00
}
}
else {
char *endptr;
if(0 == ch->hexindex)
/* This is illegal data, we received junk where we expected
a hexadecimal digit. */
return CHUNKE_ILLEGAL_HEX;
2001-03-08 02:28:22 +03:00
/* length and datap are unmodified */
ch->hexbuffer[ch->hexindex]=0;
/* convert to host encoding before calling strtoul */
result = Curl_convert_from_network(conn->data, ch->hexbuffer,
ch->hexindex);
if(result) {
/* Curl_convert_from_network calls failf if unsuccessful */
/* Treat it as a bad hex character */
return CHUNKE_ILLEGAL_HEX ;
}
ch->datasize=curlx_strtoofft(ch->hexbuffer, &endptr, 16);
if((ch->datasize == CURL_OFF_T_MAX) && (errno == ERANGE))
/* overflow is an error */
return CHUNKE_ILLEGAL_HEX;
ch->state = CHUNK_LF; /* now wait for the CRLF */
2001-03-08 02:28:22 +03:00
}
break;
case CHUNK_LF:
/* waiting for the LF after a chunk size */
if(*datap == 0x0a) {
2001-03-08 02:28:22 +03:00
/* we're now expecting data to come, unless size was zero! */
if(0 == ch->datasize) {
ch->state = CHUNK_TRAILER; /* now check for trailers */
conn->trlPos=0;
2001-03-08 02:28:22 +03:00
}
else
2001-03-08 02:28:22 +03:00
ch->state = CHUNK_DATA;
}
2001-03-08 02:28:22 +03:00
datap++;
length--;
break;
case CHUNK_DATA:
/* We expect 'datasize' of data. We have 'length' right now, it can be
more or less than 'datasize'. Get the smallest piece.
2001-03-08 02:28:22 +03:00
*/
piece = curlx_sotouz((ch->datasize >= length)?length:ch->datasize);
2001-03-08 02:28:22 +03:00
/* Write the data portion available */
#ifdef HAVE_LIBZ
switch (conn->data->set.http_ce_skip?
IDENTITY : data->req.auto_decoding) {
case IDENTITY:
#endif
if(!k->ignorebody) {
if(!data->set.http_te_skip)
result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
piece);
else
result = CURLE_OK;
}
#ifdef HAVE_LIBZ
break;
case DEFLATE:
/* update data->req.keep.str to point to the chunk data. */
data->req.str = datap;
result = Curl_unencode_deflate_write(conn, &data->req,
(ssize_t)piece);
break;
case GZIP:
/* update data->req.keep.str to point to the chunk data. */
data->req.str = datap;
result = Curl_unencode_gzip_write(conn, &data->req,
(ssize_t)piece);
break;
case COMPRESS:
default:
failf (conn->data,
"Unrecognized content encoding type. "
"libcurl understands `identity', `deflate' and `gzip' "
"content encodings.");
return CHUNKE_BAD_ENCODING;
}
#endif
2001-03-08 02:28:22 +03:00
if(result)
return CHUNKE_WRITE_ERROR;
2001-03-08 02:28:22 +03:00
*wrote += piece;
ch->datasize -= piece; /* decrease amount left to expect */
datap += piece; /* move read pointer forward */
length -= piece; /* decrease space left in this round */
if(0 == ch->datasize)
/* end of data this round, we now expect a trailing CRLF */
ch->state = CHUNK_POSTLF;
break;
2001-03-08 02:28:22 +03:00
case CHUNK_POSTLF:
if(*datap == 0x0a) {
/* The last one before we go back to hex state and start all over. */
Curl_httpchunk_init(conn); /* sets state back to CHUNK_HEX */
}
else if(*datap != 0x0d)
return CHUNKE_BAD_CHUNK;
datap++;
length--;
2001-03-08 02:28:22 +03:00
break;
case CHUNK_TRAILER:
if((*datap == 0x0d) || (*datap == 0x0a)) {
/* this is the end of a trailer, but if the trailer was zero bytes
there was no trailer and we move on */
if(conn->trlPos) {
/* we allocate trailer with 3 bytes extra room to fit this */
conn->trailer[conn->trlPos++]=0x0d;
conn->trailer[conn->trlPos++]=0x0a;
conn->trailer[conn->trlPos]=0;
/* Convert to host encoding before calling Curl_client_write */
result = Curl_convert_from_network(conn->data, conn->trailer,
conn->trlPos);
if(result)
/* Curl_convert_from_network calls failf if unsuccessful */
/* Treat it as a bad chunk */
return CHUNKE_BAD_CHUNK;
if(!data->set.http_te_skip) {
result = Curl_client_write(conn, CLIENTWRITE_HEADER,
conn->trailer, conn->trlPos);
if(result)
return CHUNKE_WRITE_ERROR;
}
conn->trlPos=0;
ch->state = CHUNK_TRAILER_CR;
if(*datap == 0x0a)
/* already on the LF */
break;
}
else {
/* no trailer, we're on the final CRLF pair */
ch->state = CHUNK_TRAILER_POSTCR;
break; /* don't advance the pointer */
}
}
else {
/* conn->trailer is assumed to be freed in url.c on a
connection basis */
if(conn->trlPos >= conn->trlMax) {
/* we always allocate three extra bytes, just because when the full
header has been received we append CRLF\0 */
char *ptr;
if(conn->trlMax) {
conn->trlMax *= 2;
ptr = realloc(conn->trailer, conn->trlMax + 3);
}
else {
conn->trlMax=128;
ptr = malloc(conn->trlMax + 3);
}
if(!ptr)
return CHUNKE_OUT_OF_MEMORY;
conn->trailer = ptr;
}
conn->trailer[conn->trlPos++]=*datap;
}
datap++;
length--;
break;
case CHUNK_TRAILER_CR:
if(*datap == 0x0a) {
ch->state = CHUNK_TRAILER_POSTCR;
datap++;
length--;
}
else
return CHUNKE_BAD_CHUNK;
break;
case CHUNK_TRAILER_POSTCR:
/* We enter this state when a CR should arrive so we expect to
have to first pass a CR before we wait for LF */
if((*datap != 0x0d) && (*datap != 0x0a)) {
/* not a CR then it must be another header in the trailer */
ch->state = CHUNK_TRAILER;
break;
}
if(*datap == 0x0d) {
/* skip if CR */
datap++;
length--;
}
/* now wait for the final LF */
ch->state = CHUNK_STOP;
break;
2001-03-08 02:28:22 +03:00
case CHUNK_STOP:
if(*datap == 0x0a) {
length--;
/* Record the length of any data left in the end of the buffer
even if there's no more chunks to read */
ch->dataleft = curlx_sotouz(length);
return CHUNKE_STOP; /* return stop */
}
else
return CHUNKE_BAD_CHUNK;
2001-03-08 02:28:22 +03:00
}
}
return CHUNKE_OK;
}
const char *Curl_chunked_strerror(CHUNKcode code)
{
switch (code) {
default:
return "OK";
case CHUNKE_TOO_LONG_HEX:
return "Too long hexadecimal number";
case CHUNKE_ILLEGAL_HEX:
return "Illegal or missing hexadecimal sequence";
case CHUNKE_BAD_CHUNK:
return "Malformed encoding found";
case CHUNKE_WRITE_ERROR:
return "Write error";
case CHUNKE_BAD_ENCODING:
return "Bad content-encoding found";
case CHUNKE_OUT_OF_MEMORY:
return "Out of memory";
}
}
#endif /* CURL_DISABLE_HTTP */