Splitting in request_handler and responder
This commit is contained in:
6
Makefile
6
Makefile
@ -46,7 +46,7 @@ bin/worker/%.o: src/worker/%.c
|
||||
$(CC) -c -o $@ $(CFLAGS) $<
|
||||
|
||||
bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o \
|
||||
bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o \
|
||||
bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o bin/worker/responder.o \
|
||||
bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
||||
bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \
|
||||
bin/lib/utils.o bin/lib/websocket.o bin/lib/mpmc.o
|
||||
@ -68,6 +68,10 @@ bin/worker/tcp_acceptor.o: src/worker/tcp_acceptor.h
|
||||
|
||||
bin/worker/tcp_closer.o: src/worker/tcp_closer.h
|
||||
|
||||
bin/worker/fastcgi_handler.o: src/worker/fastcgi_handler.h
|
||||
|
||||
bin/worker/responder.o: src/worker/responder.h
|
||||
|
||||
bin/lib/compress.o: src/lib/compress.h
|
||||
|
||||
bin/lib/config.o: src/lib/config.h src/lib/utils.h src/lib/uri.h src/logger.h
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "lib/geoip.h"
|
||||
#include "worker/tcp_closer.h"
|
||||
#include "worker/request_handler.h"
|
||||
#include "worker/responder.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
@ -113,11 +114,12 @@ static void terminate_gracefully(int sig) {
|
||||
tcp_acceptor_stop();
|
||||
request_handler_stop();
|
||||
tcp_closer_stop();
|
||||
responder_stop();
|
||||
|
||||
tcp_acceptor_destroy();
|
||||
request_handler_destroy();
|
||||
tcp_closer_destroy();
|
||||
|
||||
responder_destroy();
|
||||
|
||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||
close(sockets[i]);
|
||||
@ -265,6 +267,7 @@ int main(int argc, char *const argv[]) {
|
||||
tcp_acceptor_init(CNX_HANDLER_WORKERS, 64);
|
||||
tcp_closer_init(CNX_HANDLER_WORKERS, 64);
|
||||
request_handler_init(REQ_HANDLER_WORKERS, 64);
|
||||
responder_init(REQ_HANDLER_WORKERS, 64);
|
||||
|
||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||
async(sockets[i], POLLIN, ASYNC_KEEP, accept_cb, &sockets[i], accept_err_cb, &sockets[i]);
|
||||
|
11
src/server.h
11
src/server.h
@ -10,6 +10,9 @@
|
||||
#define SESIMOS_SERVER_H
|
||||
|
||||
#include "lib/sock.h"
|
||||
#include "lib/http.h"
|
||||
#include "lib/uri.h"
|
||||
#include "lib/config.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <maxminddb.h>
|
||||
@ -37,6 +40,14 @@ typedef struct {
|
||||
char log_prefix[512];
|
||||
char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1];
|
||||
struct timespec begin, end;
|
||||
http_req req;
|
||||
http_res res;
|
||||
http_uri uri;
|
||||
http_status_ctx status;
|
||||
int use_fastcgi, use_proxy;
|
||||
host_config_t *conf;
|
||||
FILE *file;
|
||||
long content_length;
|
||||
} client_ctx_t;
|
||||
|
||||
extern volatile sig_atomic_t server_alive;
|
||||
|
@ -6,12 +6,12 @@
|
||||
* @date 2022-12-28
|
||||
*/
|
||||
|
||||
#include "../defs.h"
|
||||
#include "request_handler.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "tcp_closer.h"
|
||||
#include "../async.h"
|
||||
|
||||
#include "../defs.h"
|
||||
#include "../server.h"
|
||||
#include "../logger.h"
|
||||
|
||||
@ -24,6 +24,7 @@
|
||||
#include "../cache_handler.h"
|
||||
#include "../lib/compress.h"
|
||||
#include "../lib/websocket.h"
|
||||
#include "responder.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
@ -34,7 +35,7 @@
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void request_handler_func(client_ctx_t *ctx);
|
||||
static void request_handler(client_ctx_t *ctx);
|
||||
static int request_handler(client_ctx_t *ctx);
|
||||
|
||||
int request_handler_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) request_handler_func, "req");
|
||||
@ -53,68 +54,68 @@ void request_handler_destroy(void) {
|
||||
}
|
||||
|
||||
static void request_handler_func(client_ctx_t *ctx) {
|
||||
request_handler(ctx);
|
||||
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
|
||||
|
||||
if (ctx->c_keep_alive && ctx->s_keep_alive && ctx->req_num < REQ_PER_CONNECTION) {
|
||||
async(ctx->socket.socket, POLLIN, 0, (void (*)(void *)) handle_request, ctx, (void (*)(void *)) tcp_close, ctx);
|
||||
logger_set_prefix(ctx->log_prefix);
|
||||
if (request_handler(ctx) == 0) {
|
||||
respond(ctx);
|
||||
} else {
|
||||
tcp_close(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void request_handler(client_ctx_t *cctx) {
|
||||
sock *client = &cctx->socket;
|
||||
struct timespec begin, end;
|
||||
static int request_handler(client_ctx_t *ctx) {
|
||||
sock *client = &ctx->socket;
|
||||
long ret;
|
||||
|
||||
char buf0[1024], buf1[1024];
|
||||
char msg_buf[8192], msg_pre_buf_1[4096], msg_pre_buf_2[4096], err_msg[256];
|
||||
char msg_buf[8192], err_msg[256];
|
||||
char msg_content[1024];
|
||||
char buffer[CHUNK_SIZE];
|
||||
const char *host_ptr, *hdr_connection;
|
||||
|
||||
msg_buf[0] = 0;
|
||||
err_msg[0] = 0;
|
||||
msg_content[0] = 0;
|
||||
|
||||
host_config_t *conf = NULL;
|
||||
FILE *file = NULL;
|
||||
|
||||
long content_length = 0;
|
||||
int accept_if_modified_since = 0;
|
||||
int use_fastcgi = 0;
|
||||
int use_proxy = 0;
|
||||
ctx->use_fastcgi = 0;
|
||||
ctx->use_proxy = 0;
|
||||
int p_len;
|
||||
|
||||
fastcgi_cnx_t fcgi_cnx = {.socket = 0, .req_id = 0, .ctx = cctx};
|
||||
fastcgi_cnx_t fcgi_cnx = {.socket = 0, .req_id = 0, .ctx = ctx};
|
||||
http_status custom_status;
|
||||
|
||||
http_res res = {.version = "1.1", .status = http_get_status(501), .hdr.field_num = 0, .hdr.last_field_num = -1};
|
||||
http_status_ctx ctx = {.status = 0, .origin = NONE, .ws_key = NULL};
|
||||
http_res *res = &ctx->res;
|
||||
res->status = http_get_status(501);
|
||||
res->hdr.field_num = 0;
|
||||
res->hdr.last_field_num = -1;
|
||||
sprintf(res->version, "1.1");
|
||||
|
||||
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, cctx->s_addr, cctx->log_prefix);
|
||||
clock_gettime(CLOCK_MONOTONIC, &begin);
|
||||
http_status_ctx *status = &ctx->status;
|
||||
status->status = 0;
|
||||
status->origin = NONE;
|
||||
status->ws_key = NULL;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
|
||||
|
||||
//ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
|
||||
|
||||
http_add_header_field(&res.hdr, "Date", http_get_date(buf0, sizeof(buf0)));
|
||||
http_add_header_field(&res.hdr, "Server", SERVER_STR);
|
||||
http_add_header_field(&res->hdr, "Date", http_get_date(buf0, sizeof(buf0)));
|
||||
http_add_header_field(&res->hdr, "Server", SERVER_STR);
|
||||
/*if (ret <= 0) {
|
||||
if (errno != 0) return;
|
||||
if (errno != 0) return 0;
|
||||
|
||||
cctx->c_keep_alive = 0;
|
||||
res.status = http_get_status(408);
|
||||
goto respond;
|
||||
ctx->c_keep_alive = 0;
|
||||
res->status = http_get_status(408);
|
||||
return 0;
|
||||
}*/
|
||||
//clock_gettime(CLOCK_MONOTONIC, &begin);
|
||||
|
||||
http_req req;
|
||||
ret = http_receive_request(client, &req);
|
||||
http_req *req = &ctx->req;
|
||||
ret = http_receive_request(client, req);
|
||||
if (ret != 0) {
|
||||
cctx->c_keep_alive = 0;
|
||||
ctx->c_keep_alive = 0;
|
||||
if (ret < 0) {
|
||||
goto abort;
|
||||
return -1;
|
||||
} else if (ret == 1) {
|
||||
sprintf(err_msg, "Unable to parse http header: Invalid header format.");
|
||||
} else if (ret == 2) {
|
||||
@ -126,222 +127,222 @@ static void request_handler(client_ctx_t *cctx) {
|
||||
} else if (ret == 5) {
|
||||
sprintf(err_msg, "Unable to parse http header: End of header not found.");
|
||||
}
|
||||
res.status = http_get_status(400);
|
||||
goto respond;
|
||||
res->status = http_get_status(400);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hdr_connection = http_get_header_field(&req.hdr, "Connection");
|
||||
cctx->c_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
|
||||
host_ptr = http_get_header_field(&req.hdr, "Host");
|
||||
hdr_connection = http_get_header_field(&req->hdr, "Connection");
|
||||
ctx->c_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
|
||||
host_ptr = http_get_header_field(&req->hdr, "Host");
|
||||
if (host_ptr != NULL && strlen(host_ptr) > 255) {
|
||||
cctx->req_host[0] = 0;
|
||||
res.status = http_get_status(400);
|
||||
ctx->req_host[0] = 0;
|
||||
res->status = http_get_status(400);
|
||||
sprintf(err_msg, "Host header field is too long.");
|
||||
goto respond;
|
||||
return 0;
|
||||
} else if (host_ptr == NULL || strchr(host_ptr, '/') != NULL) {
|
||||
if (strchr(cctx->addr, ':') == NULL) {
|
||||
strcpy(cctx->req_host, cctx->addr);
|
||||
if (strchr(ctx->addr, ':') == NULL) {
|
||||
strcpy(ctx->req_host, ctx->addr);
|
||||
} else {
|
||||
sprintf(cctx->req_host, "[%s]", cctx->addr);
|
||||
sprintf(ctx->req_host, "[%s]", ctx->addr);
|
||||
}
|
||||
res.status = http_get_status(400);
|
||||
res->status = http_get_status(400);
|
||||
sprintf(err_msg, "The client provided no or an invalid Host header field.");
|
||||
goto respond;
|
||||
return 0;
|
||||
} else {
|
||||
strcpy(cctx->req_host, host_ptr);
|
||||
strcpy(ctx->req_host, host_ptr);
|
||||
}
|
||||
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, cctx->req_host, CLR_STR, cctx->log_prefix);
|
||||
info(BLD_STR "%s %s", req.method, req.uri);
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
info(BLD_STR "%s %s", req->method, req->uri);
|
||||
|
||||
conf = get_host_config(cctx->req_host);
|
||||
if (conf == NULL) {
|
||||
ctx->conf = get_host_config(ctx->req_host);
|
||||
if (ctx->conf == NULL) {
|
||||
info("Unknown host, redirecting to default");
|
||||
res.status = http_get_status(307);
|
||||
sprintf(buf0, "https://%s%s", DEFAULT_HOST, req.uri);
|
||||
http_add_header_field(&res.hdr, "Location", buf0);
|
||||
goto respond;
|
||||
res->status = http_get_status(307);
|
||||
sprintf(buf0, "https://%s%s", DEFAULT_HOST, req->uri);
|
||||
http_add_header_field(&res->hdr, "Location", buf0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
http_uri uri;
|
||||
unsigned char dir_mode = (conf->type == CONFIG_TYPE_LOCAL ? conf->local.dir_mode : URI_DIR_MODE_NO_VALIDATION);
|
||||
ret = uri_init(&uri, conf->local.webroot, req.uri, dir_mode);
|
||||
http_uri *uri = &ctx->uri;
|
||||
unsigned char dir_mode = (ctx->conf->type == CONFIG_TYPE_LOCAL ? ctx->conf->local.dir_mode : URI_DIR_MODE_NO_VALIDATION);
|
||||
ret = uri_init(uri, ctx->conf->local.webroot, req->uri, dir_mode);
|
||||
if (ret != 0) {
|
||||
if (ret == 1) {
|
||||
sprintf(err_msg, "Invalid URI: has to start with slash.");
|
||||
res.status = http_get_status(400);
|
||||
res->status = http_get_status(400);
|
||||
} else if (ret == 2) {
|
||||
sprintf(err_msg, "Invalid URI: contains relative path change (/../).");
|
||||
res.status = http_get_status(400);
|
||||
res->status = http_get_status(400);
|
||||
} else if (ret == 3) {
|
||||
sprintf(err_msg, "The specified webroot directory does not exist.");
|
||||
res.status = http_get_status(404);
|
||||
res->status = http_get_status(404);
|
||||
} else {
|
||||
res.status = http_get_status(500);
|
||||
res->status = http_get_status(500);
|
||||
}
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dir_mode != URI_DIR_MODE_NO_VALIDATION) {
|
||||
ssize_t size = sizeof(buf0);
|
||||
url_decode(req.uri, buf0, &size);
|
||||
int change_proto = strncmp(uri.uri, "/.well-known/", 13) != 0 && !client->enc;
|
||||
if (strcmp(uri.uri, buf0) != 0 || change_proto) {
|
||||
res.status = http_get_status(308);
|
||||
size = url_encode(uri.uri, strlen(uri.uri), buf0, sizeof(buf0));
|
||||
url_decode(req->uri, buf0, &size);
|
||||
int change_proto = strncmp(uri->uri, "/.well-known/", 13) != 0 && !client->enc;
|
||||
if (strcmp(uri->uri, buf0) != 0 || change_proto) {
|
||||
res->status = http_get_status(308);
|
||||
size = url_encode(uri->uri, strlen(uri->uri), buf0, sizeof(buf0));
|
||||
if (change_proto) {
|
||||
p_len = snprintf(buf1, sizeof(buf1), "https://%s%s", cctx->req_host, buf0);
|
||||
p_len = snprintf(buf1, sizeof(buf1), "https://%s%s", ctx->req_host, buf0);
|
||||
if (p_len < 0 || p_len >= sizeof(buf1)) {
|
||||
res.status = http_get_status(500);
|
||||
res->status = http_get_status(500);
|
||||
error("Header field 'Location' too long");
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
http_add_header_field(&res.hdr, "Location", buf1);
|
||||
http_add_header_field(&res->hdr, "Location", buf1);
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "Location", buf0);
|
||||
http_add_header_field(&res->hdr, "Location", buf0);
|
||||
}
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
} else if (!client->enc) {
|
||||
res.status = http_get_status(308);
|
||||
sprintf(buf0, "https://%s%s", cctx->req_host, req.uri);
|
||||
http_add_header_field(&res.hdr, "Location", buf0);
|
||||
goto respond;
|
||||
res->status = http_get_status(308);
|
||||
sprintf(buf0, "https://%s%s", ctx->req_host, req->uri);
|
||||
http_add_header_field(&res->hdr, "Location", buf0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (conf->type == CONFIG_TYPE_LOCAL) {
|
||||
if (strcmp(req.method, "TRACE") == 0) {
|
||||
res.status = http_get_status(200);
|
||||
http_add_header_field(&res.hdr, "Content-Type", "message/http");
|
||||
if (ctx->conf->type == CONFIG_TYPE_LOCAL) {
|
||||
if (strcmp(req->method, "TRACE") == 0) {
|
||||
res->status = http_get_status(200);
|
||||
http_add_header_field(&res->hdr, "Content-Type", "message/http");
|
||||
|
||||
content_length = snprintf(msg_buf, sizeof(msg_buf) - content_length, "%s %s HTTP/%s\r\n", req.method, req.uri, req.version);
|
||||
for (int i = 0; i < req.hdr.field_num; i++) {
|
||||
const http_field *f = &req.hdr.fields[i];
|
||||
content_length += snprintf(msg_buf + content_length, sizeof(msg_buf) - content_length, "%s: %s\r\n", http_field_get_name(f), http_field_get_value(f));
|
||||
ctx->content_length = snprintf(msg_buf, sizeof(msg_buf) - ctx->content_length, "%s %s HTTP/%s\r\n", req->method, req->uri, req->version);
|
||||
for (int i = 0; i < req->hdr.field_num; i++) {
|
||||
const http_field *f = &req->hdr.fields[i];
|
||||
ctx->content_length += snprintf(msg_buf + ctx->content_length, sizeof(msg_buf) - ctx->content_length, "%s: %s\r\n", http_field_get_name(f), http_field_get_value(f));
|
||||
}
|
||||
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strncmp(uri.req_path, "/.well-known/", 13) == 0) {
|
||||
http_add_header_field(&res.hdr, "Access-Control-Allow-Origin", "*");
|
||||
if (strncmp(uri->req_path, "/.well-known/", 13) == 0) {
|
||||
http_add_header_field(&res->hdr, "Access-Control-Allow-Origin", "*");
|
||||
}
|
||||
|
||||
if (strncmp(uri.req_path, "/.well-known/", 13) != 0 && strstr(uri.path, "/.") != NULL) {
|
||||
res.status = http_get_status(403);
|
||||
if (strncmp(uri->req_path, "/.well-known/", 13) != 0 && strstr(uri->path, "/.") != NULL) {
|
||||
res->status = http_get_status(403);
|
||||
sprintf(err_msg, "Parts of this URI are hidden.");
|
||||
goto respond;
|
||||
} else if (uri.filename == NULL && (int) uri.is_static && (int) uri.is_dir && strlen(uri.pathinfo) == 0) {
|
||||
res.status = http_get_status(403);
|
||||
return 0;
|
||||
} else if (uri->filename == NULL && (int) uri->is_static && (int) uri->is_dir && strlen(uri->pathinfo) == 0) {
|
||||
res->status = http_get_status(403);
|
||||
sprintf(err_msg, "It is not allowed to list the contents of this directory.");
|
||||
goto respond;
|
||||
} else if (uri.filename == NULL && (int) !uri.is_static && (int) uri.is_dir && strlen(uri.pathinfo) == 0) {
|
||||
return 0;
|
||||
} else if (uri->filename == NULL && (int) !uri->is_static && (int) uri->is_dir && strlen(uri->pathinfo) == 0) {
|
||||
// TODO list directory contents
|
||||
res.status = http_get_status(501);
|
||||
res->status = http_get_status(501);
|
||||
sprintf(err_msg, "Listing contents of an directory is currently not implemented.");
|
||||
goto respond;
|
||||
} else if (uri.filename == NULL || (strlen(uri.pathinfo) > 0 && (int) uri.is_static)) {
|
||||
res.status = http_get_status(404);
|
||||
goto respond;
|
||||
} else if (strlen(uri.pathinfo) != 0 && conf->local.dir_mode != URI_DIR_MODE_INFO) {
|
||||
res.status = http_get_status(404);
|
||||
goto respond;
|
||||
return 0;
|
||||
} else if (uri->filename == NULL || (strlen(uri->pathinfo) > 0 && (int) uri->is_static)) {
|
||||
res->status = http_get_status(404);
|
||||
return 0;
|
||||
} else if (strlen(uri->pathinfo) != 0 && ctx->conf->local.dir_mode != URI_DIR_MODE_INFO) {
|
||||
res->status = http_get_status(404);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uri.is_static) {
|
||||
res.status = http_get_status(200);
|
||||
http_add_header_field(&res.hdr, "Accept-Ranges", "bytes");
|
||||
if (strcmp(req.method, "GET") != 0 && strcmp(req.method, "HEAD") != 0) {
|
||||
res.status = http_get_status(405);
|
||||
goto respond;
|
||||
if (uri->is_static) {
|
||||
res->status = http_get_status(200);
|
||||
http_add_header_field(&res->hdr, "Accept-Ranges", "bytes");
|
||||
if (strcmp(req->method, "GET") != 0 && strcmp(req->method, "HEAD") != 0) {
|
||||
res->status = http_get_status(405);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (http_get_header_field(&req.hdr, "Content-Length") != NULL || http_get_header_field(&req.hdr, "Transfer-Encoding") != NULL) {
|
||||
res.status = http_get_status(400);
|
||||
if (http_get_header_field(&req->hdr, "Content-Length") != NULL || http_get_header_field(&req->hdr, "Transfer-Encoding") != NULL) {
|
||||
res->status = http_get_status(400);
|
||||
sprintf(err_msg, "A GET request must not contain a payload");
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
cache_init_uri(conf->cache, &uri);
|
||||
cache_init_uri(ctx->conf->cache, uri);
|
||||
|
||||
const char *last_modified = http_format_date(uri.meta->stat.st_mtime, buf0, sizeof(buf0));
|
||||
http_add_header_field(&res.hdr, "Last-Modified", last_modified);
|
||||
sprintf(buf1, "%s; charset=%s", uri.meta->type, uri.meta->charset);
|
||||
http_add_header_field(&res.hdr, "Content-Type", buf1);
|
||||
const char *last_modified = http_format_date(uri->meta->stat.st_mtime, buf0, sizeof(buf0));
|
||||
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
|
||||
sprintf(buf1, "%s; charset=%s", uri->meta->type, uri->meta->charset);
|
||||
http_add_header_field(&res->hdr, "Content-Type", buf1);
|
||||
|
||||
|
||||
const char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding");
|
||||
const char *accept_encoding = http_get_header_field(&req->hdr, "Accept-Encoding");
|
||||
int enc = 0;
|
||||
if (accept_encoding != NULL) {
|
||||
if (uri.meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
|
||||
file = fopen(uri.meta->filename_comp_br, "rb");
|
||||
if (file == NULL) {
|
||||
cache_mark_dirty(conf->cache, uri.filename);
|
||||
if (uri->meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
|
||||
ctx->file = fopen(uri->meta->filename_comp_br, "rb");
|
||||
if (ctx->file == NULL) {
|
||||
cache_mark_dirty(ctx->conf->cache, uri->filename);
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "Content-Encoding", "br");
|
||||
http_add_header_field(&res->hdr, "Content-Encoding", "br");
|
||||
enc = COMPRESS_BR;
|
||||
}
|
||||
} else if (uri.meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
|
||||
file = fopen(uri.meta->filename_comp_gz, "rb");
|
||||
if (file == NULL) {
|
||||
cache_mark_dirty(conf->cache, uri.filename);
|
||||
} else if (uri->meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
|
||||
ctx->file = fopen(uri->meta->filename_comp_gz, "rb");
|
||||
if (ctx->file == NULL) {
|
||||
cache_mark_dirty(ctx->conf->cache, uri->filename);
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "Content-Encoding", "gzip");
|
||||
http_add_header_field(&res->hdr, "Content-Encoding", "gzip");
|
||||
enc = COMPRESS_GZ;
|
||||
}
|
||||
}
|
||||
if (enc != 0) {
|
||||
http_add_header_field(&res.hdr, "Vary", "Accept-Encoding");
|
||||
http_add_header_field(&res->hdr, "Vary", "Accept-Encoding");
|
||||
}
|
||||
}
|
||||
|
||||
if (uri.meta->etag[0] != 0) {
|
||||
if (uri->meta->etag[0] != 0) {
|
||||
if (enc) {
|
||||
sprintf(buf0, "%s-%s", uri.meta->etag, (enc & COMPRESS_BR) ? "br" : (enc & COMPRESS_GZ) ? "gzip" : "");
|
||||
http_add_header_field(&res.hdr, "ETag", buf0);
|
||||
sprintf(buf0, "%s-%s", uri->meta->etag, (enc & COMPRESS_BR) ? "br" : (enc & COMPRESS_GZ) ? "gzip" : "");
|
||||
http_add_header_field(&res->hdr, "ETag", buf0);
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "ETag", uri.meta->etag);
|
||||
http_add_header_field(&res->hdr, "ETag", uri->meta->etag);
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp(uri.meta->type, "text/", 5) == 0) {
|
||||
http_add_header_field(&res.hdr, "Cache-Control", "public, max-age=3600");
|
||||
if (strncmp(uri->meta->type, "text/", 5) == 0) {
|
||||
http_add_header_field(&res->hdr, "Cache-Control", "public, max-age=3600");
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "Cache-Control", "public, max-age=86400");
|
||||
http_add_header_field(&res->hdr, "Cache-Control", "public, max-age=86400");
|
||||
}
|
||||
|
||||
const char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since");
|
||||
const char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match");
|
||||
if ((if_none_match != NULL && strstr(if_none_match, uri.meta->etag) == NULL) ||
|
||||
const char *if_modified_since = http_get_header_field(&req->hdr, "If-Modified-Since");
|
||||
const char *if_none_match = http_get_header_field(&req->hdr, "If-None-Match");
|
||||
if ((if_none_match != NULL && strstr(if_none_match, uri->meta->etag) == NULL) ||
|
||||
(accept_if_modified_since && if_modified_since != NULL && strcmp(if_modified_since, last_modified) == 0))
|
||||
{
|
||||
res.status = http_get_status(304);
|
||||
goto respond;
|
||||
res->status = http_get_status(304);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *range = http_get_header_field(&req.hdr, "Range");
|
||||
const char *range = http_get_header_field(&req->hdr, "Range");
|
||||
if (range != NULL) {
|
||||
if (strlen(range) <= 6 || strncmp(range, "bytes=", 6) != 0) {
|
||||
res.status = http_get_status(416);
|
||||
http_remove_header_field(&res.hdr, "Content-Type", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Last-Modified", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "ETag", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Cache-Control", HTTP_REMOVE_ALL);
|
||||
goto respond;
|
||||
res->status = http_get_status(416);
|
||||
http_remove_header_field(&res->hdr, "Content-Type", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Last-Modified", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "ETag", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Cache-Control", HTTP_REMOVE_ALL);
|
||||
return 0;
|
||||
}
|
||||
range += 6;
|
||||
char *ptr = strchr(range, '-');
|
||||
if (ptr == NULL) {
|
||||
res.status = http_get_status(416);
|
||||
goto respond;
|
||||
res->status = http_get_status(416);
|
||||
return 0;
|
||||
}
|
||||
file = fopen(uri.filename, "rb");
|
||||
fseek(file, 0, SEEK_END);
|
||||
unsigned long file_len = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
ctx->file = fopen(uri->filename, "rb");
|
||||
fseek(ctx->file, 0, SEEK_END);
|
||||
unsigned long file_len = ftell(ctx->file);
|
||||
fseek(ctx->file, 0, SEEK_SET);
|
||||
if (file_len == 0) {
|
||||
content_length = 0;
|
||||
goto respond;
|
||||
ctx->content_length = 0;
|
||||
return 0;
|
||||
}
|
||||
long num1 = 0;
|
||||
long num2 = (long) file_len - 1;
|
||||
@ -350,52 +351,52 @@ static void request_handler(client_ctx_t *cctx) {
|
||||
if (ptr[1] != 0) num2 = (long) strtoul(ptr + 1, NULL, 10);
|
||||
|
||||
if (num1 >= file_len || num2 >= file_len || num1 > num2) {
|
||||
res.status = http_get_status(416);
|
||||
goto respond;
|
||||
res->status = http_get_status(416);
|
||||
return 0;
|
||||
}
|
||||
sprintf(buf0, "bytes %li-%li/%li", num1, num2, file_len);
|
||||
http_add_header_field(&res.hdr, "Content-Range", buf0);
|
||||
http_add_header_field(&res->hdr, "Content-Range", buf0);
|
||||
|
||||
res.status = http_get_status(206);
|
||||
fseek(file, num1, SEEK_SET);
|
||||
content_length = num2 - num1 + 1;
|
||||
res->status = http_get_status(206);
|
||||
fseek(ctx->file, num1, SEEK_SET);
|
||||
ctx->content_length = num2 - num1 + 1;
|
||||
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file == NULL) {
|
||||
file = fopen(uri.filename, "rb");
|
||||
if (ctx->file == NULL) {
|
||||
ctx->file = fopen(uri->filename, "rb");
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
content_length = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fseek(ctx->file, 0, SEEK_END);
|
||||
ctx->content_length = ftell(ctx->file);
|
||||
fseek(ctx->file, 0, SEEK_SET);
|
||||
} else {
|
||||
int mode;
|
||||
if (strcmp(uri.filename + strlen(uri.filename) - 4, ".ncr") == 0) {
|
||||
if (strcmp(uri->filename + strlen(uri->filename) - 4, ".ncr") == 0) {
|
||||
mode = FASTCGI_SESIMOS;
|
||||
} else if (strcmp(uri.filename + strlen(uri.filename) - 4, ".php") == 0) {
|
||||
} else if (strcmp(uri->filename + strlen(uri->filename) - 4, ".php") == 0) {
|
||||
mode = FASTCGI_PHP;
|
||||
} else {
|
||||
res.status = http_get_status(500);
|
||||
error("Invalid FastCGI extension: %s", uri.filename);
|
||||
goto respond;
|
||||
res->status = http_get_status(500);
|
||||
error("Invalid FastCGI extension: %s", uri->filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct stat statbuf;
|
||||
stat(uri.filename, &statbuf);
|
||||
stat(uri->filename, &statbuf);
|
||||
char *last_modified = http_format_date(statbuf.st_mtime, buf0, sizeof(buf0));
|
||||
http_add_header_field(&res.hdr, "Last-Modified", last_modified);
|
||||
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
|
||||
|
||||
res.status = http_get_status(200);
|
||||
if (fastcgi_init(&fcgi_cnx, mode, 0 /* TODO */, cctx->req_num, client, &req, &uri) != 0) {
|
||||
res.status = http_get_status(503);
|
||||
res->status = http_get_status(200);
|
||||
if (fastcgi_init(&fcgi_cnx, mode, 0 /* TODO */, ctx->req_num, client, req, uri) != 0) {
|
||||
res->status = http_get_status(503);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *client_content_length = http_get_header_field(&req.hdr, "Content-Length");
|
||||
const char *client_transfer_encoding = http_get_header_field(&req.hdr, "Transfer-Encoding");
|
||||
const char *client_content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||
const char *client_transfer_encoding = http_get_header_field(&req->hdr, "Transfer-Encoding");
|
||||
if (client_content_length != NULL) {
|
||||
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
|
||||
ret = fastcgi_receive(&fcgi_cnx, client, client_content_len);
|
||||
@ -406,125 +407,124 @@ static void request_handler(client_ctx_t *cctx) {
|
||||
}
|
||||
if (ret != 0) {
|
||||
if (ret < 0) {
|
||||
goto abort;
|
||||
return -1;
|
||||
} else {
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
}
|
||||
res.status = http_get_status(502);
|
||||
goto respond;
|
||||
res->status = http_get_status(502);
|
||||
return 0;
|
||||
}
|
||||
fastcgi_close_stdin(&fcgi_cnx);
|
||||
|
||||
ret = fastcgi_header(&fcgi_cnx, &res, err_msg);
|
||||
ret = fastcgi_header(&fcgi_cnx, res, err_msg);
|
||||
if (ret != 0) {
|
||||
if (ret < 0) goto abort;
|
||||
goto respond;
|
||||
return (ret < 0) ? -1 : 0;
|
||||
}
|
||||
|
||||
const char *status = http_get_header_field(&res.hdr, "Status");
|
||||
if (status != NULL) {
|
||||
int status_code = (int) strtoul(status, NULL, 10);
|
||||
res.status = http_get_status(status_code);
|
||||
http_remove_header_field(&res.hdr, "Status", HTTP_REMOVE_ALL);
|
||||
if (res.status == NULL && status_code >= 100 && status_code <= 999) {
|
||||
const char *status_hdr = http_get_header_field(&res->hdr, "Status");
|
||||
if (status_hdr != NULL) {
|
||||
int status_code = (int) strtoul(status_hdr, NULL, 10);
|
||||
res->status = http_get_status(status_code);
|
||||
http_remove_header_field(&res->hdr, "Status", HTTP_REMOVE_ALL);
|
||||
if (res->status == NULL && status_code >= 100 && status_code <= 999) {
|
||||
custom_status.code = status_code;
|
||||
strcpy(custom_status.type, "");
|
||||
strcpy(custom_status.msg, status + 4);
|
||||
res.status = &custom_status;
|
||||
} else if (res.status == NULL) {
|
||||
res.status = http_get_status(500);
|
||||
sprintf(err_msg, "The status code was set to an invalid or unknown value.");
|
||||
goto respond;
|
||||
strcpy(custom_status.msg, status_hdr + 4);
|
||||
res->status = &custom_status;
|
||||
} else if (res->status == NULL) {
|
||||
res->status = http_get_status(500);
|
||||
sprintf(err_msg, "The status_hdr code was set to an invalid or unknown value.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const char *content_length_f = http_get_header_field(&res.hdr, "Content-Length");
|
||||
content_length = (content_length_f == NULL) ? -1 : strtol(content_length_f, NULL, 10);
|
||||
const char *content_length_f = http_get_header_field(&res->hdr, "Content-Length");
|
||||
ctx->content_length = (content_length_f == NULL) ? -1 : strtol(content_length_f, NULL, 10);
|
||||
|
||||
const char *content_type = http_get_header_field(&res.hdr, "Content-Type");
|
||||
const char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||
const char *content_type = http_get_header_field(&res->hdr, "Content-Type");
|
||||
const char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
|
||||
if (content_encoding == NULL &&
|
||||
content_type != NULL &&
|
||||
strncmp(content_type, "text/html", 9) == 0 &&
|
||||
content_length != -1 &&
|
||||
content_length <= sizeof(msg_content) - 1)
|
||||
ctx->content_length != -1 &&
|
||||
ctx->content_length <= sizeof(msg_content) - 1)
|
||||
{
|
||||
fastcgi_dump(&fcgi_cnx, msg_content, sizeof(msg_content));
|
||||
goto respond;
|
||||
return 0;
|
||||
}
|
||||
|
||||
use_fastcgi = 1;
|
||||
ctx->use_fastcgi = 1;
|
||||
|
||||
if (content_length != -1 && content_length < 1024000) {
|
||||
use_fastcgi |= FASTCGI_COMPRESS_HOLD;
|
||||
if (ctx->content_length != -1 && ctx->content_length < 1024000) {
|
||||
ctx->use_fastcgi |= FASTCGI_COMPRESS_HOLD;
|
||||
}
|
||||
|
||||
content_length = -1;
|
||||
ctx->content_length = -1;
|
||||
|
||||
int http_comp = http_get_compression(&req, &res);
|
||||
int http_comp = http_get_compression(req, res);
|
||||
if (http_comp & COMPRESS) {
|
||||
if (http_comp & COMPRESS_BR) {
|
||||
use_fastcgi |= FASTCGI_COMPRESS_BR;
|
||||
ctx->use_fastcgi |= FASTCGI_COMPRESS_BR;
|
||||
sprintf(buf0, "br");
|
||||
} else if (http_comp & COMPRESS_GZ) {
|
||||
use_fastcgi |= FASTCGI_COMPRESS_GZ;
|
||||
ctx->use_fastcgi |= FASTCGI_COMPRESS_GZ;
|
||||
sprintf(buf0, "gzip");
|
||||
}
|
||||
http_add_header_field(&res.hdr, "Vary", "Accept-Encoding");
|
||||
http_add_header_field(&res.hdr, "Content-Encoding", buf0);
|
||||
http_remove_header_field(&res.hdr, "Content-Length", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res->hdr, "Vary", "Accept-Encoding");
|
||||
http_add_header_field(&res->hdr, "Content-Encoding", buf0);
|
||||
http_remove_header_field(&res->hdr, "Content-Length", HTTP_REMOVE_ALL);
|
||||
}
|
||||
|
||||
if (http_get_header_field(&res.hdr, "Content-Length") == NULL) {
|
||||
http_add_header_field(&res.hdr, "Transfer-Encoding", "chunked");
|
||||
if (http_get_header_field(&res->hdr, "Content-Length") == NULL) {
|
||||
http_add_header_field(&res->hdr, "Transfer-Encoding", "chunked");
|
||||
}
|
||||
}
|
||||
} else if (conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||
info("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->proxy.hostname, conf->proxy.port);
|
||||
http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL);
|
||||
} else if (ctx->conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||
info("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, ctx->conf->proxy.hostname, ctx->conf->proxy.port);
|
||||
http_remove_header_field(&res->hdr, "Date", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL);
|
||||
|
||||
ret = proxy_init(&req, &res, &ctx, conf, client, cctx, &custom_status, err_msg);
|
||||
use_proxy = (ret == 0);
|
||||
ret = proxy_init(req, res, status, ctx->conf, client, ctx, &custom_status, err_msg);
|
||||
ctx->use_proxy = (ret == 0);
|
||||
|
||||
if (res.status->code == 101) {
|
||||
const char *connection = http_get_header_field(&res.hdr, "Connection");
|
||||
const char *upgrade = http_get_header_field(&res.hdr, "Upgrade");
|
||||
if (res->status->code == 101) {
|
||||
const char *connection = http_get_header_field(&res->hdr, "Connection");
|
||||
const char *upgrade = http_get_header_field(&res->hdr, "Upgrade");
|
||||
if (connection != NULL && upgrade != NULL &&
|
||||
(strstr(connection, "upgrade") != NULL || strstr(connection, "Upgrade") != NULL) &&
|
||||
strcmp(upgrade, "websocket") == 0)
|
||||
{
|
||||
const char *ws_accept = http_get_header_field(&res.hdr, "Sec-WebSocket-Accept");
|
||||
if (ws_calc_accept_key(ctx.ws_key, buf0) == 0) {
|
||||
use_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
|
||||
const char *ws_accept = http_get_header_field(&res->hdr, "Sec-WebSocket-Accept");
|
||||
if (ws_calc_accept_key(status->ws_key, buf0) == 0) {
|
||||
ctx->use_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
|
||||
}
|
||||
} else {
|
||||
ctx.status = 101;
|
||||
ctx.origin = INTERNAL;
|
||||
res.status = http_get_status(501);
|
||||
status->status = 101;
|
||||
status->origin = INTERNAL;
|
||||
res->status = http_get_status(501);
|
||||
}
|
||||
}
|
||||
|
||||
// Let 300 be formatted by origin server
|
||||
if (use_proxy && res.status->code >= 301 && res.status->code < 600) {
|
||||
const char *content_type = http_get_header_field(&res.hdr, "Content-Type");
|
||||
const char *content_length_f = http_get_header_field(&res.hdr, "Content-Length");
|
||||
const char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||
if (ctx->use_proxy && res->status->code >= 301 && res->status->code < 600) {
|
||||
const char *content_type = http_get_header_field(&res->hdr, "Content-Type");
|
||||
const char *content_length_f = http_get_header_field(&res->hdr, "Content-Length");
|
||||
const char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
|
||||
if (content_encoding == NULL && content_type != NULL && content_length_f != NULL && strncmp(content_type, "text/html", 9) == 0) {
|
||||
long content_len = strtol(content_length_f, NULL, 10);
|
||||
if (content_len <= sizeof(msg_content) - 1) {
|
||||
if (ctx.status != 101) {
|
||||
ctx.status = res.status->code;
|
||||
ctx.origin = res.status->code >= 400 ? SERVER : NONE;
|
||||
if (status->status != 101) {
|
||||
status->status = res->status->code;
|
||||
status->origin = res->status->code >= 400 ? SERVER : NONE;
|
||||
}
|
||||
use_proxy = 0;
|
||||
ctx->use_proxy = 0;
|
||||
proxy_dump(msg_content, content_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||
char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
|
||||
if (use_proxy && content_encoding == NULL) {
|
||||
int http_comp = http_get_compression(&req, &res);
|
||||
if (http_comp & COMPRESS_BR) {
|
||||
@ -534,192 +534,22 @@ static void request_handler(client_ctx_t *cctx) {
|
||||
}
|
||||
}
|
||||
|
||||
char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
||||
char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding");
|
||||
int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0;
|
||||
http_remove_header_field(&res.hdr, "Transfer-Encoding", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Transfer-Encoding", HTTP_REMOVE_ALL);
|
||||
ret = sprintf(buf0, "%s%s%s",
|
||||
(use_proxy & PROXY_COMPRESS_BR) ? "br" :
|
||||
((use_proxy & PROXY_COMPRESS_GZ) ? "gzip" : ""),
|
||||
((use_proxy & PROXY_COMPRESS) && chunked) ? ", " : "",
|
||||
chunked ? "chunked" : "");
|
||||
if (ret > 0) {
|
||||
http_add_header_field(&res.hdr, "Transfer-Encoding", buf0);
|
||||
http_add_header_field(&res->hdr, "Transfer-Encoding", buf0);
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
error("Unknown host type: %i", conf->type);
|
||||
res.status = http_get_status(501);
|
||||
error("Unknown host type: %i", ctx->conf->type);
|
||||
res->status = http_get_status(501);
|
||||
}
|
||||
|
||||
respond:
|
||||
if (!use_proxy) {
|
||||
if (conf != NULL && conf->type == CONFIG_TYPE_LOCAL && uri.is_static && res.status->code == 405) {
|
||||
http_add_header_field(&res.hdr, "Allow", "GET, HEAD, TRACE");
|
||||
}
|
||||
if (http_get_header_field(&res.hdr, "Accept-Ranges") == NULL) {
|
||||
http_add_header_field(&res.hdr, "Accept-Ranges", "none");
|
||||
}
|
||||
if (!use_fastcgi && file == NULL) {
|
||||
http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Cache-Control", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Content-Type", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Content-Encoding", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res.hdr, "Date", http_get_date(buf0, sizeof(buf0)));
|
||||
http_add_header_field(&res.hdr, "Server", SERVER_STR);
|
||||
http_add_header_field(&res.hdr, "Cache-Control", "no-cache");
|
||||
http_add_header_field(&res.hdr, "Content-Type", "text/html; charset=UTF-8");
|
||||
|
||||
// TODO list Locations on 3xx Redirects
|
||||
const http_doc_info *info = http_get_status_info(res.status);
|
||||
const http_status_msg *http_msg = http_get_error_msg(res.status);
|
||||
|
||||
if (msg_content[0] == 0) {
|
||||
if (res.status->code >= 300 && res.status->code < 400) {
|
||||
const char *location = http_get_header_field(&res.hdr, "Location");
|
||||
if (location != NULL) {
|
||||
snprintf(msg_content, sizeof(msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location);
|
||||
}
|
||||
}
|
||||
} else if (strncmp(msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(msg_content, "<html", 5) == 0) {
|
||||
msg_content[0] = 0;
|
||||
// TODO let relevant information pass?
|
||||
}
|
||||
|
||||
char *proxy_doc = "";
|
||||
if (conf != NULL && conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||
const http_status *status = http_get_status(ctx.status);
|
||||
char stat_str[8];
|
||||
sprintf(stat_str, "%03i", ctx.status);
|
||||
sprintf(msg_pre_buf_2, http_proxy_document,
|
||||
" success",
|
||||
(ctx.origin == CLIENT_REQ) ? " error" : " success",
|
||||
(ctx.origin == INTERNAL) ? " error" : " success",
|
||||
(ctx.origin == SERVER_REQ) ? " error" : (ctx.status == 0 ? "" : " success"),
|
||||
(ctx.origin == CLIENT_RES) ? " error" : " success",
|
||||
(ctx.origin == SERVER) ? " error" : (ctx.status == 0 ? "" : " success"),
|
||||
(ctx.origin == SERVER_RES) ? " error" : (ctx.status == 0 ? "" : " success"),
|
||||
(ctx.origin == INTERNAL) ? " error" : " success",
|
||||
(ctx.origin == INTERNAL || ctx.origin == SERVER) ? " error" : " success",
|
||||
res.status->code,
|
||||
res.status->msg,
|
||||
(ctx.status == 0) ? "???" : stat_str,
|
||||
(status != NULL) ? status->msg : "",
|
||||
cctx->req_host);
|
||||
proxy_doc = msg_pre_buf_2;
|
||||
}
|
||||
|
||||
sprintf(msg_pre_buf_1, info->doc, res.status->code, res.status->msg, http_msg != NULL ? http_msg->msg : "", err_msg[0] != 0 ? err_msg : "");
|
||||
content_length = snprintf(msg_buf, sizeof(msg_buf), http_default_document, res.status->code,
|
||||
res.status->msg, msg_pre_buf_1, info->mode, info->icon, info->color, cctx->req_host,
|
||||
proxy_doc, msg_content[0] != 0 ? msg_content : "");
|
||||
}
|
||||
if (content_length >= 0) {
|
||||
sprintf(buf0, "%li", content_length);
|
||||
http_remove_header_field(&res.hdr, "Content-Length", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res.hdr, "Content-Length", buf0);
|
||||
} else if (http_get_header_field(&res.hdr, "Transfer-Encoding") == NULL) {
|
||||
cctx->s_keep_alive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int close_proxy = 0;
|
||||
if (use_proxy != 2) {
|
||||
const char *conn = http_get_header_field(&res.hdr, "Connection");
|
||||
close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
|
||||
http_remove_header_field(&res.hdr, "Connection", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res.hdr, "Keep-Alive", HTTP_REMOVE_ALL);
|
||||
if (cctx->s_keep_alive && cctx->c_keep_alive) {
|
||||
http_add_header_field(&res.hdr, "Connection", "keep-alive");
|
||||
sprintf(buf0, "timeout=%i, max=%i", CLIENT_TIMEOUT, REQ_PER_CONNECTION);
|
||||
http_add_header_field(&res.hdr, "Keep-Alive", buf0);
|
||||
} else {
|
||||
http_add_header_field(&res.hdr, "Connection", "close");
|
||||
}
|
||||
}
|
||||
|
||||
http_send_response(client, &res);
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
const char *location = http_get_header_field(&res.hdr, "Location");
|
||||
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
|
||||
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), use_proxy ? "-> " : "", res.status->code,
|
||||
res.status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
||||
format_duration(micros, buf0), CLR_STR);
|
||||
|
||||
// TODO access/error log file
|
||||
|
||||
if (use_proxy == 2) {
|
||||
// WebSocket
|
||||
info("Upgrading connection to WebSocket connection");
|
||||
ret = ws_handle_connection(client, &proxy);
|
||||
if (ret != 0) {
|
||||
cctx->c_keep_alive = 0;
|
||||
close_proxy = 1;
|
||||
}
|
||||
info("WebSocket connection closed");
|
||||
} else if (strcmp(req.method, "HEAD") != 0) {
|
||||
// default response
|
||||
unsigned long snd_len = 0;
|
||||
unsigned long len;
|
||||
if (msg_buf[0] != 0) {
|
||||
ret = sock_send(client, msg_buf, content_length, 0);
|
||||
if (ret <= 0) {
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
}
|
||||
snd_len += ret;
|
||||
} else if (file != NULL) {
|
||||
while (snd_len < content_length) {
|
||||
len = fread(buffer, 1, CHUNK_SIZE, file);
|
||||
if (snd_len + len > content_length) {
|
||||
len = content_length - snd_len;
|
||||
}
|
||||
ret = sock_send(client, buffer, len, feof(file) ? 0 : MSG_MORE);
|
||||
if (ret <= 0) {
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
break;
|
||||
}
|
||||
snd_len += ret;
|
||||
}
|
||||
} else if (use_fastcgi) {
|
||||
const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
||||
int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL);
|
||||
|
||||
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
|
||||
ret = fastcgi_send(&fcgi_cnx, client, flags);
|
||||
} else if (use_proxy) {
|
||||
const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
|
||||
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
|
||||
|
||||
const char *content_len = http_get_header_field(&res.hdr, "Content-Length");
|
||||
unsigned long len_to_send = 0;
|
||||
if (content_len != NULL) {
|
||||
len_to_send = strtol(content_len, NULL, 10);
|
||||
}
|
||||
|
||||
int flags = (chunked ? PROXY_CHUNKED : 0) | (use_proxy & PROXY_COMPRESS);
|
||||
ret = proxy_send(client, len_to_send, flags);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
cctx->c_keep_alive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (close_proxy && proxy.socket != 0) {
|
||||
info(BLUE_STR "Closing proxy connection");
|
||||
sock_close(&proxy);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
|
||||
info("Transfer complete: %s", format_duration(micros, buf0));
|
||||
|
||||
uri_free(&uri);
|
||||
abort:
|
||||
if (fcgi_cnx.socket != 0) {
|
||||
close(fcgi_cnx.socket);
|
||||
fcgi_cnx.socket = 0;
|
||||
}
|
||||
http_free_req(&req);
|
||||
http_free_res(&res);
|
||||
return 0;
|
||||
}
|
||||
|
251
src/worker/responder.c
Normal file
251
src/worker/responder.c
Normal file
@ -0,0 +1,251 @@
|
||||
/**
|
||||
* sesimos - secure, simple, modern web server
|
||||
* @brief HTTP responder
|
||||
* @file src/worker/responder.c
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2022-12-29
|
||||
*/
|
||||
|
||||
#include "../defs.h"
|
||||
#include "responder.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "tcp_closer.h"
|
||||
#include "../async.h"
|
||||
#include "../logger.h"
|
||||
|
||||
#include "../lib/utils.h"
|
||||
#include "../lib/config.h"
|
||||
#include "../lib/sock.h"
|
||||
#include "../lib/http.h"
|
||||
#include "../lib/proxy.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
#include "../lib/compress.h"
|
||||
#include "../lib/websocket.h"
|
||||
#include "request_handler.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/err.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void responder_func(client_ctx_t *ctx);
|
||||
static void responder(client_ctx_t *ctx);
|
||||
|
||||
int responder_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) responder_func, "req");
|
||||
}
|
||||
|
||||
int respond(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void responder_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void responder_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void responder_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
responder(ctx);
|
||||
|
||||
if (ctx->c_keep_alive && ctx->s_keep_alive && ctx->req_num < REQ_PER_CONNECTION) {
|
||||
async(ctx->socket.socket, POLLIN, 0, (void (*)(void *)) handle_request, ctx, (void (*)(void *)) tcp_close, ctx);
|
||||
} else {
|
||||
tcp_close(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void responder(client_ctx_t *ctx) {
|
||||
sock *client = &ctx->socket;
|
||||
long ret = 0;
|
||||
|
||||
char buf0[1024];
|
||||
char msg_buf[8192], msg_pre_buf_1[4096], msg_pre_buf_2[4096], err_msg[256];
|
||||
char msg_content[1024];
|
||||
char buffer[CHUNK_SIZE];
|
||||
|
||||
msg_buf[0] = 0;
|
||||
err_msg[0] = 0;
|
||||
msg_content[0] = 0;
|
||||
|
||||
fastcgi_cnx_t fcgi_cnx = {.socket = 0, .req_id = 0, .ctx = ctx};
|
||||
|
||||
http_req *req = &ctx->req;
|
||||
http_res *res = &ctx->res;
|
||||
http_status_ctx status = {.status = 0, .origin = NONE, .ws_key = NULL};
|
||||
|
||||
if (!ctx->use_proxy) {
|
||||
if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_LOCAL && ctx->uri.is_static && res->status->code == 405) {
|
||||
http_add_header_field(&res->hdr, "Allow", "GET, HEAD, TRACE");
|
||||
}
|
||||
if (http_get_header_field(&res->hdr, "Accept-Ranges") == NULL) {
|
||||
http_add_header_field(&res->hdr, "Accept-Ranges", "none");
|
||||
}
|
||||
if (!ctx->use_fastcgi && ctx->file == NULL) {
|
||||
http_remove_header_field(&res->hdr, "Date", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Cache-Control", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Content-Type", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Content-Encoding", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res->hdr, "Date", http_get_date(buf0, sizeof(buf0)));
|
||||
http_add_header_field(&res->hdr, "Server", SERVER_STR);
|
||||
http_add_header_field(&res->hdr, "Cache-Control", "no-cache");
|
||||
http_add_header_field(&res->hdr, "Content-Type", "text/html; charset=UTF-8");
|
||||
|
||||
// TODO list Locations on 3xx Redirects
|
||||
const http_doc_info *info = http_get_status_info(res->status);
|
||||
const http_status_msg *http_msg = http_get_error_msg(res->status);
|
||||
|
||||
if (msg_content[0] == 0) {
|
||||
if (res->status->code >= 300 && res->status->code < 400) {
|
||||
const char *location = http_get_header_field(&res->hdr, "Location");
|
||||
if (location != NULL) {
|
||||
snprintf(msg_content, sizeof(msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location);
|
||||
}
|
||||
}
|
||||
} else if (strncmp(msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(msg_content, "<html", 5) == 0) {
|
||||
msg_content[0] = 0;
|
||||
// TODO let relevant information pass?
|
||||
}
|
||||
|
||||
char *proxy_doc = "";
|
||||
if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_REVERSE_PROXY) {
|
||||
const http_status *status_hdr = http_get_status(status.status);
|
||||
char stat_str[8];
|
||||
sprintf(stat_str, "%03i", status.status);
|
||||
sprintf(msg_pre_buf_2, http_proxy_document,
|
||||
" success",
|
||||
(status.origin == CLIENT_REQ) ? " error" : " success",
|
||||
(status.origin == INTERNAL) ? " error" : " success",
|
||||
(status.origin == SERVER_REQ) ? " error" : (status.status == 0 ? "" : " success"),
|
||||
(status.origin == CLIENT_RES) ? " error" : " success",
|
||||
(status.origin == SERVER) ? " error" : (status.status == 0 ? "" : " success"),
|
||||
(status.origin == SERVER_RES) ? " error" : (status.status == 0 ? "" : " success"),
|
||||
(status.origin == INTERNAL) ? " error" : " success",
|
||||
(status.origin == INTERNAL || status.origin == SERVER) ? " error" : " success",
|
||||
res->status->code,
|
||||
res->status->msg,
|
||||
(status.status == 0) ? "???" : stat_str,
|
||||
(status_hdr != NULL) ? status_hdr->msg : "",
|
||||
ctx->req_host);
|
||||
proxy_doc = msg_pre_buf_2;
|
||||
}
|
||||
|
||||
sprintf(msg_pre_buf_1, info->doc, res->status->code, res->status->msg, http_msg != NULL ? http_msg->msg : "", err_msg[0] != 0 ? err_msg : "");
|
||||
ctx->content_length = snprintf(msg_buf, sizeof(msg_buf), http_default_document, res->status->code,
|
||||
res->status->msg, msg_pre_buf_1, info->mode, info->icon, info->color, ctx->req_host,
|
||||
proxy_doc, msg_content[0] != 0 ? msg_content : "");
|
||||
}
|
||||
if (ctx->content_length >= 0) {
|
||||
sprintf(buf0, "%li", ctx->content_length);
|
||||
http_remove_header_field(&res->hdr, "Content-Length", HTTP_REMOVE_ALL);
|
||||
http_add_header_field(&res->hdr, "Content-Length", buf0);
|
||||
} else if (http_get_header_field(&res->hdr, "Transfer-Encoding") == NULL) {
|
||||
ctx->s_keep_alive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int close_proxy = 0;
|
||||
if (ctx->use_proxy != 2) {
|
||||
const char *conn = http_get_header_field(&res->hdr, "Connection");
|
||||
close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
|
||||
http_remove_header_field(&res->hdr, "Connection", HTTP_REMOVE_ALL);
|
||||
http_remove_header_field(&res->hdr, "Keep-Alive", HTTP_REMOVE_ALL);
|
||||
if (ctx->s_keep_alive && ctx->c_keep_alive) {
|
||||
http_add_header_field(&res->hdr, "Connection", "keep-alive");
|
||||
sprintf(buf0, "timeout=%i, max=%i", CLIENT_TIMEOUT, REQ_PER_CONNECTION);
|
||||
http_add_header_field(&res->hdr, "Keep-Alive", buf0);
|
||||
} else {
|
||||
http_add_header_field(&res->hdr, "Connection", "close");
|
||||
}
|
||||
}
|
||||
|
||||
http_send_response(client, res);
|
||||
clock_gettime(CLOCK_MONOTONIC, &ctx->end);
|
||||
const char *location = http_get_header_field(&res->hdr, "Location");
|
||||
unsigned long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000;
|
||||
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res->status), ctx->use_proxy ? "-> " : "", res->status->code,
|
||||
res->status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
|
||||
format_duration(micros, buf0), CLR_STR);
|
||||
|
||||
// TODO access/error log file
|
||||
|
||||
if (ctx->use_proxy == 2) {
|
||||
// WebSocket
|
||||
info("Upgrading connection to WebSocket connection");
|
||||
ret = ws_handle_connection(client, &proxy);
|
||||
if (ret != 0) {
|
||||
ctx->c_keep_alive = 0;
|
||||
close_proxy = 1;
|
||||
}
|
||||
info("WebSocket connection closed");
|
||||
} else if (strcmp(req->method, "HEAD") != 0) {
|
||||
// default response
|
||||
unsigned long snd_len = 0;
|
||||
unsigned long len;
|
||||
if (msg_buf[0] != 0) {
|
||||
ret = sock_send(client, msg_buf, ctx->content_length, 0);
|
||||
if (ret <= 0) {
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
}
|
||||
snd_len += ret;
|
||||
} else if (ctx->file != NULL) {
|
||||
while (snd_len < ctx->content_length) {
|
||||
len = fread(buffer, 1, CHUNK_SIZE, ctx->file);
|
||||
if (snd_len + len > ctx->content_length) {
|
||||
len = ctx->content_length - snd_len;
|
||||
}
|
||||
ret = sock_send(client, buffer, len, feof(ctx->file) ? 0 : MSG_MORE);
|
||||
if (ret <= 0) {
|
||||
error("Unable to send: %s", sock_strerror(client));
|
||||
break;
|
||||
}
|
||||
snd_len += ret;
|
||||
}
|
||||
} else if (ctx->use_fastcgi) {
|
||||
const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding");
|
||||
int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL);
|
||||
|
||||
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (ctx->use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
|
||||
ret = fastcgi_send(&fcgi_cnx, client, flags);
|
||||
} else if (ctx->use_proxy) {
|
||||
const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding");
|
||||
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
|
||||
|
||||
const char *content_len = http_get_header_field(&res->hdr, "Content-Length");
|
||||
unsigned long len_to_send = 0;
|
||||
if (content_len != NULL) {
|
||||
len_to_send = strtol(content_len, NULL, 10);
|
||||
}
|
||||
|
||||
int flags = (chunked ? PROXY_CHUNKED : 0) | (ctx->use_proxy & PROXY_COMPRESS);
|
||||
ret = proxy_send(client, len_to_send, flags);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
ctx->c_keep_alive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (close_proxy && proxy.socket != 0) {
|
||||
info(BLUE_STR "Closing proxy connection");
|
||||
sock_close(&proxy);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &ctx->end);
|
||||
micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000;
|
||||
info("Transfer complete: %s", format_duration(micros, buf0));
|
||||
|
||||
uri_free(&ctx->uri);
|
||||
if (fcgi_cnx.socket != 0) {
|
||||
close(fcgi_cnx.socket);
|
||||
fcgi_cnx.socket = 0;
|
||||
}
|
||||
http_free_req(req);
|
||||
http_free_res(res);
|
||||
}
|
22
src/worker/responder.h
Normal file
22
src/worker/responder.h
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* sesimos - secure, simple, modern web server
|
||||
* @brief HTTP responder (header file)
|
||||
* @file src/worker/responderr.h
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2022-12-29
|
||||
*/
|
||||
|
||||
#ifndef SESIMOS_RESPONDER_H
|
||||
#define SESIMOS_RESPONDER_H
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int responder_init(int n_workers, int buf_size);
|
||||
|
||||
int respond(client_ctx_t *ctx);
|
||||
|
||||
void responder_stop(void);
|
||||
|
||||
void responder_destroy(void);
|
||||
|
||||
#endif //SESIMOS_RESPONDER_H
|
Reference in New Issue
Block a user