Full rewrite

This commit is contained in:
2025-05-07 23:57:07 +02:00
parent 82ef7ee170
commit 24736bc780
23 changed files with 4737 additions and 5791 deletions
-685
View File
@@ -1,685 +0,0 @@
/*
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
* Shigeo Mitsunari
*
* The software is licensed under either the MIT License (below) or the Perl
* license.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <stddef.h>
#include <string.h>
#ifdef __SSE4_2__
#ifdef _MSC_VER
#include <nmmintrin.h>
#else
#include <x86intrin.h>
#endif
#endif
#include "picohttpparser.h"
#if __GNUC__ >= 3
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#ifdef _MSC_VER
#define ALIGNED(n) _declspec(align(n))
#else
#define ALIGNED(n) __attribute__((aligned(n)))
#endif
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
#define CHECK_EOF() \
if (buf == buf_end) { \
*ret = -2; \
return NULL; \
}
#define EXPECT_CHAR_NO_CHECK(ch) \
if (*buf++ != ch) { \
*ret = -1; \
return NULL; \
}
#define EXPECT_CHAR(ch) \
CHECK_EOF(); \
EXPECT_CHAR_NO_CHECK(ch);
#define ADVANCE_TOKEN(tok, toklen) \
do { \
const char *tok_start = buf; \
static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
int found2; \
buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
if (!found2) { \
CHECK_EOF(); \
} \
while (1) { \
if (*buf == ' ') { \
break; \
} else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
*ret = -1; \
return NULL; \
} \
} \
++buf; \
CHECK_EOF(); \
} \
tok = tok_start; \
toklen = buf - tok_start; \
} while (0)
static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
{
*found = 0;
#if __SSE4_2__
if (likely(buf_end - buf >= 16)) {
__m128i ranges16 = _mm_loadu_si128((const __m128i *)ranges);
size_t left = (buf_end - buf) & ~15;
do {
__m128i b16 = _mm_loadu_si128((const __m128i *)buf);
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
if (unlikely(r != 16)) {
buf += r;
*found = 1;
break;
}
buf += 16;
left -= 16;
} while (likely(left != 0));
}
#else
/* suppress unused parameter warning */
(void)buf_end;
(void)ranges;
(void)ranges_size;
#endif
return buf;
}
static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
{
const char *token_start = buf;
#ifdef __SSE4_2__
static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */
"\012\037" /* allow SP and up to but not including DEL */
"\177\177"; /* allow chars w. MSB set */
int found;
buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
if (found)
goto FOUND_CTL;
#else
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
while (likely(buf_end - buf >= 8)) {
#define DOIT() \
do { \
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
goto NonPrintable; \
++buf; \
} while (0)
DOIT();
DOIT();
DOIT();
DOIT();
DOIT();
DOIT();
DOIT();
DOIT();
#undef DOIT
continue;
NonPrintable:
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
goto FOUND_CTL;
}
++buf;
}
#endif
for (;; ++buf) {
CHECK_EOF();
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
goto FOUND_CTL;
}
}
}
FOUND_CTL:
if (likely(*buf == '\015')) {
++buf;
EXPECT_CHAR('\012');
*token_len = buf - 2 - token_start;
} else if (*buf == '\012') {
*token_len = buf - token_start;
++buf;
} else {
*ret = -1;
return NULL;
}
*token = token_start;
return buf;
}
static const char *is_complete(const char *buf, const char *buf_end, size_t last_len, int *ret)
{
int ret_cnt = 0;
buf = last_len < 3 ? buf : buf + last_len - 3;
while (1) {
CHECK_EOF();
if (*buf == '\015') {
++buf;
CHECK_EOF();
EXPECT_CHAR('\012');
++ret_cnt;
} else if (*buf == '\012') {
++buf;
++ret_cnt;
} else {
++buf;
ret_cnt = 0;
}
if (ret_cnt == 2) {
return buf;
}
}
*ret = -2;
return NULL;
}
#define PARSE_INT(valp_, mul_) \
if (*buf < '0' || '9' < *buf) { \
buf++; \
*ret = -1; \
return NULL; \
} \
*(valp_) = (mul_) * (*buf++ - '0');
#define PARSE_INT_3(valp_) \
do { \
int res_ = 0; \
PARSE_INT(&res_, 100) \
*valp_ = res_; \
PARSE_INT(&res_, 10) \
*valp_ += res_; \
PARSE_INT(&res_, 1) \
*valp_ += res_; \
} while (0)
/* returned pointer is always within [buf, buf_end), or null */
static const char *parse_token(const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char,
int *ret)
{
/* We use pcmpestri to detect non-token characters. This instruction can take no more than eight character ranges (8*2*8=128
* bits that is the size of a SSE register). Due to this restriction, characters `|` and `~` are handled in the slow loop. */
static const char ALIGNED(16) ranges[] = "\x00 " /* control chars and up to SP */
"\"\"" /* 0x22 */
"()" /* 0x28,0x29 */
",," /* 0x2c */
"//" /* 0x2f */
":@" /* 0x3a-0x40 */
"[]" /* 0x5b-0x5d */
"{\xff"; /* 0x7b-0xff */
const char *buf_start = buf;
int found;
buf = findchar_fast(buf, buf_end, ranges, sizeof(ranges) - 1, &found);
if (!found) {
CHECK_EOF();
}
while (1) {
if (*buf == next_char) {
break;
} else if (!token_char_map[(unsigned char)*buf]) {
*ret = -1;
return NULL;
}
++buf;
CHECK_EOF();
}
*token = buf_start;
*token_len = buf - buf_start;
return buf;
}
/* returned pointer is always within [buf, buf_end), or null */
static const char *parse_http_version(const char *buf, const char *buf_end, int *minor_version, int *ret)
{
/* we want at least [HTTP/1.<two chars>] to try to parse */
if (buf_end - buf < 9) {
*ret = -2;
return NULL;
}
EXPECT_CHAR_NO_CHECK('H');
EXPECT_CHAR_NO_CHECK('T');
EXPECT_CHAR_NO_CHECK('T');
EXPECT_CHAR_NO_CHECK('P');
EXPECT_CHAR_NO_CHECK('/');
EXPECT_CHAR_NO_CHECK('1');
EXPECT_CHAR_NO_CHECK('.');
PARSE_INT(minor_version, 1);
return buf;
}
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
size_t max_headers, int *ret)
{
for (;; ++*num_headers) {
CHECK_EOF();
if (*buf == '\015') {
++buf;
EXPECT_CHAR('\012');
break;
} else if (*buf == '\012') {
++buf;
break;
}
if (*num_headers == max_headers) {
*ret = -1;
return NULL;
}
if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
/* parsing name, but do not discard SP before colon, see
* http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name, &headers[*num_headers].name_len, ':', ret)) == NULL) {
return NULL;
}
if (headers[*num_headers].name_len == 0) {
*ret = -1;
return NULL;
}
++buf;
for (;; ++buf) {
CHECK_EOF();
if (!(*buf == ' ' || *buf == '\t')) {
break;
}
}
} else {
headers[*num_headers].name = NULL;
headers[*num_headers].name_len = 0;
}
const char *value;
size_t value_len;
if ((buf = get_token_to_eol(buf, buf_end, &value, &value_len, ret)) == NULL) {
return NULL;
}
/* remove trailing SPs and HTABs */
const char *value_end = value + value_len;
for (; value_end != value; --value_end) {
const char c = *(value_end - 1);
if (!(c == ' ' || c == '\t')) {
break;
}
}
headers[*num_headers].value = value;
headers[*num_headers].value_len = value_end - value;
}
return buf;
}
static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path,
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers,
size_t max_headers, int *ret)
{
/* skip first empty line (some clients add CRLF after POST content) */
CHECK_EOF();
if (*buf == '\015') {
++buf;
EXPECT_CHAR('\012');
} else if (*buf == '\012') {
++buf;
}
/* parse request line */
if ((buf = parse_token(buf, buf_end, method, method_len, ' ', ret)) == NULL) {
return NULL;
}
do {
++buf;
CHECK_EOF();
} while (*buf == ' ');
ADVANCE_TOKEN(*path, *path_len);
do {
++buf;
CHECK_EOF();
} while (*buf == ' ');
if (*method_len == 0 || *path_len == 0) {
*ret = -1;
return NULL;
}
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
return NULL;
}
if (*buf == '\015') {
++buf;
EXPECT_CHAR('\012');
} else if (*buf == '\012') {
++buf;
} else {
*ret = -1;
return NULL;
}
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
}
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
{
const char *buf = buf_start, *buf_end = buf_start + len;
size_t max_headers = *num_headers;
int r;
*method = NULL;
*method_len = 0;
*path = NULL;
*path_len = 0;
*minor_version = -1;
*num_headers = 0;
/* if last_len != 0, check if the request is complete (a fast countermeasure
againt slowloris */
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
return r;
}
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers,
&r)) == NULL) {
return r;
}
return (int)(buf - buf_start);
}
static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg,
size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
{
/* parse "HTTP/1.x" */
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
return NULL;
}
/* skip space */
if (*buf != ' ') {
*ret = -1;
return NULL;
}
do {
++buf;
CHECK_EOF();
} while (*buf == ' ');
/* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
if (buf_end - buf < 4) {
*ret = -2;
return NULL;
}
PARSE_INT_3(status);
/* get message including preceding space */
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
return NULL;
}
if (*msg_len == 0) {
/* ok */
} else if (**msg == ' ') {
/* Remove preceding space. Successful return from `get_token_to_eol` guarantees that we would hit something other than SP
* before running past the end of the given buffer. */
do {
++*msg;
--*msg_len;
} while (**msg == ' ');
} else {
/* garbage found after status code */
*ret = -1;
return NULL;
}
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
}
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
struct phr_header *headers, size_t *num_headers, size_t last_len)
{
const char *buf = buf_start, *buf_end = buf + len;
size_t max_headers = *num_headers;
int r;
*minor_version = -1;
*status = 0;
*msg = NULL;
*msg_len = 0;
*num_headers = 0;
/* if last_len != 0, check if the response is complete (a fast countermeasure
against slowloris */
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
return r;
}
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == NULL) {
return r;
}
return (int)(buf - buf_start);
}
int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
{
const char *buf = buf_start, *buf_end = buf + len;
size_t max_headers = *num_headers;
int r;
*num_headers = 0;
/* if last_len != 0, check if the response is complete (a fast countermeasure
against slowloris */
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
return r;
}
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
return r;
}
return (int)(buf - buf_start);
}
enum {
CHUNKED_IN_CHUNK_SIZE,
CHUNKED_IN_CHUNK_EXT,
CHUNKED_IN_CHUNK_DATA,
CHUNKED_IN_CHUNK_CRLF,
CHUNKED_IN_TRAILERS_LINE_HEAD,
CHUNKED_IN_TRAILERS_LINE_MIDDLE
};
static int decode_hex(int ch)
{
if ('0' <= ch && ch <= '9') {
return ch - '0';
} else if ('A' <= ch && ch <= 'F') {
return ch - 'A' + 0xa;
} else if ('a' <= ch && ch <= 'f') {
return ch - 'a' + 0xa;
} else {
return -1;
}
}
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *_bufsz)
{
size_t dst = 0, src = 0, bufsz = *_bufsz;
ssize_t ret = -2; /* incomplete */
decoder->_total_read += bufsz;
while (1) {
switch (decoder->_state) {
case CHUNKED_IN_CHUNK_SIZE:
for (;; ++src) {
int v;
if (src == bufsz)
goto Exit;
if ((v = decode_hex(buf[src])) == -1) {
if (decoder->_hex_count == 0) {
ret = -1;
goto Exit;
}
/* the only characters that may appear after the chunk size are BWS, semicolon, or CRLF */
switch (buf[src]) {
case ' ':
case '\011':
case ';':
case '\012':
case '\015':
break;
default:
ret = -1;
goto Exit;
}
break;
}
if (decoder->_hex_count == sizeof(size_t) * 2) {
ret = -1;
goto Exit;
}
decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
++decoder->_hex_count;
}
decoder->_hex_count = 0;
decoder->_state = CHUNKED_IN_CHUNK_EXT;
/* fallthru */
case CHUNKED_IN_CHUNK_EXT:
/* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] == '\012')
break;
}
++src;
if (decoder->bytes_left_in_chunk == 0) {
if (decoder->consume_trailer) {
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
break;
} else {
goto Complete;
}
}
decoder->_state = CHUNKED_IN_CHUNK_DATA;
/* fallthru */
case CHUNKED_IN_CHUNK_DATA: {
size_t avail = bufsz - src;
if (avail < decoder->bytes_left_in_chunk) {
if (dst != src)
memmove(buf + dst, buf + src, avail);
src += avail;
dst += avail;
decoder->bytes_left_in_chunk -= avail;
goto Exit;
}
if (dst != src)
memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
src += decoder->bytes_left_in_chunk;
dst += decoder->bytes_left_in_chunk;
decoder->bytes_left_in_chunk = 0;
decoder->_state = CHUNKED_IN_CHUNK_CRLF;
}
/* fallthru */
case CHUNKED_IN_CHUNK_CRLF:
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] != '\015')
break;
}
if (buf[src] != '\012') {
ret = -1;
goto Exit;
}
++src;
decoder->_state = CHUNKED_IN_CHUNK_SIZE;
break;
case CHUNKED_IN_TRAILERS_LINE_HEAD:
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] != '\015')
break;
}
if (buf[src++] == '\012')
goto Complete;
decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
/* fallthru */
case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] == '\012')
break;
}
++src;
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
break;
default:
assert(!"decoder is corrupt");
}
}
Complete:
ret = bufsz - src;
Exit:
if (dst != src)
memmove(buf + dst, buf + src, bufsz - src);
*_bufsz = dst;
/* if incomplete but the overhead of the chunked encoding is >=100KB and >80%, signal an error */
if (ret == -2) {
decoder->_total_overhead += bufsz - dst;
if (decoder->_total_overhead >= 100 * 1024 && decoder->_total_read - decoder->_total_overhead < decoder->_total_read / 4)
ret = -1;
}
return ret;
}
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder)
{
return decoder->_state == CHUNKED_IN_CHUNK_DATA;
}
#undef CHECK_EOF
#undef EXPECT_CHAR
#undef ADVANCE_TOKEN
-90
View File
@@ -1,90 +0,0 @@
/*
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
* Shigeo Mitsunari
*
* The software is licensed under either the MIT License (below) or the Perl
* license.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef picohttpparser_h
#define picohttpparser_h
#include <stdint.h>
#include <sys/types.h>
#ifdef _MSC_VER
#define ssize_t intptr_t
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* contains name and value of a header (name == NULL if is a continuing line
* of a multiline header */
struct phr_header {
const char *name;
size_t name_len;
const char *value;
size_t value_len;
};
/* returns number of bytes consumed if successful, -2 if request is partial,
* -1 if failed */
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
/* ditto */
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
struct phr_header *headers, size_t *num_headers, size_t last_len);
/* ditto */
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
/* should be zero-filled before start */
struct phr_chunked_decoder {
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
char consume_trailer; /* if trailing headers should be consumed */
char _hex_count;
char _state;
uint64_t _total_read;
uint64_t _total_overhead;
};
/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
* encoding headers. When the function returns without an error, bufsz is
* updated to the length of the decoded data available. Applications should
* repeatedly call the function while it returns -2 (incomplete) every time
* supplying newly arrived data. If the end of the chunked-encoded data is
* found, the function returns a non-negative number indicating the number of
* octets left undecoded, that starts from the offset returned by `*bufsz`.
* Returns -1 on error.
*/
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
/* returns if the chunked decoder is in middle of chunked data */
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
#ifdef __cplusplus
}
#endif
#endif
+14 -430
View File
@@ -1,438 +1,22 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../http.h"
#include "test.h"
#include "picohttpparser.h"
#define TEST(X) {if (!(X)) __builtin_trap(); }
//////////////////////////////////////////////////////////////////////////////////////
// TEST CASES
//////////////////////////////////////////////////////////////////////////////////////
#define BASIC_REQUEST_STRING \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
"User-Agent: curl/7.81.0\r\n" \
"Accept: */*\r\n" \
"\r\n"
static void test_init(void)
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
int state = tinyhttp_stream_state(&stream);
// These flags must be set on init
TEST(state & TINYHTTP_STREAM_RECV);
// These must be unset
TEST(!(state & TINYHTTP_STREAM_DIED));
TEST(!(state & TINYHTTP_STREAM_READY));
TEST(!(state & TINYHTTP_STREAM_REUSE));
TEST(!(state & TINYHTTP_STREAM_RECV_STARTED));
TEST(!(state & TINYHTTP_STREAM_SEND_STARTED));
tinyhttp_stream_free(&stream);
TEST_END
}
static void test_kill(void)
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_DIED));
tinyhttp_stream_kill(&stream);
TEST(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_DIED);
tinyhttp_stream_free(&stream);
TEST_END
}
static void
test_recv_started_flag(void)
{
ptrdiff_t cap;
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_RECV_STARTED));
tinyhttp_stream_recv_buf(&stream, &cap);
TEST(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_RECV_STARTED);
tinyhttp_stream_recv_ack(&stream, 0);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_RECV_STARTED));
tinyhttp_stream_free(&stream);
TEST_END
}
static void
test_send_started_flag(void)
{
ptrdiff_t cap;
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_SEND_STARTED));
tinyhttp_stream_send_buf(&stream, &cap);
TEST(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_SEND_STARTED);
tinyhttp_stream_send_ack(&stream, 0);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_SEND_STARTED));
tinyhttp_stream_free(&stream);
TEST_END
}
static void test_basic_exchange()
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
// Send request
send_request(&stream, BASIC_REQUEST_STRING);
// Build response
tinyhttp_stream_response_status(&stream, 200);
tinyhttp_stream_response_send(&stream);
// Receive response
char buf[1<<12];
Response res;
recv_response(&stream, &res, buf, sizeof(buf));
// We expect the status line:
// HTTP/1.1 200 OK
TEST(res.minor == 1);
TEST(res.status_code == 200);
TEST(tinyhttp_streq(res.status_text, TINYHTTP_STRING("OK")));
tinyhttp_stream_free(&stream);
TEST_END
}
//////////////////////////////////////////////////////////////////////////////////////
// ENTRY POINT
//////////////////////////////////////////////////////////////////////////////////////
void test_branch_coverage(void);
int main(void)
{
test_init();
test_kill();
test_recv_started_flag();
test_send_started_flag();
test_basic_exchange();
test_reuse();
test_chunking();
test_parse_request();
printf("OK\n");
char *tests[] = {
};
for (int i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
HTTP_Request req;
int ret = http_parse_request(tests[i], strlen(tests[i]), &req);
TEST(ret == 0);
}
test_branch_coverage();
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
// Helper Functions
//////////////////////////////////////////////////////////////////////////////////////
void *memfunc(TinyHTTPMemoryFuncTag tag, void *ptr, int len, void *data)
{
(void) data;
switch (tag) {
case TINYHTTP_MEM_MALLOC:
return malloc(len);
case TINYHTTP_MEM_FREE:
free(ptr);
return NULL;
}
return NULL;
}
static int
buffer_into_stream(TinyHTTPStream *stream, const char *src, int len)
{
int state = tinyhttp_stream_state(stream);
ptrdiff_t copied = 0;
while (copied < len && (state & TINYHTTP_STREAM_RECV)) {
char *dst;
ptrdiff_t cap;
ptrdiff_t cpy;
dst = tinyhttp_stream_recv_buf(stream, &cap);
cpy = len - copied;
if (cpy > cap) cpy = cap;
memcpy(dst, src + copied, cpy);
tinyhttp_printbytes(" >> ", src + copied, cpy);
copied += cpy;
tinyhttp_stream_recv_ack(stream, cpy);
state = tinyhttp_stream_state(stream);
}
printf("\n");
return copied;
}
static int
stream_into_buffer(TinyHTTPStream *stream, char *dst, int cap)
{
int state = tinyhttp_stream_state(stream);
int copied = 0;
while (copied < cap && (state & TINYHTTP_STREAM_SEND)) {
char *src;
ptrdiff_t len;
ptrdiff_t cpy;
src = tinyhttp_stream_send_buf(stream, &len);
cpy = cap - copied;
if (cpy > len) cpy = len;
memcpy(dst + copied, src, cpy);
tinyhttp_printbytes(" << ", src, cpy);
copied += cpy;
tinyhttp_stream_send_ack(stream, cpy);
state = tinyhttp_stream_state(stream);
}
printf("\n");
return copied;
}
int match_request(TinyHTTPRequest *r1, TinyHTTPRequest *r2)
{
if (r1->method != r2->method)
return 0;
if (r1->minor != r2->minor)
return 0;
if (!tinyhttp_streq(r1->path, r2->path))
return 0;
if (r1->num_headers != r2->num_headers)
return 0;
for (int i = 0; i < r2->num_headers; i++) {
if (!tinyhttp_streq(r1->headers[i].name, r2->headers[i].name))
return 0;
if (!tinyhttp_streq(r1->headers[i].value, r2->headers[i].value))
return 0;
}
if (r1->body_len != r2->body_len)
return 0;
if (r2->body_len == 0) {
if (r1->body != NULL)
return 0;
} else {
if (memcmp(r1->body, r2->body, r2->body_len))
return 0;
}
return 1;
}
static void
expect_request(TinyHTTPStream *stream, TinyHTTPRequest expreq)
{
int state = tinyhttp_stream_state(stream);
TEST(state & TINYHTTP_STREAM_READY);
TinyHTTPRequest *req = tinyhttp_stream_request(stream);
TEST(req);
TEST(match_request(req, &expreq));
}
int parse_request(TinyHTTPString txt, TinyHTTPRequest *req, char *buf, int max)
{
const char *method;
size_t method_len;
const char *path;
size_t path_len;
int minor;
struct phr_header headers[TINYHTTP_HEADER_LIMIT];
size_t num_headers = TINYHTTP_HEADER_LIMIT;
int ret = phr_parse_request(
txt.ptr, txt.len,
&method, &method_len,
&path, &path_len,
&minor,
headers, &num_headers,
0);
ptrdiff_t head_len = ret;
if (method_len == 3 && !memcmp("GET", method, 3)) {
req->method = TINYHTTP_METHOD_GET;
} else if (method_len == 4 && !memcmp("POST", method, 4)) {
req->method = TINYHTTP_METHOD_POST;
} else {
return -1;
}
req->minor = minor;
req->path = (TinyHTTPString) { path, path_len };
req->num_headers = num_headers;
for (int i = 0; i < (int) num_headers; i++) {
req->headers[i].name = (TinyHTTPString) {
headers[i].name,
headers[i].name_len,
};
req->headers[i].value = (TinyHTTPString) {
headers[i].value,
headers[i].value_len,
};
}
int transfer_encoding_index = tinyhttp_findheader(req, TINYHTTP_STRING("Transfer-Encoding"));
if (transfer_encoding_index != -1) {
// TODO: For now, we consider request as chunked just for having the Transfer-Encoding header
if (txt.len - head_len > max)
return -1;
memcpy(buf, txt.ptr + head_len, txt.len - head_len);
struct phr_chunked_decoder decoder;
memset(&decoder, 0, sizeof(decoder));
decoder.consume_trailer = 1; // Process any trailing headers
// Decode the chunked body
size_t decoded_size = txt.len - head_len;
ssize_t ret = phr_decode_chunked(&decoder, buf, &decoded_size);
if (ret < 0)
return -1;
req->body = buf;
req->body_len = decoded_size;
return 0;
}
int content_length_index = tinyhttp_findheader(req, TINYHTTP_STRING("Content-Length"));
if (content_length_index != -1) {
__builtin_trap(); // TODO
return 0;
}
req->body = NULL;
req->body_len = 0;
return 0;
}
static void
parse_response(TinyHTTPString txt, Response *res)
{
int minor;
int status_code;
const char *status_text;
size_t status_text_len;
struct phr_header headers[TINYHTTP_HEADER_LIMIT];
size_t num_headers = TINYHTTP_HEADER_LIMIT;
int ret = phr_parse_response(
txt.ptr, txt.len,
&minor,
&status_code, &status_text, &status_text_len,
headers, &num_headers,
0);
TEST(ret == txt.len);
res->minor = minor;
res->status_code = status_code;
res->status_text = (TinyHTTPString) { status_text, status_text_len };
res->num_headers = num_headers;
for (int i = 0; i < (int) num_headers; i++) {
res->headers[i].name = (TinyHTTPString) {
headers[i].name,
headers[i].name_len
};
res->headers[i].value = (TinyHTTPString) {
headers[i].value,
headers[i].value_len
};
}
res->body = NULL; // TODO
res->body_len = 0; // TODO
}
void send_request(TinyHTTPStream *stream, const char *str)
{
int received = buffer_into_stream(stream, str, strlen(str));
TEST(received == (int) strlen(str));
TinyHTTPRequest req;
char buf[1<<12];
TEST(parse_request((TinyHTTPString) {str, strlen(str)}, &req, buf, sizeof(buf)) == 0);
expect_request(stream, req);
}
void recv_response(TinyHTTPStream *stream, Response *res, char *dst, int cap)
{
int len = stream_into_buffer(stream, dst, cap);
int state = tinyhttp_stream_state(stream);
TEST((state & TINYHTTP_STREAM_SEND) == 0);
parse_response((TinyHTTPString) { dst, len }, res);
}
int header_exists(Response *res, TinyHTTPString name)
{
for (int i = 0; i < res->num_headers; i++)
if (tinyhttp_streqcase(res->headers[i].name, name))
return 1;
return 0;
}
int header_exists_with_value(Response *res, TinyHTTPString name, TinyHTTPString value)
{
for (int i = 0; i < res->num_headers; i++)
if (tinyhttp_streqcase(res->headers[i].name, name))
return tinyhttp_streqcase(res->headers[i].value, value);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
// THE END
//////////////////////////////////////////////////////////////////////////////////////
-47
View File
@@ -1,47 +0,0 @@
#include <stdio.h>
#include "../tinyhttp.h"
typedef struct {
int minor;
int status_code;
TinyHTTPString status_text;
int num_headers;
TinyHTTPHeader headers[TINYHTTP_HEADER_LIMIT];
char *body;
int body_len;
} Response;
// Memory function used to initialize TinyHTTPStream
// TODO: Maybe choose a better name for this
void *memfunc(TinyHTTPMemoryFuncTag tag, void *ptr, int len, void *data);
// Moves the request "str" into the stream, checks that the stream
// became ready and that it parsed the request correctly. When this
// functions returns the stream is ready for a response.
void send_request(TinyHTTPStream *stream, const char *str);
// Copies into the "dst" buffer the output bytes from the stream
// (up to "cap" bytes) and parses them as an HTTP response into
// "res".
void recv_response(TinyHTTPStream *stream, Response *res, char *dst, int cap);
int parse_request(TinyHTTPString txt, TinyHTTPRequest *req, char *buf, int max);
int match_request(TinyHTTPRequest *r1, TinyHTTPRequest *r2);
int header_exists(Response *res, TinyHTTPString name);
int header_exists_with_value(Response *res, TinyHTTPString name, TinyHTTPString value);
#define TEST(X) {if (!(X)) { printf("Test failed at %s:%d\n", __FILE__, __LINE__); fflush(stdout); __builtin_trap(); }}
#define TEST_START printf("Test %s:%d\n", __FILE__, __LINE__);
#define TEST_START2(file, line) printf("Test %s:%d\n", (file), (line));
#define TEST_END
void test_reuse(void);
void test_chunking(void);
void test_parse_request(void);
+518
View File
@@ -0,0 +1,518 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../http.h"
#define COUNT(X) (int) (sizeof(X)/sizeof((X)[0]))
#define TEST(X) {if (!(X)) { fprintf(stderr, "Failed test at %s:%d\n", __FILE__, __LINE__); __builtin_trap(); }}
#define TEST_EQ(X, Y) _Generic((X), HTTP_String: testeq_str, int: testeq_int)((X), (Y), S(#X), S(#Y), __FILE__, __LINE__)
static void testeq_int(int l, int r, HTTP_String uneval_l, HTTP_String uneval_r, const char *file, int line)
{
if (l != r) {
printf("Test failed at %s:%d\n", file, line);
printf(" TEST_EQ(%.*s, %.*s) -> TEST_EQ(%d, %d)\n",
(int) uneval_l.len, uneval_l.ptr,
(int) uneval_r.len, uneval_r.ptr,
l, r);
abort();
}
}
static void testeq_str(HTTP_String l, HTTP_String r, HTTP_String uneval_l, HTTP_String uneval_r, const char *file, int line)
{
if (!http_streq(l, r)) {
printf("Test failed at %s:%d\n", file, line);
printf(" TEST_EQ(\"%.*s\", \"%.*s\") -> TEST_EQ(%.*s, %.*s)\n",
(int) uneval_l.len, uneval_l.ptr,
(int) uneval_r.len, uneval_r.ptr,
(int) l.len, l.ptr,
(int) r.len, r.ptr);
abort();
}
}
static void test_branch_coverage_parse_request(void)
{
struct {
int line;
int ret;
char *str;
} error_reqs[] = {
{ __LINE__, -1, "G * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "G@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "GE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "GE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 18, "GET * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "GET@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "P * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "P@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PO * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PO@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "POS * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "POS@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 19, "POST * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "POST@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PU * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PU@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 18, "PUT * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PUT@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "H * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "H@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HEA * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HEA@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 19, "HEAD * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HEAD@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "D * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "D@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DEL * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DEL@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DELE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DELE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DELET * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DELET@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 21, "DELETE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "DELETE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "C * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "C@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CO * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CO@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CON * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CON@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONN * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONN@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONNE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONNE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONNEC * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONNEC@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 22, "CONNECT * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "CONNECT@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "O * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "O@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PO * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PO@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPT * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPT@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTI * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTI@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTIO * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTIO@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTION * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTION@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 22, "OPTIONS * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "OPTIONS@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "T * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "T@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TR * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TR@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TRA * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TRA@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TRAC * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TRAC@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 20, "TRACE * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "TRACE@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "P * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "P@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PA * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PA@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PAT * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PAT@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PATC * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PATC@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, 20, "PATCH * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "PATCH@ * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "GET *@\r\n\r\n" },
{ __LINE__, -1, "GET * @\r\n\r\n" },
{ __LINE__, -1, "GET * H\r\n\r\n" },
{ __LINE__, -1, "GET * H@\r\n\r\n" },
{ __LINE__, -1, "GET * HT\r\n\r\n" },
{ __LINE__, -1, "GET * HT@\r\n\r\n" },
{ __LINE__, -1, "GET * HTT\r\n\r\n" },
{ __LINE__, -1, "GET * HTT@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/@\r\n\r\n" },
{ __LINE__, 16, "GET * HTTP/1\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.@\r\n\r\n" },
{ __LINE__, 18, "GET * HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1@\nname:\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r@name:\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\n@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\nn@\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\nname\r\n\r\n" },
{ __LINE__, 25, "GET * HTTP/1.1\r\nname:\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\nname:\x1B\r\n\r\n" },
{ __LINE__, 30, "GET * HTTP/1.1\r\nname:value\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\nname :value\r\n\r\n" },
{ __LINE__, -1, "GET * HTTP/1.1\r\nname:val\rue\r\n\r\n" },
{ __LINE__, 0, NULL },
};
for (int i = 0; error_reqs[i].str; i++) {
HTTP_Request req;
int ret = http_parse_request(error_reqs[i].str, strlen(error_reqs[i].str), &req);
if (ret != error_reqs[i].ret) {
fprintf(stderr, "Failed test at %s:%d (ret=%d, expected=%d)\n", __FILE__, error_reqs[i].line, ret, error_reqs[i].ret);
}
}
{
char str[] = "GET * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 18);
TEST(req.method == HTTP_METHOD_GET);
}
{
char str[] = "POST * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 19);
TEST(req.method == HTTP_METHOD_POST);
}
{
char str[] = "PUT * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 18);
TEST(req.method == HTTP_METHOD_PUT);
}
{
char str[] = "HEAD * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 19);
TEST(req.method == HTTP_METHOD_HEAD);
}
{
char str[] = "DELETE * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 21);
TEST(req.method == HTTP_METHOD_DELETE);
}
{
char str[] = "CONNECT * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 22);
TEST(req.method == HTTP_METHOD_CONNECT);
}
{
char str[] = "OPTIONS * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 22);
TEST(req.method == HTTP_METHOD_OPTIONS);
}
{
char str[] = "TRACE * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 20);
TEST(req.method == HTTP_METHOD_TRACE);
}
{
char str[] = "PATCH * HTTP/1.1\r\n\r\n";
HTTP_Request req;
int ret = http_parse_request(str, sizeof(str)-1, &req);
TEST(ret == 20);
TEST(req.method == HTTP_METHOD_PATCH);
}
}
static void test_branch_coverage_parse_response(void)
{
struct {
int line;
int ret;
char *str;
} error_ress[] = {
{ __LINE__, -1, "@\r\n\r\n" },
{ __LINE__, -1, "H\r\n\r\n" },
{ __LINE__, -1, "H@\r\n\r\n" },
{ __LINE__, -1, "HT\r\n\r\n" },
{ __LINE__, -1, "HT@\r\n\r\n" },
{ __LINE__, -1, "HTT\r\n\r\n" },
{ __LINE__, -1, "HTT@\r\n\r\n" },
{ __LINE__, -1, "HTTP\r\n\r\n" },
{ __LINE__, -1, "HTTP@\r\n\r\n" },
{ __LINE__, -1, "HTTP/\r\n\r\n" },
{ __LINE__, -1, "HTTP/@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1\r\n\r\n" },
{ __LINE__, -1, "HTTP/1@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 \r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 @\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 4\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 4@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 40\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 40@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404@\r\n\r\n" },
{ __LINE__, 17, "HTTP/1.1 404 \r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 \x1B\r\n\r\n" },
{ __LINE__, 26, "HTTP/1.1 404 Not Found\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\x1B\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\x1B\nname:\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\x1Bname:\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\n@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\nn@\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\nname\r\n\r\n" },
{ __LINE__, 33, "HTTP/1.1 404 Not Found\r\nname:\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\nname:\x1B\r\n\r\n" },
{ __LINE__, 38, "HTTP/1.1 404 Not Found\r\nname:value\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\nname :value\r\n\r\n" },
{ __LINE__, -1, "HTTP/1.1 404 Not Found\r\nname:val\rue\r\n\r\n" },
{ __LINE__, 0, NULL },
};
for (int i = 0; error_ress[i].str; i++) {
HTTP_Response res;
int ret = http_parse_response(error_ress[i].str, strlen(error_ress[i].str), &res);
if (ret != error_ress[i].ret) {
fprintf(stderr, "Failed test at %s:%d (ret=%d, expected=%d)\n", __FILE__, error_ress[i].line, ret, error_ress[i].ret);
}
}
}
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif
#define S HTTP_STR
typedef struct {
char *buf;
int cap;
int num;
} Buffer;
static void appendc(Buffer *b, char c)
{
if (b->num < b->cap)
b->buf[b->num] = c;
b->num++;
}
static void appends(Buffer *b, HTTP_String s)
{
if (b->num < b->cap) {
int cpy = s.len;
if (cpy > b->cap - b->num)
cpy = b->cap - b->num;
for (int i = 0; i < cpy; i++)
b->buf[b->num + i] = s.ptr[i];
}
b->num += s.len;
}
static void appendi(Buffer *buf, unsigned int num)
{
char tmp[10];
tmp[0] = num / 1000000000; num %= 1000000000;
tmp[1] = num / 100000000; num %= 100000000;
tmp[2] = num / 10000000; num %= 10000000;
tmp[3] = num / 1000000; num %= 1000000;
tmp[4] = num / 100000; num %= 100000;
tmp[5] = num / 10000; num %= 10000;
tmp[6] = num / 1000; num %= 1000;
tmp[7] = num / 100; num %= 100;
tmp[8] = num / 10; num %= 10;
tmp[9] = num;
int leading_zeros = 0;
while (leading_zeros < 9 && tmp[leading_zeros] == 0)
leading_zeros++;
for (int i = leading_zeros; i < 10; i++)
tmp[i] += '0';
appends(buf, (HTTP_String) {
tmp + leading_zeros,
10 - leading_zeros
});
}
static int build_url(
HTTP_String scheme,
HTTP_String userinfo,
HTTP_String host,
HTTP_HostMode mode,
int port,
HTTP_String path,
HTTP_String query,
HTTP_String fragment,
char* dst,
int cap)
{
Buffer buf = {dst, cap, 0};
appends(&buf, scheme);
appendc(&buf, ':');
if (mode != HTTP_HOST_MODE_VOID) {
appendc(&buf, '/');
appendc(&buf, '/');
if (userinfo.len) {
appends(&buf, userinfo);
appendc(&buf, '@');
}
if (mode == HTTP_HOST_MODE_IPV6)
appendc(&buf, '[');
appends(&buf, host);
if (mode == HTTP_HOST_MODE_IPV6)
appendc(&buf, ']');
if (port == -2)
appendc(&buf, ':');
else if (port != -1) {
appendc(&buf, ':');
appendi(&buf, port);
}
}
appends(&buf, path);
appends(&buf, query);
appends(&buf, fragment);
return buf.num;
}
static void test_url(HTTP_String scheme, HTTP_String userinfo,
HTTP_String host, HTTP_HostMode mode, int port,
HTTP_String path, HTTP_String query, HTTP_String fragment)
{
char mem[1<<12];
int num = build_url(scheme, userinfo, host, mode, port, path, query, fragment, mem, sizeof(mem));
TEST(num < sizeof(mem));
printf("Testing %.*s\n", num, mem);
HTTP_URL url;
int ret = http_parse_url(mem, num, &url);
TEST_EQ(ret, num);
TEST_EQ(url.scheme, scheme);
TEST_EQ(url.authority.userinfo, userinfo);
if (port < 0)
TEST_EQ(url.authority.port, 0);
else
TEST_EQ(url.authority.port, port);
TEST_EQ((int) url.authority.host.mode, (int) mode);
if (mode == HTTP_HOST_MODE_IPV4) {
char tmp[1<<12];
TEST(sizeof(tmp) > host.len);
memcpy(tmp, host.ptr, host.len);
tmp[host.len] = '\0';
HTTP_IPv4 buf;
TEST_EQ(inet_pton(AF_INET, tmp, &buf), 1);
TEST_EQ((int) buf.data, (int) url.authority.host.ipv4.data);
} else if (mode == HTTP_HOST_MODE_IPV6) {
char tmp[1<<12];
TEST(sizeof(tmp) > host.len);
memcpy(tmp, host.ptr, host.len);
tmp[host.len] = '\0';
HTTP_IPv6 buf;
TEST_EQ(inet_pton(AF_INET6, tmp, &buf), 1);
TEST(!memcmp(&buf, &url.authority.host.ipv6, sizeof(HTTP_IPv6)));
} else if (mode == HTTP_HOST_MODE_NAME) {
TEST_EQ(host, url.authority.host.name);
}
TEST_EQ(url.path, path);
TEST_EQ(url.query, query);
TEST_EQ(url.fragment, fragment);
}
static void test_branch_coverage_parse_url(void)
{
HTTP_String scheme_values[] = { S("http") };
HTTP_String userinfo_values[] = { S(""), S("xxx:yyy") };
struct host_values {
HTTP_HostMode mode;
HTTP_String text;
} host_values[] = {
{ HTTP_HOST_MODE_VOID, S("") },
{ HTTP_HOST_MODE_IPV4, S("1.2.3.4") },
{ HTTP_HOST_MODE_IPV6, S("::") },
{ HTTP_HOST_MODE_IPV6, S("::1") },
{ HTTP_HOST_MODE_IPV6, S("1:2:3:4:A:B:C:D") },
{ HTTP_HOST_MODE_NAME, S("example.com") },
{ HTTP_HOST_MODE_NAME, S("1.2.3.256") },
};
int port_values[] = { -1, 1, 8080 };
HTTP_String path_values[] = { S(""), S("/"), S("/some/path.html") };
HTTP_String query_values[] = { S(""), S("?"), S("?param1=hello&param2=sup") };
HTTP_String fragment_values[] = { S(""), S("#"), S("#section0") };
for (int i = 0; i < COUNT(scheme_values); i++)
for (int j = 0; j < COUNT(userinfo_values); j++)
for (int k = 0; k < COUNT(host_values); k++)
for (int g = 0; g < COUNT(port_values); g++)
for (int t = 0; t < COUNT(path_values); t++)
for (int p = 0; p < COUNT(query_values); p++)
for (int q = 0; q < COUNT(fragment_values); q++) {
// Don't test URLs where the host isn't specified but the port or userinfo is
if (host_values[k].mode == HTTP_HOST_MODE_VOID && (port_values[g] > -1 || userinfo_values[j].len))
continue;
// If the authority is missing, the path must not be empty
if (host_values[k].mode == HTTP_HOST_MODE_VOID && path_values[t].len == 0)
continue;
test_url(
scheme_values[i],
userinfo_values[j],
host_values[k].text,
host_values[k].mode,
port_values[g],
path_values[t],
query_values[p],
fragment_values[q]
);
}
}
void test_branch_coverage(void)
{
test_branch_coverage_parse_request();
test_branch_coverage_parse_response();
test_branch_coverage_parse_url();
}
-44
View File
@@ -1,44 +0,0 @@
#include "test.h"
void test_chunking(void)
{
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
// Send request
send_request(&stream,
"POST / HTTP/1.1\r\n"
"Host: 127.0.0.1:8080\r\n"
"User-Agent: curl/7.81.0\r\n"
"Accept: */*\r\n"
"Transfer-Encoding: Chunked\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"\r\n"
"d\r\n"
"Hello, world!\r\n"
"0\r\n"
"\r\n"
);
// Build response
tinyhttp_stream_response_status(&stream, 200);
tinyhttp_stream_response_send(&stream);
// Receive response
char buf[1<<12];
Response res;
recv_response(&stream, &res, buf, sizeof(buf));
// We expect the status line:
// HTTP/1.1 200 OK
TEST(res.minor == 1);
TEST(res.status_code == 200);
TEST(tinyhttp_streq(res.status_text, TINYHTTP_STRING("OK")));
tinyhttp_stream_free(&stream);
TEST_END
}
}
-40
View File
@@ -1,40 +0,0 @@
#include <string.h>
#include "test.h"
static void test_helper(char *src, int len, int expret, TinyHTTPRequest *expreq)
{
if (len < 0) len = strlen(src);
TinyHTTPRequest req;
int ret = tinyhttp_parserequest(src, len, -1ULL, &req);
TEST(ret == expret);
if (expret > 0) {
TEST(match_request(&req, expreq));
}
}
void test_parse_request(void)
{
{
char src[] =
"GET / HTTP/1.1\r\n"
"Host: 127.0.0.1:8080\r\n"
"Connection: Keep-Alive\r\n"
"\r\n";
TinyHTTPRequest req;
req.method = TINYHTTP_METHOD_GET;
req.minor = 1;
req.path = TINYHTTP_STRING("/");
req.num_headers = 2;
req.headers[0].name = TINYHTTP_STRING("Host");
req.headers[0].value = TINYHTTP_STRING("127.0.0.1:8080");
req.headers[1].name = TINYHTTP_STRING("Connection");
req.headers[1].value = TINYHTTP_STRING("Keep-Alive");
req.body = NULL;
req.body_len = 0;
for (int i = 0; i < strlen(src)-1; i++)
test_helper(src, i, 0, &req);
test_helper(src, strlen(src), strlen(src), &req);
}
}
-185
View File
@@ -1,185 +0,0 @@
#include "test.h"
// This file tests the behavior of the "Connection" header
// HTTP/1.1, No "Connection" header
#define REQUEST_HTTP1_1_NO_CONNECTION_HEADER \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
"\r\n"
// HTTP/1.1, "Connection" header with invalid value
#define REQUEST_HTTP1_1_CONNECTION_INVALID \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
"Connection: zzz\r\n" \
"\r\n"
// HTTP/1.1, "Connection: Keep-Alive" header
#define REQUEST_HTTP1_1_CONNECTION_KEEPALIVE \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
"Connection: Keep-Alive\r\n" \
"\r\n"
// HTTP/1.1, "Connection: Close" header
#define REQUEST_HTTP1_1_CONNECTION_CLOSE \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
"Connection: Close\r\n" \
"\r\n"
// HTTP/1.0, No "Connection" header
#define REQUEST_HTTP1_0_NO_CONNECTION_HEADER \
"GET / HTTP/1.0\r\n" \
"\r\n"
// HTTP/1.0, "Connection" header with invalid value
#define REQUEST_HTTP1_0_CONNECTION_INVALID \
"GET / HTTP/1.0\r\n" \
"Connection: zzz\r\n" \
"\r\n"
// HTTP/1.0, "Connection: Keep-Alive" header
#define REQUEST_HTTP1_0_CONNECTION_KEEPALIVE \
"GET / HTTP/1.0\r\n" \
"Connection: Keep-Alive\r\n" \
"\r\n"
// HTTP/1.0, "Connection: Close" header
#define REQUEST_HTTP1_0_CONNECTION_CLOSE \
"GET / HTTP/1.0\r\n" \
"Connection: Close\r\n" \
"\r\n"
static void test_setreuse(void)
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_REUSE));
tinyhttp_stream_setreuse(&stream, 1);
TEST(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_REUSE);
tinyhttp_stream_setreuse(&stream, 0);
TEST(!(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_REUSE));
tinyhttp_stream_setreuse(&stream, 5847295);
TEST(tinyhttp_stream_state(&stream) & TINYHTTP_STREAM_REUSE);
tinyhttp_stream_free(&stream);
TEST_END
}
typedef enum {
DONT_REUSE,
ALLOW_REUSE,
} ServerReuse;
typedef enum {
INCONNHDR_NONE,
INCONNHDR_KEEPALIVE,
INCONNHDR_CLOSE,
INCONNHDR_INVALID,
} InputConnectionHeader;
typedef enum {
OUTCONNHDR_MISSING_OR_KEEPALIVE,
OUTCONNHDR_CLOSE,
} OutputConnectionHeader;
static void test_reuse_helper(
ServerReuse server_reuse,
InputConnectionHeader input_conn_header,
int input_http_minor_version,
OutputConnectionHeader expect_output_conn_header,
int expect_output_http_minor_version,
const char *file, int line)
{
TinyHTTPStream stream;
TEST_START2(file, line)
tinyhttp_stream_init(&stream, memfunc, NULL);
tinyhttp_stream_setreuse(&stream, server_reuse);
if (input_http_minor_version == 1) {
switch (input_conn_header) {
case INCONNHDR_NONE : send_request(&stream, REQUEST_HTTP1_1_NO_CONNECTION_HEADER); break;
case INCONNHDR_KEEPALIVE: send_request(&stream, REQUEST_HTTP1_1_CONNECTION_KEEPALIVE); break;
case INCONNHDR_CLOSE : send_request(&stream, REQUEST_HTTP1_1_CONNECTION_CLOSE); break;
case INCONNHDR_INVALID : send_request(&stream, REQUEST_HTTP1_1_CONNECTION_INVALID); break;
}
} else {
switch (input_conn_header) {
case INCONNHDR_NONE : send_request(&stream, REQUEST_HTTP1_0_NO_CONNECTION_HEADER); break;
case INCONNHDR_KEEPALIVE: send_request(&stream, REQUEST_HTTP1_0_CONNECTION_KEEPALIVE); break;
case INCONNHDR_CLOSE : send_request(&stream, REQUEST_HTTP1_0_CONNECTION_CLOSE); break;
case INCONNHDR_INVALID : send_request(&stream, REQUEST_HTTP1_0_CONNECTION_INVALID); break;
}
}
// Build a dummy response
tinyhttp_stream_response_status(&stream, 200);
tinyhttp_stream_response_send(&stream);
Response res;
char buf[1<<10];
recv_response(&stream, &res, buf, sizeof(buf));
TEST(res.minor == expect_output_http_minor_version);
int state = tinyhttp_stream_state(&stream);
switch (expect_output_conn_header) {
case OUTCONNHDR_MISSING_OR_KEEPALIVE:
{
TEST(!header_exists(&res, TINYHTTP_STRING("Connection"))
|| header_exists_with_value(&res, TINYHTTP_STRING("Connection"), TINYHTTP_STRING("Keep-Alive")));
TEST((state & TINYHTTP_STREAM_DIED) == 0);
}
break;
case OUTCONNHDR_CLOSE:
{
TEST(header_exists_with_value(&res, TINYHTTP_STRING("Connection"), TINYHTTP_STRING("Close")));
TEST(state & TINYHTTP_STREAM_DIED);
}
break;
}
tinyhttp_stream_free(&stream);
TEST_END
}
void test_reuse(void)
{
// Relevant specs:
// RFC 9112, Section 9.3. (Persistence)
// RFC 9112, Section 9.6. (Tear-down)
// RFC 9110, Section 7.6.1. (Connection)
test_setreuse();
test_reuse_helper(DONT_REUSE, INCONNHDR_NONE, 1, OUTCONNHDR_CLOSE, 1, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_KEEPALIVE, 1, OUTCONNHDR_CLOSE, 1, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_CLOSE, 1, OUTCONNHDR_CLOSE, 1, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_INVALID, 1, OUTCONNHDR_CLOSE, 1, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_NONE, 1, OUTCONNHDR_MISSING_OR_KEEPALIVE, 1, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_KEEPALIVE, 1, OUTCONNHDR_MISSING_OR_KEEPALIVE, 1, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_CLOSE, 1, OUTCONNHDR_CLOSE, 1, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_INVALID, 1, OUTCONNHDR_MISSING_OR_KEEPALIVE, 1, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_NONE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_KEEPALIVE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_CLOSE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(DONT_REUSE, INCONNHDR_INVALID, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_NONE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_KEEPALIVE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_CLOSE, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
test_reuse_helper(ALLOW_REUSE, INCONNHDR_INVALID, 0, OUTCONNHDR_CLOSE, 0, __FILE__, __LINE__);
}