A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.16
*/18
// @file http.h20
#ifndef _SO_HTTP_H21
#define _SO_HTTP_H23
#include <stdbool.h>22
#include <openssl/ssl.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 SO_HTTP_IMPL31
#define SO_SOCK_IMPL32
#endif // SO_HTTP_IMPL34
// @include <so/sock.h>35
// @desc so/sock library handling server/client communication and multi-threading.36
#include <so/sock.h>38
// @enum http_err_t39
// @desc Error code.40
typedef enum {41
HTTP_ERR_OK = SOCK_ERR_OK,32
HTTP_ERR_X = SOCK_ERR__COUNT + 1,33
HTTP_ERR_Y = SOCK_ERR__COUNT + 2,34
HTTP_ERR__COUNT42
HTTP_ERR__COUNT,43
} http_err_t;45
// @enum http_status_t46
// @desc HTTP status code.47
typedef enum {48
HTTP_STATUS_CONTINUE = 100,49
HTTP_STATUS_SWITCHING_PROTOCOLS = 101,
109
__HTTP_STATUS_HANDLED = 999,110
} http_status_t;112
// @type http_t113
// @groupdef http_t114
// @desc Global context struct. Implemented here {http}.115
typedef struct http http_t;117
// @type http_host_t118
// @desc Represents a the host.119
typedef sock_host_t http_host_t;121
// @type http_bind_t122
// @desc Represents a the binding (port).123
typedef sock_bind_t http_bind_t;125
// @type http_conn_t126
// @groupdef http_conn_t127
// @desc Represents a client connection.128
typedef sock_conn_t http_conn_t;130
// @struct http_header_t131
// @desc A HTTP header.132
typedef struct {133
char *key;134
char *value;135
} http_header_t;137
// @struct http_req_t138
// @groupdef http_req_t139
// @desc A HTTP request struct.140
typedef struct {141
http_conn_t *conn;142
char *method;
149
void *ext_data;150
} http_req_t;152
// @struct http_resp_t153
// @groupdef http_resp_t154
// @desc A HTTP response struct.155
typedef struct {156
char *version;157
http_status_t status;
161
long body_len;162
} http_resp_t;164
// @type http_global_fn_t165
// @desc A function type for a global route handler.166
typedef http_resp_t (*http_global_fn_t)(http_t *, http_req_t *);168
// @type http_handle_fn_t169
// @desc A function type for a route handler.170
typedef http_resp_t (*http_handle_fn_t)(http_req_t *);172
// @type http_hook_fn_t173
// @desc A function type for a hook handler.174
typedef void (*http_hook_fn_t)(http_req_t *);176
// @struct http_route_t177
// @desc A route.178
typedef struct {179
char *method;180
char *path;181
http_handle_fn_t fn;182
} http_route_t;184
// @struct http_pair_t185
typedef struct {186
char *key;187
char *value;188
} http_pair_t;190
// @struct http_pairs_t191
// @groupdef http_pairs_t192
typedef struct {193
http_pair_t *pairs;194
size_t len;195
} http_pairs_t;197
// @struct http198
// @group http_t199
// @desc Implementation of the global context struct. Type here {http_t}.200
struct http {201
sock_t *sock;202
http_global_fn_t fn;203
};205
// @func http_init206
// @group http_t207
// @desc Initializes global library context {http_t}. Free with {http_cleanup}.208
// @return {http_t} * - Returns the global library context.209
http_t *http_init(void);211
// @func http_cleanup212
// @group http_t213
// @desc Cleans up {http_t}.214
// @param {http_t} *self - The global library context.215
void http_cleanup(http_t *self);217
// @func http_on_global218
// @group http_t219
// @desc Set and override the default global route handler {_http_on_global}.220
// @param {http_t} *self - The global library context.221
// @param {http_global_fn_t} fn - The global route handler.222
void http_on_global(http_t *self, http_global_fn_t fn);224
// @func http_on_route225
// @note Use macros like {HTTP_GET} and others to register routes more easily.226
// @desc Registers a new route.227
// @param char *method - The HTTP method.228
// @param char *url - The HTTP URL (path).229
// @param {http_handle_fn_t} fn - The route handler.230
void http_on_route(char *method, char *url, http_handle_fn_t fn);232
// @func http_on_default233
// @note Use macro {HTTP_DEFAULT} instead.234
// @desc Registers a fallback handler.235
// @param {http_handle_fn_t} fn - The route handler.236
void http_on_default(http_handle_fn_t fn);238
// @func http_on_pre_route239
// @note Use macro {HTTP_PRE_HOOK} instead.240
// @desc Registers a hook handler which is performed before the route handler is executed.241
// @param {http_hook_fn_t} fn - The hook handler.242
void http_on_pre_route(http_hook_fn_t fn);244
// @func http_on_post_route245
// @note Use macro {HTTP_POST_HOOK} instead.246
// @desc Registers a hook handler which is performed after the route handler is executed.247
// @param {http_hook_fn_t} fn - The hook handler.248
void http_on_post_route(http_hook_fn_t fn);250
// @func http_req_path251
// @group http_req_t252
// @param {http_req_t} *req - The HTTP request.253
// @return char * - Returns the request path.254
char *http_req_path(http_req_t *req);256
// @func http_req_body257
// @group http_req_t258
// @param {http_req_t} *req - The HTTP request.259
// @return char * - Returns the request body.260
char *http_req_body(http_req_t *req);262
// @func http_req_queries263
// @group http_req_t264
// @param {http_req_t} *req - The HTTP request.265
// @return {http_pairs_t} - Returns the request queries.266
http_pairs_t http_req_queries(http_req_t *req);268
// @func http_req_params269
// @group http_req_t270
// @param {http_req_t} *req - The HTTP request.271
// @return {http_pairs_t} - Returns the request params.272
http_pairs_t http_req_params(http_req_t *req);274
// @func http_pairs_get275
// @group http_pairs_t276
char *http_pairs_get(http_pairs_t *self, char *key);278
// @func http_pairs_has279
// @group http_pairs_t280
bool http_pairs_has(http_pairs_t *self, char *key);282
// @func http_pairs_free283
// @group http_pairs_t284
void http_pairs_free(http_pairs_t *self);286
// @func http_req_header287
// @group http_req_t288
// @param {http_req_t} *req - The HTTP request.289
// @param char *key - The header key.290
// @return char * - Returns a header value.291
char *http_req_header(http_req_t *req, char *key);293
// @func http_resp_header294
// @group http_resp_t295
// @desc Writes a header to the response. The header content is consumed and can be freed afterward.296
// @param {http_resp_t} *resp - The HTTP response.297
// @param char *key - The header key.298
// @param char *value - The header value.299
void http_resp_header(http_resp_t *resp, char *key, char *value);301
// @func http_resp_create302
// @group http_resp_t303
// @desc Creates a new response with a given HTTP status. Freed with {http_resp_free}.304
// @param {http_status_t} status - The HTTP status code.305
// @return {http_resp_t} - Returns a HTTP response.306
http_resp_t http_resp_create(http_status_t status);308
// @func http_resp_body309
// @group http_resp_t310
// @desc Writes body content to the response. The body content is consumed and can be freed afterward.311
// @param {http_resp_t} *resp - The HTTP response.312
// @param char *body - The body content.313
// @param long len - The body length.314
void http_resp_body(http_resp_t *resp, char *body, long len);316
// @func http_req_free317
// @group http_req_t318
// @desc Frees a request.319
// @param {http_req_t} *req - The HTTP request.320
void http_req_free(http_req_t *req);178
void http_resp_write_head(http_conn_t *conn, http_resp_t *resp);179
void http_resp_write_body(http_conn_t *conn, http_resp_t *resp);180
void http_resp_write(http_conn_t *conn, http_resp_t *resp);322
// @func http_resp_write_head323
// @group http_resp_t324
// @desc Writes a response header to the socket stream.325
// @param {http_resp_t} *resp - The HTTP response.326
// @param {http_conn_t} *conn - The HTTP connection.327
void http_resp_write_head(http_resp_t *resp, http_conn_t *conn);329
// @func http_resp_write_body330
// @group http_resp_t331
// @desc Writes a response body to the socket stream.332
// @param {http_resp_t} *resp - The HTTP response.333
// @param {http_conn_t} *conn - The HTTP connection.334
void http_resp_write_body(http_resp_t *resp, http_conn_t *conn);336
// @func http_resp_write337
// @group http_resp_t338
// @desc Writes a response to the socket stream.339
// @param {http_resp_t} *resp - The HTTP response.340
// @param {http_conn_t} *conn - The HTTP connection.341
void http_resp_write(http_resp_t *resp, http_conn_t *conn);343
// @func http_resp_free344
// @group http_resp_t345
// @desc Frees a response.346
// @param {http_resp_t} *resp - The HTTP response.347
void http_resp_free(http_resp_t *resp);349
// @func http_bind350
// @group http_t351
// @param {http_t} *self - The global library context.352
// @param char *hostname - The hostname.353
// @param int port - The port.354
// @param {http_host_t} *hosts - The host to bind to.355
// @param int hosts_len - The host length of to be bound host.356
http_err_t http_bind(http_t *self, char *hostname, int port, http_host_t *hosts, int hosts_len);358
// @func http_listen359
// @group http_t360
// @param {http_t} *self - The global library context.361
http_err_t http_listen(http_t *self);363
// @func http_bind_listen364
// @group http_t365
// @param {http_t} *self - The global library context.366
// @param char *hostname - The hostname.367
// @param int port - The port.368
http_err_t http_bind_listen(http_t *self, char *hostname, int port);370
// @func http_close371
// @group http_t372
// @param {http_t} *self - The global library context.373
void http_close(http_t *self);187
http_err_t _http_read_req(http_conn_t *conn, http_req_t *req);375
// @func _http_req_read376
// @group http_req_t377
// @internal378
// @param {http_req_t} *req - The HTTP request.379
// @param {http_conn_t} *conn - The HTTP connection.380
// @return {http_err_t} - Returns an HTTP error code.381
http_err_t _http_req_read(http_req_t *req, http_conn_t *conn);383
#ifdef SO_HTTP_IMPL
393
#include <poll.h>394
#include <errno.h>395
#include <signal.h>202
#include <openssl/ssl.h>203
#include <sys/socket.h>397
// @macro HTTP_VERSION398
// @internal399
#define HTTP_VERSION "HTTP/1.1"401
// @macro HTTP_MAX_REQUEST_HEAD_SIZE402
// @internal403
#define HTTP_MAX_REQUEST_HEAD_SIZE 1000000405
// @macro HTTP_RESP_HANDLED406
// @group http_resp_t407
// @desc Handled {http_resp_t}.408
#define HTTP_RESP_HANDLED (http_resp_t) { .status = __HTTP_STATUS_HANDLED }410
// @macro _HTTP_ON411
// @internal412
#define _HTTP_ON(method, path, fn_name) \413
static http_resp_t fn_name(http_req_t *req); \414
__attribute__((constructor)) \
417
} \418
static http_resp_t fn_name(http_req_t *req)420
// @macro HTTP_GET421
#define HTTP_GET(path, fn_name) _HTTP_ON("GET", path, fn_name)423
// @macro HTTP_GET424
#define HTTP_HEAD(path, fn_name) _HTTP_ON("HEAD", path, fn_name)426
// @macro HTTP_POST427
#define HTTP_POST(path, fn_name) _HTTP_ON("POST", path, fn_name)429
// @macro HTTP_PUT430
#define HTTP_PUT(path, fn_name) _HTTP_ON("PUT", path, fn_name)432
// @macro HTTP_DELETE433
#define HTTP_DELETE(path, fn_name) _HTTP_ON("DELETE", path, fn_name)435
// @macro HTTP_CONNECT436
#define HTTP_CONNECT(path, fn_name) _HTTP_ON("CONNECT", path, fn_name)438
// @macro HTTP_OPTIONS439
#define HTTP_OPTIONS(path, fn_name) _HTTP_ON("OPTIONS", path, fn_name)441
// @macro HTTP_TRACE442
#define HTTP_TRACE(path, fn_name) _HTTP_ON("TRACE", path, fn_name)444
// @macro HTTP_PATCH445
#define HTTP_PATCH(path, fn_name) _HTTP_ON("PATCH", path, fn_name)447
// @macro HTTP_DEFAULT448
#define HTTP_DEFAULT(fn_name) \449
static http_resp_t fn_name(http_req_t *req); \450
__attribute__((constructor)) \
453
} \454
static http_resp_t fn_name(http_req_t *req)235
#define HTTP_MIDDLEWARE_PRE(fn_name) \456
// @macro HTTP_PRE_HOOK457
#define HTTP_PRE_HOOK(fn_name) \458
static void fn_name(http_req_t *req); \459
__attribute__((constructor)) \460
static void __macro_http_fn_register_##fn_name() { \
462
} \463
static void fn_name(http_req_t *req)243
#define HTTP_MIDDLEWARE_POST(fn_name) \465
// @macro HTTP_POST_HOOK466
#define HTTP_POST_HOOK(fn_name) \467
static void fn_name(http_req_t *req); \468
__attribute__((constructor)) \469
static void __macro_http_fn_register_##fn_name() { \
471
} \472
static void fn_name(http_req_t *req)474
// @global _http_routes475
// @internal476
http_route_t *_http_routes = NULL;478
// @global _http_routes_len479
// @internal480
size_t _http_routes_len = 0;482
// @global _http_default_route483
// @internal484
http_handle_fn_t _http_default_route;486
// @global _http_pre_route487
// @internal488
http_hook_fn_t _http_pre_route;490
// @global _http_post_route491
// @internal492
http_hook_fn_t _http_post_route;494
// @func _http_on_global495
// @group http_t496
// @internal497
// @desc The default route handler. Can be overwritten with {http_on_global}.498
// @param {http_t} *self - The global library context.499
// @param {http_req_t} *req - The HTTP request.500
// @return {http_resp_t} - Returns a HTTP response.501
http_resp_t _http_on_global(http_t *self, http_req_t *req) {502
http_resp_t resp = http_resp_create(HTTP_STATUS_NOT_FOUND);503
if (_http_pre_route != NULL) {
540
return resp;541
}543
// @func _http_on_handle544
// @internal545
sock_err_t _http_on_handle(sock_t *sock, http_conn_t *conn) {546
http_t *self= (http_t *) sock->ext;547
http_req_t req;302
http_err_t err = _http_read_req(conn, &req);548
http_err_t err = _http_req_read(&req, conn);549
if (err != HTTP_ERR_OK) {550
return err;551
}552
if (req.method != NULL && req.url != NULL) {553
if (self->fn != NULL) {554
http_resp_t resp = self->fn(self, &req);309
http_resp_write(conn, &resp);555
http_resp_write(&resp, conn);556
http_resp_free(&resp);557
}558
}
625
return req->body;626
}628
// @func _http_pair_decode629
// @internal630
char *_http_pair_decode(char *str, size_t len) {631
size_t j = 0;632
size_t k = 0;
658
return ret;659
}661
// @func _http_pairs_init662
// @group http_pairs_t663
// @internal664
http_pairs_t _http_pairs_init(char *uri) {665
http_pairs_t self = (http_pairs_t) {666
.pairs = NULL,
758
}759
}761
// @func _http_status_str762
// @internal763
const char *_http_status_str(http_status_t status) {764
switch (status) {765
case HTTP_STATUS_CONTINUE:
889
}890
}639
ssize_t http_read(http_conn_t *conn, char *buffer, size_t len) {640
return sock_read(conn, buffer, len);892
// @func _http_conn_read893
// @group http_conn_t894
// @internal895
ssize_t _http_conn_read(http_conn_t *conn, char *buffer, size_t len) {896
return sock_conn_read(conn, buffer, len);897
}643
ssize_t http_write(http_conn_t *conn, char *buffer, size_t len) {644
return sock_write(conn, buffer, len);899
// @func _http_conn_write900
// @group http_conn_t901
// @internal902
ssize_t _http_conn_write(http_conn_t *conn, char *buffer, size_t len) {903
return sock_conn_write(conn, buffer, len);904
}906
char *http_req_header(http_req_t *req, char *key) {
1001
}1002
}745
http_err_t _http_read_req(http_conn_t *conn, http_req_t *req) {1004
http_err_t _http_req_read(http_req_t *req, http_conn_t *conn) {1005
int len = 0;1006
int ln = 0;1007
int start = 0;
1019
};1020
while (true) {1021
char c;763
int read = http_read(conn, &c, 1);1022
int read = _http_conn_read(conn, &c, 1);1023
if (read == 0) {1024
break;1025
}
1112
req->body_len = strtol(content_length, NULL, 10);1113
if (req->body_len > 0) {1114
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));856
http_read(conn, req->body, req->body_len);1115
_http_conn_read(conn, req->body, req->body_len);1116
req->body[req->body_len] = '\0';1117
}1118
}
1123
return HTTP_ERR_OK;1124
}867
void http_resp_write_head(http_conn_t *conn, http_resp_t *resp) {868
http_write(conn, resp->version, strlen(resp->version));869
http_write(conn, (char *) " ", 1);1126
void http_resp_write_head(http_resp_t *resp, http_conn_t *conn) {1127
_http_conn_write(conn, resp->version, strlen(resp->version));1128
_http_conn_write(conn, (char *) " ", 1);1129
char status[4];1130
snprintf(status, 4, "%d", resp->status);872
http_write(conn, status, strlen(status));873
http_write(conn, (char *) " ", 1);1131
_http_conn_write(conn, status, strlen(status));1132
_http_conn_write(conn, (char *) " ", 1);1133
const char *status_message = _http_status_str(resp->status);875
http_write(conn, (char *) status_message, strlen(status_message));876
http_write(conn, (char *) "\r\n", 2);1134
_http_conn_write(conn, (char *) status_message, strlen(status_message));1135
_http_conn_write(conn, (char *) "\r\n", 2);1136
if (resp->headers_len > 0) {1137
for (int i = 0; i < resp->headers_len; i++) {1138
http_header_t *header = &resp->headers[i];880
http_write(conn, header->key, strlen(header->key));881
http_write(conn, (char *) ": ", 2);882
http_write(conn, header->value, strlen(header->value));883
http_write(conn, (char *) "\r\n", 2);1139
_http_conn_write(conn, header->key, strlen(header->key));1140
_http_conn_write(conn, (char *) ": ", 2);1141
_http_conn_write(conn, header->value, strlen(header->value));1142
_http_conn_write(conn, (char *) "\r\n", 2);1143
}1144
}886
http_write(conn, (char *) "\r\n", 2);1145
_http_conn_write(conn, (char *) "\r\n", 2);1146
}889
void http_resp_write_body(http_conn_t *conn, http_resp_t *resp) {1148
void http_resp_write_body(http_resp_t *resp, http_conn_t *conn) {1149
if (resp->body != NULL) {891
http_write(conn, resp->body, resp->body_len);1150
_http_conn_write(conn, resp->body, resp->body_len);1151
}1152
}895
void http_resp_write(http_conn_t *conn, http_resp_t *resp) {1154
void http_resp_write(http_resp_t *resp, http_conn_t *conn) {1155
if (resp->status == __HTTP_STATUS_HANDLED) {1156
return;1157
}
1172
http_resp_header(resp, (char *) "Content-Length", content_length);1173
}1174
}916
http_resp_write_head(conn, resp);917
http_resp_write_body(conn, resp);1175
http_resp_write_head(resp, conn);1176
http_resp_write_body(resp, conn);1177
}1179
http_err_t http_bind(http_t *self, char *hostname, int port, http_host_t *hosts, int hosts_len) {