liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame^] | 1 | #ifndef _MBTK_HTTP_CHUNKS_H |
| 2 | #define _MBTK_HTTP_CHUNKS_H |
| 3 | #include "mbtk_type.h" |
| 4 | |
| 5 | /* |
| 6 | * The longest possible hexadecimal number we support in a chunked transfer. |
| 7 | * Weird enough, RFC2616 doesn't set a maximum size! Since we use strtoul() |
| 8 | * to convert it, we "only" support 2^32 bytes chunk data. |
| 9 | */ |
| 10 | #define MAXNUM_SIZE 16 |
| 11 | |
| 12 | typedef enum { |
| 13 | /* await and buffer all hexadecimal digits until we get one that isn't a |
| 14 | hexadecimal digit. When done, we go CHUNK_LF */ |
| 15 | CHUNK_HEX, |
| 16 | |
| 17 | /* wait for LF, ignore all else */ |
| 18 | CHUNK_LF, |
| 19 | |
| 20 | /* We eat the amount of data specified. When done, we move on to the |
| 21 | POST_CR state. */ |
| 22 | CHUNK_DATA, |
| 23 | |
| 24 | /* POSTLF should get a CR and then a LF and nothing else, then move back to |
| 25 | HEX as the CRLF combination marks the end of a chunk. A missing CR is no |
| 26 | big deal. */ |
| 27 | CHUNK_POSTLF, |
| 28 | |
| 29 | /* At this point optional trailer headers can be found, unless the next line |
| 30 | is CRLF */ |
| 31 | CHUNK_TRAILER, |
| 32 | |
| 33 | /* Used to mark that we're out of the game. NOTE: that there's a 'dataleft' |
| 34 | field in the struct that will tell how many bytes that were not passed to |
| 35 | the client in the end of the last buffer! */ |
| 36 | CHUNK_STOP |
| 37 | } http_chunk_state; |
| 38 | |
| 39 | typedef enum { |
| 40 | CHUNKE_STOP = -1, |
| 41 | CHUNKE_OK = 0, |
| 42 | CHUNKE_TOO_LONG_HEX = 1, |
| 43 | CHUNKE_ILLEGAL_HEX, |
| 44 | CHUNKE_BAD_CHUNK, |
| 45 | CHUNKE_BAD_ENCODING, |
| 46 | CHUNKE_OUT_OF_MEMORY, |
| 47 | CHUNKE_PASSTHRU_ERROR, /* Curl_httpchunk_read() returns a CURLcode to use */ |
| 48 | CHUNKE_LAST |
| 49 | } http_chunk_code; |
| 50 | |
| 51 | typedef struct { |
| 52 | char hexbuffer[MAXNUM_SIZE + 1]; |
| 53 | int hexindex; |
| 54 | http_chunk_state state; |
| 55 | int datasize; |
| 56 | int dataleft; /* untouched data amount at the end of the last buffer */ |
| 57 | } http_chunker_t; |
| 58 | |
| 59 | /* The following functions are defined in http_chunks.c */ |
| 60 | void http_chunk_init(http_chunker_t *chunker); |
| 61 | http_chunk_code http_chunk_parse(http_chunker_t *chunker, char *src, int src_len, |
| 62 | char *dest,int *dest_len); |
| 63 | |
| 64 | #endif /* _MBTK_HTTP_CHUNKS_H */ |
| 65 | |
| 66 | |