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
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-01-17 01:26:50 +03:00
|
|
|
* Copyright (C) 1998 - 2007, 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.
|
|
|
|
*
|
|
|
|
* $Id$
|
2002-09-03 15:52:59 +04:00
|
|
|
***************************************************************************/
|
2001-03-08 02:28:22 +03:00
|
|
|
#include "setup.h"
|
|
|
|
|
2002-06-11 15:13:01 +04:00
|
|
|
#ifndef CURL_DISABLE_HTTP
|
2001-03-08 02:28:22 +03:00
|
|
|
/* -- WIN32 approved -- */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "urldata.h" /* it includes http_chunks.h */
|
|
|
|
#include "sendf.h" /* for the client write stuff */
|
|
|
|
|
2004-05-12 11:54:44 +04:00
|
|
|
#include "content_encoding.h"
|
2004-01-29 16:56:45 +03:00
|
|
|
#include "http.h"
|
2004-05-11 15:30:23 +04:00
|
|
|
#include "memory.h"
|
2007-01-14 17:57:51 +03:00
|
|
|
#include "easyif.h" /* for Curl_convert_to_network prototype */
|
2002-09-03 02:31:18 +04:00
|
|
|
|
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"
|
|
|
|
|
2005-03-04 03:14:45 +03:00
|
|
|
/*
|
2001-03-08 02:28:22 +03:00
|
|
|
* Chunk format (simplified):
|
|
|
|
*
|
|
|
|
* <HEX SIZE>[ chunk extension ] CRLF
|
2001-09-03 16:51:23 +04:00
|
|
|
* <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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_httpchunk_init(struct connectdata *conn)
|
|
|
|
{
|
2006-09-08 01:49:20 +04:00
|
|
|
struct Curl_chunker *chunk = &conn->data->reqdata.proto.http->chunk;
|
2001-03-08 02:28:22 +03:00
|
|
|
chunk->hexindex=0; /* start at 0 */
|
2001-03-08 02:51:41 +03:00
|
|
|
chunk->dataleft=0; /* no data left yet! */
|
2001-03-08 02:28:22 +03:00
|
|
|
chunk->state = CHUNK_HEX; /* we get hex first! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-03-08 02:51:41 +03:00
|
|
|
* 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.
|
2007-01-14 17:57:51 +03:00
|
|
|
*
|
|
|
|
* 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,
|
2004-03-04 18:25:06 +03:00
|
|
|
ssize_t datalen,
|
|
|
|
ssize_t *wrotep)
|
2001-03-08 02:28:22 +03:00
|
|
|
{
|
2003-08-04 02:18:14 +04:00
|
|
|
CURLcode result=CURLE_OK;
|
2006-09-08 01:49:20 +04:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct Curl_chunker *ch = &data->reqdata.proto.http->chunk;
|
|
|
|
struct Curl_transfer_keeper *k = &data->reqdata.keep;
|
2004-03-04 18:25:06 +03:00
|
|
|
size_t piece;
|
|
|
|
size_t length = (size_t)datalen;
|
|
|
|
size_t *wrote = (size_t *)wrotep;
|
|
|
|
|
|
|
|
*wrote = 0; /* nothing's written yet */
|
2001-03-08 02:28:22 +03:00
|
|
|
|
2007-02-13 00:13:47 +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*/
|
2007-07-11 02:45:01 +04:00
|
|
|
if (data->set.http_te_skip && !k->ignorebody)
|
2007-02-13 00:13:47 +03:00
|
|
|
Curl_client_write(conn, CLIENTWRITE_BODY, datap,datalen);
|
|
|
|
|
2001-03-08 02:28:22 +03:00
|
|
|
while(length) {
|
|
|
|
switch(ch->state) {
|
|
|
|
case CHUNK_HEX:
|
2007-01-14 17:57:51 +03:00
|
|
|
/* Check for an ASCII hex digit.
|
|
|
|
We avoid the use of isxdigit to accommodate non-ASCII hosts. */
|
|
|
|
if((*datap >= 0x30 && *datap <= 0x39) /* 0-9 */
|
|
|
|
|| (*datap >= 0x41 && *datap <= 0x46) /* A-F */
|
|
|
|
|| (*datap >= 0x61 && *datap <= 0x66)) { /* a-f */
|
2001-03-08 02:28:22 +03:00
|
|
|
if(ch->hexindex < MAXNUM_SIZE) {
|
|
|
|
ch->hexbuffer[ch->hexindex] = *datap;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
ch->hexindex++;
|
|
|
|
}
|
|
|
|
else {
|
2001-03-12 18:20:35 +03:00
|
|
|
return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
|
2001-03-08 02:28:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2001-03-12 18:20:35 +03:00
|
|
|
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;
|
2007-01-14 17:57:51 +03:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
|
|
|
/* convert to host encoding before calling strtoul */
|
|
|
|
result = Curl_convert_from_network(conn->data,
|
|
|
|
ch->hexbuffer,
|
|
|
|
ch->hexindex);
|
|
|
|
if(result != CURLE_OK) {
|
|
|
|
/* Curl_convert_from_network calls failf if unsuccessful */
|
|
|
|
/* Treat it as a bad hex character */
|
|
|
|
return(CHUNKE_ILLEGAL_HEX);
|
|
|
|
}
|
|
|
|
#endif /* CURL_DOES_CONVERSIONS */
|
2001-03-08 02:28:22 +03:00
|
|
|
ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
|
|
|
|
ch->state = CHUNK_POSTHEX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_POSTHEX:
|
2001-03-12 18:20:35 +03:00
|
|
|
/* In this state, we're waiting for CRLF to arrive. We support
|
|
|
|
this to allow so called chunk-extensions to show up here
|
|
|
|
before the CRLF comes. */
|
2007-01-14 17:57:51 +03:00
|
|
|
if(*datap == 0x0d)
|
2001-03-08 02:28:22 +03:00
|
|
|
ch->state = CHUNK_CR;
|
|
|
|
length--;
|
|
|
|
datap++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_CR:
|
|
|
|
/* waiting for the LF */
|
2007-01-14 17:57:51 +03:00
|
|
|
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) {
|
2005-07-12 22:15:34 +04:00
|
|
|
if (conn->bits.trailerHdrPresent!=TRUE) {
|
|
|
|
/* No Trailer: header found - revert to original Curl processing */
|
2007-02-22 00:59:40 +03:00
|
|
|
ch->state = CHUNK_STOPCR;
|
|
|
|
|
|
|
|
/* We need to increment the datap here since we bypass the
|
|
|
|
increment below with the immediate break */
|
|
|
|
length--;
|
|
|
|
datap++;
|
|
|
|
|
|
|
|
/* This is the final byte, continue to read the final CRLF */
|
|
|
|
break;
|
2005-07-12 22:15:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ch->state = CHUNK_TRAILER; /* attempt to read trailers */
|
|
|
|
conn->trlPos=0;
|
2001-03-08 02:28:22 +03:00
|
|
|
}
|
|
|
|
}
|
2007-02-22 00:59:40 +03:00
|
|
|
else {
|
2001-03-08 02:28:22 +03:00
|
|
|
ch->state = CHUNK_DATA;
|
2007-02-22 00:59:40 +03:00
|
|
|
}
|
2001-03-08 02:28:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* previously we got a fake CR, go back to CR waiting! */
|
|
|
|
ch->state = CHUNK_CR;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_DATA:
|
|
|
|
/* we get pure and fine data
|
|
|
|
|
|
|
|
We expect another 'datasize' of data. We have 'length' right now,
|
|
|
|
it can be more or less than 'datasize'. Get the smallest piece.
|
|
|
|
*/
|
|
|
|
piece = (ch->datasize >= length)?length:ch->datasize;
|
|
|
|
|
|
|
|
/* Write the data portion available */
|
2002-09-03 02:31:18 +04:00
|
|
|
#ifdef HAVE_LIBZ
|
2007-02-13 00:13:47 +03:00
|
|
|
switch (conn->data->set.http_ce_skip?
|
|
|
|
IDENTITY : data->reqdata.keep.content_encoding) {
|
2002-09-03 02:31:18 +04:00
|
|
|
case IDENTITY:
|
|
|
|
#endif
|
2007-02-13 00:13:47 +03:00
|
|
|
if(!k->ignorebody) {
|
|
|
|
if ( !data->set.http_te_skip )
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
|
|
|
|
piece);
|
|
|
|
else
|
|
|
|
result = CURLE_OK;
|
|
|
|
}
|
2002-09-03 02:31:18 +04:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
break;
|
|
|
|
|
2005-03-04 03:14:45 +03:00
|
|
|
case DEFLATE:
|
2006-09-08 01:49:20 +04:00
|
|
|
/* update data->reqdata.keep.str to point to the chunk data. */
|
|
|
|
data->reqdata.keep.str = datap;
|
|
|
|
result = Curl_unencode_deflate_write(conn, &data->reqdata.keep,
|
2004-05-12 11:54:44 +04:00
|
|
|
(ssize_t)piece);
|
2002-09-03 02:31:18 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP:
|
2006-09-08 01:49:20 +04:00
|
|
|
/* update data->reqdata.keep.str to point to the chunk data. */
|
|
|
|
data->reqdata.keep.str = datap;
|
|
|
|
result = Curl_unencode_gzip_write(conn, &data->reqdata.keep,
|
2004-05-12 11:54:44 +04:00
|
|
|
(ssize_t)piece);
|
2003-04-11 12:49:20 +04:00
|
|
|
break;
|
|
|
|
|
2002-09-03 02:31:18 +04:00
|
|
|
case COMPRESS:
|
|
|
|
default:
|
|
|
|
failf (conn->data,
|
|
|
|
"Unrecognized content encoding type. "
|
2003-04-23 02:33:39 +04:00
|
|
|
"libcurl understands `identity', `deflate' and `gzip' "
|
2002-09-03 02:31:18 +04:00
|
|
|
"content encodings.");
|
|
|
|
return CHUNKE_BAD_ENCODING;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-03-08 02:28:22 +03:00
|
|
|
if(result)
|
|
|
|
return CHUNKE_WRITE_ERROR;
|
2004-03-04 18:25:06 +03:00
|
|
|
|
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)
|
2001-03-14 01:16:42 +03:00
|
|
|
/* end of data this round, we now expect a trailing CRLF */
|
|
|
|
ch->state = CHUNK_POSTCR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_POSTCR:
|
2007-01-14 17:57:51 +03:00
|
|
|
if(*datap == 0x0d) {
|
2001-03-14 01:16:42 +03:00
|
|
|
ch->state = CHUNK_POSTLF;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
2007-02-22 00:59:40 +03:00
|
|
|
else {
|
2001-03-14 01:16:42 +03:00
|
|
|
return CHUNKE_BAD_CHUNK;
|
2007-02-22 00:59:40 +03:00
|
|
|
}
|
2001-03-14 01:16:42 +03:00
|
|
|
break;
|
2001-03-08 02:28:22 +03:00
|
|
|
|
2001-03-14 01:16:42 +03:00
|
|
|
case CHUNK_POSTLF:
|
2007-01-14 17:57:51 +03:00
|
|
|
if(*datap == 0x0a) {
|
2001-03-14 01:16:42 +03:00
|
|
|
/*
|
|
|
|
* The last one before we go back to hex state and start all
|
|
|
|
* over.
|
|
|
|
*/
|
|
|
|
Curl_httpchunk_init(conn);
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
2007-02-22 00:59:40 +03:00
|
|
|
else {
|
2001-03-14 01:16:42 +03:00
|
|
|
return CHUNKE_BAD_CHUNK;
|
2007-02-22 00:59:40 +03:00
|
|
|
}
|
|
|
|
|
2001-03-08 02:28:22 +03:00
|
|
|
break;
|
2001-03-14 01:16:42 +03:00
|
|
|
|
2005-07-12 22:15:34 +04:00
|
|
|
case CHUNK_TRAILER:
|
|
|
|
/* conn->trailer is assumed to be freed in url.c on a
|
|
|
|
connection basis */
|
|
|
|
if (conn->trlPos >= conn->trlMax) {
|
|
|
|
char *ptr;
|
|
|
|
if(conn->trlMax) {
|
|
|
|
conn->trlMax *= 2;
|
|
|
|
ptr = (char*)realloc(conn->trailer,conn->trlMax);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
conn->trlMax=128;
|
|
|
|
ptr = (char*)malloc(conn->trlMax);
|
|
|
|
}
|
|
|
|
if(!ptr)
|
|
|
|
return CHUNKE_OUT_OF_MEMORY;
|
|
|
|
conn->trailer = ptr;
|
|
|
|
}
|
|
|
|
conn->trailer[conn->trlPos++]=*datap;
|
|
|
|
|
2007-01-14 17:57:51 +03:00
|
|
|
if(*datap == 0x0d)
|
2005-07-12 22:15:34 +04:00
|
|
|
ch->state = CHUNK_TRAILER_CR;
|
|
|
|
else {
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_TRAILER_CR:
|
2007-01-14 17:57:51 +03:00
|
|
|
if(*datap == 0x0d) {
|
2005-07-12 22:15:34 +04:00
|
|
|
ch->state = CHUNK_TRAILER_POSTCR;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return CHUNKE_BAD_CHUNK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_TRAILER_POSTCR:
|
2007-01-14 17:57:51 +03:00
|
|
|
if (*datap == 0x0a) {
|
|
|
|
conn->trailer[conn->trlPos++]=0x0a;
|
2005-07-12 22:15:34 +04:00
|
|
|
conn->trailer[conn->trlPos]=0;
|
|
|
|
if (conn->trlPos==2) {
|
|
|
|
ch->state = CHUNK_STOP;
|
2007-02-22 00:59:40 +03:00
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that this case skips over the final STOP states since we've
|
|
|
|
* already read the final CRLF and need to return
|
|
|
|
*/
|
|
|
|
|
|
|
|
ch->dataleft = length;
|
|
|
|
|
|
|
|
return CHUNKE_STOP; /* return stop */
|
2005-07-12 22:15:34 +04:00
|
|
|
}
|
|
|
|
else {
|
2007-01-14 17:57:51 +03:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
2007-01-17 01:26:50 +03:00
|
|
|
/* Convert to host encoding before calling Curl_client_write */
|
|
|
|
result = Curl_convert_from_network(conn->data,
|
|
|
|
conn->trailer,
|
|
|
|
conn->trlPos);
|
|
|
|
if(result != CURLE_OK) {
|
|
|
|
/* Curl_convert_from_network calls failf if unsuccessful */
|
|
|
|
/* Treat it as a bad chunk */
|
|
|
|
return(CHUNKE_BAD_CHUNK);
|
|
|
|
}
|
2007-01-14 17:57:51 +03:00
|
|
|
#endif /* CURL_DOES_CONVERSIONS */
|
2007-02-13 00:13:47 +03:00
|
|
|
if ( !data->set.http_te_skip )
|
2007-02-22 00:59:40 +03:00
|
|
|
Curl_client_write(conn, CLIENTWRITE_HEADER,
|
|
|
|
conn->trailer, conn->trlPos);
|
2005-07-12 22:15:34 +04:00
|
|
|
}
|
|
|
|
ch->state = CHUNK_TRAILER;
|
|
|
|
conn->trlPos=0;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return CHUNKE_BAD_CHUNK;
|
|
|
|
break;
|
|
|
|
|
2007-02-22 00:59:40 +03:00
|
|
|
case CHUNK_STOPCR:
|
|
|
|
/* Read the final CRLF that ends all chunk bodies */
|
|
|
|
|
|
|
|
if(*datap == 0x0d) {
|
|
|
|
ch->state = CHUNK_STOP;
|
|
|
|
datap++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CHUNKE_BAD_CHUNK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-03-08 02:28:22 +03:00
|
|
|
case CHUNK_STOP:
|
2007-02-22 00:59:40 +03:00
|
|
|
if (*datap == 0x0a) {
|
|
|
|
datap++;
|
|
|
|
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 = length;
|
|
|
|
return CHUNKE_STOP; /* return stop */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CHUNKE_BAD_CHUNK;
|
|
|
|
}
|
2007-02-22 09:22:19 +03:00
|
|
|
|
2007-02-22 00:59:40 +03:00
|
|
|
|
2001-03-08 02:28:22 +03:00
|
|
|
default:
|
|
|
|
return CHUNKE_STATE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CHUNKE_OK;
|
|
|
|
}
|
2002-06-11 15:13:01 +04:00
|
|
|
#endif /* CURL_DISABLE_HTTP */
|