A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
11
## Usage13
```c14
#define LIB_HTTP_IMPL14
#define SO_HTTP_IMPL15
#include <so/http.h>17
#define HOST "0.0.0.0"
1
#ifndef LIB_HTTP_H2
#define LIB_HTTP_H1
#ifndef _SO_HTTP_H2
#define _SO_HTTP_H4
#include <stdbool.h>5
#include <openssl/ssl.h>6
#include <sys/socket.h>8
#ifdef LIB_HTTP_IMPL9
#define LIB_SOCK_IMPL10
#endif8
#ifdef SO_HTTP_IMPL9
#define SO_SOCK_IMPL10
#endif // SO_HTTP_IMPL11
#include <so/sock.h>13
typedef enum {
139
};141
http_t *http_init(void);142
void http_cleanup(http_t *http);143
void http_on_global(http_t *http, http_global_fn_t fn);142
void http_cleanup(http_t *self);143
void http_on_global(http_t *self, http_global_fn_t fn);144
void http_on_route(char *method, char *url, http_handle_fn_t fn);145
void http_on_default(http_handle_fn_t fn);146
void http_on_pre_route(http_hook_fn_t fn);
161
void http_resp_write_body(http_conn_t *conn, http_resp_t *resp);162
void http_resp_write(http_conn_t *conn, http_resp_t *resp);163
void http_resp_free(http_resp_t *resp);164
http_err_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len);165
http_err_t http_listen(http_t *http);166
http_err_t http_bind_listen(http_t *http, char *hostname, int port);167
void http_close(http_t *http);164
http_err_t http_bind(http_t *self, char *hostname, int port, http_host_t *hosts, int hosts_len);165
http_err_t http_listen(http_t *self);166
http_err_t http_bind_listen(http_t *self, char *hostname, int port);167
void http_close(http_t *self);169
http_err_t _http_read_req(http_conn_t *conn, http_req_t *req);171
#ifdef LIB_HTTP_IMPL171
#ifdef SO_HTTP_IMPL173
#include <stdio.h>174
#include <stdlib.h>
235
http_hook_fn_t _http_pre_route;236
http_hook_fn_t _http_post_route;238
http_resp_t _http_on_global(http_t *http, http_req_t *req) {238
http_resp_t _http_on_global(http_t *self, http_req_t *req) {239
http_resp_t resp = http_resp_create(HTTP_STATUS_NOT_FOUND);240
if (_http_pre_route != NULL) {241
_http_pre_route(req);
278
}280
sock_err_t _http_on_handle(sock_t *sock, http_conn_t *conn) {281
http_t *http = (http_t *) sock->ext;281
http_t *self= (http_t *) sock->ext;282
http_req_t req;283
http_err_t err = _http_read_req(conn, &req);284
if (err != HTTP_ERR_OK) {285
return err;286
}287
if (req.method != NULL && req.url != NULL) {288
if (http->fn != NULL) {289
http_resp_t resp = http->fn(http, &req);288
if (self->fn != NULL) {289
http_resp_t resp = self->fn(self, &req);290
http_resp_write(conn, &resp);291
http_resp_free(&resp);292
}
296
}298
http_t *http_init(void) {299
http_t *http = (http_t *) malloc(sizeof(http_t));300
*http = (http_t) {299
http_t *self = (http_t *) malloc(sizeof(http_t));300
*self = (http_t) {301
.sock = sock_init(),302
.fn = &_http_on_global,303
};304
sock_set_ext(http->sock, http);305
sock_on_global(http->sock, _http_on_handle);306
return http;304
sock_set_ext(self->sock, self);305
sock_on_global(self->sock, _http_on_handle);306
return self;307
}309
void http_cleanup(http_t *http) {310
if (http->sock != NULL) {311
sock_cleanup(http->sock);309
void http_cleanup(http_t *self) {310
if (self->sock != NULL) {311
sock_cleanup(self->sock);312
}313
free(http);313
free(self);314
}316
void http_on_global(http_t *http, http_global_fn_t fn) {317
http->fn = fn;316
void http_on_global(http_t *self, http_global_fn_t fn) {317
self->fn = fn;318
}320
void http_on_route(char *method, char *path, http_handle_fn_t fn) {
895
http_resp_write_body(conn, resp);896
}898
http_err_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len) {899
return sock_bind(http->sock, hostname, port, hosts, hosts_len);898
http_err_t http_bind(http_t *self, char *hostname, int port, http_host_t *hosts, int hosts_len) {899
return sock_bind(self->sock, hostname, port, hosts, hosts_len);900
}902
http_err_t http_listen(http_t *http) {903
return sock_listen(http->sock);902
http_err_t http_listen(http_t *self) {903
return sock_listen(self->sock);904
}906
http_err_t http_bind_listen(http_t *http, char *hostname, int port) {907
return sock_bind_listen(http->sock, hostname, port);906
http_err_t http_bind_listen(http_t *self, char *hostname, int port) {907
return sock_bind_listen(self->sock, hostname, port);908
}910
void http_close(http_t *http) {911
sock_close(http->sock);910
void http_close(http_t *self) {911
sock_close(self->sock);912
}914
#endif /* LIB_HTTP_IMPL */915
#endif /* LIB_HTTP_H */914
#endif // SO_HTTP_IMPL915
#endif // _SO_HTTP_H