A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
17
HTTP_ERR_INVALID_PRIVATE_KEY = 8,18
HTTP_ERR_READ_TIMEOUT = 9,19
HTTP_ERR_REQUEST_TOO_LONG = 10,20
} http_error_t;20
} http_err_t;22
typedef enum {23
HTTP_STATUS_CONTINUE = 100,
124
char *body;125
long body_len;126
void *ext_data;127
} http_request_t;127
} http_req_t;129
typedef struct {130
char *version;
133
int headers_len;134
char *body;135
long body_len;136
} http_response_t;136
} http_resp_t;138
struct Http {139
http_bind_t *binds;
141
pthread_t thread;142
bool quit;143
SSL_CTX *ssl_ctx;144
http_response_t (*on_request_fn)(http_request_t *);144
http_resp_t (*on_req_fn)(http_req_t *);145
};147
http_t *http_init(void);148
void http_cleanup(http_t *http);149
void http_on_request(http_t *http, http_response_t fn(http_request_t *));150
char *http_header_get(http_request_t *request, char *key);151
void http_header_set(http_response_t *response, char *key, char *value);152
http_response_t http_response_create(http_status_t status);153
void http_response_body(http_response_t *response, char *body, long len);154
void http_response_file(http_response_t *response, char *filename);155
http_error_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len);156
http_error_t http_listen(http_t *http);149
void http_on_req(http_t *http, http_resp_t fn(http_req_t *));150
char *http_header_get(http_req_t *req, char *key);151
void http_header_set(http_resp_t *resp, char *key, char *value);152
http_resp_t http_resp_create(http_status_t status);153
void http_resp_body_set(http_resp_t *resp, char *body, long len);154
http_err_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len);155
http_err_t http_listen(http_t *http);156
void http_close(http_t *http);158
#ifdef LIB_HTTP_IMPL
170
#include <openssl/ssl.h>171
#include <sys/socket.h>173
#define HTTP_VERSION "HTTP/1.1"174
#define HTTP_MAX_REQUEST_HEAD_SIZE 1000000175
#define HTTP_SERVER_BACKLOG 5176
#define HTTP_SERVER_SLEEP_TIME 100000000177
#define HTTP_SERVER_POLL_TIME 100179
http_t *http_init(void) {180
http_t *http = (http_t *) malloc(sizeof(http_t));
183
.binds_len = 0,184
.quit = false,185
.ssl_ctx = NULL,183
.on_request_fn = NULL,186
.on_req_fn = NULL,187
};188
return http;189
}
192
free(http);193
}192
void http_on_request(http_t *http, http_response_t fn(http_request_t *)) {193
http->on_request_fn = fn;194
}196
char *http_read_file(char *filename) {197
FILE *fp = fopen(filename, "r");198
fseek(fp, 0, SEEK_END);199
long len = ftell(fp);200
fseek(fp, 0, SEEK_SET);201
char *content = (char *) malloc(sizeof(char) * (len + 1));202
fread(content, len, sizeof(char), fp);203
content[len] = '\0';204
fclose(fp);205
return content;195
void http_on_req(http_t *http, http_resp_t fn(http_req_t *)) {196
http->on_req_fn = fn;197
}199
const char *http_status_str(http_status_t status) {
339
return write(conn->sockfd, buffer, len);340
}351
char *http_header_get(http_request_t *request, char *key) {352
if (request->headers_len > 0) {353
for (int i = 0; i < request->headers_len; i++) {354
http_header_t *header = &request->headers[i];342
char *http_header_get(http_req_t *req, char *key) {343
if (req->headers_len > 0) {344
for (int i = 0; i < req->headers_len; i++) {345
http_header_t *header = &req->headers[i];346
if (strcmp(header->key, key) == 0) {347
return header->value;348
}
351
return NULL;352
}363
void http_header_set(http_response_t *response, char *key, char *value) {364
response->headers = (http_header_t *) realloc(response->headers, sizeof(http_header_t) * (response->headers_len + 1));365
http_header_t *new_header = &response->headers[response->headers_len];366
response->headers_len++;354
void http_header_set(http_resp_t *resp, char *key, char *value) {355
resp->headers = (http_header_t *) realloc(resp->headers, sizeof(http_header_t) * (resp->headers_len + 1));356
http_header_t *new_header = &resp->headers[resp->headers_len];357
resp->headers_len++;358
int key_len = strlen(key);359
new_header->key = (char *) malloc(sizeof(char) * (key_len + 1));360
memcpy(new_header->key, key, key_len);
365
new_header->value[value_len] = '\0';366
}377
http_response_t http_response_create(http_status_t status) {378
const char *version = "HTTP/1.1";379
int version_len = strlen(version);380
http_response_t response = { NULL, status, NULL, 0, NULL, 0 };381
response.version = (char *) malloc(sizeof(char) * (version_len + 1));382
memcpy(response.version, version, version_len);383
response.version[version_len] = '\0';384
return response;368
http_resp_t http_resp_create(http_status_t status) {369
return (http_resp_t) {370
.version = strdup(HTTP_VERSION),371
.status = status,372
.headers = NULL,373
.headers_len = 0,374
.body = NULL,375
.body_len = 0,376
};377
}387
void http_response_body(http_response_t *response, char *body, long len) {388
if (response != NULL && body != NULL) {389
response->body = (char *) malloc(sizeof(char) * len);390
memcpy(response->body, body, len);391
response->body_len = len;379
void http_resp_body_set(http_resp_t *resp, char *body, long len) {380
if (resp != NULL && body != NULL) {381
resp->body = (char *) malloc(sizeof(char) * len);382
memcpy(resp->body, body, len);383
resp->body_len = len;384
char content_length[50];385
snprintf(content_length, 50, "%ld", len);394
http_header_set(response, (char *) "Content-Length", content_length);386
http_header_set(resp, (char *) "Content-Length", content_length);387
}388
}398
void http_response_file(http_response_t *response, char *filename) {399
char *content = http_read_file(filename);400
if (content != NULL) {401
http_response_body(response, content, strlen(content));402
free(content);390
void http_req_free(http_req_t *req) {391
if (req->method != NULL) {392
free(req->method);393
}404
}406
void http_request_free(http_request_t *request) {407
if (request->method != NULL) {408
free(request->method);394
if (req->url != NULL) {395
free(req->url);396
}410
if (request->url != NULL) {411
free(request->url);397
if (req->version != NULL) {398
free(req->version);399
}413
if (request->version != NULL) {414
free(request->version);415
}416
if (request->headers_len > 0) {417
for (int i = 0; i < request->headers_len; i++) {418
http_header_t *header = &request->headers[i];400
if (req->headers_len > 0) {401
for (int i = 0; i < req->headers_len; i++) {402
http_header_t *header = &req->headers[i];403
if (header->key != NULL) {404
free(header->key);405
}
408
}409
}410
}427
if (request->body != NULL) {428
free(request->body);411
if (req->body != NULL) {412
free(req->body);413
}430
if (request->ext_data != NULL) {431
free(request->ext_data);414
if (req->ext_data != NULL) {415
free(req->ext_data);416
}417
}435
void http_response_free(http_response_t *response) {436
if (response->version != NULL) {437
free(response->version);419
void http_resp_free(http_resp_t *resp) {420
if (resp->version != NULL) {421
free(resp->version);422
}439
if (response->headers_len > 0) {440
for (int i = 0; i < response->headers_len; i++) {441
http_header_t *header = &response->headers[i];423
if (resp->headers_len > 0) {424
for (int i = 0; i < resp->headers_len; i++) {425
http_header_t *header = &resp->headers[i];426
if (header->key != NULL) {427
free(header->key);428
}
430
free(header->value);431
}432
}433
if (resp->headers != NULL) {434
free(resp->headers);435
}436
}450
if (response->body != NULL) {451
free(response->body);437
if (resp->body != NULL) {438
free(resp->body);439
}440
}455
http_error_t http_read_request(http_conn_t *conn, http_request_t *request) {442
http_err_t http_read_req(http_conn_t *conn, http_req_t *req) {443
int len = 0;444
int ln = 0;445
int start = 0;446
char buffer[HTTP_MAX_REQUEST_HEAD_SIZE];460
*request = (http_request_t) {447
*req = (http_req_t) {448
.conn = conn,449
.method = NULL,450
.url = NULL,
492
// read method493
if (arg == 0) {494
int method_length = i - start;508
request->method = (char *) malloc(sizeof(char) * (method_length + 1));509
memcpy(request->method, line + start, method_length);510
request->method[method_length] = '\0';495
req->method = (char *) malloc(sizeof(char) * (method_length + 1));496
memcpy(req->method, line + start, method_length);497
req->method[method_length] = '\0';498
}499
// read url500
if (arg == 1) {501
int url_length = i - start;515
request->url = (char *) malloc(sizeof(char) * (url_length + 1));516
memcpy(request->url, line + start, url_length);517
request->url[url_length] = '\0';502
req->url = (char *) malloc(sizeof(char) * (url_length + 1));503
memcpy(req->url, line + start, url_length);504
req->url[url_length] = '\0';505
}506
// read version507
if (arg == 2) {508
int version_length = i - start;522
request->version = (char *) malloc(sizeof(char) * (version_length + 1));523
memcpy(request->version, line + start, version_length);524
request->version[version_length] = '\0';509
req->version = (char *) malloc(sizeof(char) * (version_length + 1));510
memcpy(req->version, line + start, version_length);511
req->version[version_length] = '\0';512
}513
start = i + 1;514
arg++;
518
// read headers519
else {520
int sep = 0;534
request->headers = (http_header_t *) realloc(request->headers, sizeof(http_header_t) * (request->headers_len + 1));535
http_header_t *header = &request->headers[request->headers_len];536
request->headers_len++;521
req->headers = (http_header_t *) realloc(req->headers, sizeof(http_header_t) * (req->headers_len + 1));522
http_header_t *header = &req->headers[req->headers_len];523
req->headers_len++;524
for (int i = 0; i < line_len; i++) {525
if (line[i] == ':') {526
int key_len = i;
545
if (len >= 4) {546
char *slice = buffer + (len - 4);547
if (strncmp(slice, "\r\n\r\n", 4) == 0) {561
char *content_length = http_header_get(request, (char *) "Content-Length");548
char *content_length = http_header_get(req, (char *) "Content-Length");549
if (content_length != NULL) {563
request->body_len = strtol(content_length, NULL, 10);564
if (request->body_len > 0) {565
request->body = (char *) malloc(sizeof(char) * (request->body_len + 1));566
http_read(conn, request->body, request->body_len);567
request->body[request->body_len] = '\0';550
req->body_len = strtol(content_length, NULL, 10);551
if (req->body_len > 0) {552
req->body = (char *) malloc(sizeof(char) * (req->body_len + 1));553
http_read(conn, req->body, req->body_len);554
req->body[req->body_len] = '\0';555
}556
}557
break;
561
return HTTP_ERR_OK;562
}577
void http_write_response(http_conn_t *conn, http_response_t response) {578
http_write(conn, response.version, strlen(response.version));564
void http_resp_write(http_conn_t *conn, http_resp_t resp) {565
http_write(conn, resp.version, strlen(resp.version));566
http_write(conn, (char *) " ", 1);567
char status[4];581
snprintf(status, 4, "%d", response.status);568
snprintf(status, 4, "%d", resp.status);569
http_write(conn, status, strlen(status));570
http_write(conn, (char *) " ", 1);584
const char *status_message = http_status_str(response.status);571
const char *status_message = http_status_str(resp.status);572
http_write(conn, (char *) status_message, strlen(status_message));573
http_write(conn, (char *) "\r\n", 2);587
if (response.headers_len > 0) {588
for (int i = 0; i < response.headers_len; i++) {589
http_header_t *header = &response.headers[i];574
if (resp.headers_len > 0) {575
for (int i = 0; i < resp.headers_len; i++) {576
http_header_t *header = &resp.headers[i];577
http_write(conn, header->key, strlen(header->key));578
http_write(conn, (char *) ": ", 2);579
http_write(conn, header->value, strlen(header->value));
581
}582
}583
http_write(conn, (char *) "\r\n", 2);597
if (response.body != NULL) {598
http_write(conn, response.body, response.body_len);584
if (resp.body != NULL) {585
http_write(conn, resp.body, resp.body_len);586
}587
}602
void *http_handle_client(void *ptr) {589
void *http_client_handle(void *ptr) {590
if (!ptr) {591
pthread_exit(0);592
}
609
pthread_exit(0);610
}611
}625
http_request_t request;626
http_error_t err = http_read_request(conn, &request);612
http_req_t req;613
http_err_t err = http_read_req(conn, &req);614
if (err != HTTP_ERR_OK) {615
if (conn->ssl != NULL) {616
SSL_shutdown(conn->ssl);
620
free(conn);621
pthread_exit(0);622
}636
if (request.method != NULL && request.url != NULL) {637
if (conn->http->on_request_fn != NULL) {638
http_response_t response = conn->http->on_request_fn(&request);639
http_write_response(conn, response);640
http_response_free(&response);623
if (req.method != NULL && req.url != NULL) {624
if (conn->http->on_req_fn != NULL) {625
http_resp_t resp = conn->http->on_req_fn(&req);626
http_resp_write(conn, resp);627
http_resp_free(&resp);628
}629
}643
http_request_free(&request);630
http_req_free(&req);631
if (conn->ssl != NULL) {632
SSL_shutdown(conn->ssl);633
SSL_free(conn->ssl);
637
pthread_exit(0);638
}653
void *http_handle_server(void *ptr) {640
void *http_server_handle(void *ptr) {641
http_t *http = (http_t *) ptr;642
struct pollfd fds[http->binds_len];643
for (int i = 0; i < http->binds_len; i++) {
647
while (!http->quit) {648
struct timespec sleep_req, sleep_rem;649
sleep_req.tv_sec = 0;663
sleep_req.tv_nsec = 100000000;650
sleep_req.tv_nsec = HTTP_SERVER_SLEEP_TIME;651
nanosleep(&sleep_req, &sleep_rem);665
if (poll(fds, http->binds_len, 100)) {652
if (poll(fds, http->binds_len, HTTP_SERVER_POLL_TIME)) {653
for (int i = 0; i < http->binds_len; i++) {654
if (fds[i].revents & POLLIN) {655
http_conn_t *conn = (http_conn_t *) malloc(sizeof(http_conn_t));
666
pthread_attr_t thread_attr;667
pthread_attr_init(&thread_attr);668
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);682
pthread_create(&thread, &thread_attr, http_handle_client, (void *) conn);669
pthread_create(&thread, &thread_attr, http_client_handle, (void *) conn);670
pthread_attr_destroy(&thread_attr);671
}672
}
677
}679
int http_tls_sni_callback(SSL *ssl, int *al, void *arg) {693
(void)(al);680
(void) al;681
http_conn_t *conn = (http_conn_t *) arg;682
const char *hostname = SSL_get_servername(ssl, 0);683
if (hostname != NULL) {
695
return SSL_TLSEXT_ERR_NOACK;696
}711
http_error_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len) {698
http_err_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len) {699
http->binds = (http_bind_t *) realloc(http->binds, sizeof(http_bind_t) * (http->binds_len + 1));700
http_bind_t *http_bind = &http->binds[http->binds_len];701
http_bind->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
718
if (bind(http_bind->sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) {719
return HTTP_ERR_ADDR_IN_USE;720
}734
if (listen(http_bind->sockfd, 5) < 0) {721
if (listen(http_bind->sockfd, HTTP_SERVER_BACKLOG) < 0) {722
return HTTP_ERR_NO_LISTEN;723
}724
for (int i = 0; i < hosts_len; i++) {
744
return HTTP_ERR_OK;745
}760
http_error_t http_listen(http_t *http) {747
http_err_t http_listen(http_t *http) {748
signal(SIGPIPE, SIG_IGN);749
const SSL_METHOD *method = TLS_server_method();750
http->ssl_ctx = SSL_CTX_new(method);
752
return HTTP_ERR_NO_SSL_CONTEXT;753
}754
SSL_CTX_set_tlsext_servername_callback(http->ssl_ctx, http_tls_sni_callback);768
pthread_create(&http->thread, 0, http_handle_server, (void *) http);755
pthread_create(&http->thread, 0, http_server_handle, (void *) http);756
return HTTP_ERR_OK;757
}