A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
blob: 90876ee3da0c262713b5b19a8dccd5465ab1dacf (36402 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_status_t44
// @desc TPEE status code.45
typedef enum {46
TPEE_STATUS_CONTINUE = 100,47
TPEE_STATUS_SWITCHING_PROTOCOLS = 101,48
TPEE_STATUS_PROCESSING = 102,49
TPEE_STATUS_EARLY_HINTS = 103,50
TPEE_STATUS_OK = 200,51
TPEE_STATUS_CREATED = 201,52
TPEE_STATUS_ACCEPTED = 202,53
TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,54
TPEE_STATUS_NO_CONTENT = 204,55
TPEE_STATUS_RESET_CONTENT = 205,56
TPEE_STATUS_PARTIAL_CONTENT = 206,57
TPEE_STATUS_MULTI_STATUS = 207,58
TPEE_STATUS_ALREADY_REPORTED = 208,59
TPEE_STATUS_IM_USED = 226,60
TPEE_STATUS_MULTIPLE_CHOICES = 300,61
TPEE_STATUS_MOVED_PERMANENTLY = 301,62
TPEE_STATUS_FOUND = 302,63
TPEE_STATUS_SEE_OTHER = 303,64
TPEE_STATUS_NOT_MODIFIED = 304,65
TPEE_STATUS_TEMPORARY_REDIRECT = 307,66
TPEE_STATUS_PERMANENT_REDIRECT = 308,67
TPEE_STATUS_BAD_REQUEST = 400,68
TPEE_STATUS_UNAUTHORIZED = 401,69
TPEE_STATUS_PAYMENT_REQUIRED = 402,70
TPEE_STATUS_FORBIDDEN = 403,71
TPEE_STATUS_NOT_FOUND = 404,72
TPEE_STATUS_METHOD_NOT_ALLOWED = 405,73
TPEE_STATUS_NOT_ACCEPTABLE = 406,74
TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,75
TPEE_STATUS_REQUEST_TIMEOUT = 408,76
TPEE_STATUS_CONFLICT = 409,77
TPEE_STATUS_GONE = 410,78
TPEE_STATUS_LENGTH_REQUIRED = 411,79
TPEE_STATUS_PRECONDITION_FAILED = 412,80
TPEE_STATUS_PAYLOAD_TOO_LARGE = 413,81
TPEE_STATUS_URI_TOO_LONG = 414,82
TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,83
TPEE_STATUS_RANGE_NOT_SATISFIABLE = 416,84
TPEE_STATUS_EXPECTATION_FAILED = 417,85
TPEE_STATUS_I_AM_A_TEAPOT = 418,86
TPEE_STATUS_MISDIRECTED_REQUEST = 421,87
TPEE_STATUS_UNPROCESSABLE_CONTENT = 422,88
TPEE_STATUS_LOCKED = 423,89
TPEE_STATUS_FAILED_DEPENDENCY = 424,90
TPEE_STATUS_TOO_EARLY = 425,91
TPEE_STATUS_UPGRADE_REQUIRED = 426,92
TPEE_STATUS_PRECONDITION_REQUIRED = 428,93
TPEE_STATUS_TOO_MANY_REQUESTS = 429,94
TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,95
TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,96
TPEE_STATUS_INTERNAL_SERVER_ERROR = 500,97
TPEE_STATUS_NOT_IMPLEMENTED = 501,98
TPEE_STATUS_BAD_GATEWAY = 502,99
TPEE_STATUS_SERVICE_UNAVAILABLE = 503,100
TPEE_STATUS_GATEWAY_TIMEOUT = 504,101
TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,102
TPEE_STATUS_VARIANT_ALSO_NEGOTITATES = 506,103
TPEE_STATUS_INSUFFICIENT_STORAGE = 507,104
TPEE_STATUS_LOOP_DETECTED = 508,105
TPEE_STATUS_NOT_EXTENDED = 510,106
TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,107
__TPEE_STATUS_HANDLED = 999,108
} tpee_status_t;110
// @type tpee_t111
// @groupdef tpee_t112
// @desc Global context struct. Implemented here {tpee}.113
typedef struct tpee tpee_t;115
// @type tpee_host_t116
// @desc Represents the host.117
typedef sock_host_t tpee_host_t;119
// @type tpee_bind_t120
// @desc Represents the binding (port).121
typedef sock_bind_t tpee_bind_t;123
// @type tpee_conn_t124
// @groupdef tpee_conn_t125
// @desc Represents a client connection.126
typedef sock_conn_t tpee_conn_t;128
// @struct tpee_header_t129
// @desc A TPEE header.130
typedef struct {131
char *key;132
char *value;133
} tpee_header_t;135
// @struct tpee_req_t136
// @groupdef tpee_req_t137
// @desc A TPEE request struct.138
typedef struct {139
tpee_conn_t *conn;140
char *method;141
char *url;142
char *version;143
tpee_header_t *headers;144
int headers_len;145
char *body;146
long body_len;147
void *ext_data;148
} tpee_req_t;150
// @struct tpee_resp_t151
// @groupdef tpee_resp_t152
// @desc A TPEE response struct.153
typedef struct {154
char *version;155
tpee_status_t status;156
tpee_header_t *headers;157
int headers_len;158
char *body;159
long body_len;160
} tpee_resp_t;162
// @type tpee_global_fn_t163
// @desc A function type for a global route handler.164
typedef tpee_resp_t (*tpee_global_fn_t)(tpee_t *, tpee_req_t *);166
// @type tpee_handle_fn_t167
// @desc A function type for a route handler.168
typedef tpee_resp_t (*tpee_handle_fn_t)(tpee_req_t *);170
// @type tpee_hook_fn_t171
// @desc A function type for a hook handler.172
typedef void (*tpee_hook_fn_t)(tpee_req_t *);174
// @struct tpee_route_t175
// @desc A route.176
typedef struct {177
char *method;178
char *path;179
tpee_handle_fn_t fn;180
} tpee_route_t;182
// @struct tpee_pair_t183
typedef struct {184
char *key;185
char *value;186
} tpee_pair_t;188
// @struct tpee_pairs_t189
// @groupdef tpee_pairs_t190
typedef struct {191
tpee_pair_t *pairs;192
size_t len;193
} tpee_pairs_t;195
// @struct tpee196
// @group tpee_t197
// @desc Implementation of the global context struct. Type here {tpee_t}.198
struct tpee {199
sock_t *sock;200
tpee_global_fn_t fn;201
};203
// @func tpee_init204
// @group tpee_t205
// @desc Initializes global library context {tpee_t}. Free with {tpee_cleanup}.206
// @return {tpee_t} * - Returns the global library context.207
tpee_t *tpee_init(void);209
// @func tpee_cleanup210
// @group tpee_t211
// @desc Cleans up {tpee_t}.212
// @param {tpee_t} *self - The global library context.213
void tpee_cleanup(tpee_t *self);215
// @func tpee_on_global216
// @group tpee_t217
// @desc Set and override the default global route handler {_tpee_on_global}.218
// @param {tpee_t} *self - The global library context.219
// @param {tpee_global_fn_t} fn - The global route handler.220
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn);222
// @func tpee_on_route223
// @note Use macros like {TPEE_GET} and others to register routes more easily.224
// @desc Registers a new route.225
// @param char *method - The TPEE method.226
// @param char *url - The TPEE URL (path).227
// @param {tpee_handle_fn_t} fn - The route handler.228
void tpee_on_route(char *method, char *url, tpee_handle_fn_t fn);230
// @func tpee_on_default231
// @note Use macro {TPEE_DEFAULT} instead.232
// @desc Registers a fallback handler.233
// @param {tpee_handle_fn_t} fn - The route handler.234
void tpee_on_default(tpee_handle_fn_t fn);236
// @func tpee_on_pre_route237
// @note Use macro {TPEE_PRE_HOOK} instead.238
// @desc Registers a hook handler which is performed before the route handler is executed.239
// @param {tpee_hook_fn_t} fn - The hook handler.240
void tpee_on_pre_route(tpee_hook_fn_t fn);242
// @func tpee_on_post_route243
// @note Use macro {TPEE_POST_HOOK} instead.244
// @desc Registers a hook handler which is performed after the route handler is executed.245
// @param {tpee_hook_fn_t} fn - The hook handler.246
void tpee_on_post_route(tpee_hook_fn_t fn);248
// @func tpee_req_path249
// @group tpee_req_t250
// @param {tpee_req_t} *req - The TPEE request.251
// @return char * - Returns the request path.252
char *tpee_req_path(tpee_req_t *req);254
// @func tpee_req_body255
// @group tpee_req_t256
// @param {tpee_req_t} *req - The TPEE request.257
// @return char * - Returns the request body.258
char *tpee_req_body(tpee_req_t *req);260
// @func tpee_req_queries261
// @group tpee_req_t262
// @param {tpee_req_t} *req - The TPEE request.263
// @return {tpee_pairs_t} - Returns the request queries.264
tpee_pairs_t tpee_req_queries(tpee_req_t *req);266
// @func tpee_req_params267
// @group tpee_req_t268
// @param {tpee_req_t} *req - The TPEE request.269
// @return {tpee_pairs_t} - Returns the request params.270
tpee_pairs_t tpee_req_params(tpee_req_t *req);272
// @func tpee_pairs_get273
// @group tpee_pairs_t274
char *tpee_pairs_get(tpee_pairs_t *self, char *key);276
// @func tpee_pairs_has277
// @group tpee_pairs_t278
bool tpee_pairs_has(tpee_pairs_t *self, char *key);280
// @func tpee_pairs_free281
// @group tpee_pairs_t282
void tpee_pairs_free(tpee_pairs_t *self);284
// @func tpee_req_header285
// @group tpee_req_t286
// @param {tpee_req_t} *req - The TPEE request.287
// @param char *key - The header key.288
// @return char * - Returns a header value.289
char *tpee_req_header(tpee_req_t *req, char *key);291
// @func tpee_resp_header292
// @group tpee_resp_t293
// @desc Writes a header to the response. The header content is consumed and can be freed afterward.294
// @param {tpee_resp_t} *resp - The TPEE response.295
// @param char *key - The header key.296
// @param char *value - The header value.297
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value);299
// @func tpee_resp_create300
// @group tpee_resp_t301
// @desc Creates a new response with a given TPEE status. Freed with {tpee_resp_free}.302
// @param {tpee_status_t} status - The TPEE status code.303
// @return {tpee_resp_t} - Returns a TPEE response.304
tpee_resp_t tpee_resp_create(tpee_status_t status);306
// @func tpee_resp_body307
// @group tpee_resp_t308
// @desc Writes body content to the response. The body content is consumed and can be freed afterward.309
// @param {tpee_resp_t} *resp - The TPEE response.310
// @param char *body - The body content.311
// @param long len - The body length.312
void tpee_resp_body(tpee_resp_t *resp, char *body, long len);314
// @func tpee_req_free315
// @group tpee_req_t316
// @desc Frees a request.317
// @param {tpee_req_t} *req - The TPEE request.318
void tpee_req_free(tpee_req_t *req);320
// @func tpee_resp_write_head321
// @group tpee_resp_t322
// @desc Writes a response header to the socket stream.323
// @param {tpee_resp_t} *resp - The TPEE response.324
// @param {tpee_conn_t} *conn - The TPEE connection.325
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn);327
// @func tpee_resp_write_body328
// @group tpee_resp_t329
// @desc Writes a response body to the socket stream.330
// @param {tpee_resp_t} *resp - The TPEE response.331
// @param {tpee_conn_t} *conn - The TPEE connection.332
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn);334
// @func tpee_resp_write335
// @group tpee_resp_t336
// @desc Writes a response to the socket stream.337
// @param {tpee_resp_t} *resp - The TPEE response.338
// @param {tpee_conn_t} *conn - The TPEE connection.339
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn);341
// @func tpee_resp_free342
// @group tpee_resp_t343
// @desc Frees a response.344
// @param {tpee_resp_t} *resp - The TPEE response.345
void tpee_resp_free(tpee_resp_t *resp);347
// @func tpee_bind348
// @group tpee_t349
// @param {tpee_t} *self - The global library context.350
// @param char *hostname - The hostname.351
// @param int port - The port.352
// @param {tpee_host_t} *hosts - The host to bind to.353
// @param int hosts_len - The host length of to be bound host.354
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len);356
// @func tpee_listen357
// @group tpee_t358
// @param {tpee_t} *self - The global library context.359
tpee_err_t tpee_listen(tpee_t *self);361
// @func tpee_bind_listen362
// @group tpee_t363
// @param {tpee_t} *self - The global library context.364
// @param char *hostname - The hostname.365
// @param int port - The port.366
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port);368
// @func tpee_close369
// @group tpee_t370
// @param {tpee_t} *self - The global library context.371
void tpee_close(tpee_t *self);373
// @func _tpee_req_read374
// @group tpee_req_t375
// @internal376
// @param {tpee_req_t} *req - The TPEE request.377
// @param {tpee_conn_t} *conn - The TPEE connection.378
// @return {tpee_err_t} - Returns an TPEE error code.379
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn);381
#ifdef TPEE_IMPL383
#include <stdio.h>384
#include <stdlib.h>385
#include <string.h>386
#include <ctype.h>387
#include <time.h>388
#include <unistd.h>389
#include <pthread.h>390
#include <netdb.h>391
#include <poll.h>392
#include <errno.h>393
#include <signal.h>395
// @macro TPEE_VERSION396
// @internal397
#define TPEE_VERSION "HTTP/1.1"399
// @macro TPEE_MAX_REQUEST_HEAD_SIZE400
// @internal401
#define TPEE_MAX_REQUEST_HEAD_SIZE 1000000403
// @macro TPEE_RESP_HANDLED404
// @group tpee_resp_t405
// @desc Handled {tpee_resp_t}.406
#define TPEE_RESP_HANDLED (tpee_resp_t) { .status = __TPEE_STATUS_HANDLED }408
// @macro _TPEE_ON409
// @internal410
#define _TPEE_ON(method, path, fn_name) \411
static tpee_resp_t fn_name(tpee_req_t *req); \412
__attribute__((constructor)) \413
static void __macro_tpee_fn_register_##fn_name(void) { \414
tpee_on_route(method, path, &fn_name); \415
} \416
static tpee_resp_t fn_name(tpee_req_t *req)418
// @macro TPEE_GET419
#define TPEE_GET(path, fn_name) _TPEE_ON("GET", path, fn_name)421
// @macro TPEE_GET422
#define TPEE_HEAD(path, fn_name) _TPEE_ON("HEAD", path, fn_name)424
// @macro TPEE_POST425
#define TPEE_POST(path, fn_name) _TPEE_ON("POST", path, fn_name)427
// @macro TPEE_PUT428
#define TPEE_PUT(path, fn_name) _TPEE_ON("PUT", path, fn_name)430
// @macro TPEE_DELETE431
#define TPEE_DELETE(path, fn_name) _TPEE_ON("DELETE", path, fn_name)433
// @macro TPEE_CONNECT434
#define TPEE_CONNECT(path, fn_name) _TPEE_ON("CONNECT", path, fn_name)436
// @macro TPEE_OPTIONS437
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON("OPTIONS", path, fn_name)439
// @macro TPEE_TRACE440
#define TPEE_TRACE(path, fn_name) _TPEE_ON("TRACE", path, fn_name)442
// @macro TPEE_PATCH443
#define TPEE_PATCH(path, fn_name) _TPEE_ON("PATCH", path, fn_name)445
// @macro TPEE_DEFAULT446
#define TPEE_DEFAULT(fn_name) \447
static tpee_resp_t fn_name(tpee_req_t *req); \448
__attribute__((constructor)) \449
static void __macro_tpee_fn_register_##fn_name(void) { \450
tpee_on_default(&fn_name); \451
} \452
static tpee_resp_t fn_name(tpee_req_t *req)454
// @macro TPEE_PRE_HOOK455
#define TPEE_PRE_HOOK(fn_name) \456
static void fn_name(tpee_req_t *req); \457
__attribute__((constructor)) \458
static void __macro_tpee_fn_register_##fn_name(void) { \459
tpee_on_pre_route(&fn_name); \460
} \461
static void fn_name(tpee_req_t *req)463
// @macro TPEE_POST_HOOK464
#define TPEE_POST_HOOK(fn_name) \465
static void fn_name(tpee_req_t *req); \466
__attribute__((constructor)) \467
static void __macro_tpee_fn_register_##fn_name(void) { \468
tpee_on_post_route(&fn_name); \469
} \470
static void fn_name(tpee_req_t *req)472
// @global _tpee_routes473
// @internal474
tpee_route_t *_tpee_routes = NULL;476
// @global _tpee_routes_len477
// @internal478
size_t _tpee_routes_len = 0;480
// @global _tpee_default_route481
// @internal482
tpee_handle_fn_t _tpee_default_route;484
// @global _tpee_pre_route485
// @internal486
tpee_hook_fn_t _tpee_pre_route;488
// @global _tpee_post_route489
// @internal490
tpee_hook_fn_t _tpee_post_route;492
// @func _tpee_on_global493
// @group tpee_t494
// @internal495
// @desc The default route handler. Can be overwritten with {tpee_on_global}.496
// @param {tpee_t} *self - The global library context.497
// @param {tpee_req_t} *req - The TPEE request.498
// @return {tpee_resp_t} - Returns a TPEE response.499
tpee_resp_t _tpee_on_global(tpee_t *self, tpee_req_t *req) {500
(void) self;501
tpee_resp_t resp = tpee_resp_create(TPEE_STATUS_NOT_FOUND);502
if (_tpee_pre_route != NULL) {503
_tpee_pre_route(req);504
}505
for (size_t i = 0; i < _tpee_routes_len; i++) {506
tpee_route_t *route = &_tpee_routes[i];507
char *path = tpee_req_path(req);508
if (strcmp(route->method, req->method) == 0) {509
size_t len = strlen(route->path);510
char c = route->path[len - 1];511
if (c == '*') {512
if (strncmp(route->path, path, len - 1) == 0) {513
tpee_resp_free(&resp);514
resp = route->fn(req);515
}516
}517
else {518
if (strcmp(route->path, path) == 0) {519
tpee_resp_free(&resp);520
resp = route->fn(req);521
}522
}523
}524
free(path);525
if (resp.status != TPEE_STATUS_NOT_FOUND) {526
if (_tpee_post_route != NULL) {527
_tpee_post_route(req);528
}529
return resp;530
}531
}532
if (_tpee_default_route != NULL) {533
tpee_resp_free(&resp);534
resp = _tpee_default_route(req);535
}536
if (_tpee_post_route != NULL) {537
_tpee_post_route(req);538
}539
return resp;540
}542
// @func _tpee_on_handle543
// @internal544
sock_err_t _tpee_on_handle(sock_t *sock, tpee_conn_t *conn) {545
tpee_t *self= (tpee_t *) sock->ext;546
tpee_req_t req;547
tpee_err_t err = _tpee_req_read(&req, conn);548
if (err != TPEE_ERR_OK) {549
return (sock_err_t) err;550
}551
if (req.method != NULL && req.url != NULL) {552
if (self->fn != NULL) {553
tpee_resp_t resp = self->fn(self, &req);554
tpee_resp_write(&resp, conn);555
tpee_resp_free(&resp);556
}557
}558
tpee_req_free(&req);559
return (sock_err_t) TPEE_ERR_OK;560
}562
// @impl tpee_init563
tpee_t *tpee_init(void) {564
tpee_t *self = (tpee_t *) malloc(sizeof(tpee_t));565
*self = (tpee_t) {566
.sock = sock_init(),567
.fn = &_tpee_on_global,568
};569
sock_set_ext(self->sock, self);570
sock_on_global(self->sock, _tpee_on_handle);571
return self;572
}574
// @impl tpee_cleanup575
void tpee_cleanup(tpee_t *self) {576
if (self->sock != NULL) {577
sock_cleanup(self->sock);578
}579
if (_tpee_routes != NULL && _tpee_routes_len > 0) {580
free(_tpee_routes);581
_tpee_routes = NULL;582
_tpee_routes_len = 0;583
}584
free(self);585
}587
// @impl tpee_on_global588
void tpee_on_global(tpee_t *self, tpee_global_fn_t fn) {589
self->fn = fn;590
}592
// @impl tpee_on_route593
void tpee_on_route(char *method, char *path, tpee_handle_fn_t fn) {594
_tpee_routes = realloc(_tpee_routes, sizeof(tpee_route_t) * (_tpee_routes_len + 1));595
_tpee_routes[_tpee_routes_len] = (tpee_route_t) {596
.method = method,597
.path = path,598
.fn = fn,599
};600
_tpee_routes_len++;601
}603
// @impl tpee_on_default604
void tpee_on_default(tpee_handle_fn_t fn) {605
_tpee_default_route = fn;606
}608
// @impl tpee_on_pre_route609
void tpee_on_pre_route(tpee_hook_fn_t fn) {610
_tpee_pre_route = fn;611
}613
// @impl tpee_on_post_route614
void tpee_on_post_route(tpee_hook_fn_t fn) {615
_tpee_post_route = fn;616
}618
// @impl tpee_req_path619
char *tpee_req_path(tpee_req_t *req) {620
char *uri = req->url;621
size_t url_len = strlen(uri);622
size_t i = 0;623
for (; i < url_len; i++) {624
size_t c = uri[i];625
if (c == '?') {626
break;627
}628
}629
size_t len = i;630
char *path = malloc(len + 1);631
memcpy(path, uri, len);632
path[len] = '\0';633
return path;634
}636
// @impl tpee_req_body637
char *tpee_req_body(tpee_req_t *req) {638
return req->body;639
}641
// @func _tpee_pair_decode642
// @internal643
char *_tpee_pair_decode(char *str, size_t len) {644
size_t j = 0;645
size_t k = 0;646
char decoded[len + 1];647
while (j < len) {648
if (str[j] == '%') {649
if (j + 2 < len && isxdigit(str[j + 1]) && isxdigit(str[j + 2])) {650
char hex[3] = { str[j + 1], str[j + 2], '\0' };651
decoded[k++] = (char) strtol(hex, NULL, 16);652
j += 3;653
continue;654
}655
decoded[k++] = '%';656
j++;657
continue;658
}659
if (str[j] == '+') {660
decoded[k++] = ' ';661
j++;662
continue;663
}664
decoded[k++] = str[j++];665
}666
decoded[k] = '\0';667
size_t ret_len = strlen(decoded);668
char *ret = malloc(ret_len + 1);669
memcpy(ret, decoded, ret_len);670
ret[ret_len] = '\0';671
return ret;672
}674
// @func _tpee_pairs_init675
// @group tpee_pairs_t676
// @internal677
tpee_pairs_t _tpee_pairs_init(char *uri) {678
tpee_pairs_t self = (tpee_pairs_t) {679
.pairs = NULL,680
.len = 0,681
};682
size_t url_len = strlen(uri);683
size_t set = 0;684
size_t lim = 0;685
for (size_t i = 0; i < url_len + 1; i++) {686
// find 'key=value' pair687
if (i == url_len || uri[i] == '&') {688
size_t key_len = i - set;689
if (key_len > 0) {690
if (lim > set) {691
key_len = lim - set;692
}693
self.pairs = realloc(self.pairs, sizeof(tpee_pair_t) * (self.len + 1));694
tpee_pair_t *pair = &self.pairs[self.len];695
// read key696
pair->key = _tpee_pair_decode(uri + set, key_len);697
pair->value = NULL;698
if (i > set + key_len + 1) {699
// read value700
size_t value_len = i - (set + key_len + 1);701
pair->value = _tpee_pair_decode(uri + set + key_len + 1, value_len);702
}703
self.len++;704
}705
set = i + 1;706
}707
// find '=' delimiter708
if (uri[i] == '=') {709
lim = i;710
}711
}712
return self;713
}715
// @impl tpee_req_queries716
tpee_pairs_t tpee_req_queries(tpee_req_t *req) {717
size_t url_len = strlen(req->url);718
// skip path719
for (size_t i = 0; i < url_len; i++) {720
if (req->url[i] == '?') {721
return _tpee_pairs_init(req->url + i + 1);722
}723
}724
return (tpee_pairs_t) {725
.pairs = NULL,726
.len = 0,727
};728
}730
// @impl tpee_req_params731
tpee_pairs_t tpee_req_params(tpee_req_t *req) {732
return _tpee_pairs_init(req->body);733
}735
// @impl tpee_pairs_get736
char *tpee_pairs_get(tpee_pairs_t *self, char *key) {737
if (self && self->len > 0) {738
for (size_t i = 0; i < self->len; i++) {739
tpee_pair_t *pair = &self->pairs[i];740
if (strcmp(pair->key, key) == 0) {741
return pair->value;742
}743
}744
}745
return NULL;746
}748
// @impl tpee_pairs_has749
bool tpee_pairs_has(tpee_pairs_t *self, char *key) {750
if (self && self->len > 0) {751
for (size_t i = 0; i < self->len; i++) {752
tpee_pair_t *pair = &self->pairs[i];753
if (strcmp(pair->key, key) == 0) {754
return true;755
}756
}757
}758
return false;759
}761
// @impl tpee_pairs_free762
void tpee_pairs_free(tpee_pairs_t *self) {763
if (self == NULL) {764
return;765
}766
if (self->len > 0) {767
for (size_t i = 0; i < self->len; i++) {768
tpee_pair_t *pair = &self->pairs[i];769
free(pair->key);770
if (pair->value) {771
free(pair->value);772
}773
}774
free(self->pairs);775
}776
}778
// @func _tpee_status_str779
// @internal780
const char *_tpee_status_str(tpee_status_t status) {781
switch (status) {782
case TPEE_STATUS_CONTINUE:783
return "Continue";784
case TPEE_STATUS_SWITCHING_PROTOCOLS:785
return "Switching Protocols";786
case TPEE_STATUS_PROCESSING:787
return "Processing";788
case TPEE_STATUS_EARLY_HINTS:789
return "Early Hints";790
case TPEE_STATUS_OK:791
return "OK";792
case TPEE_STATUS_CREATED:793
return "Created";794
case TPEE_STATUS_ACCEPTED:795
return "Accepted";796
case TPEE_STATUS_NON_AUTHORITATIVE_INFORMATION:797
return "Non-Authoritative Information";798
case TPEE_STATUS_NO_CONTENT:799
return "No Content";800
case TPEE_STATUS_RESET_CONTENT:801
return "Reset Content";802
case TPEE_STATUS_PARTIAL_CONTENT:803
return "Partial Content";804
case TPEE_STATUS_MULTI_STATUS:805
return "Multi-Status";806
case TPEE_STATUS_ALREADY_REPORTED:807
return "Already Reported";808
case TPEE_STATUS_IM_USED:809
return "IM Used";810
case TPEE_STATUS_MULTIPLE_CHOICES:811
return "Multiple Choices";812
case TPEE_STATUS_MOVED_PERMANENTLY:813
return "Moved Permanently";814
case TPEE_STATUS_FOUND:815
return "Found";816
case TPEE_STATUS_SEE_OTHER:817
return "See Other";818
case TPEE_STATUS_NOT_MODIFIED:819
return "Not Modified";820
case TPEE_STATUS_TEMPORARY_REDIRECT:821
return "Temporary Redirect";822
case TPEE_STATUS_PERMANENT_REDIRECT:823
return "Permanent Redirect";824
case TPEE_STATUS_BAD_REQUEST:825
return "Bad Request";826
case TPEE_STATUS_UNAUTHORIZED:827
return "Unauthorized";828
case TPEE_STATUS_PAYMENT_REQUIRED:829
return "Payment Required";830
case TPEE_STATUS_FORBIDDEN:831
return "Forbidden";832
case TPEE_STATUS_NOT_FOUND:833
return "Not Found";834
case TPEE_STATUS_METHOD_NOT_ALLOWED:835
return "Method Not Allowed";836
case TPEE_STATUS_NOT_ACCEPTABLE:837
return "Not Acceptable";838
case TPEE_STATUS_PROXY_AUTHENTICATION_REQUIRED:839
return "Proxy Authentication Required";840
case TPEE_STATUS_REQUEST_TIMEOUT:841
return "Request Timeout";842
case TPEE_STATUS_CONFLICT:843
return "Conflict";844
case TPEE_STATUS_GONE:845
return "Gone";846
case TPEE_STATUS_LENGTH_REQUIRED:847
return "Length Required";848
case TPEE_STATUS_PRECONDITION_FAILED:849
return "Precondition Failed";850
case TPEE_STATUS_PAYLOAD_TOO_LARGE:851
return "Payload Too Large";852
case TPEE_STATUS_URI_TOO_LONG:853
return "URI Too Long";854
case TPEE_STATUS_UNSUPPORTED_MEDIA_TYPE:855
return "Unsupported Media Type";856
case TPEE_STATUS_RANGE_NOT_SATISFIABLE:857
return "Range Not Satisfiable";858
case TPEE_STATUS_EXPECTATION_FAILED:859
return "Expectation Failed";860
case TPEE_STATUS_I_AM_A_TEAPOT:861
return "I'm a teapot";862
case TPEE_STATUS_MISDIRECTED_REQUEST:863
return "Misdirected Request";864
case TPEE_STATUS_UNPROCESSABLE_CONTENT:865
return "Unprocessable Content";866
case TPEE_STATUS_LOCKED:867
return "Locked";868
case TPEE_STATUS_FAILED_DEPENDENCY:869
return "Failed Dependency";870
case TPEE_STATUS_TOO_EARLY:871
return "Too Early";872
case TPEE_STATUS_UPGRADE_REQUIRED:873
return "Upgrade Required";874
case TPEE_STATUS_PRECONDITION_REQUIRED:875
return "Precondition Required";876
case TPEE_STATUS_TOO_MANY_REQUESTS:877
return "Too Many Requests";878
case TPEE_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE:879
return "Request Header Fields Too Large";880
case TPEE_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS:881
return "Unavailable For Legal Reasons";882
case TPEE_STATUS_INTERNAL_SERVER_ERROR:883
return "Internal Server Error";884
case TPEE_STATUS_NOT_IMPLEMENTED:885
return "Not Implemented";886
case TPEE_STATUS_BAD_GATEWAY:887
return "Bad Gateway";888
case TPEE_STATUS_SERVICE_UNAVAILABLE:889
return "Service Unavailable";890
case TPEE_STATUS_GATEWAY_TIMEOUT:891
return "Gateway Timeout";892
case TPEE_STATUS_HTTP_VERSION_NOT_SUPPORTED:893
return "HTTP Version Not Supported";894
case TPEE_STATUS_VARIANT_ALSO_NEGOTITATES:895
return "Variant Also Negotiates";896
case TPEE_STATUS_INSUFFICIENT_STORAGE:897
return "Insufficient Storage";898
case TPEE_STATUS_LOOP_DETECTED:899
return "Loop Detected";900
case TPEE_STATUS_NOT_EXTENDED:901
return "Not Extended";902
case TPEE_STATUS_NETWORK_AUTHENTICATION_REQUIRED:903
return "Network Authentication Required";904
default:905
return NULL;906
}907
}909
// @func _tpee_conn_read910
// @group tpee_conn_t911
// @internal912
ssize_t _tpee_conn_read(tpee_conn_t *conn, char *buffer, size_t len) {913
return sock_conn_read(conn, buffer, len);914
}916
// @func _tpee_conn_write917
// @group tpee_conn_t918
// @internal919
ssize_t _tpee_conn_write(tpee_conn_t *conn, char *buffer, size_t len) {920
return sock_conn_write(conn, buffer, len);921
}923
// @impl tpee_req_header924
char *tpee_req_header(tpee_req_t *req, char *key) {925
if (req->headers_len > 0) {926
for (int i = 0; i < req->headers_len; i++) {927
tpee_header_t *header = &req->headers[i];928
if (strcasecmp(header->key, key) == 0) {929
return header->value;930
}931
}932
}933
return NULL;934
}936
// @impl tpee_resp_header937
void tpee_resp_header(tpee_resp_t *resp, char *key, char *value) {938
resp->headers = (tpee_header_t *) realloc(resp->headers, sizeof(tpee_header_t) * (resp->headers_len + 1));939
tpee_header_t *new_header = &resp->headers[resp->headers_len];940
resp->headers_len++;941
int key_len = strlen(key);942
new_header->key = (char *) malloc(sizeof(char) * (key_len + 1));943
memcpy(new_header->key, key, key_len);944
new_header->key[key_len] = '\0';945
int value_len = strlen(value);946
new_header->value = (char *) malloc(sizeof(char) * (value_len + 1));947
memcpy(new_header->value, value, value_len);948
new_header->value[value_len] = '\0';949
}951
// @impl tpee_resp_create952
tpee_resp_t tpee_resp_create(tpee_status_t status) {953
return (tpee_resp_t) {954
.version = strdup(TPEE_VERSION),955
.status = status,956
.headers = NULL,957
.headers_len = 0,958
.body = NULL,959
.body_len = 0,960
};961
}963
// @impl tpee_resp_body964
void tpee_resp_body(tpee_resp_t *resp, char *body, long len) {965
if (resp != NULL && body != NULL) {966
resp->body = (char *) malloc(sizeof(char) * len);967
memcpy(resp->body, body, len);968
resp->body_len = len;969
}970
}972
// @impl tpee_req_free973
void tpee_req_free(tpee_req_t *req) {974
if (req->method != NULL) {975
free(req->method);976
}977
if (req->url != NULL) {978
free(req->url);979
}980
if (req->version != NULL) {981
free(req->version);982
}983
if (req->headers_len > 0) {984
for (int i = 0; i < req->headers_len; i++) {985
tpee_header_t *header = &req->headers[i];986
if (header->key != NULL) {987
free(header->key);988
}989
if (header->value != NULL) {990
free(header->value);991
}992
}993
free(req->headers);994
}995
if (req->body != NULL) {996
free(req->body);997
}998
if (req->ext_data != NULL) {999
free(req->ext_data);1000
}1001
}1003
// @impl tpee_resp_free1004
void tpee_resp_free(tpee_resp_t *resp) {1005
if (resp->version != NULL) {1006
free(resp->version);1007
}1008
if (resp->headers_len > 0) {1009
for (int i = 0; i < resp->headers_len; i++) {1010
tpee_header_t *header = &resp->headers[i];1011
if (header->key != NULL) {1012
free(header->key);1013
}1014
if (header->value != NULL) {1015
free(header->value);1016
}1017
}1018
if (resp->headers != NULL) {1019
free(resp->headers);1020
}1021
}1022
if (resp->body != NULL) {1023
free(resp->body);1024
}1025
}1027
// @impl _tpee_req_read1028
tpee_err_t _tpee_req_read(tpee_req_t *req, tpee_conn_t *conn) {1029
int len = 0;1030
int ln = 0;1031
int start = 0;1032
char buffer[TPEE_MAX_REQUEST_HEAD_SIZE];1033
*req = (tpee_req_t) {1034
.conn = conn,1035
.method = NULL,1036
.url = NULL,1037
.version = NULL,1038
.headers = NULL,1039
.headers_len = 0,1040
.body = NULL,1041
.body_len = 0,1042
.ext_data = NULL,1043
};1044
while (true) {1045
char c;1046
int read = _tpee_conn_read(conn, &c, 1);1047
if (read == 0) {1048
break;1049
}1050
// read timeout1051
if (read == -1 || read == SSL_ERROR_SYSCALL) {1052
if (errno == EWOULDBLOCK || errno == EAGAIN) {1053
return (tpee_err_t) SOCK_ERR_READ_TIMEOUT;1054
}1055
}1056
// break if too long1057
if (len == TPEE_MAX_REQUEST_HEAD_SIZE) {1058
return (tpee_err_t) SOCK_ERR_REQUEST_TOO_LONG;1059
}1060
buffer[len] = c;1061
len++;1062
// read head1063
if (len >= 2) {1064
char *slice = buffer + (len - 2);1065
if (strncmp(slice, "\r\n", 2) == 0) {1066
int line_len = (len - 2) - start;1067
if (line_len > 0) {1068
// read line1069
char line[line_len + 1];1070
memcpy(line, buffer + start, line_len);1071
line[line_len] = '\0';1072
// read method, url, and version1073
if (ln == 0) {1074
int arg = 0;1075
int start = 0;1076
for (int i = 0; i <= line_len; i++) {1077
if (line[i] == ' ' || i == line_len) {1078
// read method1079
if (arg == 0) {1080
int method_length = i - start;1081
req->method = (char *) malloc(sizeof(char) * (method_length + 1));1082
memcpy(req->method, line + start, method_length);1083
req->method[method_length] = '\0';1084
}1085
// read url1086
if (arg == 1) {1087
int url_length = i - start;1088
req->url = (char *) malloc(sizeof(char) * (url_length + 1));1089
memcpy(req->url, line + start, url_length);1090
req->url[url_length] = '\0';1091
}1092
// read version1093
if (arg == 2) {1094
int version_length = i - start;1095
req->version = (char *) malloc(sizeof(char) * (version_length + 1));1096
memcpy(req->version, line + start, version_length);1097
req->version[version_length] = '\0';1098
}1099
start = i + 1;1100
arg++;1101
}1102
}1103
}1104
// read headers1105
else {1106
int sep = 0;1107
req->headers = (tpee_header_t *) realloc(req->headers, sizeof(tpee_header_t) * (req->headers_len + 1));1108
tpee_header_t *header = &req->headers[req->headers_len];1109
req->headers_len++;1110
for (int i = 0; i < line_len; i++) {1111
if (line[i] == ':') {1112
int key_len = i;1113
header->key = (char *) malloc(sizeof(char) * (key_len + 1));1114
memcpy(header->key, line, key_len);1115
header->key[key_len] = '\0';1116
sep = i + 2;1117
break;1118
}1119
}1120
int value_len = line_len - sep;1121
header->value = (char *) malloc(sizeof(char) * (value_len + 1));1122
memcpy(header->value, line + sep, value_len);1123
header->value[value_len] = '\0';1124
}1125
}1126
start = len;1127
ln++;1128
}1129
}1130
// read body1131
if (len >= 4) {1132
char *slice = buffer + (len - 4);1133
if (strncmp(slice, "\r\n\r\n", 4) == 0) {1134
char *content_length = tpee_req_header(req, (char *) "Content-Length");1135
if (content_length != NULL) {1136
req->body_len = strtol(content_length, NULL, 10);1137
if (req->body_len > 0) {1138
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));1139
_tpee_conn_read(conn, req->body, req->body_len);1140
req->body[req->body_len] = '\0';1141
}1142
}1143
break;1144
}1145
}1146
}1147
return TPEE_ERR_OK;1148
}1150
// @impl tpee_resp_write_head1151
void tpee_resp_write_head(tpee_resp_t *resp, tpee_conn_t *conn) {1152
_tpee_conn_write(conn, resp->version, strlen(resp->version));1153
_tpee_conn_write(conn, (char *) " ", 1);1154
char status[4];1155
snprintf(status, 4, "%d", resp->status);1156
_tpee_conn_write(conn, status, strlen(status));1157
_tpee_conn_write(conn, (char *) " ", 1);1158
const char *status_message = _tpee_status_str(resp->status);1159
_tpee_conn_write(conn, (char *) status_message, strlen(status_message));1160
_tpee_conn_write(conn, (char *) "\r\n", 2);1161
if (resp->headers_len > 0) {1162
for (int i = 0; i < resp->headers_len; i++) {1163
tpee_header_t *header = &resp->headers[i];1164
_tpee_conn_write(conn, header->key, strlen(header->key));1165
_tpee_conn_write(conn, (char *) ": ", 2);1166
_tpee_conn_write(conn, header->value, strlen(header->value));1167
_tpee_conn_write(conn, (char *) "\r\n", 2);1168
}1169
}1170
_tpee_conn_write(conn, (char *) "\r\n", 2);1171
}1173
// @impl tpee_resp_write_body1174
void tpee_resp_write_body(tpee_resp_t *resp, tpee_conn_t *conn) {1175
if (resp->body != NULL) {1176
_tpee_conn_write(conn, resp->body, resp->body_len);1177
}1178
}1180
// @impl tpee_resp_write1181
void tpee_resp_write(tpee_resp_t *resp, tpee_conn_t *conn) {1182
if (resp->status == __TPEE_STATUS_HANDLED) {1183
return;1184
}1185
bool has_content_length = false;1186
if (resp->body != NULL) {1187
if (resp->headers_len > 0) {1188
for (int i = 0; i < resp->headers_len; i++) {1189
tpee_header_t *header = &resp->headers[i];1190
if (strcasecmp(header->key, "Content-Length") == 0) {1191
has_content_length = true;1192
break;1193
}1194
}1195
}1196
if (!has_content_length) {1197
char content_length[50];1198
snprintf(content_length, 50, "%ld", resp->body_len);1199
tpee_resp_header(resp, (char *) "Content-Length", content_length);1200
}1201
}1202
tpee_resp_write_head(resp, conn);1203
tpee_resp_write_body(resp, conn);1204
}1206
// @impl tpee_bind1207
tpee_err_t tpee_bind(tpee_t *self, char *hostname, int port, tpee_host_t *hosts, int hosts_len) {1208
return (tpee_err_t) sock_bind(self->sock, hostname, port, hosts, hosts_len);1209
}1211
// @impl tpee_listen1212
tpee_err_t tpee_listen(tpee_t *self) {1213
return (tpee_err_t) sock_listen(self->sock);1214
}1216
// @impl tpee_bind_listen1217
tpee_err_t tpee_bind_listen(tpee_t *self, char *hostname, int port) {1218
return (tpee_err_t) sock_bind_listen(self->sock, hostname, port);1219
}1221
// @impl tpee_close1222
void tpee_close(tpee_t *self) {1223
sock_close(self->sock);1224
}1226
#endif // TPEE_IMPL