A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
blob: 2f45b7518b9e6fb0775c6a21c5bd97d5dd503f38 (38186 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
#pragma once21
#include <stdbool.h>22
#include <sys/socket.h>24
// @include <openssl/ssl.h>25
// @desc OpenSSL library handling TLS/SSL.26
#include <openssl/ssl.h>28
#ifdef TPEE_IMPL29
#define SOCK_IMPL30
#endif // TPEE_IMPL32
// @include <sock.h>33
// @desc sock library handling server/client communication and multi-threading.34
#include <sock.h>36
// @enum tpee_err_t37
// @desc Error code.38
typedef enum {39
TPEE_ERR_OK = SOCK_ERR_OK,40
TPEE_ERR__COUNT,41
} tpee_err_t;43
// @enum tpee_method_t44
// @desc tpee method type.45
typedef enum {46
TPEE_METHOD_GET,47
TPEE_METHOD_HEAD,48
TPEE_METHOD_POST,49
TPEE_METHOD_PUT,50
TPEE_METHOD_DELETE,51
TPEE_METHOD_CONNECT,52
TPEE_METHOD_OPTIONS,53
TPEE_METHOD_TRACE,54
TPEE_METHOD_PATCH,55
} tpee_method_t;57
// @enum tpee_status_t58
// @desc tpee status code.59
typedef enum {60
TPEE_STATUS_CONTINUE = 100,61
TPEE_STATUS_SWITCHING_PROTOCOLS = 101,62
TPEE_STATUS_PROCESSING = 102,63
TPEE_STATUS_EARLY_HINTS = 103,64
TPEE_STATUS_OK = 200,65
TPEE_STATUS_CREATED = 201,66
TPEE_STATUS_ACCEPTED = 202,67
TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,68
TPEE_STATUS_NO_CONTENT = 204,69
TPEE_STATUS_RESET_CONTENT = 205,70
TPEE_STATUS_PARTIAL_CONTENT = 206,71
TPEE_STATUS_MULTI_STATUS = 207,72
TPEE_STATUS_ALREADY_REPORTED = 208,73
TPEE_STATUS_IM_USED = 226,74
TPEE_STATUS_MULTIPLE_CHOICES = 300,75
TPEE_STATUS_MOVED_PERMANENTLY = 301,76
TPEE_STATUS_FOUND = 302,77
TPEE_STATUS_SEE_OTHER = 303,78
TPEE_STATUS_NOT_MODIFIED = 304,79
TPEE_STATUS_TEMPORARY_REDIRECT = 307,80
TPEE_STATUS_PERMANENT_REDIRECT = 308,81
TPEE_STATUS_BAD_REQUEST = 400,82
TPEE_STATUS_UNAUTHORIZED = 401,83
TPEE_STATUS_PAYMENT_REQUIRED = 402,84
TPEE_STATUS_FORBIDDEN = 403,85
TPEE_STATUS_NOT_FOUND = 404,86
TPEE_STATUS_METHOD_NOT_ALLOWED = 405,87
TPEE_STATUS_NOT_ACCEPTABLE = 406,88
TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,89
TPEE_STATUS_REQUEST_TIMEOUT = 408,90
TPEE_STATUS_CONFLICT = 409,91
TPEE_STATUS_GONE = 410,92
TPEE_STATUS_LENGTH_REQUIRED = 411,93
TPEE_STATUS_PRECONDITION_FAILED = 412,94
TPEE_STATUS_PAYLOAD_TOO_LARGE = 413,95
TPEE_STATUS_URI_TOO_LONG = 414,96
TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,97
TPEE_STATUS_RANGE_NOT_SATISFIABLE = 416,98
TPEE_STATUS_EXPECTATION_FAILED = 417,99
TPEE_STATUS_I_AM_A_TEAPOT = 418,100
TPEE_STATUS_MISDIRECTED_REQUEST = 421,101
TPEE_STATUS_UNPROCESSABLE_CONTENT = 422,102
TPEE_STATUS_LOCKED = 423,103
TPEE_STATUS_FAILED_DEPENDENCY = 424,104
TPEE_STATUS_TOO_EARLY = 425,105
TPEE_STATUS_UPGRADE_REQUIRED = 426,106
TPEE_STATUS_PRECONDITION_REQUIRED = 428,107
TPEE_STATUS_TOO_MANY_REQUESTS = 429,108
TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,109
TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,110
TPEE_STATUS_INTERNAL_SERVER_ERROR = 500,111
TPEE_STATUS_NOT_IMPLEMENTED = 501,112
TPEE_STATUS_BAD_GATEWAY = 502,113
TPEE_STATUS_SERVICE_UNAVAILABLE = 503,114
TPEE_STATUS_GATEWAY_TIMEOUT = 504,115
TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,116
TPEE_STATUS_VARIANT_ALSO_NEGOTITATES = 506,117
TPEE_STATUS_INSUFFICIENT_STORAGE = 507,118
TPEE_STATUS_LOOP_DETECTED = 508,119
TPEE_STATUS_NOT_EXTENDED = 510,120
TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,121
__TPEE_STATUS_HANDLED = 999,122
} tpee_status_t;124
// @type tpee_t125
// @groupdef tpee_t126
// @desc Global context struct. Implemented here {tpee}.127
typedef struct tpee tpee_t;129
// @type tpee_host_t130
// @desc Represents the host.131
typedef sock_host_t tpee_host_t;133
// @type tpee_bind_t134
// @desc Represents the binding (port).135
typedef sock_bind_t tpee_bind_t;137
// @type tpee_conn_t138
// @groupdef tpee_conn_t139
// @desc Represents a client connection.140
typedef sock_conn_t tpee_conn_t;142
// @struct tpee_header_t143
// @desc A tpee header.144
typedef struct {145
char *key;146
char *value;147
} tpee_header_t;149
// @struct tpee_req_t150
// @groupdef tpee_req_t151
// @desc A tpee request struct.152
typedef struct {153
tpee_conn_t *conn;154
char *method;155
char *url;156
char *version;157
tpee_header_t *headers;158
int headers_len;159
char *body;160
long body_len;161
void *ext_data;162
} tpee_req_t;164
// @struct tpee_resp_t165
// @groupdef tpee_resp_t166
// @desc A tpee response struct.167
typedef struct {168
char *version;169
tpee_status_t status;170
tpee_header_t *headers;171
int headers_len;172
char *body;173
long body_len;174
} tpee_resp_t;176
// @type tpee_global_fn_t177
// @desc A function type for a global route handler.178
typedef tpee_resp_t (*tpee_global_fn_t)(tpee_t *, tpee_req_t *);180
// @type tpee_handle_fn_t181
// @desc A function type for a route handler.182
typedef tpee_resp_t (*tpee_handle_fn_t)(tpee_req_t *);184
// @type tpee_hook_fn_t185
// @desc A function type for a hook handler.186
typedef void (*tpee_hook_fn_t)(tpee_req_t *);188
// @struct tpee_route_t189
// @desc A route.190
typedef struct {191
tpee_method_t method;192
char *path;193
tpee_handle_fn_t fn;194
} tpee_route_t;196
// @struct tpee_pair_t197
typedef struct {198
char *key;199
char *value;200
} tpee_pair_t;202
// @struct tpee_pair_list_t203
// @groupdef tpee_pair_list_t204
typedef struct {205
tpee_pair_t *pair_list;206
size_t len;207
} tpee_pair_list_t;209
// @struct tpee210
// @group tpee_t211
// @desc Implementation of the global context struct. Type here {tpee_t}.212
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
#ifdef TPEE_IMPL416
#include <stdio.h>417
#include <stdlib.h>418
#include <string.h>419
#include <ctype.h>420
#include <time.h>421
#include <unistd.h>422
#include <pthread.h>423
#include <netdb.h>424
#include <poll.h>425
#include <errno.h>426
#include <signal.h>428
// @macro TPEE_VERSION429
// @internal430
#define TPEE_VERSION "HTTP/1.1"432
// @macro TPEE_MAX_REQUEST_HEAD_SIZE433
// @internal434
#define TPEE_MAX_REQUEST_HEAD_SIZE 1000000436
// @macro TPEE_RESP_HANDLED437
// @group tpee_resp_t438
// @desc Handled {tpee_resp_t}.439
#define TPEE_RESP_HANDLED (tpee_resp_t) { .status = __TPEE_STATUS_HANDLED }441
// @macro _TPEE_ON442
// @internal443
#define _TPEE_ON(method, path, fn_name) \444
static tpee_resp_t fn_name(tpee_req_t *req); \445
__attribute__((constructor)) \446
static void __macro_tpee_fn_register_##fn_name(void) { \447
tpee_on_route(method, path, &fn_name); \448
} \449
static tpee_resp_t fn_name(tpee_req_t *req)451
// @macro TPEE_GET452
#define TPEE_GET(path, fn_name) _TPEE_ON(TPEE_METHOD_GET, path, fn_name)454
// @macro TPEE_HEAD455
#define TPEE_HEAD(path, fn_name) _TPEE_ON(TPEE_METHOD_HEAD, path, fn_name)457
// @macro TPEE_POST458
#define TPEE_POST(path, fn_name) _TPEE_ON(TPEE_METHOD_POST, path, fn_name)460
// @macro TPEE_PUT461
#define TPEE_PUT(path, fn_name) _TPEE_ON(TPEE_METHOD_PUT, path, fn_name)463
// @macro TPEE_DELETE464
#define TPEE_DELETE(path, fn_name) _TPEE_ON(TPEE_METHOD_DELETE, path, fn_name)466
// @macro TPEE_CONNECT467
#define TPEE_CONNECT(path, fn_name) _TPEE_ON(TPEE_METHOD_CONNECT, path, fn_name)469
// @macro TPEE_OPTIONS470
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON(TPEE_METHOD_OPTIONS, path, fn_name)472
// @macro TPEE_TRACE473
#define TPEE_TRACE(path, fn_name) _TPEE_ON(TPEE_METHOD_TRACE, path, fn_name)475
// @macro TPEE_PATCH476
#define TPEE_PATCH(path, fn_name) _TPEE_ON(TPEE_METHOD_PATCH, path, fn_name)478
// @macro TPEE_DEFAULT479
#define TPEE_DEFAULT(fn_name) \480
static tpee_resp_t fn_name(tpee_req_t *req); \481
__attribute__((constructor)) \482
static void __macro_tpee_fn_register_##fn_name(void) { \483
tpee_on_default(&fn_name); \484
} \485
static tpee_resp_t fn_name(tpee_req_t *req)487
// @macro TPEE_PRE_HOOK488
#define TPEE_PRE_HOOK(fn_name) \489
static void fn_name(tpee_req_t *req); \490
__attribute__((constructor)) \491
static void __macro_tpee_fn_register_##fn_name(void) { \492
tpee_on_pre_route(&fn_name); \493
} \494
static void fn_name(tpee_req_t *req)496
// @macro TPEE_POST_HOOK497
#define TPEE_POST_HOOK(fn_name) \498
static void fn_name(tpee_req_t *req); \499
__attribute__((constructor)) \500
static void __macro_tpee_fn_register_##fn_name(void) { \501
tpee_on_post_route(&fn_name); \502
} \503
static void fn_name(tpee_req_t *req)505
// @global _tpee_routes506
// @internal507
tpee_route_t *_tpee_routes = NULL;509
// @global _tpee_routes_len510
// @internal511
size_t _tpee_routes_len = 0;513
// @global _tpee_default_route514
// @internal515
tpee_handle_fn_t _tpee_default_route;517
// @global _tpee_pre_route518
// @internal519
tpee_hook_fn_t _tpee_pre_route;521
// @global _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_routes_len; i++) {539
tpee_route_t *route = &_tpee_routes[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_routes != NULL && _tpee_routes_len > 0) {614
free(_tpee_routes);615
_tpee_routes = NULL;616
_tpee_routes_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_routes = realloc(_tpee_routes, sizeof(tpee_route_t) * (_tpee_routes_len + 1));629
_tpee_routes[_tpee_routes_len] = (tpee_route_t) {630
.method = method,631
.path = path,632
.fn = fn,633
};634
_tpee_routes_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
// @func _tpee_method_str813
// @internal814
const char *_tpee_method_str(tpee_method_t method) {815
switch (method) {816
case TPEE_METHOD_GET:817
return "GET";818
case TPEE_METHOD_HEAD:819
return "HEAD";820
case TPEE_METHOD_POST:821
return "POST";822
case TPEE_METHOD_PUT:823
return "PUT";824
case TPEE_METHOD_DELETE:825
return "DELETE";826
case TPEE_METHOD_CONNECT:827
return "CONNECT";828
case TPEE_METHOD_OPTIONS:829
return "OPTIONS";830
case TPEE_METHOD_TRACE:831
return "TRACE";832
case TPEE_METHOD_PATCH:833
return "PATCH";834
default:835
return NULL;836
}837
}839
// @func _tpee_status_str840
// @internal841
const char *_tpee_status_str(tpee_status_t status) {842
switch (status) {843
case TPEE_STATUS_CONTINUE:844
return "Continue";845
case TPEE_STATUS_SWITCHING_PROTOCOLS:846
return "Switching Protocols";847
case TPEE_STATUS_PROCESSING:848
return "Processing";849
case TPEE_STATUS_EARLY_HINTS:850
return "Early Hints";851
case TPEE_STATUS_OK:852
return "OK";853
case TPEE_STATUS_CREATED:854
return "Created";855
case TPEE_STATUS_ACCEPTED:856
return "Accepted";857
case TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION:858
return "Non-Authoritative Information";859
case TPEE_STATUS_NO_CONTENT:860
return "No Content";861
case TPEE_STATUS_RESET_CONTENT:862
return "Reset Content";863
case TPEE_STATUS_PARTIAL_CONTENT:864
return "Partial Content";865
case TPEE_STATUS_MULTI_STATUS:866
return "Multi-Status";867
case TPEE_STATUS_ALREADY_REPORTED:868
return "Already Reported";869
case TPEE_STATUS_IM_USED:870
return "IM Used";871
case TPEE_STATUS_MULTIPLE_CHOICES:872
return "Multiple Choices";873
case TPEE_STATUS_MOVED_PERMANENTLY:874
return "Moved Permanently";875
case TPEE_STATUS_FOUND:876
return "Found";877
case TPEE_STATUS_SEE_OTHER:878
return "See Other";879
case TPEE_STATUS_NOT_MODIFIED:880
return "Not Modified";881
case TPEE_STATUS_TEMPORARY_REDIRECT:882
return "Temporary Redirect";883
case TPEE_STATUS_PERMANENT_REDIRECT:884
return "Permanent Redirect";885
case TPEE_STATUS_BAD_REQUEST:886
return "Bad Request";887
case TPEE_STATUS_UNAUTHORIZED:888
return "Unauthorized";889
case TPEE_STATUS_PAYMENT_REQUIRED:890
return "Payment Required";891
case TPEE_STATUS_FORBIDDEN:892
return "Forbidden";893
case TPEE_STATUS_NOT_FOUND:894
return "Not Found";895
case TPEE_STATUS_METHOD_NOT_ALLOWED:896
return "Method Not Allowed";897
case TPEE_STATUS_NOT_ACCEPTABLE:898
return "Not Acceptable";899
case TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED:900
return "Proxy Authentication Required";901
case TPEE_STATUS_REQUEST_TIMEOUT:902
return "Request Timeout";903
case TPEE_STATUS_CONFLICT:904
return "Conflict";905
case TPEE_STATUS_GONE:906
return "Gone";907
case TPEE_STATUS_LENGTH_REQUIRED:908
return "Length Required";909
case TPEE_STATUS_PRECONDITION_FAILED:910
return "Precondition Failed";911
case TPEE_STATUS_PAYLOAD_TOO_LARGE:912
return "Payload Too Large";913
case TPEE_STATUS_URI_TOO_LONG:914
return "URI Too Long";915
case TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE:916
return "Unsupported Media Type";917
case TPEE_STATUS_RANGE_NOT_SATISFIABLE:918
return "Range Not Satisfiable";919
case TPEE_STATUS_EXPECTATION_FAILED:920
return "Expectation Failed";921
case TPEE_STATUS_I_AM_A_TEAPOT:922
return "I'm a teapot";923
case TPEE_STATUS_MISDIRECTED_REQUEST:924
return "Misdirected Request";925
case TPEE_STATUS_UNPROCESSABLE_CONTENT:926
return "Unprocessable Content";927
case TPEE_STATUS_LOCKED:928
return "Locked";929
case TPEE_STATUS_FAILED_DEPENDENCY:930
return "Failed Dependency";931
case TPEE_STATUS_TOO_EARLY:932
return "Too Early";933
case TPEE_STATUS_UPGRADE_REQUIRED:934
return "Upgrade Required";935
case TPEE_STATUS_PRECONDITION_REQUIRED:936
return "Precondition Required";937
case TPEE_STATUS_TOO_MANY_REQUESTS:938
return "Too Many Requests";939
case TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE:940
return "Request Header Fields Too Large";941
case TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS:942
return "Unavailable For Legal Reasons";943
case TPEE_STATUS_INTERNAL_SERVER_ERROR:944
return "Internal Server Error";945
case TPEE_STATUS_NOT_IMPLEMENTED:946
return "Not Implemented";947
case TPEE_STATUS_BAD_GATEWAY:948
return "Bad Gateway";949
case TPEE_STATUS_SERVICE_UNAVAILABLE:950
return "Service Unavailable";951
case TPEE_STATUS_GATEWAY_TIMEOUT:952
return "Gateway Timeout";953
case TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED:954
return "HTTP Version Not Supported";955
case TPEE_STATUS_VARIANT_ALSO_NEGOTITATES:956
return "Variant Also Negotiates";957
case TPEE_STATUS_INSUFFICIENT_STORAGE:958
return "Insufficient Storage";959
case TPEE_STATUS_LOOP_DETECTED:960
return "Loop Detected";961
case TPEE_STATUS_NOT_EXTENDED:962
return "Not Extended";963
case TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED:964
return "Network Authentication Required";965
default:966
return NULL;967
}968
}970
// @impl tpee_conn_read971
ssize_t tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len) {972
return sock_conn_read(conn, buffer, len);973
}975
// @impl tpee_conn_write976
ssize_t tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len) {977
return sock_conn_write(conn, buffer, len);978
}980
// @impl tpee_req_header981
char *tpee_req_header(tpee_req_t *req, char *key) {982
if (req->headers_len > 0) {983
for (int i = 0; i < req->headers_len; i++) {984
tpee_header_t *header = &req->headers[i];985
if (strcasecmp(header->key, key) == 0) {986
return header->value;987
}988
}989
}990
return NULL;991
}993
// @impl tpee_resp_header994
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value) {995
resp->headers = (tpee_header_t *) realloc(resp->headers, sizeof(tpee_header_t) * (resp->headers_len + 1));996
tpee_header_t *new_header = &resp->headers[resp->headers_len];997
resp->headers_len++;998
int key_len = strlen(key);999
new_header->key = (char *) malloc(sizeof(char) * (key_len + 1));1000
memcpy(new_header->key, key, key_len);1001
new_header->key[key_len] = '\0';1002
int value_len = strlen(value);1003
new_header->value = (char *) malloc(sizeof(char) * (value_len + 1));1004
memcpy(new_header->value, value, value_len);1005
new_header->value[value_len] = '\0';1006
}1008
// @impl tpee_resp_create1009
tpee_resp_t tpee_resp_create(tpee_status_t status) {1010
return (tpee_resp_t) {1011
.version = strdup(TPEE_VERSION),1012
.status = status,1013
.headers = NULL,1014
.headers_len = 0,1015
.body = NULL,1016
.body_len = 0,1017
};1018
}1020
// @impl tpee_resp_body1021
void tpee_resp_body(tpee_resp_t *resp, char *body, long len) {1022
if (resp != NULL && body != NULL) {1023
resp->body = (char *) malloc(sizeof(char) * len);1024
memcpy(resp->body, body, len);1025
resp->body_len = len;1026
}1027
}1029
// @impl tpee_req_free1030
void tpee_req_free(tpee_req_t *req) {1031
if (req->method != NULL) {1032
free(req->method);1033
}1034
if (req->url != NULL) {1035
free(req->url);1036
}1037
if (req->version != NULL) {1038
free(req->version);1039
}1040
if (req->headers_len > 0) {1041
for (int i = 0; i < req->headers_len; i++) {1042
tpee_header_t *header = &req->headers[i];1043
if (header->key != NULL) {1044
free(header->key);1045
}1046
if (header->value != NULL) {1047
free(header->value);1048
}1049
}1050
free(req->headers);1051
}1052
if (req->body != NULL) {1053
free(req->body);1054
}1055
if (req->ext_data != NULL) {1056
free(req->ext_data);1057
}1058
}1060
// @impl tpee_resp_free1061
void tpee_resp_free(tpee_resp_t *resp) {1062
if (resp->version != NULL) {1063
free(resp->version);1064
}1065
if (resp->headers_len > 0) {1066
for (int i = 0; i < resp->headers_len; i++) {1067
tpee_header_t *header = &resp->headers[i];1068
if (header->key != NULL) {1069
free(header->key);1070
}1071
if (header->value != NULL) {1072
free(header->value);1073
}1074
}1075
if (resp->headers != NULL) {1076
free(resp->headers);1077
}1078
}1079
if (resp->body != NULL) {1080
free(resp->body);1081
}1082
}1084
// @impl _tpee_req_read1085
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn) {1086
int len = 0;1087
int ln = 0;1088
int start = 0;1089
char buffer[TPEE_MAX_REQUEST_HEAD_SIZE];1090
*req = (tpee_req_t) {1091
.conn = conn,1092
.method = NULL,1093
.url = NULL,1094
.version = NULL,1095
.headers = NULL,1096
.headers_len = 0,1097
.body = NULL,1098
.body_len = 0,1099
.ext_data = NULL,1100
};1101
while (true) {1102
char c;1103
int read = tpee_conn_read(conn, &c, 1);1104
if (read == 0) {1105
break;1106
}1107
// read timeout1108
if (read == -1 || read == SSL_ERROR_SYSCALL) {1109
if (errno == EWOULDBLOCK || errno == EAGAIN) {1110
return (tpee_err_t) SOCK_ERR_READ_TIMEOUT;1111
}1112
}1113
// break if too long1114
if (len == TPEE_MAX_REQUEST_HEAD_SIZE) {1115
return (tpee_err_t) SOCK_ERR_REQUEST_TOO_LONG;1116
}1117
buffer[len] = c;1118
len++;1119
// read head1120
if (len >= 2) {1121
char *slice = buffer + (len - 2);1122
if (strncmp(slice, "\r\n", 2) == 0) {1123
int line_len = (len - 2) - start;1124
if (line_len > 0) {1125
// read line1126
char line[line_len + 1];1127
memcpy(line, buffer + start, line_len);1128
line[line_len] = '\0';1129
// read method, url, and version1130
if (ln == 0) {1131
int arg = 0;1132
int start = 0;1133
for (int i = 0; i <= line_len; i++) {1134
if (line[i] == ' ' || i == line_len) {1135
// read method1136
if (arg == 0) {1137
int method_length = i - start;1138
req->method = (char *) malloc(sizeof(char) * (method_length + 1));1139
memcpy(req->method, line + start, method_length);1140
req->method[method_length] = '\0';1141
}1142
// read url1143
if (arg == 1) {1144
int url_length = i - start;1145
req->url = (char *) malloc(sizeof(char) * (url_length + 1));1146
memcpy(req->url, line + start, url_length);1147
req->url[url_length] = '\0';1148
}1149
// read version1150
if (arg == 2) {1151
int version_length = i - start;1152
req->version = (char *) malloc(sizeof(char) * (version_length + 1));1153
memcpy(req->version, line + start, version_length);1154
req->version[version_length] = '\0';1155
}1156
start = i + 1;1157
arg++;1158
}1159
}1160
}1161
// read headers1162
else {1163
int sep = 0;1164
req->headers = (tpee_header_t *) realloc(req->headers, sizeof(tpee_header_t) * (req->headers_len + 1));1165
tpee_header_t *header = &req->headers[req->headers_len];1166
req->headers_len++;1167
for (int i = 0; i < line_len; i++) {1168
if (line[i] == ':') {1169
int key_len = i;1170
header->key = (char *) malloc(sizeof(char) * (key_len + 1));1171
memcpy(header->key, line, key_len);1172
header->key[key_len] = '\0';1173
sep = i + 2;1174
break;1175
}1176
}1177
int value_len = line_len - sep;1178
header->value = (char *) malloc(sizeof(char) * (value_len + 1));1179
memcpy(header->value, line + sep, value_len);1180
header->value[value_len] = '\0';1181
}1182
}1183
start = len;1184
ln++;1185
}1186
}1187
// read body1188
if (len >= 4) {1189
char *slice = buffer + (len - 4);1190
if (strncmp(slice, "\r\n\r\n", 4) == 0) {1191
char *content_length = tpee_req_header(req, (char *) "Content-Length");1192
if (content_length != NULL) {1193
req->body_len = strtol(content_length, NULL, 10);1194
if (req->body_len > 0) {1195
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));1196
tpee_conn_read(conn, req->body, req->body_len);1197
req->body[req->body_len] = '\0';1198
}1199
}1200
break;1201
}1202
}1203
}1204
return TPEE_ERR_OK;1205
}1207
// @impl tpee_resp_write_head1208
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn) {1209
tpee_conn_write(conn, resp->version, strlen(resp->version));1210
tpee_conn_write(conn, (char *) " ", 1);1211
char status[4];1212
snprintf(status, 4, "%d", resp->status);1213
tpee_conn_write(conn, status, strlen(status));1214
tpee_conn_write(conn, (char *) " ", 1);1215
const char *status_message = _tpee_status_str(resp->status);1216
tpee_conn_write(conn, (char *) status_message, strlen(status_message));1217
tpee_conn_write(conn, (char *) "\r\n", 2);1218
if (resp->headers_len > 0) {1219
for (int i = 0; i < resp->headers_len; i++) {1220
tpee_header_t *header = &resp->headers[i];1221
tpee_conn_write(conn, header->key, strlen(header->key));1222
tpee_conn_write(conn, (char *) ": ", 2);1223
tpee_conn_write(conn, header->value, strlen(header->value));1224
tpee_conn_write(conn, (char *) "\r\n", 2);1225
}1226
}1227
tpee_conn_write(conn, (char *) "\r\n", 2);1228
}1230
// @impl tpee_resp_write_body1231
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn) {1232
if (resp->body != NULL) {1233
tpee_conn_write(conn, resp->body, resp->body_len);1234
}1235
}1237
// @impl tpee_resp_write1238
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn) {1239
if (resp->status == __TPEE_STATUS_HANDLED) {1240
return;1241
}1242
bool has_content_length = false;1243
if (resp->body != NULL) {1244
if (resp->headers_len > 0) {1245
for (int i = 0; i < resp->headers_len; i++) {1246
tpee_header_t *header = &resp->headers[i];1247
if (strcasecmp(header->key, "Content-Length") == 0) {1248
has_content_length = true;1249
break;1250
}1251
}1252
}1253
if (!has_content_length) {1254
char content_length[50];1255
snprintf(content_length, 50, "%ld", resp->body_len);1256
tpee_resp_header(resp, (char *) "Content-Length", content_length);1257
}1258
}1259
tpee_resp_write_head(resp, conn);1260
tpee_resp_write_body(resp, conn);1261
}1263
// @impl tpee_bind1264
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len) {1265
return (tpee_err_t) sock_bind(self->sock, hostname, port, hosts, hosts_len);1266
}1268
// @impl tpee_listen1269
tpee_err_t tpee_listen(tpee_t *self) {1270
return (tpee_err_t) sock_listen(self->sock);1271
}1273
// @impl tpee_bind_listen1274
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port) {1275
return (tpee_err_t) sock_bind_listen(self->sock, hostname, port);1276
}1278
// @impl tpee_close1279
void tpee_close(tpee_t *self) {1280
sock_close(self->sock);1281
}1283
#endif // TPEE_IMPL