A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
1
# libhttp1
# so/http3
A basic HTTP Framework
14
#define LIB_HTTP_IMPL15
#include <so/http.h>17
#define HOST "0.0.0.0"18
#define PORT 8020
HTTP_GET("/", index_page) {21
(void) req;22
http_resp_t resp = http_resp_create(HTTP_STATUS_OK);23
http_resp_header(&resp, "Content-Type", "text/html");21
http_resp_body(&resp, "<h1>Hello, World!</h1>", 22);24
char *html = "<h1>Hello, World!</h1>";25
http_resp_body(&resp, html, strlen(html));26
return resp;27
}29
int main(void) {30
http_t *http = http_init();27
if (http_bind(http, "0.0.0.0", 80, NULL, 0) != HTTP_ERR_OK) {28
printf("failed to bind port :80\n");29
http_cleanup(http);30
return 1;31
}32
if (http_listen(http) == HTTP_ERR_OK) {33
printf("server is running on port :80\n");31
http_err_t err = http_bind_listen(http, HOST, PORT);32
if (err == HTTP_ERR_OK) {33
printf("server is running on %s:%d\n", HOST, PORT);34
pause();35
http_close(http);36
}37
else {38
printf("failed to start server on %s:%d\n", HOST, PORT);39
}36
http_close(http);40
printf("server stopped\n");41
http_cleanup(http);42
return 0;
5
#include <openssl/ssl.h>6
#include <sys/socket.h>8
#ifdef LIB_HTTP_IMPL9
#define LIB_SOCK_IMPL10
#endif11
#include <so/sock.h>13
typedef enum {9
HTTP_ERR_OK = 0,10
HTTP_ERR_NO_SOCKET = 1,11
HTTP_ERR_UNKNOWN_HOST = 2,12
HTTP_ERR_NO_ADDR_REUSE = 3,13
HTTP_ERR_ADDR_IN_USE = 4,14
HTTP_ERR_NO_LISTEN = 5,15
HTTP_ERR_NO_SSL_CONTEXT = 6,16
HTTP_ERR_INVALID_CERTIFICATE = 7,17
HTTP_ERR_INVALID_PRIVATE_KEY = 8,18
HTTP_ERR_READ_TIMEOUT = 9,19
HTTP_ERR_REQUEST_TOO_LONG = 10,14
HTTP_ERR_OK = SOCK_ERR_OK,15
HTTP_ERR_X = SOCK_ERR__COUNT + 1,16
HTTP_ERR_Y = SOCK_ERR__COUNT + 2,17
HTTP_ERR__COUNT18
} http_err_t;20
typedef enum {
84
typedef struct http http_t;88
typedef struct {89
char *hostname;90
char *certificate;91
char *private_key;92
SSL_CTX *ssl_ctx;93
} http_host_t;95
typedef struct {96
int sockfd;97
http_host_t *hosts;98
int hosts_len;99
int port;100
} http_bind_t;102
typedef struct {103
int sockfd;104
struct sockaddr addr;105
int addr_len;106
SSL *ssl;107
http_bind_t *curr_bind;108
http_host_t *curr_host;109
http_t *http;110
} http_conn_t;86
typedef sock_host_t http_host_t;87
typedef sock_bind_t http_bind_t;88
typedef sock_conn_t http_conn_t;90
typedef struct {91
char *key;
134
} http_pairs_t;136
struct http {159
http_bind_t *binds;160
size_t binds_len;161
pthread_t thread;162
bool quit;163
SSL_CTX *ssl_ctx;137
sock_t *sock;138
http_global_fn_t fn;139
};
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);169
http_err_t _http_read_req(http_conn_t *conn, http_req_t *req);171
#ifdef LIB_HTTP_IMPL173
#include <stdio.h>
187
#define HTTP_VERSION "HTTP/1.1"188
#define HTTP_MAX_REQUEST_HEAD_SIZE 1000000212
#define HTTP_SERVER_BACKLOG 5213
#define HTTP_SERVER_SLEEP_TIME 100000000214
#define HTTP_SERVER_POLL_TIME 100190
#define _HTTP_ON(method, path, fn_name) \191
static http_resp_t fn_name(http_req_t *req); \
277
return resp;278
}280
sock_err_t _http_on_handle(sock_t *sock, http_conn_t *conn) {281
http_t *http = (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);290
http_resp_write(conn, &resp);291
http_resp_free(&resp);292
}293
}294
http_req_free(&req);295
return HTTP_ERR_OK;296
}298
http_t *http_init(void) {299
http_t *http = (http_t *) malloc(sizeof(http_t));300
*http = (http_t) {309
.binds = NULL,310
.binds_len = 0,311
.quit = false,312
.ssl_ctx = NULL,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;307
}309
void http_cleanup(http_t *http) {319
if (http->binds != NULL) {320
for (size_t i = 0; i < http->binds_len; i++) {321
for (size_t j = 0; j < http->binds[i].hosts_len; j++) {322
if (http->binds[i].hosts[j].ssl_ctx != NULL) {323
SSL_CTX_free(http->binds[i].hosts[j].ssl_ctx);324
}325
}326
}327
free(http->binds);328
}329
if (http->ssl_ctx != NULL) {330
SSL_CTX_free(http->ssl_ctx);331
}332
if (_http_routes != NULL && _http_routes_len > 0) {333
free(_http_routes);310
if (http->sock != NULL) {311
sock_cleanup(http->sock);312
}313
free(http);314
}
618
}620
ssize_t http_read(http_conn_t *conn, char *buffer, size_t len) {643
if (conn->ssl != NULL) {644
return SSL_read(conn->ssl, buffer, len);645
}646
return read(conn->sockfd, buffer, len);621
return sock_read(conn, buffer, len);622
}624
ssize_t http_write(http_conn_t *conn, char *buffer, size_t len) {650
if (conn->ssl != NULL) {651
return SSL_write(conn->ssl, buffer, len);652
}653
return write(conn->sockfd, buffer, len);625
return sock_write(conn, buffer, len);626
}628
char *http_req_header(http_req_t *req, char *key) {
723
}724
}754
http_err_t http_read_req(http_conn_t *conn, http_req_t *req) {726
http_err_t _http_read_req(http_conn_t *conn, http_req_t *req) {727
int len = 0;728
int ln = 0;729
int start = 0;
748
// read timeout749
if (read == -1 || read == SSL_ERROR_SYSCALL) {750
if (errno == EWOULDBLOCK || errno == EAGAIN) {779
return HTTP_ERR_READ_TIMEOUT;751
return SOCK_ERR_READ_TIMEOUT;752
}753
}754
// break if too long755
if (len == HTTP_MAX_REQUEST_HEAD_SIZE) {784
return HTTP_ERR_REQUEST_TOO_LONG;756
return SOCK_ERR_REQUEST_TOO_LONG;757
}758
buffer[len] = c;759
len++;
895
http_resp_write_body(conn, resp);896
}926
void *_http_client_handle(void *ptr) {927
if (!ptr) {928
pthread_exit(0);929
}930
http_conn_t *conn = (http_conn_t *) ptr;931
struct timeval timeout;932
timeout.tv_sec = 5;933
timeout.tv_usec = 0;934
if (setsockopt(conn->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout)) < 0) {935
close(conn->sockfd);936
free(conn);937
pthread_exit(0);938
}939
if (conn->curr_bind->hosts_len > 0) {940
SSL_CTX_set_tlsext_servername_arg(conn->http->ssl_ctx, (void *) conn);941
conn->ssl = SSL_new(conn->http->ssl_ctx);942
SSL_set_fd(conn->ssl, conn->sockfd);943
if (SSL_accept(conn->ssl) <= 0) {944
close(conn->sockfd);945
free(conn);946
pthread_exit(0);947
}948
}949
http_req_t req;950
http_err_t err = http_read_req(conn, &req);951
if (err != HTTP_ERR_OK) {952
if (conn->ssl != NULL) {953
SSL_shutdown(conn->ssl);954
SSL_free(conn->ssl);955
}956
close(conn->sockfd);957
free(conn);958
pthread_exit(0);959
}960
if (req.method != NULL && req.url != NULL) {961
if (conn->http->fn != NULL) {962
http_resp_t resp = conn->http->fn(conn->http, &req);963
http_resp_write(conn, &resp);964
http_resp_free(&resp);965
}966
}967
http_req_free(&req);968
if (conn->ssl != NULL) {969
SSL_shutdown(conn->ssl);970
SSL_free(conn->ssl);971
}972
close(conn->sockfd);973
free(conn);974
pthread_exit(0);975
}977
void *_http_server_handle(void *ptr) {978
http_t *http = (http_t *) ptr;979
struct pollfd fds[http->binds_len];980
for (size_t i = 0; i < http->binds_len; i++) {981
fds[i].fd = http->binds[i].sockfd;982
fds[i].events = POLLIN | POLLPRI;983
}984
while (!http->quit) {985
struct timespec sleep_req, sleep_rem;986
sleep_req.tv_sec = 0;987
sleep_req.tv_nsec = HTTP_SERVER_SLEEP_TIME;988
nanosleep(&sleep_req, &sleep_rem);989
if (poll(fds, http->binds_len, HTTP_SERVER_POLL_TIME)) {990
for (size_t i = 0; i < http->binds_len; i++) {991
if (fds[i].revents & POLLIN) {992
http_conn_t *conn = (http_conn_t *) malloc(sizeof(http_conn_t));993
conn->addr_len = sizeof(conn->addr);994
conn->sockfd = accept(http->binds[i].sockfd, &conn->addr, (socklen_t *) &conn->addr_len);995
conn->curr_bind = &http->binds[i];996
conn->ssl = NULL;997
conn->http = http;998
if (conn->sockfd <= 0) {999
free(conn);1000
}1001
else {1002
pthread_t thread;1003
pthread_attr_t thread_attr;1004
pthread_attr_init(&thread_attr);1005
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);1006
pthread_create(&thread, &thread_attr, _http_client_handle, (void *) conn);1007
pthread_attr_destroy(&thread_attr);1008
}1009
}1010
}1011
}1012
}1013
pthread_exit(0);1014
}1016
int _http_tls_sni_callback(SSL *ssl, int *al, void *arg) {1017
(void) al;1018
http_conn_t *conn = (http_conn_t *) arg;1019
const char *hostname = SSL_get_servername(ssl, 0);1020
if (hostname != NULL) {1021
for (size_t i = 0; i < conn->http->binds_len; i++) {1022
for (size_t j = 0; j < conn->http->binds[i].hosts_len; j++) {1023
http_host_t *http_host = &conn->http->binds[i].hosts[j];1024
if (strcmp(http_host->hostname, hostname) == 0) {1025
conn->curr_host = http_host;1026
SSL_set_SSL_CTX(ssl, http_host->ssl_ctx);1027
return SSL_TLSEXT_ERR_OK;1028
}1029
}1030
}1031
}1032
return SSL_TLSEXT_ERR_NOACK;1033
}898
http_err_t http_bind(http_t *http, char *hostname, int port, http_host_t *hosts, int hosts_len) {1036
http->binds = (http_bind_t *) realloc(http->binds, sizeof(http_bind_t) * (http->binds_len + 1));1037
http_bind_t *http_bind = &http->binds[http->binds_len];1038
http_bind->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);1039
http_bind->port = port;1040
if (http_bind->sockfd <= 0) {1041
return HTTP_ERR_NO_SOCKET;1042
}1043
struct hostent *host = gethostbyname(hostname);1044
if (!host) {1045
return HTTP_ERR_UNKNOWN_HOST;1046
}1047
struct sockaddr_in addr;1048
addr.sin_family = AF_INET;1049
memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);1050
addr.sin_port = htons(port);1051
int enable = 1;1052
if (setsockopt(http_bind->sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {1053
return HTTP_ERR_NO_ADDR_REUSE;1054
}1055
if (bind(http_bind->sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) {1056
return HTTP_ERR_ADDR_IN_USE;1057
}1058
if (listen(http_bind->sockfd, HTTP_SERVER_BACKLOG) < 0) {1059
return HTTP_ERR_NO_LISTEN;1060
}1061
for (int i = 0; i < hosts_len; i++) {1062
http_host_t *http_host = &hosts[i];1063
http_host->ssl_ctx = NULL;1064
if (http_host->certificate != NULL && http_host->private_key != NULL) {1065
const SSL_METHOD *method = TLS_server_method();1066
http_host->ssl_ctx = SSL_CTX_new(method);1067
if (!http_host->ssl_ctx) {1068
return HTTP_ERR_NO_SSL_CONTEXT;1069
}1070
if (SSL_CTX_use_certificate_chain_file(http_host->ssl_ctx, http_host->certificate) <= 0) {1071
return HTTP_ERR_INVALID_CERTIFICATE;1072
}1073
if (SSL_CTX_use_PrivateKey_file(http_host->ssl_ctx, http_host->private_key, SSL_FILETYPE_PEM) <= 0) {1074
return HTTP_ERR_INVALID_PRIVATE_KEY;1075
}1076
}1077
}1078
http_bind->hosts = hosts;1079
http_bind->hosts_len = hosts_len;1080
http->binds_len++;1081
return HTTP_ERR_OK;899
return sock_bind(http->sock, hostname, port, hosts, hosts_len);900
}902
http_err_t http_listen(http_t *http) {1085
signal(SIGPIPE, SIG_IGN);1086
const SSL_METHOD *method = TLS_server_method();1087
http->ssl_ctx = SSL_CTX_new(method);1088
if (!http->ssl_ctx) {1089
return HTTP_ERR_NO_SSL_CONTEXT;1090
}1091
SSL_CTX_set_tlsext_servername_callback(http->ssl_ctx, _http_tls_sni_callback);1092
pthread_create(&http->thread, 0, _http_server_handle, (void *) http);1093
return HTTP_ERR_OK;903
return sock_listen(http->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);908
}910
void http_close(http_t *http) {1097
http->quit = true;1098
pthread_join(http->thread, NULL);1099
for (size_t i = 0; i < http->binds_len; i++) {1100
close(http->binds[i].sockfd);1101
}911
sock_close(http->sock);912
}914
#endif /* LIB_HTTP_IMPL */