A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
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_t44
// @desc TPEE status code.58
// @desc tpee status code.59
typedef enum {60
TPEE_STATUS_CONTINUE = 100,61
TPEE_STATUS_SWITCHING_PROTOCOLS = 101,
140
typedef sock_conn_t tpee_conn_t;142
// @struct tpee_header_t129
// @desc A TPEE header.143
// @desc A tpee header.144
typedef struct {145
char *key;146
char *value;
149
// @struct tpee_req_t150
// @groupdef tpee_req_t137
// @desc A TPEE request struct.151
// @desc A tpee request struct.152
typedef struct {153
tpee_conn_t *conn;154
char *method;
164
// @struct tpee_resp_t165
// @groupdef tpee_resp_t152
// @desc A TPEE response struct.166
// @desc A tpee response struct.167
typedef struct {168
char *version;169
tpee_status_t status;
188
// @struct tpee_route_t189
// @desc A route.190
typedef struct {177
char *method;191
tpee_method_t method;192
char *path;193
tpee_handle_fn_t fn;194
} tpee_route_t;
199
char *value;200
} tpee_pair_t;188
// @struct tpee_pairs_t189
// @groupdef tpee_pairs_t202
// @struct tpee_pair_list_t203
// @groupdef tpee_pair_list_t204
typedef struct {191
tpee_pair_t *pairs;205
tpee_pair_t *pair_list;206
size_t len;193
} tpee_pairs_t;207
} tpee_pair_list_t;209
// @struct tpee210
// @group tpee_t
236
// @func tpee_on_route237
// @note Use macros like {TPEE_GET} and others to register routes more easily.238
// @desc Registers a new route.225
// @param char *method - The TPEE method.226
// @param char *url - The TPEE URL (path).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.228
void tpee_on_route(char *method, char *url, tpee_handle_fn_t fn);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.
262
// @func tpee_req_path263
// @group tpee_req_t250
// @param {tpee_req_t} *req - The TPEE request.264
// @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_t256
// @param {tpee_req_t} *req - The TPEE request.270
// @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_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);276
// @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_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);282
// @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);272
// @func tpee_pairs_get273
// @group tpee_pairs_t274
char *tpee_pairs_get(tpee_pairs_t *self, char *key);286
// @func tpee_pair_list_get287
// @group tpee_pair_list_t288
char *tpee_pair_list_get(tpee_pair_list_t *self, char *key);276
// @func tpee_pairs_has277
// @group tpee_pairs_t278
bool tpee_pairs_has(tpee_pairs_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);280
// @func tpee_pairs_free281
// @group tpee_pairs_t282
void tpee_pairs_free(tpee_pairs_t *self);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_t
312
// @func tpee_req_header313
// @group tpee_req_t300
// @param {tpee_req_t} *req - The TPEE request.314
// @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.308
// @param {tpee_resp_t} *resp - The TPEE response.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_t315
// @desc Creates a new response with a given TPEE status. Freed with {tpee_resp_free}.316
// @param {tpee_status_t} status - The TPEE status code.317
// @return {tpee_resp_t} - Returns a TPEE response.329
// @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.323
// @param {tpee_resp_t} *resp - The TPEE response.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.331
// @param {tpee_req_t} *req - The TPEE 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.337
// @param {tpee_resp_t} *resp - The TPEE response.338
// @param {tpee_conn_t} *conn - The TPEE connection.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.344
// @param {tpee_resp_t} *resp - The TPEE response.345
// @param {tpee_conn_t} *conn - The TPEE connection.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.351
// @param {tpee_resp_t} *resp - The TPEE response.352
// @param {tpee_conn_t} *conn - The TPEE connection.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.358
// @param {tpee_resp_t} *resp - The TPEE response.372
// @param {tpee_resp_t} *resp - The tpee response.373
void tpee_resp_free(tpee_resp_t *resp);375
// @func tpee_bind
401
// @func _tpee_req_read402
// @group tpee_req_t403
// @internal390
// @param {tpee_req_t} *req - The TPEE request.391
// @param {tpee_conn_t} *conn - The TPEE connection.392
// @return {tpee_err_t} - Returns an TPEE error code.404
// @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>
449
static tpee_resp_t fn_name(tpee_req_t *req)451
// @macro TPEE_GET433
#define TPEE_GET(path, fn_name) _TPEE_ON("GET", path, fn_name)452
#define TPEE_GET(path, fn_name) _TPEE_ON(TPEE_METHOD_GET, path, fn_name)435
// @macro TPEE_GET436
#define TPEE_HEAD(path, fn_name) _TPEE_ON("HEAD", path, fn_name)454
// @macro TPEE_HEAD455
#define TPEE_HEAD(path, fn_name) _TPEE_ON(TPEE_METHOD_HEAD, path, fn_name)457
// @macro TPEE_POST439
#define TPEE_POST(path, fn_name) _TPEE_ON("POST", path, fn_name)458
#define TPEE_POST(path, fn_name) _TPEE_ON(TPEE_METHOD_POST, path, fn_name)460
// @macro TPEE_PUT442
#define TPEE_PUT(path, fn_name) _TPEE_ON("PUT", path, fn_name)461
#define TPEE_PUT(path, fn_name) _TPEE_ON(TPEE_METHOD_PUT, path, fn_name)463
// @macro TPEE_DELETE445
#define TPEE_DELETE(path, fn_name) _TPEE_ON("DELETE", path, fn_name)464
#define TPEE_DELETE(path, fn_name) _TPEE_ON(TPEE_METHOD_DELETE, path, fn_name)466
// @macro TPEE_CONNECT448
#define TPEE_CONNECT(path, fn_name) _TPEE_ON("CONNECT", path, fn_name)467
#define TPEE_CONNECT(path, fn_name) _TPEE_ON(TPEE_METHOD_CONNECT, path, fn_name)469
// @macro TPEE_OPTIONS451
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON("OPTIONS", path, fn_name)470
#define TPEE_OPTIONS(path, fn_name) _TPEE_ON(TPEE_METHOD_OPTIONS, path, fn_name)472
// @macro TPEE_TRACE454
#define TPEE_TRACE(path, fn_name) _TPEE_ON("TRACE", path, fn_name)473
#define TPEE_TRACE(path, fn_name) _TPEE_ON(TPEE_METHOD_TRACE, path, fn_name)475
// @macro TPEE_PATCH457
#define TPEE_PATCH(path, fn_name) _TPEE_ON("PATCH", path, fn_name)476
#define TPEE_PATCH(path, fn_name) _TPEE_ON(TPEE_METHOD_PATCH, path, fn_name)478
// @macro TPEE_DEFAULT479
#define TPEE_DEFAULT(fn_name) \
527
// @internal528
// @desc The default route handler. Can be overwritten with {tpee_on_global}.529
// @param {tpee_t} *self - The global library context.511
// @param {tpee_req_t} *req - The TPEE request.512
// @return {tpee_resp_t} - Returns a TPEE response.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);
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);522
if (strcmp(route->method, req->method) == 0) {542
if (strcmp(method, req->method) == 0) {543
size_t len = strlen(route->path);544
char c = route->path[len - 1];545
if (c == '*') {
624
}626
// @impl tpee_on_route607
void tpee_on_route(char *method, char *path, tpee_handle_fn_t fn) {627
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,
705
return ret;706
}688
// @func _tpee_pairs_init689
// @group tpee_pairs_t708
// @func _tpee_pair_list_init709
// @group tpee_pair_list_t710
// @internal691
tpee_pairs_t _tpee_pairs_init(char *uri) {692
tpee_pairs_t self = (tpee_pairs_t) {693
.pairs = NULL,711
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);
724
if (lim > set) {725
key_len = lim - set;726
}707
self.pairs = realloc(self.pairs, sizeof(tpee_pair_t) * (self.len + 1));708
tpee_pair_t *pair = &self.pairs[self.len];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;
747
}749
// @impl tpee_req_queries730
tpee_pairs_t tpee_req_queries(tpee_req_t *req) {750
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] == '?') {735
return _tpee_pairs_init(req->url + i + 1);755
return _tpee_pair_list_init(req->url + i + 1);756
}757
}738
return (tpee_pairs_t) {739
.pairs = NULL,758
return (tpee_pair_list_t) {759
.pair_list = NULL,760
.len = 0,761
};762
}764
// @impl tpee_req_params745
tpee_pairs_t tpee_req_params(tpee_req_t *req) {746
return _tpee_pairs_init(req->body);765
tpee_pair_list_t tpee_req_params(tpee_req_t *req) {766
return _tpee_pair_list_init(req->body);767
}749
// @impl tpee_pairs_get750
char *tpee_pairs_get(tpee_pairs_t *self, char *key) {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++) {753
tpee_pair_t *pair = &self->pairs[i];773
tpee_pair_t *pair = &self->pair_list[i];774
if (strcmp(pair->key, key) == 0) {775
return pair->value;776
}
779
return NULL;780
}762
// @impl tpee_pairs_has763
bool tpee_pairs_has(tpee_pairs_t *self, char *key) {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++) {766
tpee_pair_t *pair = &self->pairs[i];786
tpee_pair_t *pair = &self->pair_list[i];787
if (strcmp(pair->key, key) == 0) {788
return true;789
}
792
return false;793
}775
// @impl tpee_pairs_free776
void tpee_pairs_free(tpee_pairs_t *self) {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++) {782
tpee_pair_t *pair = &self->pairs[i];802
tpee_pair_t *pair = &self->pair_list[i];803
free(pair->key);804
if (pair->value) {805
free(pair->value);806
}807
}788
free(self->pairs);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
}