First version of chunk encoding

This commit is contained in:
2025-04-20 19:07:16 +02:00
parent a86ded5035
commit 1573d831db
8 changed files with 172 additions and 103 deletions
+3 -55
View File
@@ -9,7 +9,6 @@
// TEST CASES
//////////////////////////////////////////////////////////////////////////////////////
// Plain HTTP 1.1 request string with no Connection header
#define BASIC_REQUEST_STRING \
"GET / HTTP/1.1\r\n" \
"Host: 127.0.0.1:8080\r\n" \
@@ -40,31 +39,6 @@ static void test_init(void)
TEST_END
}
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
}
static void test_kill(void)
{
TinyHTTPStream stream;
@@ -128,15 +102,13 @@ test_send_started_flag(void)
TEST_END
}
static void test_exchange(int reuse)
static void test_basic_exchange()
{
TinyHTTPStream stream;
TEST_START
tinyhttp_stream_init(&stream, memfunc, NULL);
tinyhttp_stream_setreuse(&stream, reuse);
// Send request
send_request(&stream, BASIC_REQUEST_STRING);
@@ -155,33 +127,10 @@ static void test_exchange(int reuse)
TEST(res.status_code == 200);
TEST(tinyhttp_streq(res.status_text, TINYHTTP_STRING("OK")));
if (reuse) {
// If we allowed connection reuse on this stream, we expect
// the connection to be kept alive. The response must therefore
// contain the "Connection: Keep-Alive" header or no "Connection"
// header at all since "Keep-Alive" is the default.
TEST(!header_exists(&res, TINYHTTP_STRING("Connection")) ||
header_exists_with_value(&res, TINYHTTP_STRING("Connection"), TINYHTTP_STRING("Keep-Alive")));
} else {
// If we didn't allow connection reuse, then the response must
// contain the "Connection: Close" header
TEST(header_exists_with_value(&res, TINYHTTP_STRING("Connection"), TINYHTTP_STRING("Close")));
}
tinyhttp_stream_free(&stream);
TEST_END
}
/*
KEEP ALIVE TESTS
no connection header in request
invalid connection header in request
keep-alive value in request
close value in request
*/
//////////////////////////////////////////////////////////////////////////////////////
// ENTRY POINT
//////////////////////////////////////////////////////////////////////////////////////
@@ -189,13 +138,12 @@ KEEP ALIVE TESTS
int main(void)
{
test_init();
test_setreuse();
test_kill();
test_recv_started_flag();
test_send_started_flag();
test_exchange(0);
test_exchange(1);
test_basic_exchange();
test_reuse();
test_chunking();
printf("OK\n");
return 0;
}
+2 -1
View File
@@ -38,4 +38,5 @@ int header_exists_with_value(Response *res, TinyHTTPString name, TinyHTTPString
#define TEST_START2(file, line) printf("Test %s:%d\n", (file), (line));
#define TEST_END
void test_reuse(void);
void test_reuse(void);
void test_chunking(void);
+6
View File
@@ -0,0 +1,6 @@
#include "test.h"
void test_chunking(void)
{
// TODO
}
+27
View File
@@ -52,6 +52,32 @@
"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,
@@ -139,6 +165,7 @@ void test_reuse(void)
// 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__);