A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
blob: 6165231261570fca4f98ba53b952f43cdbac9812 (38273 bytes)[doc][blame][raw]
1
/*2
* Copyright (C) 2026 Sophie Sitzler3
*4
* This program is free software: you can redistribute it and/or modify5
* it under the terms of the GNU General Public License as published by6
* the Free Software Foundation, either version 3 of the License, or7
* (at your option) any later version.8
*9
* This program is distributed in the hope that it will be useful,10
* but WITHOUT ANY WARRANTY; without even the implied warranty of11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12
* GNU General Public License for more details.13
*14
* You should have received a copy of the GNU General Public License15
* along with this program. If not, see <https://www.gnu.org/licenses/>.16
*/18
// @file tpee.h19
// @desc A low-key HTTP micro-framework for people who hate web bloat21
#pragma once23
#include <stdbool.h>24
#include <sys/socket.h>26
#ifdef TPEE_IMPL27
#define SOCK_IMPL28
#endif // TPEE_IMPL30
// @include <sock.h>31
// @desc sock library handling server/client communication and multi-threading.32
#include <sock.h>34
// @enum tpee_err_t35
// @desc Error code.36
typedef enum {37
TPEE_ERR_OK = SOCK_ERR_OK,38
TPEE_ERR__COUNT,39
} tpee_err_t;41
// @enum tpee_method_t42
// @desc tpee method type.43
typedef enum {44
TPEE_METHOD_GET,45
TPEE_METHOD_HEAD,46
TPEE_METHOD_POST,47
TPEE_METHOD_PUT,48
TPEE_METHOD_DELETE,49
TPEE_METHOD_CONNECT,50
TPEE_METHOD_OPTIONS,51
TPEE_METHOD_TRACE,52
TPEE_METHOD_PATCH,53
} tpee_method_t;55
// @enum tpee_status_t56
// @desc tpee status code.57
typedef enum {58
TPEE_STATUS_CONTINUE = 100,59
TPEE_STATUS_SWITCHING_PROTOCOLS = 101,60
TPEE_STATUS_PROCESSING = 102,61
TPEE_STATUS_EARLY_HINTS = 103,62
TPEE_STATUS_OK = 200,63
TPEE_STATUS_CREATED = 201,64
TPEE_STATUS_ACCEPTED = 202,65
TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,66
TPEE_STATUS_NO_CONTENT = 204,67
TPEE_STATUS_RESET_CONTENT = 205,68
TPEE_STATUS_PARTIAL_CONTENT = 206,69
TPEE_STATUS_MULTI_STATUS = 207,70
TPEE_STATUS_ALREADY_REPORTED = 208,71
TPEE_STATUS_IM_USED = 226,72
TPEE_STATUS_MULTIPLE_CHOICES = 300,73
TPEE_STATUS_MOVED_PERMANENTLY = 301,74
TPEE_STATUS_FOUND = 302,75
TPEE_STATUS_SEE_OTHER = 303,76
TPEE_STATUS_NOT_MODIFIED = 304,77
TPEE_STATUS_TEMPORARY_REDIRECT = 307,78
TPEE_STATUS_PERMANENT_REDIRECT = 308,79
TPEE_STATUS_BAD_REQUEST = 400,80
TPEE_STATUS_UNAUTHORIZED = 401,81
TPEE_STATUS_PAYMENT_REQUIRED = 402,82
TPEE_STATUS_FORBIDDEN = 403,83
TPEE_STATUS_NOT_FOUND = 404,84
TPEE_STATUS_METHOD_NOT_ALLOWED = 405,85
TPEE_STATUS_NOT_ACCEPTABLE = 406,86
TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,87
TPEE_STATUS_REQUEST_TIMEOUT = 408,88
TPEE_STATUS_CONFLICT = 409,89
TPEE_STATUS_GONE = 410,90
TPEE_STATUS_LENGTH_REQUIRED = 411,91
TPEE_STATUS_PRECONDITION_FAILED = 412,92
TPEE_STATUS_PAYLOAD_TOO_LARGE = 413,93
TPEE_STATUS_URI_TOO_LONG = 414,94
TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,95
TPEE_STATUS_RANGE_NOT_SATISFIABLE = 416,96
TPEE_STATUS_EXPECTATION_FAILED = 417,97
TPEE_STATUS_I_AM_A_TEAPOT = 418,98
TPEE_STATUS_MISDIRECTED_REQUEST = 421,99
TPEE_STATUS_UNPROCESSABLE_CONTENT = 422,100
TPEE_STATUS_LOCKED = 423,101
TPEE_STATUS_FAILED_DEPENDENCY = 424,102
TPEE_STATUS_TOO_EARLY = 425,103
TPEE_STATUS_UPGRADE_REQUIRED = 426,104
TPEE_STATUS_PRECONDITION_REQUIRED = 428,105
TPEE_STATUS_TOO_MANY_REQUESTS = 429,106
TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,107
TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,108
TPEE_STATUS_INTERNAL_SERVER_ERROR = 500,109
TPEE_STATUS_NOT_IMPLEMENTED = 501,110
TPEE_STATUS_BAD_GATEWAY = 502,111
TPEE_STATUS_SERVICE_UNAVAILABLE = 503,112
TPEE_STATUS_GATEWAY_TIMEOUT = 504,113
TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,114
TPEE_STATUS_VARIANT_ALSO_NEGOTITATES = 506,115
TPEE_STATUS_INSUFFICIENT_STORAGE = 507,116
TPEE_STATUS_LOOP_DETECTED = 508,117
TPEE_STATUS_NOT_EXTENDED = 510,118
TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,119
__TPEE_STATUS_HANDLED = 999,120
} tpee_status_t;122
// @struct tpee_t123
// @groupdef tpee_t124
// @desc Global context struct. Implemented here {tpee}.125
typedef struct tpee tpee_t;127
// @type tpee_host_t128
// @desc Represents the host.129
typedef sock_host_t tpee_host_t;131
// @type tpee_bind_t132
// @desc Represents the binding (port).133
typedef sock_bind_t tpee_bind_t;135
// @type tpee_conn_t136
// @groupdef tpee_conn_t137
// @desc Represents a client connection.138
typedef sock_conn_t tpee_conn_t;140
// @struct tpee_header_t141
// @desc A tpee header.142
typedef struct {143
char *key;144
char *value;145
} tpee_header_t;147
// @struct tpee_req_t148
// @groupdef tpee_req_t149
// @desc A tpee request struct.150
typedef struct {151
tpee_conn_t *conn;152
char *method;153
char *url;154
char *version;155
tpee_header_t *headers;156
int headers_len;157
char *body;158
long body_len;159
void *ext_data;160
} tpee_req_t;162
// @struct tpee_resp_t163
// @groupdef tpee_resp_t164
// @desc A tpee response struct.165
typedef struct {166
char *version;167
tpee_status_t status;168
tpee_header_t *headers;169
int headers_len;170
char *body;171
long body_len;172
} tpee_resp_t;174
// @type tpee_global_fn_t175
// @desc A function type for a global route handler.176
typedef tpee_resp_t (*tpee_global_fn_t)(tpee_t *, tpee_req_t *);178
// @type tpee_handle_fn_t179
// @desc A function type for a route handler.180
typedef tpee_resp_t (*tpee_handle_fn_t)(tpee_req_t *);182
// @type tpee_hook_fn_t183
// @desc A function type for a hook handler.184
typedef void (*tpee_hook_fn_t)(tpee_req_t *);186
// @struct tpee_route_t187
// @desc A route.188
typedef struct {189
tpee_method_t method;190
char *path;191
tpee_handle_fn_t fn;192
} tpee_route_t;194
// @struct tpee_pair_t195
typedef struct {196
char *key;197
char *value;198
} tpee_pair_t;200
// @struct tpee_pair_list_t201
// @groupdef tpee_pair_list_t202
typedef struct {203
tpee_pair_t *pair_list;204
size_t len;205
} tpee_pair_list_t;207
// @impl tpee_t208
struct tpee {209
sock_t *sock;210
tpee_global_fn_t fn;211
};213
// @func tpee_init214
// @group tpee_t215
// @desc Initializes global library context {tpee_t}. Free with {tpee_cleanup}.216
// @return {tpee_t} * - Returns the global library context.217
tpee_t *tpee_init(void);219
// @func tpee_cleanup220
// @group tpee_t221
// @desc Cleans up {tpee_t}.222
// @param {tpee_t} *self - The global library context.223
void tpee_cleanup(tpee_t *self);225
// @func tpee_on_global226
// @group tpee_t227
// @desc Set and override the default global route handler {_tpee_on_global}.228
// @param {tpee_t} *self - The global library context.229
// @param {tpee_global_fn_t} fn - The global route handler.230
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn);232
// @func tpee_on_route233
// @note Use macros like {TPEE_GET} and others to register routes more easily.234
// @desc Registers a new route.235
// @param {tpee_method_t} method - The tpee method.236
// @param char *url - The tpee URL (path).237
// @param {tpee_handle_fn_t} fn - The route handler.238
void tpee_on_route(tpee_method_t method, char *url, tpee_handle_fn_t fn);240
// @func tpee_on_default241
// @note Use macro {TPEE_DEFAULT} instead.242
// @desc Registers a fallback handler.243
// @param {tpee_handle_fn_t} fn - The route handler.244
void tpee_on_default(tpee_handle_fn_t fn);246
// @func tpee_on_pre_route247
// @note Use macro {TPEE_PRE_HOOK} instead.248
// @desc Registers a hook handler which is performed before the route handler is executed.249
// @param {tpee_hook_fn_t} fn - The hook handler.250
void tpee_on_pre_route(tpee_hook_fn_t fn);252
// @func tpee_on_post_route253
// @note Use macro {TPEE_POST_HOOK} instead.254
// @desc Registers a hook handler which is performed after the route handler is executed.255
// @param {tpee_hook_fn_t} fn - The hook handler.256
void tpee_on_post_route(tpee_hook_fn_t fn);258
// @func tpee_req_path259
// @group tpee_req_t260
// @param {tpee_req_t} *req - The tpee request.261
// @return char * - Returns the request path.262
char *tpee_req_path(tpee_req_t *req);264
// @func tpee_req_body265
// @group tpee_req_t266
// @param {tpee_req_t} *req - The tpee request.267
// @return char * - Returns the request body.268
char *tpee_req_body(tpee_req_t *req);270
// @func tpee_req_queries271
// @group tpee_req_t272
// @param {tpee_req_t} *req - The tpee request.273
// @return {tpee_pair_list_t} - Returns the request queries.274
tpee_pair_list_t tpee_req_queries(tpee_req_t *req);276
// @func tpee_req_params277
// @group tpee_req_t278
// @param {tpee_req_t} *req - The tpee request.279
// @return {tpee_pair_list_t} - Returns the request params.280
tpee_pair_list_t tpee_req_params(tpee_req_t *req);282
// @func tpee_pair_list_get283
// @group tpee_pair_list_t284
char *tpee_pair_list_get(tpee_pair_list_t *self, char *key);286
// @func tpee_pair_list_has287
// @group tpee_pair_list_t288
bool tpee_pair_list_has(tpee_pair_list_t *self, char *key);290
// @func tpee_pair_list_free291
// @group tpee_pair_list_t292
void tpee_pair_list_free(tpee_pair_list_t *self);294
// @func tpee_conn_read295
// @group tpee_conn_t296
// @param {tpee_conn_t} *conn - The connection.297
// @param char * - The buffer to read into.298
// @param size_t - The size of the buffer.299
ssize_t tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len);301
// @func tpee_conn_write302
// @group tpee_conn_t303
// @param {tpee_conn_t} *conn - The connection.304
// @param char * - The buffer to be written.305
// @param size_t - The size of the buffer.306
ssize_t tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len);308
// @func tpee_req_header309
// @group tpee_req_t310
// @param {tpee_req_t} *req - The tpee request.311
// @param char *key - The header key.312
// @return char * - Returns a header value.313
char *tpee_req_header(tpee_req_t *req, char *key);315
// @func tpee_resp_header316
// @group tpee_resp_t317
// @desc Writes a header to the response. The header content is consumed and can be freed afterward.318
// @param {tpee_resp_t} *resp - The tpee response.319
// @param char *key - The header key.320
// @param char *value - The header value.321
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value);323
// @func tpee_resp_create324
// @group tpee_resp_t325
// @desc Creates a new response with a given tpee status. Freed with {tpee_resp_free}.326
// @param {tpee_status_t} status - The tpee status code.327
// @return {tpee_resp_t} - Returns a tpee response.328
tpee_resp_t tpee_resp_create(tpee_status_t status);330
// @func tpee_resp_body331
// @group tpee_resp_t332
// @desc Writes body content to the response. The body content is consumed and can be freed afterward.333
// @param {tpee_resp_t} *resp - The tpee response.334
// @param char *body - The body content.335
// @param long len - The body length.336
void tpee_resp_body(tpee_resp_t *resp, char *body, long len);338
// @func tpee_req_free339
// @group tpee_req_t340
// @desc Frees a request.341
// @param {tpee_req_t} *req - The tpee request.342
void tpee_req_free(tpee_req_t *req);344
// @func tpee_resp_write_head345
// @group tpee_resp_t346
// @desc Writes a response header to the socket stream.347
// @param {tpee_resp_t} *resp - The tpee response.348
// @param {tpee_conn_t} *conn - The tpee connection.349
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn);351
// @func tpee_resp_write_body352
// @group tpee_resp_t353
// @desc Writes a response body to the socket stream.354
// @param {tpee_resp_t} *resp - The tpee response.355
// @param {tpee_conn_t} *conn - The tpee connection.356
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn);358
// @func tpee_resp_write359
// @group tpee_resp_t360
// @desc Writes a response to the socket stream.361
// @param {tpee_resp_t} *resp - The tpee response.362
// @param {tpee_conn_t} *conn - The tpee connection.363
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn);365
// @func tpee_resp_free366
// @group tpee_resp_t367
// @desc Frees a response.368
// @param {tpee_resp_t} *resp - The tpee response.369
void tpee_resp_free(tpee_resp_t *resp);371
// @func tpee_bind372
// @group tpee_t373
// @param {tpee_t} *self - The global library context.374
// @param char *hostname - The hostname.375
// @param int port - The port.376
// @param {tpee_host_t} *hosts - The host to bind to.377
// @param int hosts_len - The host length of to be bound host.378
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len);380
// @func tpee_listen381
// @group tpee_t382
// @param {tpee_t} *self - The global library context.383
tpee_err_t tpee_listen(tpee_t *self);385
// @func tpee_bind_listen386
// @group tpee_t387
// @param {tpee_t} *self - The global library context.388
// @param char *hostname - The hostname.389
// @param int port - The port.390
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port);392
// @func tpee_close393
// @group tpee_t394
// @param {tpee_t} *self - The global library context.395
void tpee_close(tpee_t *self);397
// @func _tpee_req_read398
// @group tpee_req_t399
// @internal400
// @param {tpee_req_t} *req - The tpee request.401
// @param {tpee_conn_t} *conn - The tpee connection.402
// @return {tpee_err_t} - Returns an tpee error code.403
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn);405
// @func _tpee_method_str406
// @internal407
// @desc Get tpee method string.408
const char *_tpee_method_str(tpee_method_t method);410
// @macro TPEE_RESP_HANDLED411
// @group tpee_resp_t412
// @desc Handled {tpee_resp_t}.413
#define TPEE_RESP_HANDLED (tpee_resp_t) { .status = __TPEE_STATUS_HANDLED }415
// @macro _TPEE_ON416
// @internal417
#define _TPEE_ON(method, path, fn_name) \418
static tpee_resp_t fn_name(tpee_req_t *req); \419
__attribute__((constructor)) \420
static void __macro_tpee_fn_register_##fn_name(void) { \421
tpee_on_route(method, path, &fn_name); \422
} \423
static tpee_resp_t fn_name(tpee_req_t *req)425
// @macro TPEE_GET426
#define TPEE_GET(path, fn_name) _TPEE_ON(TPEE_METHOD_GET, path, fn_name)428
// @macro TPEE_HEAD429
#define TPEE_HEAD(path, fn_name) _TPEE_ON(TPEE_METHOD_HEAD, path, fn_name)431
// @macro TPEE_POST432
#define TPEE_POST(path, fn_name) _TPEE_ON(TPEE_METHOD_POST, path, fn_name)434
// @macro TPEE_PUT435
#define TPEE_PUT(path, fn_name) _TPEE_ON(TPEE_METHOD_PUT, path, fn_name)437
// @macro TPEE_DELETE438
#define TPEE_DELETE(path, fn_name) _TPEE_ON(TPEE_METHOD_DELETE, path, fn_name)440
// @macro TPEE_CONNECT441
#define TPEE_CONNECT(path, fn_name) _TPEE_ON(TPEE_METHOD_CONNECT, path, fn_name)443
// @macro TPEE_OPTIONS444
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON(TPEE_METHOD_OPTIONS, path, fn_name)446
// @macro TPEE_TRACE447
#define TPEE_TRACE(path, fn_name) _TPEE_ON(TPEE_METHOD_TRACE, path, fn_name)449
// @macro TPEE_PATCH450
#define TPEE_PATCH(path, fn_name) _TPEE_ON(TPEE_METHOD_PATCH, path, fn_name)452
// @macro TPEE_DEFAULT453
#define TPEE_DEFAULT(fn_name) \454
static tpee_resp_t fn_name(tpee_req_t *req); \455
__attribute__((constructor)) \456
static void __macro_tpee_fn_register_##fn_name(void) { \457
tpee_on_default(&fn_name); \458
} \459
static tpee_resp_t fn_name(tpee_req_t *req)461
// @macro TPEE_PRE_HOOK462
#define TPEE_PRE_HOOK(fn_name) \463
static void fn_name(tpee_req_t *req); \464
__attribute__((constructor)) \465
static void __macro_tpee_fn_register_##fn_name(void) { \466
tpee_on_pre_route(&fn_name); \467
} \468
static void fn_name(tpee_req_t *req)470
// @macro TPEE_POST_HOOK471
#define TPEE_POST_HOOK(fn_name) \472
static void fn_name(tpee_req_t *req); \473
__attribute__((constructor)) \474
static void __macro_tpee_fn_register_##fn_name(void) { \475
tpee_on_post_route(&fn_name); \476
} \477
static void fn_name(tpee_req_t *req)479
#ifdef TPEE_IMPL481
#include <stdio.h>482
#include <stdlib.h>483
#include <string.h>484
#include <ctype.h>485
#include <time.h>486
#include <unistd.h>487
#include <pthread.h>488
#include <netdb.h>489
#include <poll.h>490
#include <errno.h>491
#include <signal.h>493
// @macro TPEE_VERSION494
// @internal495
#define TPEE_VERSION "HTTP/1.1"497
// @macro TPEE_MAX_REQUEST_HEAD_SIZE498
// @internal499
#define TPEE_MAX_REQUEST_HEAD_SIZE 1000000501
// @var _tpee_route_list502
// @internal503
tpee_route_t *_tpee_route_list = NULL;505
// @var _tpee_route_list_len506
// @internal507
size_t _tpee_route_list_len = 0;509
// @var _tpee_default_route510
// @internal511
tpee_handle_fn_t _tpee_default_route;513
// @var _tpee_pre_route514
// @internal515
tpee_hook_fn_t _tpee_pre_route;517
// @var _tpee_post_route518
// @internal519
tpee_hook_fn_t _tpee_post_route;521
// @func _tpee_on_global522
// @group tpee_t523
// @internal524
// @desc The default route handler. Can be overwritten with {tpee_on_global}.525
// @param {tpee_t} *self - The global library context.526
// @param {tpee_req_t} *req - The tpee request.527
// @return {tpee_resp_t} - Returns a tpee response.528
tpee_resp_t _tpee_on_global(tpee_t *self, tpee_req_t *req) {529
(void) self;530
tpee_resp_t resp = tpee_resp_create(TPEE_STATUS_NOT_FOUND);531
if (_tpee_pre_route != NULL) {532
_tpee_pre_route(req);533
}534
for (size_t i = 0; i < _tpee_route_list_len; i++) {535
tpee_route_t *route = &_tpee_route_list[i];536
const char *method = _tpee_method_str(route->method);537
char *path = tpee_req_path(req);538
if (strcmp(method, req->method) == 0) {539
size_t len = strlen(route->path);540
char c = route->path[len - 1];541
if (c == '*') {542
if (strncmp(route->path, path, len - 1) == 0) {543
tpee_resp_free(&resp);544
resp = route->fn(req);545
}546
}547
else {548
if (strcmp(route->path, path) == 0) {549
tpee_resp_free(&resp);550
resp = route->fn(req);551
}552
}553
}554
free(path);555
if (resp.status != TPEE_STATUS_NOT_FOUND) {556
if (_tpee_post_route != NULL) {557
_tpee_post_route(req);558
}559
return resp;560
}561
}562
if (_tpee_default_route != NULL) {563
tpee_resp_free(&resp);564
resp = _tpee_default_route(req);565
}566
if (_tpee_post_route != NULL) {567
_tpee_post_route(req);568
}569
return resp;570
}572
// @func _tpee_on_handle573
// @internal574
sock_err_t _tpee_on_handle(sock_t *sock, tpee_conn_t *conn) {575
tpee_t *self= (tpee_t *) sock->ext;576
tpee_req_t req;577
tpee_err_t err = _tpee_req_read(&req, conn);578
if (err != TPEE_ERR_OK) {579
return (sock_err_t) err;580
}581
if (req.method != NULL && req.url != NULL) {582
if (self->fn != NULL) {583
tpee_resp_t resp = self->fn(self, &req);584
tpee_resp_write(&resp, conn);585
tpee_resp_free(&resp);586
}587
}588
tpee_req_free(&req);589
return (sock_err_t) TPEE_ERR_OK;590
}592
// @impl tpee_init593
tpee_t *tpee_init(void) {594
tpee_t *self = (tpee_t *) malloc(sizeof(tpee_t));595
*self = (tpee_t) {596
.sock = sock_init(),597
.fn = &_tpee_on_global,598
};599
sock_set_ext(self->sock, self);600
sock_on_global(self->sock, _tpee_on_handle);601
return self;602
}604
// @impl tpee_cleanup605
void tpee_cleanup(tpee_t *self) {606
if (self->sock != NULL) {607
sock_cleanup(self->sock);608
}609
if (_tpee_route_list != NULL && _tpee_route_list_len > 0) {610
for (size_t i = 0; i < _tpee_route_list_len; i++) {611
tpee_route_t *route = &_tpee_route_list[i];612
if (route->path) free(route->path);613
}614
free(_tpee_route_list);615
_tpee_route_list = NULL;616
_tpee_route_list_len = 0;617
}618
free(self);619
}621
// @impl tpee_on_global622
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn) {623
self->fn = fn;624
}626
// @impl tpee_on_route627
void tpee_on_route(tpee_method_t method, char *path, tpee_handle_fn_t fn) {628
_tpee_route_list = realloc(_tpee_route_list, sizeof(tpee_route_t) * (_tpee_route_list_len + 1));629
_tpee_route_list[_tpee_route_list_len] = (tpee_route_t) {630
.method = method,631
.path = strdup(path),632
.fn = fn,633
};634
_tpee_route_list_len++;635
}637
// @impl tpee_on_default638
void tpee_on_default(tpee_handle_fn_t fn) {639
_tpee_default_route = fn;640
}642
// @impl tpee_on_pre_route643
void tpee_on_pre_route(tpee_hook_fn_t fn) {644
_tpee_pre_route = fn;645
}647
// @impl tpee_on_post_route648
void tpee_on_post_route(tpee_hook_fn_t fn) {649
_tpee_post_route = fn;650
}652
// @impl tpee_req_path653
char *tpee_req_path(tpee_req_t *req) {654
char *uri = req->url;655
size_t url_len = strlen(uri);656
size_t i = 0;657
for (; i < url_len; i++) {658
size_t c = uri[i];659
if (c == '?') {660
break;661
}662
}663
size_t len = i;664
char *path = malloc(len + 1);665
memcpy(path, uri, len);666
path[len] = '\0';667
return path;668
}670
// @impl tpee_req_body671
char *tpee_req_body(tpee_req_t *req) {672
return req->body;673
}675
// @func _tpee_pair_decode676
// @internal677
char *_tpee_pair_decode(char *str, size_t len) {678
size_t j = 0;679
size_t k = 0;680
char decoded[len + 1];681
while (j < len) {682
if (str[j] == '%') {683
if (j + 2 < len && isxdigit(str[j + 1]) && isxdigit(str[j + 2])) {684
char hex[3] = { str[j + 1], str[j + 2], '\0' };685
decoded[k++] = (char) strtol(hex, NULL, 16);686
j += 3;687
continue;688
}689
decoded[k++] = '%';690
j++;691
continue;692
}693
if (str[j] == '+') {694
decoded[k++] = ' ';695
j++;696
continue;697
}698
decoded[k++] = str[j++];699
}700
decoded[k] = '\0';701
size_t ret_len = strlen(decoded);702
char *ret = malloc(ret_len + 1);703
memcpy(ret, decoded, ret_len);704
ret[ret_len] = '\0';705
return ret;706
}708
// @func _tpee_pair_list_init709
// @group tpee_pair_list_t710
// @internal711
tpee_pair_list_t _tpee_pair_list_init(char *uri) {712
tpee_pair_list_t self = (tpee_pair_list_t) {713
.pair_list = NULL,714
.len = 0,715
};716
size_t url_len = strlen(uri);717
size_t set = 0;718
size_t lim = 0;719
for (size_t i = 0; i < url_len + 1; i++) {720
// find 'key=value' pair721
if (i == url_len || uri[i] == '&') {722
size_t key_len = i - set;723
if (key_len > 0) {724
if (lim > set) {725
key_len = lim - set;726
}727
self.pair_list = realloc(self.pair_list, sizeof(tpee_pair_t) * (self.len + 1));728
tpee_pair_t *pair = &self.pair_list[self.len];729
// read key730
pair->key = _tpee_pair_decode(uri + set, key_len);731
pair->value = NULL;732
if (i > set + key_len + 1) {733
// read value734
size_t value_len = i - (set + key_len + 1);735
pair->value = _tpee_pair_decode(uri + set + key_len + 1, value_len);736
}737
self.len++;738
}739
set = i + 1;740
}741
// find '=' delimiter742
if (uri[i] == '=') {743
lim = i;744
}745
}746
return self;747
}749
// @impl tpee_req_queries750
tpee_pair_list_t tpee_req_queries(tpee_req_t *req) {751
size_t url_len = strlen(req->url);752
// skip path753
for (size_t i = 0; i < url_len; i++) {754
if (req->url[i] == '?') {755
return _tpee_pair_list_init(req->url + i + 1);756
}757
}758
return (tpee_pair_list_t) {759
.pair_list = NULL,760
.len = 0,761
};762
}764
// @impl tpee_req_params765
tpee_pair_list_t tpee_req_params(tpee_req_t *req) {766
return _tpee_pair_list_init(req->body);767
}769
// @impl tpee_pair_list_get770
char *tpee_pair_list_get(tpee_pair_list_t *self, char *key) {771
if (self && self->len > 0) {772
for (size_t i = 0; i < self->len; i++) {773
tpee_pair_t *pair = &self->pair_list[i];774
if (strcmp(pair->key, key) == 0) {775
return pair->value;776
}777
}778
}779
return NULL;780
}782
// @impl tpee_pair_list_has783
bool tpee_pair_list_has(tpee_pair_list_t *self, char *key) {784
if (self && self->len > 0) {785
for (size_t i = 0; i < self->len; i++) {786
tpee_pair_t *pair = &self->pair_list[i];787
if (strcmp(pair->key, key) == 0) {788
return true;789
}790
}791
}792
return false;793
}795
// @impl tpee_pair_list_free796
void tpee_pair_list_free(tpee_pair_list_t *self) {797
if (self == NULL) {798
return;799
}800
if (self->len > 0) {801
for (size_t i = 0; i < self->len; i++) {802
tpee_pair_t *pair = &self->pair_list[i];803
free(pair->key);804
if (pair->value) {805
free(pair->value);806
}807
}808
free(self->pair_list);809
}810
}812
// @impl _tpee_method_str813
const char *_tpee_method_str(tpee_method_t method) {814
switch (method) {815
case TPEE_METHOD_GET:816
return "GET";817
case TPEE_METHOD_HEAD:818
return "HEAD";819
case TPEE_METHOD_POST:820
return "POST";821
case TPEE_METHOD_PUT:822
return "PUT";823
case TPEE_METHOD_DELETE:824
return "DELETE";825
case TPEE_METHOD_CONNECT:826
return "CONNECT";827
case TPEE_METHOD_OPTIONS:828
return "OPTIONS";829
case TPEE_METHOD_TRACE:830
return "TRACE";831
case TPEE_METHOD_PATCH:832
return "PATCH";833
default:834
return NULL;835
}836
}838
// @func _tpee_status_str839
// @internal840
const char *_tpee_status_str(tpee_status_t status) {841
switch (status) {842
case TPEE_STATUS_CONTINUE:843
return "Continue";844
case TPEE_STATUS_SWITCHING_PROTOCOLS:845
return "Switching Protocols";846
case TPEE_STATUS_PROCESSING:847
return "Processing";848
case TPEE_STATUS_EARLY_HINTS:849
return "Early Hints";850
case TPEE_STATUS_OK:851
return "OK";852
case TPEE_STATUS_CREATED:853
return "Created";854
case TPEE_STATUS_ACCEPTED:855
return "Accepted";856
case TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION:857
return "Non-Authoritative Information";858
case TPEE_STATUS_NO_CONTENT:859
return "No Content";860
case TPEE_STATUS_RESET_CONTENT:861
return "Reset Content";862
case TPEE_STATUS_PARTIAL_CONTENT:863
return "Partial Content";864
case TPEE_STATUS_MULTI_STATUS:865
return "Multi-Status";866
case TPEE_STATUS_ALREADY_REPORTED:867
return "Already Reported";868
case TPEE_STATUS_IM_USED:869
return "IM Used";870
case TPEE_STATUS_MULTIPLE_CHOICES:871
return "Multiple Choices";872
case TPEE_STATUS_MOVED_PERMANENTLY:873
return "Moved Permanently";874
case TPEE_STATUS_FOUND:875
return "Found";876
case TPEE_STATUS_SEE_OTHER:877
return "See Other";878
case TPEE_STATUS_NOT_MODIFIED:879
return "Not Modified";880
case TPEE_STATUS_TEMPORARY_REDIRECT:881
return "Temporary Redirect";882
case TPEE_STATUS_PERMANENT_REDIRECT:883
return "Permanent Redirect";884
case TPEE_STATUS_BAD_REQUEST:885
return "Bad Request";886
case TPEE_STATUS_UNAUTHORIZED:887
return "Unauthorized";888
case TPEE_STATUS_PAYMENT_REQUIRED:889
return "Payment Required";890
case TPEE_STATUS_FORBIDDEN:891
return "Forbidden";892
case TPEE_STATUS_NOT_FOUND:893
return "Not Found";894
case TPEE_STATUS_METHOD_NOT_ALLOWED:895
return "Method Not Allowed";896
case TPEE_STATUS_NOT_ACCEPTABLE:897
return "Not Acceptable";898
case TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED:899
return "Proxy Authentication Required";900
case TPEE_STATUS_REQUEST_TIMEOUT:901
return "Request Timeout";902
case TPEE_STATUS_CONFLICT:903
return "Conflict";904
case TPEE_STATUS_GONE:905
return "Gone";906
case TPEE_STATUS_LENGTH_REQUIRED:907
return "Length Required";908
case TPEE_STATUS_PRECONDITION_FAILED:909
return "Precondition Failed";910
case TPEE_STATUS_PAYLOAD_TOO_LARGE:911
return "Payload Too Large";912
case TPEE_STATUS_URI_TOO_LONG:913
return "URI Too Long";914
case TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE:915
return "Unsupported Media Type";916
case TPEE_STATUS_RANGE_NOT_SATISFIABLE:917
return "Range Not Satisfiable";918
case TPEE_STATUS_EXPECTATION_FAILED:919
return "Expectation Failed";920
case TPEE_STATUS_I_AM_A_TEAPOT:921
return "I'm a teapot";922
case TPEE_STATUS_MISDIRECTED_REQUEST:923
return "Misdirected Request";924
case TPEE_STATUS_UNPROCESSABLE_CONTENT:925
return "Unprocessable Content";926
case TPEE_STATUS_LOCKED:927
return "Locked";928
case TPEE_STATUS_FAILED_DEPENDENCY:929
return "Failed Dependency";930
case TPEE_STATUS_TOO_EARLY:931
return "Too Early";932
case TPEE_STATUS_UPGRADE_REQUIRED:933
return "Upgrade Required";934
case TPEE_STATUS_PRECONDITION_REQUIRED:935
return "Precondition Required";936
case TPEE_STATUS_TOO_MANY_REQUESTS:937
return "Too Many Requests";938
case TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE:939
return "Request Header Fields Too Large";940
case TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS:941
return "Unavailable For Legal Reasons";942
case TPEE_STATUS_INTERNAL_SERVER_ERROR:943
return "Internal Server Error";944
case TPEE_STATUS_NOT_IMPLEMENTED:945
return "Not Implemented";946
case TPEE_STATUS_BAD_GATEWAY:947
return "Bad Gateway";948
case TPEE_STATUS_SERVICE_UNAVAILABLE:949
return "Service Unavailable";950
case TPEE_STATUS_GATEWAY_TIMEOUT:951
return "Gateway Timeout";952
case TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED:953
return "HTTP Version Not Supported";954
case TPEE_STATUS_VARIANT_ALSO_NEGOTITATES:955
return "Variant Also Negotiates";956
case TPEE_STATUS_INSUFFICIENT_STORAGE:957
return "Insufficient Storage";958
case TPEE_STATUS_LOOP_DETECTED:959
return "Loop Detected";960
case TPEE_STATUS_NOT_EXTENDED:961
return "Not Extended";962
case TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED:963
return "Network Authentication Required";964
default:965
return NULL;966
}967
}969
// @impl tpee_conn_read970
ssize_t tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len) {971
return sock_conn_read(conn, buffer, len);972
}974
// @impl tpee_conn_write975
ssize_t tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len) {976
return sock_conn_write(conn, buffer, len);977
}979
// @impl tpee_req_header980
char *tpee_req_header(tpee_req_t *req, char *key) {981
if (req->headers_len > 0) {982
for (int i = 0; i < req->headers_len; i++) {983
tpee_header_t *header = &req->headers[i];984
if (strcasecmp(header->key, key) == 0) {985
return header->value;986
}987
}988
}989
return NULL;990
}992
// @impl tpee_resp_header993
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value) {994
resp->headers = (tpee_header_t *) realloc(resp->headers, sizeof(tpee_header_t) * (resp->headers_len + 1));995
tpee_header_t *new_header = &resp->headers[resp->headers_len];996
resp->headers_len++;997
int key_len = strlen(key);998
new_header->key = (char *) malloc(sizeof(char) * (key_len + 1));999
memcpy(new_header->key, key, key_len);1000
new_header->key[key_len] = '\0';1001
int value_len = strlen(value);1002
new_header->value = (char *) malloc(sizeof(char) * (value_len + 1));1003
memcpy(new_header->value, value, value_len);1004
new_header->value[value_len] = '\0';1005
}1007
// @impl tpee_resp_create1008
tpee_resp_t tpee_resp_create(tpee_status_t status) {1009
return (tpee_resp_t) {1010
.version = strdup(TPEE_VERSION),1011
.status = status,1012
.headers = NULL,1013
.headers_len = 0,1014
.body = NULL,1015
.body_len = 0,1016
};1017
}1019
// @impl tpee_resp_body1020
void tpee_resp_body(tpee_resp_t *resp, char *body, long len) {1021
if (resp != NULL && body != NULL) {1022
resp->body = (char *) malloc(sizeof(char) * len);1023
memcpy(resp->body, body, len);1024
resp->body_len = len;1025
}1026
}1028
// @impl tpee_req_free1029
void tpee_req_free(tpee_req_t *req) {1030
if (req->method != NULL) {1031
free(req->method);1032
}1033
if (req->url != NULL) {1034
free(req->url);1035
}1036
if (req->version != NULL) {1037
free(req->version);1038
}1039
if (req->headers_len > 0) {1040
for (int i = 0; i < req->headers_len; i++) {1041
tpee_header_t *header = &req->headers[i];1042
if (header->key != NULL) {1043
free(header->key);1044
}1045
if (header->value != NULL) {1046
free(header->value);1047
}1048
}1049
free(req->headers);1050
}1051
if (req->body != NULL) {1052
free(req->body);1053
}1054
if (req->ext_data != NULL) {1055
free(req->ext_data);1056
}1057
}1059
// @impl tpee_resp_free1060
void tpee_resp_free(tpee_resp_t *resp) {1061
if (resp->version != NULL) {1062
free(resp->version);1063
}1064
if (resp->headers_len > 0) {1065
for (int i = 0; i < resp->headers_len; i++) {1066
tpee_header_t *header = &resp->headers[i];1067
if (header->key != NULL) {1068
free(header->key);1069
}1070
if (header->value != NULL) {1071
free(header->value);1072
}1073
}1074
if (resp->headers != NULL) {1075
free(resp->headers);1076
}1077
}1078
if (resp->body != NULL) {1079
free(resp->body);1080
}1081
}1083
// @impl _tpee_req_read1084
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn) {1085
int len = 0;1086
int ln = 0;1087
int start = 0;1088
char buffer[TPEE_MAX_REQUEST_HEAD_SIZE];1089
*req = (tpee_req_t) {1090
.conn = conn,1091
.method = NULL,1092
.url = NULL,1093
.version = NULL,1094
.headers = NULL,1095
.headers_len = 0,1096
.body = NULL,1097
.body_len = 0,1098
.ext_data = NULL,1099
};1100
while (true) {1101
char c;1102
int read = tpee_conn_read(conn, &c, 1);1103
if (read == 0) {1104
break;1105
}1106
// read timeout1107
if (read == -1 || read == SSL_ERROR_SYSCALL) {1108
if (errno == EWOULDBLOCK || errno == EAGAIN) {1109
return (tpee_err_t) SOCK_ERR_READ_TIMEOUT;1110
}1111
}1112
// break if too long1113
if (len == TPEE_MAX_REQUEST_HEAD_SIZE) {1114
return (tpee_err_t) SOCK_ERR_REQUEST_TOO_LONG;1115
}1116
buffer[len] = c;1117
len++;1118
// read head1119
if (len >= 2) {1120
char *slice = buffer + (len - 2);1121
if (strncmp(slice, "\r\n", 2) == 0) {1122
int line_len = (len - 2) - start;1123
if (line_len > 0) {1124
// read line1125
char line[line_len + 1];1126
memcpy(line, buffer + start, line_len);1127
line[line_len] = '\0';1128
// read method, url, and version1129
if (ln == 0) {1130
int arg = 0;1131
int start = 0;1132
for (int i = 0; i <= line_len; i++) {1133
if (line[i] == ' ' || i == line_len) {1134
// read method1135
if (arg == 0) {1136
int method_length = i - start;1137
req->method = (char *) malloc(sizeof(char) * (method_length + 1));1138
memcpy(req->method, line + start, method_length);1139
req->method[method_length] = '\0';1140
}1141
// read url1142
if (arg == 1) {1143
int url_length = i - start;1144
req->url = (char *) malloc(sizeof(char) * (url_length + 1));1145
memcpy(req->url, line + start, url_length);1146
req->url[url_length] = '\0';1147
}1148
// read version1149
if (arg == 2) {1150
int version_length = i - start;1151
req->version = (char *) malloc(sizeof(char) * (version_length + 1));1152
memcpy(req->version, line + start, version_length);1153
req->version[version_length] = '\0';1154
}1155
start = i + 1;1156
arg++;1157
}1158
}1159
}1160
// read headers1161
else {1162
int sep = 0;1163
req->headers = (tpee_header_t *) realloc(req->headers, sizeof(tpee_header_t) * (req->headers_len + 1));1164
tpee_header_t *header = &req->headers[req->headers_len];1165
req->headers_len++;1166
for (int i = 0; i < line_len; i++) {1167
if (line[i] == ':') {1168
int key_len = i;1169
header->key = (char *) malloc(sizeof(char) * (key_len + 1));1170
memcpy(header->key, line, key_len);1171
header->key[key_len] = '\0';1172
sep = i + 2;1173
break;1174
}1175
}1176
int value_len = line_len - sep;1177
header->value = (char *) malloc(sizeof(char) * (value_len + 1));1178
memcpy(header->value, line + sep, value_len);1179
header->value[value_len] = '\0';1180
}1181
}1182
start = len;1183
ln++;1184
}1185
}1186
// read body1187
if (len >= 4) {1188
char *slice = buffer + (len - 4);1189
if (strncmp(slice, "\r\n\r\n", 4) == 0) {1190
char *content_length = tpee_req_header(req, (char *) "Content-Length");1191
if (content_length != NULL) {1192
req->body_len = strtol(content_length, NULL, 10);1193
if (req->body_len > 0) {1194
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));1195
tpee_conn_read(conn, req->body, req->body_len);1196
req->body[req->body_len] = '\0';1197
}1198
}1199
break;1200
}1201
}1202
}1203
return TPEE_ERR_OK;1204
}1206
// @impl tpee_resp_write_head1207
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn) {1208
tpee_conn_write(conn, resp->version, strlen(resp->version));1209
tpee_conn_write(conn, (char *) " ", 1);1210
char status[4];1211
snprintf(status, 4, "%d", resp->status);1212
tpee_conn_write(conn, status, strlen(status));1213
tpee_conn_write(conn, (char *) " ", 1);1214
const char *status_message = _tpee_status_str(resp->status);1215
tpee_conn_write(conn, (char *) status_message, strlen(status_message));1216
tpee_conn_write(conn, (char *) "\r\n", 2);1217
if (resp->headers_len > 0) {1218
for (int i = 0; i < resp->headers_len; i++) {1219
tpee_header_t *header = &resp->headers[i];1220
tpee_conn_write(conn, header->key, strlen(header->key));1221
tpee_conn_write(conn, (char *) ": ", 2);1222
tpee_conn_write(conn, header->value, strlen(header->value));1223
tpee_conn_write(conn, (char *) "\r\n", 2);1224
}1225
}1226
tpee_conn_write(conn, (char *) "\r\n", 2);1227
}1229
// @impl tpee_resp_write_body1230
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn) {1231
if (resp->body != NULL) {1232
tpee_conn_write(conn, resp->body, resp->body_len);1233
}1234
}1236
// @impl tpee_resp_write1237
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn) {1238
if (resp->status == __TPEE_STATUS_HANDLED) {1239
return;1240
}1241
bool has_content_length = false;1242
if (resp->body != NULL) {1243
if (resp->headers_len > 0) {1244
for (int i = 0; i < resp->headers_len; i++) {1245
tpee_header_t *header = &resp->headers[i];1246
if (strcasecmp(header->key, "Content-Length") == 0) {1247
has_content_length = true;1248
break;1249
}1250
}1251
}1252
if (!has_content_length) {1253
char content_length[50];1254
snprintf(content_length, 50, "%ld", resp->body_len);1255
tpee_resp_header(resp, (char *) "Content-Length", content_length);1256
}1257
}1258
tpee_resp_write_head(resp, conn);1259
tpee_resp_write_body(resp, conn);1260
}1262
// @impl tpee_bind1263
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len) {1264
return (tpee_err_t) sock_bind(self->sock, hostname, port, hosts, hosts_len);1265
}1267
// @impl tpee_listen1268
tpee_err_t tpee_listen(tpee_t *self) {1269
return (tpee_err_t) sock_listen(self->sock);1270
}1272
// @impl tpee_bind_listen1273
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port) {1274
return (tpee_err_t) sock_bind_listen(self->sock, hostname, port);1275
}1277
// @impl tpee_close1278
void tpee_close(tpee_t *self) {1279
sock_close(self->sock);1280
}1282
#endif // TPEE_IMPL