A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
560
return HTTP_ERR_OK;561
}563
// @impl http_init564
http_t *http_init(void) {565
http_t *self = (http_t *) malloc(sizeof(http_t));566
*self = (http_t) {
572
return self;573
}575
// @impl http_cleanup576
void http_cleanup(http_t *self) {577
if (self->sock != NULL) {578
sock_cleanup(self->sock);
580
free(self);581
}583
// @impl http_on_global584
void http_on_global(http_t *self, http_global_fn_t fn) {585
self->fn = fn;586
}588
// @impl http_on_route589
void http_on_route(char *method, char *path, http_handle_fn_t fn) {590
_http_routes = realloc(_http_routes, sizeof(http_route_t) * (_http_routes_len + 1));591
_http_routes[_http_routes_len] = (http_route_t) {
596
_http_routes_len++;597
}599
// @impl http_on_default600
void http_on_default(http_handle_fn_t fn) {601
_http_default_route = fn;602
}604
// @impl http_on_pre_route605
void http_on_pre_route(http_hook_fn_t fn) {606
_http_pre_route = fn;607
}609
// @impl http_on_post_route610
void http_on_post_route(http_hook_fn_t fn) {611
_http_post_route = fn;612
}614
// @impl http_req_path615
char *http_req_path(http_req_t *req) {616
char *uri = req->url;617
size_t url_len = strlen(uri);
629
return path;630
}632
// @impl http_req_body633
char *http_req_body(http_req_t *req) {634
return req->body;635
}
709
return self;710
}712
// @impl http_req_queries713
http_pairs_t http_req_queries(http_req_t *req) {714
size_t url_len = strlen(req->url);715
// skip path
724
};725
}727
// @impl http_req_params728
http_pairs_t http_req_params(http_req_t *req) {729
return _http_pairs_init(req->body);730
}732
// @impl http_pairs_get733
char *http_pairs_get(http_pairs_t *self, char *key) {734
if (self && self->len > 0) {735
for (size_t i = 0; i < self->len; i++) {
742
return NULL;743
}745
// @impl http_pairs_has746
bool http_pairs_has(http_pairs_t *self, char *key) {747
if (self && self->len > 0) {748
for (size_t i = 0; i < self->len; i++) {
755
return false;756
}758
// @impl http_pairs_free759
void http_pairs_free(http_pairs_t *self) {760
if (self == NULL) {761
return;
917
return sock_conn_write(conn, buffer, len);918
}920
// @impl http_req_header921
char *http_req_header(http_req_t *req, char *key) {922
if (req->headers_len > 0) {923
for (int i = 0; i < req->headers_len; i++) {
930
return NULL;931
}933
// @impl http_resp_header934
void http_resp_header(http_resp_t *resp, char *key, char *value) {935
resp->headers = (http_header_t *) realloc(resp->headers, sizeof(http_header_t) * (resp->headers_len + 1));936
http_header_t *new_header = &resp->headers[resp->headers_len];
945
new_header->value[value_len] = '\0';946
}948
// @impl http_resp_create949
http_resp_t http_resp_create(http_status_t status) {950
return (http_resp_t) {951
.version = strdup(HTTP_VERSION),
957
};958
}960
// @impl http_resp_body961
void http_resp_body(http_resp_t *resp, char *body, long len) {962
if (resp != NULL && body != NULL) {963
resp->body = (char *) malloc(sizeof(char) * len);
966
}967
}969
// @impl http_req_free970
void http_req_free(http_req_t *req) {971
if (req->method != NULL) {972
free(req->method);
997
}998
}1000
// @impl http_resp_free1001
void http_resp_free(http_resp_t *resp) {1002
if (resp->version != NULL) {1003
free(resp->version);
1021
}1022
}1024
// @impl _http_req_read1025
http_err_t _http_req_read(http_req_t *req, http_conn_t *conn) {1026
int len = 0;1027
int ln = 0;
1144
return HTTP_ERR_OK;1145
}1147
// @impl http_resp_write_head1148
void http_resp_write_head(http_resp_t *resp, http_conn_t *conn) {1149
_http_conn_write(conn, resp->version, strlen(resp->version));1150
_http_conn_write(conn, (char *) " ", 1);
1167
_http_conn_write(conn, (char *) "\r\n", 2);1168
}1170
// @impl http_resp_write_body1171
void http_resp_write_body(http_resp_t *resp, http_conn_t *conn) {1172
if (resp->body != NULL) {1173
_http_conn_write(conn, resp->body, resp->body_len);1174
}1175
}1177
// @impl http_resp_write1178
void http_resp_write(http_resp_t *resp, http_conn_t *conn) {1179
if (resp->status == __HTTP_STATUS_HANDLED) {1180
return;
1200
http_resp_write_body(resp, conn);1201
}1203
// @impl http_bind1204
http_err_t http_bind(http_t *self, char *hostname, int port, http_host_t *hosts, int hosts_len) {1205
return sock_bind(self->sock, hostname, port, hosts, hosts_len);1206
}1208
// @impl http_listen1209
http_err_t http_listen(http_t *self) {1210
return sock_listen(self->sock);1211
}1213
// @impl http_bind_listen1214
http_err_t http_bind_listen(http_t *self, char *hostname, int port) {1215
return sock_bind_listen(self->sock, hostname, port);1216
}1218
// @impl http_close1219
void http_close(http_t *self) {1220
sock_close(self->sock);1221
}