1 #ifndef HEADER_CURL_HTTP_CHUNKS_H
2 #define HEADER_CURL_HTTP_CHUNKS_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.haxx.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  ***************************************************************************/
24 
25 struct connectdata;
26 
27 /*
28  * The longest possible hexadecimal number we support in a chunked transfer.
29  * Weird enough, RFC2616 doesn't set a maximum size! Since we use strtoul()
30  * to convert it, we "only" support 2^32 bytes chunk data.
31  */
32 #define MAXNUM_SIZE 16
33 
34 typedef enum {
35   /* await and buffer all hexadecimal digits until we get one that isn't a
36      hexadecimal digit. When done, we go CHUNK_LF */
37   CHUNK_HEX,
38 
39   /* wait for LF, ignore all else */
40   CHUNK_LF,
41 
42   /* We eat the amount of data specified. When done, we move on to the
43      POST_CR state. */
44   CHUNK_DATA,
45 
46   /* POSTLF should get a CR and then a LF and nothing else, then move back to
47      HEX as the CRLF combination marks the end of a chunk. A missing CR is no
48      big deal. */
49   CHUNK_POSTLF,
50 
51   /* Used to mark that we're out of the game.  NOTE: that there's a 'dataleft'
52      field in the struct that will tell how many bytes that were not passed to
53      the client in the end of the last buffer! */
54   CHUNK_STOP,
55 
56   /* At this point optional trailer headers can be found, unless the next line
57      is CRLF */
58   CHUNK_TRAILER,
59 
60   /* A trailer CR has been found - next state is CHUNK_TRAILER_POSTCR.
61      Next char must be a LF */
62   CHUNK_TRAILER_CR,
63 
64   /* A trailer LF must be found now, otherwise CHUNKE_BAD_CHUNK will be
65      signalled If this is an empty trailer CHUNKE_STOP will be signalled.
66      Otherwise the trailer will be broadcasted via Curl_client_write() and the
67      next state will be CHUNK_TRAILER */
68   CHUNK_TRAILER_POSTCR
69 } ChunkyState;
70 
71 typedef enum {
72   CHUNKE_STOP = -1,
73   CHUNKE_OK = 0,
74   CHUNKE_TOO_LONG_HEX = 1,
75   CHUNKE_ILLEGAL_HEX,
76   CHUNKE_BAD_CHUNK,
77   CHUNKE_BAD_ENCODING,
78   CHUNKE_OUT_OF_MEMORY,
79   CHUNKE_PASSTHRU_ERROR, /* Curl_httpchunk_read() returns a CURLcode to use */
80   CHUNKE_LAST
81 } CHUNKcode;
82 
83 const char *Curl_chunked_strerror(CHUNKcode code);
84 
85 struct Curl_chunker {
86   char hexbuffer[ MAXNUM_SIZE + 1];
87   int hexindex;
88   ChunkyState state;
89   curl_off_t datasize;
90   size_t dataleft; /* untouched data amount at the end of the last buffer */
91 };
92 
93 /* The following functions are defined in http_chunks.c */
94 void Curl_httpchunk_init(struct connectdata *conn);
95 CHUNKcode Curl_httpchunk_read(struct connectdata *conn, char *datap,
96                               ssize_t length, ssize_t *wrote,
97                               CURLcode *passthru);
98 
99 #endif /* HEADER_CURL_HTTP_CHUNKS_H */
100