A low-key HTTP micro-framework for people who hate web bloat
git clone git://soophie.de/tpee
6
#include <sys/socket.h>8
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,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,20
} http_error_t;22
typedef enum {
166
#include <pthread.h>167
#include <netdb.h>168
#include <poll.h>169
#include <errno.h>170
#include <signal.h>168
#include <fcntl.h>171
#include <openssl/ssl.h>172
#include <sys/socket.h>
452
}453
}453
http_request_t http_read_request(http_conn_t *conn) {455
http_error_t http_read_request(http_conn_t *conn, http_request_t *request) {456
int len = 0;457
int ln = 0;458
int start = 0;459
char buffer[HTTP_MAX_REQUEST_HEAD_SIZE];458
http_request_t request = {460
*request = (http_request_t) {461
.conn = conn,462
.method = NULL,463
.url = NULL,
474
if (read == 0) {475
break;476
}477
// read timeout478
if (read == -1 || read == SSL_ERROR_SYSCALL) {479
if (errno == EWOULDBLOCK || errno == EAGAIN) {480
return HTTP_ERR_READ_TIMEOUT;481
}482
}483
// break if too long484
if (len == HTTP_MAX_REQUEST_HEAD_SIZE) {477
perror("request is too long");478
break;485
return HTTP_ERR_REQUEST_TOO_LONG;486
}487
buffer[len] = c;488
len++;
505
// read method506
if (arg == 0) {507
int method_length = i - start;501
request.method = (char *) malloc(sizeof(char) * (method_length + 1));502
memcpy(request.method, line + start, method_length);503
request.method[method_length] = '\0';508
request->method = (char *) malloc(sizeof(char) * (method_length + 1));509
memcpy(request->method, line + start, method_length);510
request->method[method_length] = '\0';511
}512
// read url513
if (arg == 1) {514
int url_length = i - start;508
request.url = (char *) malloc(sizeof(char) * (url_length + 1));509
memcpy(request.url, line + start, url_length);510
request.url[url_length] = '\0';515
request->url = (char *) malloc(sizeof(char) * (url_length + 1));516
memcpy(request->url, line + start, url_length);517
request->url[url_length] = '\0';518
}519
// read version520
if (arg == 2) {521
int version_length = i - start;515
request.version = (char *) malloc(sizeof(char) * (version_length + 1));516
memcpy(request.version, line + start, version_length);517
request.version[version_length] = '\0';522
request->version = (char *) malloc(sizeof(char) * (version_length + 1));523
memcpy(request->version, line + start, version_length);524
request->version[version_length] = '\0';525
}526
start = i + 1;527
arg++;
531
// read headers532
else {533
int sep = 0;527
request.headers = (http_header_t *) realloc(request.headers, sizeof(http_header_t) * (request.headers_len + 1));528
http_header_t *header = &request.headers[request.headers_len];529
request.headers_len++;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++;537
for (int i = 0; i < line_len; i++) {538
if (line[i] == ':') {539
int key_len = i;
558
if (len >= 4) {559
char *slice = buffer + (len - 4);560
if (strncmp(slice, "\r\n\r\n", 4) == 0) {554
char *content_length = http_header_get(&request, (char *) "Content-Length");561
char *content_length = http_header_get(request, (char *) "Content-Length");562
if (content_length != NULL) {556
request.body_len = strtol(content_length, NULL, 10);557
if (request.body_len > 0) {558
request.body = (char *) malloc(sizeof(char) * (request.body_len + 1));559
http_read(conn, request.body, request.body_len);560
request.body[request.body_len] = '\0';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';568
}569
}570
break;571
}572
}573
}567
return request;574
return HTTP_ERR_OK;575
}577
void http_write_response(http_conn_t *conn, http_response_t response) {
604
pthread_exit(0);605
}606
http_conn_t *conn = (http_conn_t *) ptr;607
struct timeval timeout;608
timeout.tv_sec = 5;609
timeout.tv_usec = 0;610
if (setsockopt(conn->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout)) < 0) {611
close(conn->sockfd);612
free(conn);613
pthread_exit(0);614
}615
if (conn->curr_bind->hosts_len > 0) {616
SSL_CTX_set_tlsext_servername_arg(conn->http->ssl_ctx, (void *) conn);617
conn->ssl = SSL_new(conn->http->ssl_ctx);618
SSL_set_fd(conn->ssl, conn->sockfd);619
if (SSL_accept(conn->ssl) <= 0) {620
close(conn->sockfd);621
free(conn);622
pthread_exit(0);623
}624
}608
int flags = fcntl(conn->sockfd, F_GETFL, 0);609
if (flags == -1) {610
close(conn->sockfd);611
free(conn);612
pthread_exit(0);613
}614
flags |= O_NONBLOCK;615
if (fcntl(conn->sockfd, F_SETFL, flags) == -1) {625
http_request_t request;626
http_error_t err = http_read_request(conn, &request);627
if (err != HTTP_ERR_OK) {628
if (conn->ssl != NULL) {629
SSL_shutdown(conn->ssl);630
SSL_free(conn->ssl);631
}632
close(conn->sockfd);633
free(conn);634
pthread_exit(0);635
}620
http_request_t request = http_read_request(conn);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);