A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
blob: 705cbe1dc27fedfb272c8b51071ddde86dd1e77c (38370 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
// @include <openssl/ssl.h>27
// @desc OpenSSL library handling TLS/SSL.28
#include <openssl/ssl.h>30
#ifdef TPEE_IMPL31
#define SOCK_IMPL32
#endif // TPEE_IMPL34
// @include <sock.h>35
// @desc sock library handling server/client communication and multi-threading.36
#include <sock.h>38
// @enum tpee_err_t39
// @desc Error code.40
typedef enum {41
TPEE_ERR_OK = SOCK_ERR_OK,42
TPEE_ERR__COUNT,43
} tpee_err_t;45
// @enum tpee_method_t46
// @desc tpee method type.47
typedef enum {48
TPEE_METHOD_GET,49
TPEE_METHOD_HEAD,50
TPEE_METHOD_POST,51
TPEE_METHOD_PUT,52
TPEE_METHOD_DELETE,53
TPEE_METHOD_CONNECT,54
TPEE_METHOD_OPTIONS,55
TPEE_METHOD_TRACE,56
TPEE_METHOD_PATCH,57
} tpee_method_t;59
// @enum tpee_status_t60
// @desc tpee status code.61
typedef enum {62
TPEE_STATUS_CONTINUE = 100,63
TPEE_STATUS_SWITCHING_PROTOCOLS = 101,64
TPEE_STATUS_PROCESSING = 102,65
TPEE_STATUS_EARLY_HINTS = 103,66
TPEE_STATUS_OK = 200,67
TPEE_STATUS_CREATED = 201,68
TPEE_STATUS_ACCEPTED = 202,69
TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,70
TPEE_STATUS_NO_CONTENT = 204,71
TPEE_STATUS_RESET_CONTENT = 205,72
TPEE_STATUS_PARTIAL_CONTENT = 206,73
TPEE_STATUS_MULTI_STATUS = 207,74
TPEE_STATUS_ALREADY_REPORTED = 208,75
TPEE_STATUS_IM_USED = 226,76
TPEE_STATUS_MULTIPLE_CHOICES = 300,77
TPEE_STATUS_MOVED_PERMANENTLY = 301,78
TPEE_STATUS_FOUND = 302,79
TPEE_STATUS_SEE_OTHER = 303,80
TPEE_STATUS_NOT_MODIFIED = 304,81
TPEE_STATUS_TEMPORARY_REDIRECT = 307,82
TPEE_STATUS_PERMANENT_REDIRECT = 308,83
TPEE_STATUS_BAD_REQUEST = 400,84
TPEE_STATUS_UNAUTHORIZED = 401,85
TPEE_STATUS_PAYMENT_REQUIRED = 402,86
TPEE_STATUS_FORBIDDEN = 403,87
TPEE_STATUS_NOT_FOUND = 404,88
TPEE_STATUS_METHOD_NOT_ALLOWED = 405,89
TPEE_STATUS_NOT_ACCEPTABLE = 406,90
TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,91
TPEE_STATUS_REQUEST_TIMEOUT = 408,92
TPEE_STATUS_CONFLICT = 409,93
TPEE_STATUS_GONE = 410,94
TPEE_STATUS_LENGTH_REQUIRED = 411,95
TPEE_STATUS_PRECONDITION_FAILED = 412,96
TPEE_STATUS_PAYLOAD_TOO_LARGE = 413,97
TPEE_STATUS_URI_TOO_LONG = 414,98
TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,99
TPEE_STATUS_RANGE_NOT_SATISFIABLE = 416,100
TPEE_STATUS_EXPECTATION_FAILED = 417,101
TPEE_STATUS_I_AM_A_TEAPOT = 418,102
TPEE_STATUS_MISDIRECTED_REQUEST = 421,103
TPEE_STATUS_UNPROCESSABLE_CONTENT = 422,104
TPEE_STATUS_LOCKED = 423,105
TPEE_STATUS_FAILED_DEPENDENCY = 424,106
TPEE_STATUS_TOO_EARLY = 425,107
TPEE_STATUS_UPGRADE_REQUIRED = 426,108
TPEE_STATUS_PRECONDITION_REQUIRED = 428,109
TPEE_STATUS_TOO_MANY_REQUESTS = 429,110
TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,111
TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,112
TPEE_STATUS_INTERNAL_SERVER_ERROR = 500,113
TPEE_STATUS_NOT_IMPLEMENTED = 501,114
TPEE_STATUS_BAD_GATEWAY = 502,115
TPEE_STATUS_SERVICE_UNAVAILABLE = 503,116
TPEE_STATUS_GATEWAY_TIMEOUT = 504,117
TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,118
TPEE_STATUS_VARIANT_ALSO_NEGOTITATES = 506,119
TPEE_STATUS_INSUFFICIENT_STORAGE = 507,120
TPEE_STATUS_LOOP_DETECTED = 508,121
TPEE_STATUS_NOT_EXTENDED = 510,122
TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,123
__TPEE_STATUS_HANDLED = 999,124
} tpee_status_t;126
// @struct tpee_t127
// @groupdef tpee_t128
// @desc Global context struct. Implemented here {tpee}.129
typedef struct tpee tpee_t;131
// @type tpee_host_t132
// @desc Represents the host.133
typedef sock_host_t tpee_host_t;135
// @type tpee_bind_t136
// @desc Represents the binding (port).137
typedef sock_bind_t tpee_bind_t;139
// @type tpee_conn_t140
// @groupdef tpee_conn_t141
// @desc Represents a client connection.142
typedef sock_conn_t tpee_conn_t;144
// @struct tpee_header_t145
// @desc A tpee header.146
typedef struct {147
char *key;148
char *value;149
} tpee_header_t;151
// @struct tpee_req_t152
// @groupdef tpee_req_t153
// @desc A tpee request struct.154
typedef struct {155
tpee_conn_t *conn;156
char *method;157
char *url;158
char *version;159
tpee_header_t *headers;160
int headers_len;161
char *body;162
long body_len;163
void *ext_data;164
} tpee_req_t;166
// @struct tpee_resp_t167
// @groupdef tpee_resp_t168
// @desc A tpee response struct.169
typedef struct {170
char *version;171
tpee_status_t status;172
tpee_header_t *headers;173
int headers_len;174
char *body;175
long body_len;176
} tpee_resp_t;178
// @type tpee_global_fn_t179
// @desc A function type for a global route handler.180
typedef tpee_resp_t (*tpee_global_fn_t)(tpee_t *, tpee_req_t *);182
// @type tpee_handle_fn_t183
// @desc A function type for a route handler.184
typedef tpee_resp_t (*tpee_handle_fn_t)(tpee_req_t *);186
// @type tpee_hook_fn_t187
// @desc A function type for a hook handler.188
typedef void (*tpee_hook_fn_t)(tpee_req_t *);190
// @struct tpee_route_t191
// @desc A route.192
typedef struct {193
tpee_method_t method;194
char *path;195
tpee_handle_fn_t fn;196
} tpee_route_t;198
// @struct tpee_pair_t199
typedef struct {200
char *key;201
char *value;202
} tpee_pair_t;204
// @struct tpee_pair_list_t205
// @groupdef tpee_pair_list_t206
typedef struct {207
tpee_pair_t *pair_list;208
size_t len;209
} tpee_pair_list_t;211
// @impl tpee_t212
struct tpee {213
sock_t *sock;214
tpee_global_fn_t fn;215
};217
// @func tpee_init218
// @group tpee_t219
// @desc Initializes global library context {tpee_t}. Free with {tpee_cleanup}.220
// @return {tpee_t} * - Returns the global library context.221
tpee_t *tpee_init(void);223
// @func tpee_cleanup224
// @group tpee_t225
// @desc Cleans up {tpee_t}.226
// @param {tpee_t} *self - The global library context.227
void tpee_cleanup(tpee_t *self);229
// @func tpee_on_global230
// @group tpee_t231
// @desc Set and override the default global route handler {_tpee_on_global}.232
// @param {tpee_t} *self - The global library context.233
// @param {tpee_global_fn_t} fn - The global route handler.234
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn);236
// @func tpee_on_route237
// @note Use macros like {TPEE_GET} and others to register routes more easily.238
// @desc Registers a new route.239
// @param {tpee_method_t} method - The tpee method.240
// @param char *url - The tpee URL (path).241
// @param {tpee_handle_fn_t} fn - The route handler.242
void tpee_on_route(tpee_method_t method, char *url, tpee_handle_fn_t fn);244
// @func tpee_on_default245
// @note Use macro {TPEE_DEFAULT} instead.246
// @desc Registers a fallback handler.247
// @param {tpee_handle_fn_t} fn - The route handler.248
void tpee_on_default(tpee_handle_fn_t fn);250
// @func tpee_on_pre_route251
// @note Use macro {TPEE_PRE_HOOK} instead.252
// @desc Registers a hook handler which is performed before the route handler is executed.253
// @param {tpee_hook_fn_t} fn - The hook handler.254
void tpee_on_pre_route(tpee_hook_fn_t fn);256
// @func tpee_on_post_route257
// @note Use macro {TPEE_POST_HOOK} instead.258
// @desc Registers a hook handler which is performed after the route handler is executed.259
// @param {tpee_hook_fn_t} fn - The hook handler.260
void tpee_on_post_route(tpee_hook_fn_t fn);262
// @func tpee_req_path263
// @group tpee_req_t264
// @param {tpee_req_t} *req - The tpee request.265
// @return char * - Returns the request path.266
char *tpee_req_path(tpee_req_t *req);268
// @func tpee_req_body269
// @group tpee_req_t270
// @param {tpee_req_t} *req - The tpee request.271
// @return char * - Returns the request body.272
char *tpee_req_body(tpee_req_t *req);274
// @func tpee_req_queries275
// @group tpee_req_t276
// @param {tpee_req_t} *req - The tpee request.277
// @return {tpee_pair_list_t} - Returns the request queries.278
tpee_pair_list_t tpee_req_queries(tpee_req_t *req);280
// @func tpee_req_params281
// @group tpee_req_t282
// @param {tpee_req_t} *req - The tpee request.283
// @return {tpee_pair_list_t} - Returns the request params.284
tpee_pair_list_t tpee_req_params(tpee_req_t *req);286
// @func tpee_pair_list_get287
// @group tpee_pair_list_t288
char *tpee_pair_list_get(tpee_pair_list_t *self, char *key);290
// @func tpee_pair_list_has291
// @group tpee_pair_list_t292
bool tpee_pair_list_has(tpee_pair_list_t *self, char *key);294
// @func tpee_pair_list_free295
// @group tpee_pair_list_t296
void tpee_pair_list_free(tpee_pair_list_t *self);298
// @func tpee_conn_read299
// @group tpee_conn_t300
// @param {tpee_conn_t} *conn - The connection.301
// @param char * - The buffer to read into.302
// @param size_t - The size of the buffer.303
ssize_t tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len);305
// @func tpee_conn_write306
// @group tpee_conn_t307
// @param {tpee_conn_t} *conn - The connection.308
// @param char * - The buffer to be written.309
// @param size_t - The size of the buffer.310
ssize_t tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len);312
// @func tpee_req_header313
// @group tpee_req_t314
// @param {tpee_req_t} *req - The tpee request.315
// @param char *key - The header key.316
// @return char * - Returns a header value.317
char *tpee_req_header(tpee_req_t *req, char *key);319
// @func tpee_resp_header320
// @group tpee_resp_t321
// @desc Writes a header to the response. The header content is consumed and can be freed afterward.322
// @param {tpee_resp_t} *resp - The tpee response.323
// @param char *key - The header key.324
// @param char *value - The header value.325
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value);327
// @func tpee_resp_create328
// @group tpee_resp_t329
// @desc Creates a new response with a given tpee status. Freed with {tpee_resp_free}.330
// @param {tpee_status_t} status - The tpee status code.331
// @return {tpee_resp_t} - Returns a tpee response.332
tpee_resp_t tpee_resp_create(tpee_status_t status);334
// @func tpee_resp_body335
// @group tpee_resp_t336
// @desc Writes body content to the response. The body content is consumed and can be freed afterward.337
// @param {tpee_resp_t} *resp - The tpee response.338
// @param char *body - The body content.339
// @param long len - The body length.340
void tpee_resp_body(tpee_resp_t *resp, char *body, long len);342
// @func tpee_req_free343
// @group tpee_req_t344
// @desc Frees a request.345
// @param {tpee_req_t} *req - The tpee request.346
void tpee_req_free(tpee_req_t *req);348
// @func tpee_resp_write_head349
// @group tpee_resp_t350
// @desc Writes a response header to the socket stream.351
// @param {tpee_resp_t} *resp - The tpee response.352
// @param {tpee_conn_t} *conn - The tpee connection.353
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn);355
// @func tpee_resp_write_body356
// @group tpee_resp_t357
// @desc Writes a response body to the socket stream.358
// @param {tpee_resp_t} *resp - The tpee response.359
// @param {tpee_conn_t} *conn - The tpee connection.360
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn);362
// @func tpee_resp_write363
// @group tpee_resp_t364
// @desc Writes a response to the socket stream.365
// @param {tpee_resp_t} *resp - The tpee response.366
// @param {tpee_conn_t} *conn - The tpee connection.367
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn);369
// @func tpee_resp_free370
// @group tpee_resp_t371
// @desc Frees a response.372
// @param {tpee_resp_t} *resp - The tpee response.373
void tpee_resp_free(tpee_resp_t *resp);375
// @func tpee_bind376
// @group tpee_t377
// @param {tpee_t} *self - The global library context.378
// @param char *hostname - The hostname.379
// @param int port - The port.380
// @param {tpee_host_t} *hosts - The host to bind to.381
// @param int hosts_len - The host length of to be bound host.382
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len);384
// @func tpee_listen385
// @group tpee_t386
// @param {tpee_t} *self - The global library context.387
tpee_err_t tpee_listen(tpee_t *self);389
// @func tpee_bind_listen390
// @group tpee_t391
// @param {tpee_t} *self - The global library context.392
// @param char *hostname - The hostname.393
// @param int port - The port.394
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port);396
// @func tpee_close397
// @group tpee_t398
// @param {tpee_t} *self - The global library context.399
void tpee_close(tpee_t *self);401
// @func _tpee_req_read402
// @group tpee_req_t403
// @internal404
// @param {tpee_req_t} *req - The tpee request.405
// @param {tpee_conn_t} *conn - The tpee connection.406
// @return {tpee_err_t} - Returns an tpee error code.407
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn);409
// @func _tpee_method_str410
// @internal411
// @desc Get tpee method string.412
const char *_tpee_method_str(tpee_method_t method);414
// @macro TPEE_RESP_HANDLED415
// @group tpee_resp_t416
// @desc Handled {tpee_resp_t}.417
#define TPEE_RESP_HANDLED (tpee_resp_t) { .status = __TPEE_STATUS_HANDLED }419
// @macro _TPEE_ON420
// @internal421
#define _TPEE_ON(method, path, fn_name) \422
static tpee_resp_t fn_name(tpee_req_t *req); \423
__attribute__((constructor)) \424
static void __macro_tpee_fn_register_##fn_name(void) { \425
tpee_on_route(method, path, &fn_name); \426
} \427
static tpee_resp_t fn_name(tpee_req_t *req)429
// @macro TPEE_GET430
#define TPEE_GET(path, fn_name) _TPEE_ON(TPEE_METHOD_GET, path, fn_name)432
// @macro TPEE_HEAD433
#define TPEE_HEAD(path, fn_name) _TPEE_ON(TPEE_METHOD_HEAD, path, fn_name)435
// @macro TPEE_POST436
#define TPEE_POST(path, fn_name) _TPEE_ON(TPEE_METHOD_POST, path, fn_name)438
// @macro TPEE_PUT439
#define TPEE_PUT(path, fn_name) _TPEE_ON(TPEE_METHOD_PUT, path, fn_name)441
// @macro TPEE_DELETE442
#define TPEE_DELETE(path, fn_name) _TPEE_ON(TPEE_METHOD_DELETE, path, fn_name)444
// @macro TPEE_CONNECT445
#define TPEE_CONNECT(path, fn_name) _TPEE_ON(TPEE_METHOD_CONNECT, path, fn_name)447
// @macro TPEE_OPTIONS448
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON(TPEE_METHOD_OPTIONS, path, fn_name)450
// @macro TPEE_TRACE451
#define TPEE_TRACE(path, fn_name) _TPEE_ON(TPEE_METHOD_TRACE, path, fn_name)453
// @macro TPEE_PATCH454
#define TPEE_PATCH(path, fn_name) _TPEE_ON(TPEE_METHOD_PATCH, path, fn_name)456
// @macro TPEE_DEFAULT457
#define TPEE_DEFAULT(fn_name) \458
static tpee_resp_t fn_name(tpee_req_t *req); \459
__attribute__((constructor)) \460
static void __macro_tpee_fn_register_##fn_name(void) { \461
tpee_on_default(&fn_name); \462
} \463
static tpee_resp_t fn_name(tpee_req_t *req)465
// @macro TPEE_PRE_HOOK466
#define TPEE_PRE_HOOK(fn_name) \467
static void fn_name(tpee_req_t *req); \468
__attribute__((constructor)) \469
static void __macro_tpee_fn_register_##fn_name(void) { \470
tpee_on_pre_route(&fn_name); \471
} \472
static void fn_name(tpee_req_t *req)474
// @macro TPEE_POST_HOOK475
#define TPEE_POST_HOOK(fn_name) \476
static void fn_name(tpee_req_t *req); \477
__attribute__((constructor)) \478
static void __macro_tpee_fn_register_##fn_name(void) { \479
tpee_on_post_route(&fn_name); \480
} \481
static void fn_name(tpee_req_t *req)483
#ifdef TPEE_IMPL485
#include <stdio.h>486
#include <stdlib.h>487
#include <string.h>488
#include <ctype.h>489
#include <time.h>490
#include <unistd.h>491
#include <pthread.h>492
#include <netdb.h>493
#include <poll.h>494
#include <errno.h>495
#include <signal.h>497
// @macro TPEE_VERSION498
// @internal499
#define TPEE_VERSION "HTTP/1.1"501
// @macro TPEE_MAX_REQUEST_HEAD_SIZE502
// @internal503
#define TPEE_MAX_REQUEST_HEAD_SIZE 1000000505
// @var _tpee_route_list506
// @internal507
tpee_route_t *_tpee_route_list = NULL;509
// @var _tpee_route_list_len510
// @internal511
size_t _tpee_route_list_len = 0;513
// @var _tpee_default_route514
// @internal515
tpee_handle_fn_t _tpee_default_route;517
// @var _tpee_pre_route518
// @internal519
tpee_hook_fn_t _tpee_pre_route;521
// @var _tpee_post_route522
// @internal523
tpee_hook_fn_t _tpee_post_route;525
// @func _tpee_on_global526
// @group tpee_t527
// @internal528
// @desc The default route handler. Can be overwritten with {tpee_on_global}.529
// @param {tpee_t} *self - The global library context.530
// @param {tpee_req_t} *req - The tpee request.531
// @return {tpee_resp_t} - Returns a tpee response.532
tpee_resp_t _tpee_on_global(tpee_t *self, tpee_req_t *req) {533
(void) self;534
tpee_resp_t resp = tpee_resp_create(TPEE_STATUS_NOT_FOUND);535
if (_tpee_pre_route != NULL) {536
_tpee_pre_route(req);537
}538
for (size_t i = 0; i < _tpee_route_list_len; i++) {539
tpee_route_t *route = &_tpee_route_list[i];540
const char *method = _tpee_method_str(route->method);541
char *path = tpee_req_path(req);542
if (strcmp(method, req->method) == 0) {543
size_t len = strlen(route->path);544
char c = route->path[len - 1];545
if (c == '*') {546
if (strncmp(route->path, path, len - 1) == 0) {547
tpee_resp_free(&resp);548
resp = route->fn(req);549
}550
}551
else {552
if (strcmp(route->path, path) == 0) {553
tpee_resp_free(&resp);554
resp = route->fn(req);555
}556
}557
}558
free(path);559
if (resp.status != TPEE_STATUS_NOT_FOUND) {560
if (_tpee_post_route != NULL) {561
_tpee_post_route(req);562
}563
return resp;564
}565
}566
if (_tpee_default_route != NULL) {567
tpee_resp_free(&resp);568
resp = _tpee_default_route(req);569
}570
if (_tpee_post_route != NULL) {571
_tpee_post_route(req);572
}573
return resp;574
}576
// @func _tpee_on_handle577
// @internal578
sock_err_t _tpee_on_handle(sock_t *sock, tpee_conn_t *conn) {579
tpee_t *self= (tpee_t *) sock->ext;580
tpee_req_t req;581
tpee_err_t err = _tpee_req_read(&req, conn);582
if (err != TPEE_ERR_OK) {583
return (sock_err_t) err;584
}585
if (req.method != NULL && req.url != NULL) {586
if (self->fn != NULL) {587
tpee_resp_t resp = self->fn(self, &req);588
tpee_resp_write(&resp, conn);589
tpee_resp_free(&resp);590
}591
}592
tpee_req_free(&req);593
return (sock_err_t) TPEE_ERR_OK;594
}596
// @impl tpee_init597
tpee_t *tpee_init(void) {598
tpee_t *self = (tpee_t *) malloc(sizeof(tpee_t));599
*self = (tpee_t) {600
.sock = sock_init(),601
.fn = &_tpee_on_global,602
};603
sock_set_ext(self->sock, self);604
sock_on_global(self->sock, _tpee_on_handle);605
return self;606
}608
// @impl tpee_cleanup609
void tpee_cleanup(tpee_t *self) {610
if (self->sock != NULL) {611
sock_cleanup(self->sock);612
}613
if (_tpee_route_list != NULL && _tpee_route_list_len > 0) {614
for (size_t i = 0; i < _tpee_route_list_len; i++) {615
tpee_route_t *route = &_tpee_route_list[i];616
if (route->path) free(route->path);617
}618
free(_tpee_route_list);619
_tpee_route_list = NULL;620
_tpee_route_list_len = 0;621
}622
free(self);623
}625
// @impl tpee_on_global626
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn) {627
self->fn = fn;628
}630
// @impl tpee_on_route631
void tpee_on_route(tpee_method_t method, char *path, tpee_handle_fn_t fn) {632
_tpee_route_list = realloc(_tpee_route_list, sizeof(tpee_route_t) * (_tpee_route_list_len + 1));633
_tpee_route_list[_tpee_route_list_len] = (tpee_route_t) {634
.method = method,635
.path = strdup(path),636
.fn = fn,637
};638
_tpee_route_list_len++;639
}641
// @impl tpee_on_default642
void tpee_on_default(tpee_handle_fn_t fn) {643
_tpee_default_route = fn;644
}646
// @impl tpee_on_pre_route647
void tpee_on_pre_route(tpee_hook_fn_t fn) {648
_tpee_pre_route = fn;649
}651
// @impl tpee_on_post_route652
void tpee_on_post_route(tpee_hook_fn_t fn) {653
_tpee_post_route = fn;654
}656
// @impl tpee_req_path657
char *tpee_req_path(tpee_req_t *req) {658
char *uri = req->url;659
size_t url_len = strlen(uri);660
size_t i = 0;661
for (; i < url_len; i++) {662
size_t c = uri[i];663
if (c == '?') {664
break;665
}666
}667
size_t len = i;668
char *path = malloc(len + 1);669
memcpy(path, uri, len);670
path[len] = '\0';671
return path;672
}674
// @impl tpee_req_body675
char *tpee_req_body(tpee_req_t *req) {676
return req->body;677
}679
// @func _tpee_pair_decode680
// @internal681
char *_tpee_pair_decode(char *str, size_t len) {682
size_t j = 0;683
size_t k = 0;684
char decoded[len + 1];685
while (j < len) {686
if (str[j] == '%') {687
if (j + 2 < len && isxdigit(str[j + 1]) && isxdigit(str[j + 2])) {688
char hex[3] = { str[j + 1], str[j + 2], '\0' };689
decoded[k++] = (char) strtol(hex, NULL, 16);690
j += 3;691
continue;692
}693
decoded[k++] = '%';694
j++;695
continue;696
}697
if (str[j] == '+') {698
decoded[k++] = ' ';699
j++;700
continue;701
}702
decoded[k++] = str[j++];703
}704
decoded[k] = '\0';705
size_t ret_len = strlen(decoded);706
char *ret = malloc(ret_len + 1);707
memcpy(ret, decoded, ret_len);708
ret[ret_len] = '\0';709
return ret;710
}712
// @func _tpee_pair_list_init713
// @group tpee_pair_list_t714
// @internal715
tpee_pair_list_t _tpee_pair_list_init(char *uri) {716
tpee_pair_list_t self = (tpee_pair_list_t) {717
.pair_list = NULL,718
.len = 0,719
};720
size_t url_len = strlen(uri);721
size_t set = 0;722
size_t lim = 0;723
for (size_t i = 0; i < url_len + 1; i++) {724
// find 'key=value' pair725
if (i == url_len || uri[i] == '&') {726
size_t key_len = i - set;727
if (key_len > 0) {728
if (lim > set) {729
key_len = lim - set;730
}731
self.pair_list = realloc(self.pair_list, sizeof(tpee_pair_t) * (self.len + 1));732
tpee_pair_t *pair = &self.pair_list[self.len];733
// read key734
pair->key = _tpee_pair_decode(uri + set, key_len);735
pair->value = NULL;736
if (i > set + key_len + 1) {737
// read value738
size_t value_len = i - (set + key_len + 1);739
pair->value = _tpee_pair_decode(uri + set + key_len + 1, value_len);740
}741
self.len++;742
}743
set = i + 1;744
}745
// find '=' delimiter746
if (uri[i] == '=') {747
lim = i;748
}749
}750
return self;751
}753
// @impl tpee_req_queries754
tpee_pair_list_t tpee_req_queries(tpee_req_t *req) {755
size_t url_len = strlen(req->url);756
// skip path757
for (size_t i = 0; i < url_len; i++) {758
if (req->url[i] == '?') {759
return _tpee_pair_list_init(req->url + i + 1);760
}761
}762
return (tpee_pair_list_t) {763
.pair_list = NULL,764
.len = 0,765
};766
}768
// @impl tpee_req_params769
tpee_pair_list_t tpee_req_params(tpee_req_t *req) {770
return _tpee_pair_list_init(req->body);771
}773
// @impl tpee_pair_list_get774
char *tpee_pair_list_get(tpee_pair_list_t *self, char *key) {775
if (self && self->len > 0) {776
for (size_t i = 0; i < self->len; i++) {777
tpee_pair_t *pair = &self->pair_list[i];778
if (strcmp(pair->key, key) == 0) {779
return pair->value;780
}781
}782
}783
return NULL;784
}786
// @impl tpee_pair_list_has787
bool tpee_pair_list_has(tpee_pair_list_t *self, char *key) {788
if (self && self->len > 0) {789
for (size_t i = 0; i < self->len; i++) {790
tpee_pair_t *pair = &self->pair_list[i];791
if (strcmp(pair->key, key) == 0) {792
return true;793
}794
}795
}796
return false;797
}799
// @impl tpee_pair_list_free800
void tpee_pair_list_free(tpee_pair_list_t *self) {801
if (self == NULL) {802
return;803
}804
if (self->len > 0) {805
for (size_t i = 0; i < self->len; i++) {806
tpee_pair_t *pair = &self->pair_list[i];807
free(pair->key);808
if (pair->value) {809
free(pair->value);810
}811
}812
free(self->pair_list);813
}814
}816
// @impl _tpee_method_str817
const char *_tpee_method_str(tpee_method_t method) {818
switch (method) {819
case TPEE_METHOD_GET:820
return "GET";821
case TPEE_METHOD_HEAD:822
return "HEAD";823
case TPEE_METHOD_POST:824
return "POST";825
case TPEE_METHOD_PUT:826
return "PUT";827
case TPEE_METHOD_DELETE:828
return "DELETE";829
case TPEE_METHOD_CONNECT:830
return "CONNECT";831
case TPEE_METHOD_OPTIONS:832
return "OPTIONS";833
case TPEE_METHOD_TRACE:834
return "TRACE";835
case TPEE_METHOD_PATCH:836
return "PATCH";837
default:838
return NULL;839
}840
}842
// @func _tpee_status_str843
// @internal844
const char *_tpee_status_str(tpee_status_t status) {845
switch (status) {846
case TPEE_STATUS_CONTINUE:847
return "Continue";848
case TPEE_STATUS_SWITCHING_PROTOCOLS:849
return "Switching Protocols";850
case TPEE_STATUS_PROCESSING:851
return "Processing";852
case TPEE_STATUS_EARLY_HINTS:853
return "Early Hints";854
case TPEE_STATUS_OK:855
return "OK";856
case TPEE_STATUS_CREATED:857
return "Created";858
case TPEE_STATUS_ACCEPTED:859
return "Accepted";860
case TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION:861
return "Non-Authoritative Information";862
case TPEE_STATUS_NO_CONTENT:863
return "No Content";864
case TPEE_STATUS_RESET_CONTENT:865
return "Reset Content";866
case TPEE_STATUS_PARTIAL_CONTENT:867
return "Partial Content";868
case TPEE_STATUS_MULTI_STATUS:869
return "Multi-Status";870
case TPEE_STATUS_ALREADY_REPORTED:871
return "Already Reported";872
case TPEE_STATUS_IM_USED:873
return "IM Used";874
case TPEE_STATUS_MULTIPLE_CHOICES:875
return "Multiple Choices";876
case TPEE_STATUS_MOVED_PERMANENTLY:877
return "Moved Permanently";878
case TPEE_STATUS_FOUND:879
return "Found";880
case TPEE_STATUS_SEE_OTHER:881
return "See Other";882
case TPEE_STATUS_NOT_MODIFIED:883
return "Not Modified";884
case TPEE_STATUS_TEMPORARY_REDIRECT:885
return "Temporary Redirect";886
case TPEE_STATUS_PERMANENT_REDIRECT:887
return "Permanent Redirect";888
case TPEE_STATUS_BAD_REQUEST:889
return "Bad Request";890
case TPEE_STATUS_UNAUTHORIZED:891
return "Unauthorized";892
case TPEE_STATUS_PAYMENT_REQUIRED:893
return "Payment Required";894
case TPEE_STATUS_FORBIDDEN:895
return "Forbidden";896
case TPEE_STATUS_NOT_FOUND:897
return "Not Found";898
case TPEE_STATUS_METHOD_NOT_ALLOWED:899
return "Method Not Allowed";900
case TPEE_STATUS_NOT_ACCEPTABLE:901
return "Not Acceptable";902
case TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED:903
return "Proxy Authentication Required";904
case TPEE_STATUS_REQUEST_TIMEOUT:905
return "Request Timeout";906
case TPEE_STATUS_CONFLICT:907
return "Conflict";908
case TPEE_STATUS_GONE:909
return "Gone";910
case TPEE_STATUS_LENGTH_REQUIRED:911
return "Length Required";912
case TPEE_STATUS_PRECONDITION_FAILED:913
return "Precondition Failed";914
case TPEE_STATUS_PAYLOAD_TOO_LARGE:915
return "Payload Too Large";916
case TPEE_STATUS_URI_TOO_LONG:917
return "URI Too Long";918
case TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE:919
return "Unsupported Media Type";920
case TPEE_STATUS_RANGE_NOT_SATISFIABLE:921
return "Range Not Satisfiable";922
case TPEE_STATUS_EXPECTATION_FAILED:923
return "Expectation Failed";924
case TPEE_STATUS_I_AM_A_TEAPOT:925
return "I'm a teapot";926
case TPEE_STATUS_MISDIRECTED_REQUEST:927
return "Misdirected Request";928
case TPEE_STATUS_UNPROCESSABLE_CONTENT:929
return "Unprocessable Content";930
case TPEE_STATUS_LOCKED:931
return "Locked";932
case TPEE_STATUS_FAILED_DEPENDENCY:933
return "Failed Dependency";934
case TPEE_STATUS_TOO_EARLY:935
return "Too Early";936
case TPEE_STATUS_UPGRADE_REQUIRED:937
return "Upgrade Required";938
case TPEE_STATUS_PRECONDITION_REQUIRED:939
return "Precondition Required";940
case TPEE_STATUS_TOO_MANY_REQUESTS:941
return "Too Many Requests";942
case TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE:943
return "Request Header Fields Too Large";944
case TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS:945
return "Unavailable For Legal Reasons";946
case TPEE_STATUS_INTERNAL_SERVER_ERROR:947
return "Internal Server Error";948
case TPEE_STATUS_NOT_IMPLEMENTED:949
return "Not Implemented";950
case TPEE_STATUS_BAD_GATEWAY:951
return "Bad Gateway";952
case TPEE_STATUS_SERVICE_UNAVAILABLE:953
return "Service Unavailable";954
case TPEE_STATUS_GATEWAY_TIMEOUT:955
return "Gateway Timeout";956
case TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED:957
return "HTTP Version Not Supported";958
case TPEE_STATUS_VARIANT_ALSO_NEGOTITATES:959
return "Variant Also Negotiates";960
case TPEE_STATUS_INSUFFICIENT_STORAGE:961
return "Insufficient Storage";962
case TPEE_STATUS_LOOP_DETECTED:963
return "Loop Detected";964
case TPEE_STATUS_NOT_EXTENDED:965
return "Not Extended";966
case TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED:967
return "Network Authentication Required";968
default:969
return NULL;970
}971
}973
// @impl tpee_conn_read974
ssize_t tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len) {975
return sock_conn_read(conn, buffer, len);976
}978
// @impl tpee_conn_write979
ssize_t tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len) {980
return sock_conn_write(conn, buffer, len);981
}983
// @impl tpee_req_header984
char *tpee_req_header(tpee_req_t *req, char *key) {985
if (req->headers_len > 0) {986
for (int i = 0; i < req->headers_len; i++) {987
tpee_header_t *header = &req->headers[i];988
if (strcasecmp(header->key, key) == 0) {989
return header->value;990
}991
}992
}993
return NULL;994
}996
// @impl tpee_resp_header997
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value) {998
resp->headers = (tpee_header_t *) realloc(resp->headers, sizeof(tpee_header_t) * (resp->headers_len + 1));999
tpee_header_t *new_header = &resp->headers[resp->headers_len];1000
resp->headers_len++;1001
int key_len = strlen(key);1002
new_header->key = (char *) malloc(sizeof(char) * (key_len + 1));1003
memcpy(new_header->key, key, key_len);1004
new_header->key[key_len] = '\0';1005
int value_len = strlen(value);1006
new_header->value = (char *) malloc(sizeof(char) * (value_len + 1));1007
memcpy(new_header->value, value, value_len);1008
new_header->value[value_len] = '\0';1009
}1011
// @impl tpee_resp_create1012
tpee_resp_t tpee_resp_create(tpee_status_t status) {1013
return (tpee_resp_t) {1014
.version = strdup(TPEE_VERSION),1015
.status = status,1016
.headers = NULL,1017
.headers_len = 0,1018
.body = NULL,1019
.body_len = 0,1020
};1021
}1023
// @impl tpee_resp_body1024
void tpee_resp_body(tpee_resp_t *resp, char *body, long len) {1025
if (resp != NULL && body != NULL) {1026
resp->body = (char *) malloc(sizeof(char) * len);1027
memcpy(resp->body, body, len);1028
resp->body_len = len;1029
}1030
}1032
// @impl tpee_req_free1033
void tpee_req_free(tpee_req_t *req) {1034
if (req->method != NULL) {1035
free(req->method);1036
}1037
if (req->url != NULL) {1038
free(req->url);1039
}1040
if (req->version != NULL) {1041
free(req->version);1042
}1043
if (req->headers_len > 0) {1044
for (int i = 0; i < req->headers_len; i++) {1045
tpee_header_t *header = &req->headers[i];1046
if (header->key != NULL) {1047
free(header->key);1048
}1049
if (header->value != NULL) {1050
free(header->value);1051
}1052
}1053
free(req->headers);1054
}1055
if (req->body != NULL) {1056
free(req->body);1057
}1058
if (req->ext_data != NULL) {1059
free(req->ext_data);1060
}1061
}1063
// @impl tpee_resp_free1064
void tpee_resp_free(tpee_resp_t *resp) {1065
if (resp->version != NULL) {1066
free(resp->version);1067
}1068
if (resp->headers_len > 0) {1069
for (int i = 0; i < resp->headers_len; i++) {1070
tpee_header_t *header = &resp->headers[i];1071
if (header->key != NULL) {1072
free(header->key);1073
}1074
if (header->value != NULL) {1075
free(header->value);1076
}1077
}1078
if (resp->headers != NULL) {1079
free(resp->headers);1080
}1081
}1082
if (resp->body != NULL) {1083
free(resp->body);1084
}1085
}1087
// @impl _tpee_req_read1088
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn) {1089
int len = 0;1090
int ln = 0;1091
int start = 0;1092
char buffer[TPEE_MAX_REQUEST_HEAD_SIZE];1093
*req = (tpee_req_t) {1094
.conn = conn,1095
.method = NULL,1096
.url = NULL,1097
.version = NULL,1098
.headers = NULL,1099
.headers_len = 0,1100
.body = NULL,1101
.body_len = 0,1102
.ext_data = NULL,1103
};1104
while (true) {1105
char c;1106
int read = tpee_conn_read(conn, &c, 1);1107
if (read == 0) {1108
break;1109
}1110
// read timeout1111
if (read == -1 || read == SSL_ERROR_SYSCALL) {1112
if (errno == EWOULDBLOCK || errno == EAGAIN) {1113
return (tpee_err_t) SOCK_ERR_READ_TIMEOUT;1114
}1115
}1116
// break if too long1117
if (len == TPEE_MAX_REQUEST_HEAD_SIZE) {1118
return (tpee_err_t) SOCK_ERR_REQUEST_TOO_LONG;1119
}1120
buffer[len] = c;1121
len++;1122
// read head1123
if (len >= 2) {1124
char *slice = buffer + (len - 2);1125
if (strncmp(slice, "\r\n", 2) == 0) {1126
int line_len = (len - 2) - start;1127
if (line_len > 0) {1128
// read line1129
char line[line_len + 1];1130
memcpy(line, buffer + start, line_len);1131
line[line_len] = '\0';1132
// read method, url, and version1133
if (ln == 0) {1134
int arg = 0;1135
int start = 0;1136
for (int i = 0; i <= line_len; i++) {1137
if (line[i] == ' ' || i == line_len) {1138
// read method1139
if (arg == 0) {1140
int method_length = i - start;1141
req->method = (char *) malloc(sizeof(char) * (method_length + 1));1142
memcpy(req->method, line + start, method_length);1143
req->method[method_length] = '\0';1144
}1145
// read url1146
if (arg == 1) {1147
int url_length = i - start;1148
req->url = (char *) malloc(sizeof(char) * (url_length + 1));1149
memcpy(req->url, line + start, url_length);1150
req->url[url_length] = '\0';1151
}1152
// read version1153
if (arg == 2) {1154
int version_length = i - start;1155
req->version = (char *) malloc(sizeof(char) * (version_length + 1));1156
memcpy(req->version, line + start, version_length);1157
req->version[version_length] = '\0';1158
}1159
start = i + 1;1160
arg++;1161
}1162
}1163
}1164
// read headers1165
else {1166
int sep = 0;1167
req->headers = (tpee_header_t *) realloc(req->headers, sizeof(tpee_header_t) * (req->headers_len + 1));1168
tpee_header_t *header = &req->headers[req->headers_len];1169
req->headers_len++;1170
for (int i = 0; i < line_len; i++) {1171
if (line[i] == ':') {1172
int key_len = i;1173
header->key = (char *) malloc(sizeof(char) * (key_len + 1));1174
memcpy(header->key, line, key_len);1175
header->key[key_len] = '\0';1176
sep = i + 2;1177
break;1178
}1179
}1180
int value_len = line_len - sep;1181
header->value = (char *) malloc(sizeof(char) * (value_len + 1));1182
memcpy(header->value, line + sep, value_len);1183
header->value[value_len] = '\0';1184
}1185
}1186
start = len;1187
ln++;1188
}1189
}1190
// read body1191
if (len >= 4) {1192
char *slice = buffer + (len - 4);1193
if (strncmp(slice, "\r\n\r\n", 4) == 0) {1194
char *content_length = tpee_req_header(req, (char *) "Content-Length");1195
if (content_length != NULL) {1196
req->body_len = strtol(content_length, NULL, 10);1197
if (req->body_len > 0) {1198
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));1199
tpee_conn_read(conn, req->body, req->body_len);1200
req->body[req->body_len] = '\0';1201
}1202
}1203
break;1204
}1205
}1206
}1207
return TPEE_ERR_OK;1208
}1210
// @impl tpee_resp_write_head1211
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn) {1212
tpee_conn_write(conn, resp->version, strlen(resp->version));1213
tpee_conn_write(conn, (char *) " ", 1);1214
char status[4];1215
snprintf(status, 4, "%d", resp->status);1216
tpee_conn_write(conn, status, strlen(status));1217
tpee_conn_write(conn, (char *) " ", 1);1218
const char *status_message = _tpee_status_str(resp->status);1219
tpee_conn_write(conn, (char *) status_message, strlen(status_message));1220
tpee_conn_write(conn, (char *) "\r\n", 2);1221
if (resp->headers_len > 0) {1222
for (int i = 0; i < resp->headers_len; i++) {1223
tpee_header_t *header = &resp->headers[i];1224
tpee_conn_write(conn, header->key, strlen(header->key));1225
tpee_conn_write(conn, (char *) ": ", 2);1226
tpee_conn_write(conn, header->value, strlen(header->value));1227
tpee_conn_write(conn, (char *) "\r\n", 2);1228
}1229
}1230
tpee_conn_write(conn, (char *) "\r\n", 2);1231
}1233
// @impl tpee_resp_write_body1234
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn) {1235
if (resp->body != NULL) {1236
tpee_conn_write(conn, resp->body, resp->body_len);1237
}1238
}1240
// @impl tpee_resp_write1241
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn) {1242
if (resp->status == __TPEE_STATUS_HANDLED) {1243
return;1244
}1245
bool has_content_length = false;1246
if (resp->body != NULL) {1247
if (resp->headers_len > 0) {1248
for (int i = 0; i < resp->headers_len; i++) {1249
tpee_header_t *header = &resp->headers[i];1250
if (strcasecmp(header->key, "Content-Length") == 0) {1251
has_content_length = true;1252
break;1253
}1254
}1255
}1256
if (!has_content_length) {1257
char content_length[50];1258
snprintf(content_length, 50, "%ld", resp->body_len);1259
tpee_resp_header(resp, (char *) "Content-Length", content_length);1260
}1261
}1262
tpee_resp_write_head(resp, conn);1263
tpee_resp_write_body(resp, conn);1264
}1266
// @impl tpee_bind1267
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len) {1268
return (tpee_err_t) sock_bind(self->sock, hostname, port, hosts, hosts_len);1269
}1271
// @impl tpee_listen1272
tpee_err_t tpee_listen(tpee_t *self) {1273
return (tpee_err_t) sock_listen(self->sock);1274
}1276
// @impl tpee_bind_listen1277
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port) {1278
return (tpee_err_t) sock_bind_listen(self->sock, hostname, port);1279
}1281
// @impl tpee_close1282
void tpee_close(tpee_t *self) {1283
sock_close(self->sock);1284
}1286
#endif // TPEE_IMPL