Basic test infrastructure

This commit is contained in:
2025-04-20 14:11:52 +02:00
parent a4aafb1ee8
commit e1dca84a44
9 changed files with 1407 additions and 260 deletions
+13 -31
View File
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include "tinyhttp.h"
#include "../tinyhttp.h"
sig_atomic_t should_exit = 0;
@@ -12,22 +12,6 @@ signal_handler(int sig)
should_exit = 1;
}
static 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;
}
int main(void)
{
signal(SIGINT, signal_handler);
@@ -39,31 +23,29 @@ int main(void)
.plain_backlog = 32,
};
TinyHTTPServer *server = tinyhttp_server_init(config, memfunc, NULL);
TinyHTTPServer *server = tinyhttp_server_init(config);
if (server == NULL)
return -1;
while (!should_exit) {
int ret;
TinyHTTPRequest *req;
TinyHTTPResponse res;
int ret = tinyhttp_server_wait(server, &req, &res, 1000);
ret = tinyhttp_server_wait(server, &req, &res, 1000);
if (ret < 0) return -1; // Error
if (ret > 0) continue; // Timeout
tinyhttp_response_status(res, 200);
tinyhttp_response_body_setmincap(res, 1<<10);
ptrdiff_t cap;
char *buf = tinyhttp_response_body_buf(res, &cap);
int len = buf ? snprintf(buf, cap, "Hello, world!") : 0;
if (len < 0 || len > cap) abort();
tinyhttp_response_body_ack(res, len);
tinyhttp_response_body(res, "Hello, world!", -1);
//tinyhttp_response_undo(res);
//tinyhttp_response_status(res, 500);
tinyhttp_response_send(res);
}
tinyhttp_server_free(server);
return 0;
}
}