4 Commits

Author SHA1 Message Date
96f3225f51 Add clock_micros() 2022-12-31 00:53:36 +01:00
9ad1ecf1da Rev proxy working again 2022-12-31 00:28:20 +01:00
28d7cf68df Outsource from request_handler 2022-12-30 16:48:48 +01:00
204317f46a Remove responder 2022-12-30 16:06:49 +01:00
20 changed files with 500 additions and 400 deletions

View File

@@ -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/workers.o \
bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o bin/worker/responder.o \
bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o \
bin/worker/fastcgi_handler.o bin/worker/local_handler.o bin/worker/proxy_handler.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 \
@@ -73,8 +73,6 @@ bin/worker/tcp_closer.o: src/worker/func.h
bin/worker/fastcgi_handler.o: src/worker/func.h
bin/worker/responder.o: src/worker/func.h
bin/worker/local_handler.o: src/worker/func.h
bin/worker/proxy_handler.o: src/worker/func.h

View File

@@ -14,6 +14,7 @@
#define SERVER_STR_HTML "Sesimos&nbsp;web&nbsp;server&nbsp;" SERVER_VERSION
#define CHUNK_SIZE 8192
#define MAX_PROXY_CNX_PER_HOST 16
#ifndef DEFAULT_HOST
# define DEFAULT_HOST "www.necronda.net"

View File

@@ -12,25 +12,62 @@
#include "proxy.h"
#include "utils.h"
#include "compress.h"
#include "config.h"
#include <openssl/ssl.h>
#include <string.h>
#include <errno.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
sock proxy;
char *proxy_host = NULL;
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
static SSL_CTX *proxy_ctx = NULL;
static void *proxies = NULL;
int proxy_preload(void) {
proxy.ctx = SSL_CTX_new(TLS_client_method());
int n = 0;
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config_t *hc = &config.hosts[i];
if (hc->type == CONFIG_TYPE_UNSET) break;
if (hc->type != CONFIG_TYPE_REVERSE_PROXY) continue;
n++;
}
// FIXME return value check
proxy_ctx = SSL_CTX_new(TLS_client_method());
proxies = malloc(n * PROXY_ARRAY_SIZE);
memset(proxies, 0, n * PROXY_ARRAY_SIZE);
return 0;
}
int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
void proxy_unload(void) {
SSL_CTX_free(proxy_ctx);
free(proxies);
}
static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
int n = 0;
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config_t *hc = &config.hosts[i];
if (hc->type == CONFIG_TYPE_UNSET) break;
if (hc->type != CONFIG_TYPE_REVERSE_PROXY) continue;
if (hc == conf) break;
n++;
}
void *ptr = proxies + n * PROXY_ARRAY_SIZE;
for (int i = 0; i < MAX_PROXY_CNX_PER_HOST; i++, ptr += PROXY_ARRAY_SIZE) {
proxy_ctx_t *ctx = ptr;
if (!ctx->in_use) {
return ctx;
}
}
return NULL;
}
int proxy_request_header(http_req *req, sock *sock) {
char buf1[256], buf2[256];
int p_len;
@@ -50,13 +87,13 @@ int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
const char *host = http_get_header_field(&req->hdr, "Host");
const char *forwarded = http_get_header_field(&req->hdr, "Forwarded");
int client_ipv6 = strchr(ctx->addr, ':') != NULL;
int server_ipv6 = strchr(ctx->s_addr, ':') != NULL;
int client_ipv6 = strchr(sock->addr, ':') != NULL;
int server_ipv6 = strchr(sock->s_addr, ':') != NULL;
p_len = snprintf(buf1, sizeof(buf1), "by=%s%s%s;for=%s%s%s;host=%s;proto=%s",
server_ipv6 ? "\"[" : "", ctx->s_addr, server_ipv6 ? "]\"" : "",
client_ipv6 ? "\"[" : "", ctx->addr, client_ipv6 ? "]\"" : "",
host, enc ? "https" : "http");
server_ipv6 ? "\"[" : "", sock->s_addr, server_ipv6 ? "]\"" : "",
client_ipv6 ? "\"[" : "", sock->addr, client_ipv6 ? "]\"" : "",
host, sock->enc ? "https" : "http");
if (p_len < 0 || p_len >= sizeof(buf1)) {
error("Appended part of header field 'Forwarded' too long");
return -1;
@@ -76,9 +113,9 @@ int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
const char *xff = http_get_header_field(&req->hdr, "X-Forwarded-For");
if (xff == NULL) {
http_add_header_field(&req->hdr, "X-Forwarded-For", ctx->addr);
http_add_header_field(&req->hdr, "X-Forwarded-For", sock->addr);
} else {
sprintf(buf1, "%s, %s", xff, ctx->addr);
sprintf(buf1, "%s, %s", xff, sock->addr);
http_remove_header_field(&req->hdr, "X-Forwarded-For", HTTP_REMOVE_ALL);
http_add_header_field(&req->hdr, "X-Forwarded-For", buf1);
}
@@ -107,7 +144,7 @@ int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
const char *xfp = http_get_header_field(&req->hdr, "X-Forwarded-Proto");
if (xfp == NULL) {
if (forwarded == NULL) {
http_add_header_field(&req->hdr, "X-Forwarded-Proto", enc ? "https" : "http");
http_add_header_field(&req->hdr, "X-Forwarded-Proto", sock->enc ? "https" : "http");
} else {
char *ptr = strchr(forwarded, ',');
unsigned long len;
@@ -180,36 +217,41 @@ int proxy_response_header(http_req *req, http_res *res, host_config_t *conf) {
return 0;
}
int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg) {
proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, http_status *custom_status, char *err_msg) {
char buffer[CHUNK_SIZE];
const char *connection, *upgrade, *ws_version;
long ret;
int tries = 0, retry = 0;
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
if (proxy.socket != 0 && strcmp(proxy_host, conf->name) == 0 && sock_check(&proxy) == 0)
proxy_ctx_t *proxy = proxy_get_by_conf(conf);
proxy->in_use = 1;
if (proxy->initialized && sock_check(&proxy->proxy) == 0)
goto proxy;
retry:
if (proxy.socket != 0) {
if (proxy->initialized) {
info(BLUE_STR "Closing proxy connection");
sock_close(&proxy);
sock_close(&proxy->proxy);
proxy->initialized = 0;
}
retry = 0;
tries++;
proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
if (proxy.socket < 0) {
proxy->proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
if (proxy->proxy.socket < 0) {
error("Unable to create socket");
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return -1;
return NULL;
}
server_timeout.tv_sec = SERVER_TIMEOUT_INIT;
server_timeout.tv_usec = 0;
if (setsockopt(proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err;
if (setsockopt(proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err;
struct hostent *host_ent = gethostbyname2(conf->proxy.hostname, AF_INET6);
@@ -236,7 +278,7 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->proxy.port);
if (connect(proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
if (connect(proxy->proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
res->status = http_get_status(504);
ctx->origin = SERVER_REQ;
@@ -254,9 +296,9 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
server_timeout.tv_sec = SERVER_TIMEOUT;
server_timeout.tv_usec = 0;
if (setsockopt(proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err;
if (setsockopt(proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
proxy_timeout_err:
res->status = http_get_status(500);
ctx->origin = INTERNAL;
@@ -266,25 +308,26 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
}
if (conf->proxy.enc) {
proxy.ssl = SSL_new(proxy.ctx);
SSL_set_fd(proxy.ssl, proxy.socket);
SSL_set_connect_state(proxy.ssl);
proxy->proxy.ssl = SSL_new(proxy_ctx);
SSL_set_fd(proxy->proxy.ssl, proxy->proxy.socket);
SSL_set_connect_state(proxy->proxy.ssl);
ret = SSL_do_handshake(proxy.ssl);
proxy._last_ret = ret;
proxy._errno = errno;
proxy._ssl_error = ERR_get_error();
proxy.enc = 1;
ret = SSL_do_handshake(proxy->proxy.ssl);
proxy->proxy._last_ret = ret;
proxy->proxy._errno = errno;
proxy->proxy._ssl_error = ERR_get_error();
proxy->proxy.enc = 1;
if (ret < 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
error("Unable to perform handshake: %s", sock_strerror(&proxy));
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&proxy));
error("Unable to perform handshake: %s", sock_strerror(&proxy->proxy));
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&proxy->proxy));
goto proxy_err;
}
}
proxy_host = conf->name;
proxy->initialized = 1;
proxy->host = conf->name;
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->proxy.port);
proxy:
@@ -297,26 +340,26 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
} else {
res->status = http_get_status(501);
ctx->origin = INTERNAL;
return -1;
return NULL;
}
} else {
http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL);
http_add_header_field(&req->hdr, "Connection", "keep-alive");
}
ret = proxy_request_header(req, (int) client->enc, cctx);
ret = proxy_request_header(req, client);
if (ret != 0) {
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return -1;
return NULL;
}
ret = http_send_request(&proxy, req);
ret = http_send_request(&proxy->proxy, req);
if (ret < 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
error("Unable to send request to server (1): %s", sock_strerror(&proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy));
error("Unable to send request to server (1): %s", sock_strerror(&proxy->proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy->proxy));
retry = tries < 4;
goto proxy_err;
}
@@ -327,17 +370,17 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
ret = 0;
if (content_len > 0) {
ret = sock_splice(&proxy, client, buffer, sizeof(buffer), content_len);
ret = sock_splice(&proxy->proxy, client, buffer, sizeof(buffer), content_len);
} else if (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL) {
ret = sock_splice_chunked(&proxy, client, buffer, sizeof(buffer));
ret = sock_splice_chunked(&proxy->proxy, client, buffer, sizeof(buffer));
}
if (ret < 0 || (content_len != 0 && ret != content_len)) {
if (ret == -1) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
error("Unable to send request to server (2): %s", sock_strerror(&proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy));
error("Unable to send request to server (2): %s", sock_strerror(&proxy->proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&proxy->proxy));
retry = tries < 4;
goto proxy_err;
} else if (ret == -2) {
@@ -345,17 +388,17 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
ctx->origin = CLIENT_REQ;
error("Unable to receive request from client: %s", sock_strerror(client));
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
return -1;
return NULL;
}
res->status = http_get_status(500);
ctx->origin = INTERNAL;
error("Unknown Error");
return -1;
return NULL;
}
ret = sock_recv(&proxy, buffer, sizeof(buffer), MSG_PEEK);
ret = sock_recv(&proxy->proxy, buffer, sizeof(buffer), MSG_PEEK);
if (ret <= 0) {
int enc_err = sock_enc_error(&proxy);
int enc_err = sock_enc_error(&proxy->proxy);
if (errno == EAGAIN || errno == EINPROGRESS || enc_err == SSL_ERROR_WANT_READ ||
enc_err == SSL_ERROR_WANT_WRITE)
{
@@ -365,8 +408,8 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
}
error("Unable to receive response from server: %s", sock_strerror(&proxy));
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&proxy));
error("Unable to receive response from server: %s", sock_strerror(&proxy->proxy));
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&proxy->proxy));
retry = tries < 4;
goto proxy_err;
}
@@ -440,23 +483,24 @@ int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t
}
ptr = pos0 + 2;
}
sock_recv(&proxy, buffer, header_len, 0);
sock_recv(&proxy->proxy, buffer, header_len, 0);
ret = proxy_response_header(req, res, conf);
if (ret != 0) {
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return -1;
return NULL;
}
return 0;
return proxy;
proxy_err:
errno = 0;
if (retry) goto retry;
return -1;
return NULL;
}
int proxy_send(sock *client, unsigned long len_to_send, int flags) {
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
char buffer[CHUNK_SIZE], comp_out[CHUNK_SIZE], buf[256], *ptr;
long ret = 0, len, snd_len;
int finish_comp = 0;
@@ -479,12 +523,12 @@ int proxy_send(sock *client, unsigned long len_to_send, int flags) {
do {
snd_len = 0;
if (flags & PROXY_CHUNKED) {
ret = sock_get_chunk_header(&proxy);
ret = sock_get_chunk_header(&proxy->proxy);
if (ret < 0) {
if (ret == -1) {
error("Unable to receive from server: Malformed chunk header");
} else {
error("Unable to receive from server: %s", sock_strerror(&proxy));
error("Unable to receive from server: %s", sock_strerror(&proxy->proxy));
}
break;
}
@@ -502,9 +546,9 @@ int proxy_send(sock *client, unsigned long len_to_send, int flags) {
}
while (snd_len < len_to_send) {
unsigned long avail_in, avail_out;
ret = sock_recv(&proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
ret = sock_recv(&proxy->proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
if (ret <= 0) {
error("Unable to receive from server: %s", sock_strerror(&proxy));
error("Unable to receive from server: %s", sock_strerror(&proxy->proxy));
break;
}
len = ret;
@@ -544,7 +588,7 @@ int proxy_send(sock *client, unsigned long len_to_send, int flags) {
if (finish_comp) goto finish;
}
if (ret <= 0) break;
if (flags & PROXY_CHUNKED) sock_recv(&proxy, buffer, 2, 0);
if (flags & PROXY_CHUNKED) sock_recv(&proxy->proxy, buffer, 2, 0);
} while ((flags & PROXY_CHUNKED) && len_to_send > 0);
if (ret <= 0) return -1;
@@ -560,8 +604,7 @@ int proxy_send(sock *client, unsigned long len_to_send, int flags) {
return 0;
}
int proxy_dump(char *buf, long len) {
sock_recv(&proxy, buf, len, 0);
sock_close(&proxy);
int proxy_dump(proxy_ctx_t *proxy, char *buf, long len) {
sock_recv(&proxy->proxy, buf, len, 0);
return 0;
}

View File

@@ -18,22 +18,30 @@
# define SERVER_NAME "reverse proxy"
#endif
#define PROXY_ARRAY_SIZE (MAX_PROXY_CNX_PER_HOST * sizeof(proxy_ctx_t))
#include "http.h"
#include "config.h"
#include "../server.h"
extern sock proxy;
typedef struct {
unsigned char initialized:1;
unsigned char in_use:1;
sock proxy;
char *host;
} proxy_ctx_t;
int proxy_preload(void);
int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx);
void proxy_unload(void);
int proxy_request_header(http_req *req, sock *sock);
int proxy_response_header(http_req *req, http_res *res, host_config_t *conf);
int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg);
proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, http_status *custom_status, char *err_msg);
int proxy_send(sock *client, unsigned long len_to_send, int flags);
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags);
int proxy_dump(char *buf, long len);
int proxy_dump(proxy_ctx_t *proxy, char *buf, long len);
#endif //SESIMOS_PROXY_H

View File

@@ -19,7 +19,8 @@ typedef struct {
union {
struct sockaddr sock;
struct sockaddr_in6 ipv6;
} addr;
} _addr;
char *addr, *s_addr;
SSL_CTX *ctx;
SSL *ssl;
long _last_ret;

View File

@@ -11,6 +11,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -203,3 +204,9 @@ int base64_encode(void *data, unsigned long data_len, char *output, unsigned lon
return 0;
}
long clock_micros(void) {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 10000000 + time.tv_nsec / 1000;
}

View File

@@ -37,4 +37,6 @@ int str_trim_lws(char **start, char **end);
int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len);
long clock_micros(void);
#endif //SESIMOS_UTILS_H

View File

@@ -16,6 +16,7 @@
#include "lib/proxy.h"
#include "lib/geoip.h"
#include "workers.h"
#include "worker/func.h"
#include <stdio.h>
#include <getopt.h>
@@ -70,8 +71,8 @@ static void accept_cb(void *arg) {
sock *client = &client_ctx->socket;
client->ctx = contexts[0];
socklen_t addr_len = sizeof(client->addr);
int client_fd = accept(fd, &client->addr.sock, &addr_len);
socklen_t addr_len = sizeof(client->_addr);
int client_fd = accept(fd, &client->_addr.sock, &addr_len);
if (client_fd < 0) {
critical("Unable to accept connection");
return;

View File

@@ -9,16 +9,7 @@
#ifndef SESIMOS_SERVER_H
#define SESIMOS_SERVER_H
#include "lib/sock.h"
#include "lib/http.h"
#include "lib/uri.h"
#include "lib/config.h"
#include "lib/fastcgi.h"
#include <sys/time.h>
#include <maxminddb.h>
#include <signal.h>
#include <arpa/inet.h>
#define NUM_SOCKETS 2
#define LISTEN_BACKLOG 16
@@ -31,30 +22,6 @@
#define CNX_HANDLER_WORKERS 8
#define REQ_HANDLER_WORKERS 16
typedef struct {
sock socket;
int req_num;
char *addr, *s_addr;
unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1;
char cc[3], host[256];
char req_host[256], err_msg[256];
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;
http_status custom_status;
int use_fastcgi, use_proxy;
host_config_t *conf;
FILE *file;
long content_length;
fastcgi_cnx_t fcgi_cnx;
char msg_buf[8192], msg_content[1024];
} client_ctx_t;
extern volatile sig_atomic_t server_alive;
#endif //SESIMOS_SERVER_H

View File

@@ -11,26 +11,38 @@
#include "../lib/utils.h"
#include "../lib/compress.h"
#include "../workers.h"
#include "../lib/fastcgi.h"
#include <string.h>
static int fastcgi_handler(client_ctx_t *ctx);
static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx);
static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx);
void fastcgi_handler_func(client_ctx_t *ctx) {
fastcgi_cnx_t fcgi_cnx;
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
// TODO
fastcgi_handler(ctx);
fastcgi_handler_1(ctx, &fcgi_cnx);
respond(ctx);
fastcgi_handler_2(ctx, &fcgi_cnx);
request_complete(ctx);
handle_request(ctx);
}
static int fastcgi_handler(client_ctx_t *ctx) {
static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
http_res *res = &ctx->res;
http_req *req = &ctx->req;
http_uri *uri = &ctx->uri;
sock *client = &ctx->socket;
fastcgi_cnx_t *fcgi_cnx = &ctx->fcgi_cnx;
char *err_msg = ctx->err_msg;
fcgi_cnx->socket = 0;
fcgi_cnx->req_id = 0;
fcgi_cnx->r_addr = ctx->socket.addr;
fcgi_cnx->r_host = (ctx->host[0] != 0) ? ctx->host : NULL;
char buf[1024];
int mode, ret;
@@ -142,3 +154,22 @@ static int fastcgi_handler(client_ctx_t *ctx) {
return 0;
}
static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
const char *transfer_encoding = http_get_header_field(&ctx->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));
int ret = fastcgi_send(fcgi_cnx, &ctx->socket, flags);
if (ret < 0) {
ctx->c_keep_alive = 0;
}
if (fcgi_cnx->socket != 0) {
close(fcgi_cnx->socket);
fcgi_cnx->socket = 0;
}
return ret;
}

View File

@@ -9,7 +9,38 @@
#ifndef SESIMOS_FUNC_H
#define SESIMOS_FUNC_H
#include "../server.h"
#include "../lib/sock.h"
#include "../lib/http.h"
#include "../lib/uri.h"
#include "../lib/config.h"
#include "../lib/proxy.h"
typedef struct {
sock socket;
int req_num;
unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1;
char cc[3], host[256];
char req_host[256], err_msg[256];
char log_prefix[512];
char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1];
long cnx_s, cnx_e, req_s, res_ts, req_e;
http_req req;
http_res res;
http_uri uri;
http_status_ctx status;
http_status custom_status;
int use_fastcgi, use_proxy;
host_config_t *conf;
FILE *file;
long content_length;
char msg_buf[8192], msg_content[1024];
proxy_ctx_t *proxy;
} client_ctx_t;
typedef struct {
client_ctx_t *client;
sock *s1, *s2, *s, *r;
} ws_ctx_t;
void tcp_acceptor_func(client_ctx_t *ctx);
@@ -17,12 +48,16 @@ void tcp_closer_func(client_ctx_t *ctx);
void request_handler_func(client_ctx_t *ctx);
void responder_func(client_ctx_t *ctx);
void local_handler_func(client_ctx_t *ctx);
void fastcgi_handler_func(client_ctx_t *ctx);
void proxy_handler_func(client_ctx_t *ctx);
void ws_frame_handler_func(ws_ctx_t *ctx);
int respond(client_ctx_t *ctx);
int request_complete(client_ctx_t *ctx);
#endif //SESIMOS_FUNC_H

View File

@@ -20,9 +20,17 @@ void local_handler_func(client_ctx_t *ctx) {
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
switch (local_handler(ctx)) {
case 0: respond(ctx); break;
case 1: fastcgi_handle(ctx); break;
default: tcp_close(ctx); break;
case 0:
respond(ctx);
request_complete(ctx);
handle_request(ctx);
break;
case 1:
fastcgi_handle(ctx);
break;
default:
tcp_close(ctx);
break;
}
}
@@ -36,6 +44,7 @@ static int local_handler(client_ctx_t *ctx) {
int accept_if_modified_since = 0;
if (strcmp(req->method, "TRACE") == 0) {
// FIXME not working?
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Content-Type", "message/http");

View File

@@ -1,7 +1,7 @@
/**
* sesimos - secure, simple, modern web server
* @brief Proxy handler
* @file src/worker/proxy_handler.c
* @file src/worker/proxy_handler_1.c
* @author Lorenz Stechauner
* @date 2022-12-29
*/
@@ -15,22 +15,23 @@
#include <string.h>
static int proxy_handler(client_ctx_t *ctx);
static int proxy_handler_1(client_ctx_t *ctx);
static int proxy_handler_2(client_ctx_t *ctx);
void proxy_handler_func(client_ctx_t *ctx) {
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
proxy_handler(ctx);
proxy_handler_1(ctx);
respond(ctx);
proxy_handler_2(ctx);
request_complete(ctx);
handle_request(ctx);
}
static int proxy_handler(client_ctx_t *ctx) {
static int proxy_handler_1(client_ctx_t *ctx) {
http_res *res = &ctx->res;
http_req *req = &ctx->req;
http_uri *uri = &ctx->uri;
http_status_ctx *status = &ctx->status;
sock *client = &ctx->socket;
char *err_msg = ctx->err_msg;
char buf[1024];
@@ -38,8 +39,8 @@ static int proxy_handler(client_ctx_t *ctx) {
http_remove_header_field(&res->hdr, "Date", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL);
int ret = proxy_init(req, res, status, ctx->conf, client, ctx, &ctx->custom_status, err_msg);
ctx->use_proxy = (ret == 0);
ctx->proxy = proxy_init(&ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg);
ctx->use_proxy = (ctx->proxy != NULL);
if (res->status->code == 101) {
const char *connection = http_get_header_field(&res->hdr, "Connection");
@@ -72,7 +73,7 @@ static int proxy_handler(client_ctx_t *ctx) {
status->origin = res->status->code >= 400 ? SERVER : NONE;
}
ctx->use_proxy = 0;
proxy_dump(ctx->msg_content, content_len);
proxy_dump(ctx->proxy, ctx->msg_content, content_len);
}
}
}
@@ -103,3 +104,25 @@ static int proxy_handler(client_ctx_t *ctx) {
return 0;
}
static int proxy_handler_2(client_ctx_t *ctx) {
const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding");
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
const char *content_len = http_get_header_field(&ctx->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);
int ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, flags);
ctx->proxy->in_use = 0;
ctx->proxy = NULL;
if (ret < 0) {
ctx->c_keep_alive = 0;
}
return ret;
}

View File

@@ -12,6 +12,8 @@
#include "../lib/mpmc.h"
#include "../logger.h"
#include "../lib/utils.h"
#include "../lib/websocket.h"
#include "../server.h"
#include <string.h>
#include <openssl/err.h>
@@ -20,13 +22,22 @@
static int request_handler(client_ctx_t *ctx);
void request_handler_func(client_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
switch (request_handler(ctx)) {
case 0: respond(ctx); break;
case 1: local_handle(ctx); break;
case 2: proxy_handle(ctx); break;
default: tcp_close(ctx); break;
case 0:
respond(ctx);
handle_request(ctx);
break;
case 1:
local_handle(ctx);
break;
case 2:
proxy_handle(ctx);
break;
default:
tcp_close(ctx);
break;
}
}
@@ -39,8 +50,15 @@ static int request_handler(client_ctx_t *ctx) {
err_msg[0] = 0;
ctx->file = NULL;
ctx->proxy = NULL;
ctx->use_fastcgi = 0;
ctx->use_proxy = 0;
ctx->proxy = NULL;
ctx->msg_content[0] = 0;
ctx->msg_buf[0] = 0;
ctx->req_host[0] = 0;
ctx->err_msg[0] = 0;
http_res *res = &ctx->res;
res->status = http_get_status(501);
@@ -53,25 +71,22 @@ static int request_handler(client_ctx_t *ctx) {
status->origin = NONE;
status->ws_key = NULL;
ctx->fcgi_cnx.socket = 0;
ctx->fcgi_cnx.req_id = 0;
ctx->fcgi_cnx.r_addr = ctx->addr;
ctx->fcgi_cnx.r_host = (ctx->host[0] != 0) ? ctx->host : NULL;
ctx->req_s = clock_micros();
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
//ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
// FIXME async poll
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);
/*if (ret <= 0) {
if (ret <= 0) {
if (errno != 0) return 0;
ctx->c_keep_alive = 0;
res->status = http_get_status(408);
return 0;
}*/
//clock_gettime(CLOCK_MONOTONIC, &begin);
}
ctx->req_s = clock_micros();
http_req *req = &ctx->req;
ret = http_receive_request(client, req);
@@ -103,10 +118,10 @@ static int request_handler(client_ctx_t *ctx) {
sprintf(err_msg, "Host header field is too long.");
return 0;
} else if (host_ptr == NULL || strchr(host_ptr, '/') != NULL) {
if (strchr(ctx->addr, ':') == NULL) {
strcpy(ctx->req_host, ctx->addr);
if (strchr(ctx->socket.addr, ':') == NULL) {
strcpy(ctx->req_host, ctx->socket.addr);
} else {
sprintf(ctx->req_host, "[%s]", ctx->addr);
sprintf(ctx->req_host, "[%s]", ctx->socket.addr);
}
res->status = http_get_status(400);
sprintf(err_msg, "The client provided no or an invalid Host header field.");
@@ -184,3 +199,174 @@ static int request_handler(client_ctx_t *ctx) {
return 0;
}
int respond(client_ctx_t *ctx) {
http_req *req = &ctx->req;
http_res *res = &ctx->res;
sock *client = &ctx->socket;
http_status_ctx *status = &ctx->status;
char *err_msg = ctx->err_msg;
long ret = 0;
char buf0[1024];
char msg_pre_buf_1[4096], msg_pre_buf_2[4096];
char buffer[CHUNK_SIZE];
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 (ctx->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(ctx->msg_content, sizeof(ctx->msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location);
}
}
} else if (strncmp(ctx->msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(ctx->msg_content, "<html", 5) == 0) {
ctx->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(ctx->msg_buf, sizeof(ctx->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, ctx->msg_content[0] != 0 ? ctx->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);
ctx->res_ts = clock_micros();
const char *location = http_get_header_field(&res->hdr, "Location");
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(ctx->res_ts - ctx->req_s, 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, &ctx->proxy->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 (ctx->msg_buf[0] != 0) {
ret = sock_send(client, ctx->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) {
return 2;
} else if (ctx->use_proxy) {
return 3;
}
if (ret < 0) {
ctx->c_keep_alive = 0;
}
}
return 0;
}
int request_complete(client_ctx_t *ctx) {
// FIXME
//if (close_proxy && proxy.socket != 0) {
// info(BLUE_STR "Closing proxy connection");
// sock_close(&proxy);
//}
char buf[32];
ctx->req_e = clock_micros();
info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf));
uri_free(&ctx->uri);
http_free_req(&ctx->req);
http_free_res(&ctx->res);
return 0;
}

View File

@@ -1,222 +0,0 @@
/**
* 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 "func.h"
#include "../async.h"
#include "../logger.h"
#include "../lib/utils.h"
#include "../lib/proxy.h"
#include "../lib/fastcgi.h"
#include "../lib/websocket.h"
#include "../workers.h"
#include <string.h>
#include <unistd.h>
#include <openssl/err.h>
#include <arpa/inet.h>
static int responder(client_ctx_t *ctx);
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 int responder(client_ctx_t *ctx) {
http_req *req = &ctx->req;
http_res *res = &ctx->res;
sock *client = &ctx->socket;
http_status_ctx *status = &ctx->status;
fastcgi_cnx_t *fcgi_cnx = &ctx->fcgi_cnx;
char *err_msg = ctx->err_msg;
long ret = 0;
char buf0[1024];
char msg_pre_buf_1[4096], msg_pre_buf_2[4096];
char buffer[CHUNK_SIZE];
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 (ctx->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(ctx->msg_content, sizeof(ctx->msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location);
}
}
} else if (strncmp(ctx->msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(ctx->msg_content, "<html", 5) == 0) {
ctx->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(ctx->msg_buf, sizeof(ctx->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, ctx->msg_content[0] != 0 ? ctx->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 (ctx->msg_buf[0] != 0) {
ret = sock_send(client, ctx->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);
return 0;
}

View File

@@ -13,6 +13,7 @@
#include "../lib/utils.h"
#include "../lib/geoip.h"
#include "../workers.h"
#include "../server.h"
#include <string.h>
#include <errno.h>
@@ -32,36 +33,36 @@ void tcp_acceptor_func(client_ctx_t *ctx) {
static int tcp_acceptor(client_ctx_t *ctx) {
struct sockaddr_in6 server_addr;
inet_ntop(ctx->socket.addr.ipv6.sin6_family, &ctx->socket.addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
inet_ntop(ctx->socket._addr.ipv6.sin6_family, &ctx->socket._addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
if (strncmp(ctx->_c_addr, "::ffff:", 7) == 0) {
ctx->addr = ctx->_c_addr + 7;
ctx->socket.addr = ctx->_c_addr + 7;
} else {
ctx->addr = ctx->_c_addr;
ctx->socket.addr = ctx->_c_addr;
}
socklen_t len = sizeof(server_addr);
getsockname(ctx->socket.socket, (struct sockaddr *) &server_addr, &len);
inet_ntop(server_addr.sin6_family, (void *) &server_addr.sin6_addr, ctx->_s_addr, sizeof(ctx->_s_addr));
if (strncmp(ctx->_s_addr, "::ffff:", 7) == 0) {
ctx->s_addr = ctx->_s_addr + 7;
ctx->socket.s_addr = ctx->_s_addr + 7;
} else {
ctx->s_addr = ctx->_s_addr;
ctx->socket.s_addr = ctx->_s_addr;
}
sprintf(ctx->log_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) ctx->socket.enc ? HTTPS_STR : HTTP_STR,
ntohs(server_addr.sin6_port), CLR_STR, /*color_table[0]*/ "", INET6_ADDRSTRLEN, ctx->addr,
ntohs(ctx->socket.addr.ipv6.sin6_port), CLR_STR);
ntohs(server_addr.sin6_port), CLR_STR, /*color_table[0]*/ "", INET6_ADDRSTRLEN, ctx->socket.addr,
ntohs(ctx->socket._addr.ipv6.sin6_port), CLR_STR);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
int ret;
char buf[1024];
sock *client = &ctx->socket;
clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
ctx->cnx_s = clock_micros();
if (config.dns_server[0] != 0) {
sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->addr);
sprintf(buf, "dig @%s +short +time=1 -x %s", config.dns_server, ctx->socket.addr);
FILE *dig = popen(buf, "r");
if (dig == NULL) {
error("Unable to start dig: %s", strerror(errno));
@@ -85,9 +86,9 @@ static int tcp_acceptor(client_ctx_t *ctx) {
}
ctx->cc[0] = 0;
geoip_lookup_country(&client->addr.sock, ctx->cc);
geoip_lookup_country(&client->_addr.sock, ctx->cc);
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
info("Connection accepted from %s %s%s%s[%s]", ctx->socket.addr, ctx->host[0] != 0 ? "(" : "",
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
ctx->cc[0] != 0 ? ctx->cc : "N/A");

View File

@@ -13,14 +13,13 @@
#include <memory.h>
void tcp_closer_func(client_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->socket.s_addr, ctx->log_prefix);
sock_close(&ctx->socket);
ctx->cnx_e = clock_micros();
char buf[32];
clock_gettime(CLOCK_MONOTONIC, &ctx->end);
unsigned long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000;
info("Connection closed (%s)", format_duration(micros, buf));
info("Connection closed (%s)", format_duration(ctx->cnx_e - ctx->cnx_s, buf));
memset(ctx, 0, sizeof(*ctx));
}

View File

@@ -0,0 +1,19 @@
/**
* sesimos - secure, simple, modern web server
* @brief WebSocket frame handler
* @file src/worker/ws_frame_handler.c
* @author Lorenz Stechauner
* @date 2022-12-30
*/
#include "func.h"
static int ws_frame_handler(ws_ctx_t *ctx);
void ws_frame_handler_func(ws_ctx_t *ctx) {
}
static int ws_frame_handler(ws_ctx_t *ctx) {
return 0;
}

View File

@@ -11,14 +11,13 @@
#include "worker/func.h"
static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx, responder_ctx,
static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx,
local_handler_ctx, fastcgi_handler_cxt, proxy_handler_ctx;
int workers_init(void) {
mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp_a");
mpmc_init(&tcp_closer_ctx, 8, 64, (void (*)(void *)) tcp_closer_func, "tcp_c");
mpmc_init(&request_handler_ctx, 16, 64, (void (*)(void *)) request_handler_func, "req");
mpmc_init(&responder_ctx, 16, 64, (void (*)(void *)) responder_func, "res");
mpmc_init(&local_handler_ctx, 16, 64, (void (*)(void *)) local_handler_func, "local");
mpmc_init(&fastcgi_handler_cxt, 16, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi");
mpmc_init(&proxy_handler_ctx, 16, 64, (void (*)(void *)) proxy_handler_func, "proxy");
@@ -31,7 +30,6 @@ void workers_stop(void) {
mpmc_stop(&fastcgi_handler_cxt);
mpmc_stop(&proxy_handler_ctx);
mpmc_stop(&request_handler_ctx);
mpmc_stop(&responder_ctx);
mpmc_stop(&tcp_closer_ctx);
}
@@ -41,7 +39,6 @@ void workers_destroy(void) {
mpmc_destroy(&fastcgi_handler_cxt);
mpmc_destroy(&proxy_handler_ctx);
mpmc_destroy(&request_handler_ctx);
mpmc_destroy(&responder_ctx);
mpmc_destroy(&tcp_closer_ctx);
}
@@ -57,10 +54,6 @@ int handle_request(client_ctx_t *ctx) {
return mpmc_queue(&request_handler_ctx, ctx);
}
int respond(client_ctx_t *ctx) {
return mpmc_queue(&responder_ctx, ctx);
}
int local_handle(client_ctx_t *ctx) {
return mpmc_queue(&local_handler_ctx, ctx);
}

View File

@@ -9,7 +9,7 @@
#ifndef SESIMOS_WORKERS_H
#define SESIMOS_WORKERS_H
#include "server.h"
#include "worker/func.h"
int workers_init(void);
@@ -23,8 +23,6 @@ int tcp_close(client_ctx_t *ctx);
int handle_request(client_ctx_t *ctx);
int respond(client_ctx_t *ctx);
int local_handle(client_ctx_t *ctx);
int fastcgi_handle(client_ctx_t *ctx);