A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
14
#include "libhttp.h"16
#define MAX_REQUEST_HEAD_SIZE 4096016
#define HTTP_MAX_REQUEST_HEAD_SIZE 100000018
int http_sockfd;18
http_bind_t *http_binds = NULL;19
int http_binds_len = 0;20
pthread_t http_thread;21
bool http_quit = false;22
bool http_error = false;22
SSL_CTX *http_ctx = NULL;23
SSL_CTX *http_ssl_ctx = NULL;25
http_response_t (*on_request_fn)(http_request_t *) = NULL;
169
}170
}171
ssize_t http_read(conn_t *conn, void *buffer, size_t len) {172
ssize_t http_read(http_conn_t *conn, void *buffer, size_t len) {173
if (conn->ssl != NULL) {174
return SSL_read(conn->ssl, buffer, len);175
}176
return read(conn->sockfd, buffer, len);177
}178
ssize_t http_write(conn_t *conn, void *buffer, size_t len) {179
ssize_t http_write(http_conn_t *conn, void *buffer, size_t len) {180
if (conn->ssl != NULL) {181
return SSL_write(conn->ssl, buffer, len);182
}
299
}300
}301
http_request_t http_read_request(conn_t *conn) {302
http_request_t http_read_request(http_conn_t *conn) {303
int len = 0;304
int ln = 0;305
int start = 0;305
char buffer[MAX_REQUEST_HEAD_SIZE];306
char buffer[HTTP_MAX_REQUEST_HEAD_SIZE];307
http_request_t request = {308
.conn = conn,309
.method = NULL,
322
break;323
}324
// break if too long324
if (len == MAX_REQUEST_HEAD_SIZE) {325
if (len == HTTP_MAX_REQUEST_HEAD_SIZE) {326
perror("request is too long");327
break;328
}
414
return request;415
}416
void http_write_response(conn_t *conn, http_response_t response) {417
void http_write_response(http_conn_t *conn, http_response_t response) {418
http_write(conn, response.version, strlen(response.version));419
http_write(conn, " ", 1);420
char status[4];
443
if (!ptr) {444
pthread_exit(0);445
}445
conn_t *conn = (conn_t *) ptr;446
if (http_ctx != NULL) {447
conn->ssl = SSL_new(http_ctx);446
http_conn_t *conn = (http_conn_t *) ptr;447
if (conn->bind->hosts_len > 0) {448
SSL_CTX_set_tlsext_servername_arg(http_ssl_ctx, (void *) conn);449
conn->ssl = SSL_new(http_ssl_ctx);450
SSL_set_fd(conn->ssl, conn->sockfd);451
if (SSL_accept(conn->ssl) <= 0) {450
perror("cannot SSL handshake");452
pthread_exit(0);453
}454
}455
http_request_t request = http_read_request(conn);456
if (request.method != NULL && request.url != NULL) {456
// long addr = ((struct sockaddr_in *) &request.conn->addr)->sin_addr.s_addr;457
// printf(458
// "%ld.%ld.%ld.%ld -> %s %s\n",459
// (addr ) & 0xff,460
// (addr >> 8) & 0xff,461
// (addr >> 16) & 0xff,462
// (addr >> 24) & 0xff,463
// request.method,464
// request.url465
// );457
if (on_request_fn != NULL) {458
http_response_t response = on_request_fn(&request);459
http_write_response(conn, response);
461
}462
}463
http_request_free(&request);473
if (http_ctx != NULL) {464
if (conn->ssl != NULL) {465
SSL_shutdown(conn->ssl);466
SSL_free(conn->ssl);467
}
471
}473
void *http_handle_server(void *ptr) {483
if (!ptr) {484
pthread_exit(0);474
(void)(ptr);475
struct pollfd fds[http_binds_len];476
for (int i = 0; i < http_binds_len; i++) {477
fds[i].fd = http_binds[i].sockfd;478
fds[i].events = POLLIN | POLLPRI;479
}486
int sockfd = *((int *) ptr);487
struct pollfd fds[1];488
fds[0].fd = sockfd;489
fds[0].events = POLLIN | POLLPRI;480
while (!http_quit) {481
struct timespec sleep_req, sleep_rem;482
sleep_req.tv_sec = 0;483
sleep_req.tv_nsec = 100000000;484
nanosleep(&sleep_req, &sleep_rem);495
if (poll(fds, 1, 100)) {496
conn_t *conn = malloc(sizeof(conn_t));497
conn->addr_len = sizeof(conn->addr);498
conn->sockfd = accept(sockfd, &conn->addr, (socklen_t *) &conn->addr_len);499
conn->ssl = NULL;500
if (conn->sockfd <= 0) {501
free(conn);502
}503
else {504
pthread_t thread;505
pthread_attr_t thread_attr;506
pthread_attr_init(&thread_attr);507
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);508
pthread_create(&thread, &thread_attr, http_handle_client, (void *) conn);509
pthread_attr_destroy(&thread_attr);485
if (poll(fds, http_binds_len, 100)) {486
for (int i = 0; i < http_binds_len; i++) {487
if (fds[i].revents & POLLIN) {488
http_conn_t *conn = malloc(sizeof(http_conn_t));489
conn->addr_len = sizeof(conn->addr);490
conn->sockfd = accept(http_binds[i].sockfd, &conn->addr, (socklen_t *) &conn->addr_len);491
conn->bind = &http_binds[i];492
conn->ssl = NULL;493
if (conn->sockfd <= 0) {494
free(conn);495
}496
else {497
pthread_t thread;498
pthread_attr_t thread_attr;499
pthread_attr_init(&thread_attr);500
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);501
pthread_create(&thread, &thread_attr, http_handle_client, (void *) conn);502
pthread_attr_destroy(&thread_attr);503
}504
}505
}506
}507
}508
pthread_exit(0);509
}516
void http_tls_enable(char *certificate, char *private_key) {517
const SSL_METHOD *method = TLS_server_method();518
http_ctx = SSL_CTX_new(method);519
if (!http_ctx) {520
perror("cannot create SSL context");521
errno = HTTP_ERR_NO_SSL_CONTEXT;522
http_error = true;523
return;524
}525
if (SSL_CTX_use_certificate_file(http_ctx, certificate, SSL_FILETYPE_PEM) <= 0) {526
perror("cannot use certificate file");527
errno = HTTP_ERR_INVALID_CERTIFICATE;528
http_error = true;529
}530
if (SSL_CTX_use_PrivateKey_file(http_ctx, private_key, SSL_FILETYPE_PEM) <= 0) {531
perror("cannot use private key file");532
errno = HTTP_ERR_INVALID_PRIVATE_KEY;533
http_error = true;511
int http_tls_sni_callback(SSL *ssl, int *al, void *arg) {512
(void)(al);513
http_conn_t *conn = (http_conn_t *) arg;514
const char *hostname = SSL_get_servername(ssl, 0);515
if (hostname != NULL) {516
for (int i = 0; i < http_binds_len; i++) {517
for (int j = 0; j < http_binds[i].hosts_len; j++) {518
http_host_t *http_host = &http_binds[i].hosts[j];519
if (strcmp(http_host->hostname, hostname) == 0) {520
conn->curr_host = http_host;521
SSL_set_SSL_CTX(ssl, http_host->ssl_ctx);522
return SSL_TLSEXT_ERR_OK;523
}524
}525
}526
}527
return SSL_TLSEXT_ERR_NOACK;528
}537
void http_listen(char *hostname, int port) {538
signal(SIGPIPE, SIG_IGN);539
if (http_error) {540
return;541
}542
errno = 0;543
http_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);544
if (http_sockfd <= 0) {530
void http_bind(char *hostname, int port, http_host_t *hosts, int hosts_len) {531
http_binds = realloc(http_binds, sizeof(http_bind_t) * (http_binds_len + 1));532
http_bind_t *http_bind = &http_binds[http_binds_len];533
http_bind->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);534
if (http_bind->sockfd <= 0) {535
perror("cannot create socket");536
errno = HTTP_ERR_NO_SOCKET;537
http_error = true;
549
memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);550
addr.sin_port = htons(port);551
int enable = 1;562
if (setsockopt(http_sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {552
if (setsockopt(http_bind->sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {553
perror("cannot reuse address");554
errno = HTTP_ERR_NO_ADDR_REUSE;555
http_error = true;556
return;557
}568
if (bind(http_sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) {558
if (bind(http_bind->sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) {559
perror("cannot bind socket to port");560
errno = HTTP_ERR_ADDR_IN_USE;561
http_error = true;562
return;563
}574
if (listen(http_sockfd, 5) < 0) {564
if (listen(http_bind->sockfd, 5) < 0) {565
perror("cannot listen on port");566
errno = HTTP_ERR_NO_LISTEN;567
http_error = true;568
return;569
}580
pthread_create(&http_thread, 0, http_handle_server, (void *) &http_sockfd);570
for (int i = 0; i < hosts_len; i++) {571
http_host_t *http_host = &hosts[i];572
if (http_host->certificate != NULL && http_host->private_key != NULL) {573
const SSL_METHOD *method = TLS_server_method();574
http_host->ssl_ctx = SSL_CTX_new(method);575
if (!http_host->ssl_ctx) {576
perror("cannot create SSL context");577
errno = HTTP_ERR_NO_SSL_CONTEXT;578
http_error = true;579
return;580
}581
if (SSL_CTX_use_certificate_file(http_host->ssl_ctx, http_host->certificate, SSL_FILETYPE_PEM) <= 0) {582
perror("cannot use certificate file");583
errno = HTTP_ERR_INVALID_CERTIFICATE;584
http_error = true;585
}586
if (SSL_CTX_use_PrivateKey_file(http_host->ssl_ctx, http_host->private_key, SSL_FILETYPE_PEM) <= 0) {587
perror("cannot use private key file");588
errno = HTTP_ERR_INVALID_PRIVATE_KEY;589
http_error = true;590
}591
}592
else {593
http_host->ssl_ctx = NULL;594
}595
}596
http_bind->hosts = hosts;597
http_bind->hosts_len = hosts_len;598
http_binds_len++;599
}583
void http_close() {601
void http_listen(void) {602
signal(SIGPIPE, SIG_IGN);603
if (http_error) {604
return;605
}606
errno = 0;607
const SSL_METHOD *method = TLS_server_method();608
http_ssl_ctx = SSL_CTX_new(method);609
if (!http_ssl_ctx) {610
perror("cannot create SSL context");611
errno = HTTP_ERR_NO_SSL_CONTEXT;612
http_error = true;613
return;614
}615
SSL_CTX_set_tlsext_servername_callback(http_ssl_ctx, http_tls_sni_callback);616
pthread_create(&http_thread, 0, http_handle_server, NULL);617
}619
void http_close(void) {620
http_quit = true;621
if (!http_error) {622
pthread_join(http_thread, NULL);623
}588
close(http_sockfd);589
if (http_ctx != NULL) {590
SSL_CTX_free(http_ctx);624
for (int i = 0; i < http_binds_len; i++) {625
close(http_binds[i].sockfd);626
for (int j = 0; j < http_binds[i].hosts_len; j++) {627
if (http_binds[i].hosts[j].ssl_ctx != NULL) {628
SSL_CTX_free(http_binds[i].hosts[j].ssl_ctx);629
}630
}631
}632
if (http_ssl_ctx != NULL) {633
free(http_ssl_ctx);634
}635
}
79
} http_status_t;81
typedef struct {82
char *hostname;83
char *certificate;84
char *private_key;85
SSL_CTX *ssl_ctx;86
} http_host_t;88
typedef struct {89
int sockfd;90
http_host_t *hosts;91
int hosts_len;92
} http_bind_t;94
typedef struct {95
int sockfd;96
struct sockaddr addr;97
int addr_len;98
SSL *ssl;86
} conn_t;99
http_bind_t *bind;100
http_host_t *curr_host;101
} http_conn_t;103
typedef struct {104
char *key;
106
} http_header_t;108
typedef struct {94
conn_t *conn;109
http_conn_t *conn;110
char *method;111
char *url;112
char *version;
135
void http_response_body(http_response_t *response, char *body, long len);136
void http_response_file(http_response_t *response, char *filename);123
void http_tls_enable(char *certificate, char *private_key);124
void http_listen(char *hostname, int port);125
void http_close();138
void http_bind(char *hostname, int port, http_host_t *hosts, int hosts_len);139
void http_listen(void);140
void http_close(void);