More workers

This commit is contained in:
2022-12-29 23:21:40 +01:00
parent db966b3a66
commit a56002a409
12 changed files with 526 additions and 403 deletions

View File

@ -47,7 +47,7 @@ bin/worker/%.o: src/worker/%.c
bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o bin/workers.o \ 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/responder.o \
bin/worker/fastcgi_handler.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/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/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 bin/lib/utils.o bin/lib/websocket.o bin/lib/mpmc.o
@ -75,6 +75,10 @@ bin/worker/fastcgi_handler.o: src/worker/func.h
bin/worker/responder.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
bin/lib/compress.o: src/lib/compress.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 bin/lib/config.o: src/lib/config.h src/lib/utils.h src/lib/uri.h src/logger.h

View File

@ -132,8 +132,8 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int client_num, unsigne
addr = (struct sockaddr_in6 *) &addr_storage; addr = (struct sockaddr_in6 *) &addr_storage;
sprintf(buf0, "%i", addr->sin6_port); sprintf(buf0, "%i", addr->sin6_port);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_PORT", buf0); param_ptr = fastcgi_add_param(param_ptr, "REMOTE_PORT", buf0);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_ADDR", conn->ctx->addr); param_ptr = fastcgi_add_param(param_ptr, "REMOTE_ADDR", conn->r_addr);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_HOST", conn->ctx->host[0] != 0 ? conn->ctx->host : conn->ctx->addr); param_ptr = fastcgi_add_param(param_ptr, "REMOTE_HOST", conn->r_host != NULL ? conn->r_host : conn->r_addr);
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_IDENT", ""); //param_ptr = fastcgi_add_param(param_ptr, "REMOTE_IDENT", "");
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_USER", ""); //param_ptr = fastcgi_add_param(param_ptr, "REMOTE_USER", "");

View File

@ -12,7 +12,6 @@
#include "include/fastcgi.h" #include "include/fastcgi.h"
#include "http.h" #include "http.h"
#include "uri.h" #include "uri.h"
#include "../server.h"
#define FASTCGI_CHUNKED 1 #define FASTCGI_CHUNKED 1
#define FASTCGI_COMPRESS_GZ 2 #define FASTCGI_COMPRESS_GZ 2
@ -37,7 +36,8 @@ typedef struct {
const char *webroot; const char *webroot;
unsigned short out_len; unsigned short out_len;
unsigned short out_off; unsigned short out_off;
client_ctx_t *ctx; char *r_addr;
char *r_host;
} fastcgi_cnx_t; } fastcgi_cnx_t;
char *fastcgi_add_param(char *buf, const char *key, const char *value); char *fastcgi_add_param(char *buf, const char *key, const char *value);

View File

@ -13,6 +13,7 @@
#include "lib/http.h" #include "lib/http.h"
#include "lib/uri.h" #include "lib/uri.h"
#include "lib/config.h" #include "lib/config.h"
#include "lib/fastcgi.h"
#include <sys/time.h> #include <sys/time.h>
#include <maxminddb.h> #include <maxminddb.h>
@ -36,7 +37,7 @@ typedef struct {
char *addr, *s_addr; char *addr, *s_addr;
unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1; unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1;
char cc[3], host[256]; char cc[3], host[256];
char req_host[256]; char req_host[256], err_msg[256];
char log_prefix[512]; char log_prefix[512];
char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1]; char _c_addr[INET6_ADDRSTRLEN + 1], _s_addr[INET6_ADDRSTRLEN + 1];
struct timespec begin, end; struct timespec begin, end;
@ -44,10 +45,13 @@ typedef struct {
http_res res; http_res res;
http_uri uri; http_uri uri;
http_status_ctx status; http_status_ctx status;
http_status custom_status;
int use_fastcgi, use_proxy; int use_fastcgi, use_proxy;
host_config_t *conf; host_config_t *conf;
FILE *file; FILE *file;
long content_length; long content_length;
fastcgi_cnx_t fcgi_cnx;
char msg_buf[8192], msg_content[1024];
} client_ctx_t; } client_ctx_t;
extern volatile sig_atomic_t server_alive; extern volatile sig_atomic_t server_alive;

View File

@ -1,6 +1,6 @@
/** /**
* sesimos - secure, simple, modern web server * sesimos - secure, simple, modern web server
* @brief TCP closer * @brief FastCGI handler
* @file src/worker/fastcgi_handler.c * @file src/worker/fastcgi_handler.c
* @author Lorenz Stechauner * @author Lorenz Stechauner
* @date 2022-12-28 * @date 2022-12-28
@ -9,8 +9,136 @@
#include "func.h" #include "func.h"
#include "../logger.h" #include "../logger.h"
#include "../lib/utils.h" #include "../lib/utils.h"
#include "../lib/compress.h"
#include "../workers.h"
#include <string.h>
static int fastcgi_handler(client_ctx_t *ctx);
void fastcgi_handler_func(client_ctx_t *ctx) { void fastcgi_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); logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
// TODO // TODO
fastcgi_handler(ctx);
respond(ctx);
}
static int fastcgi_handler(client_ctx_t *ctx) {
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;
char buf[1024];
int mode, ret;
if (strcmp(uri->filename + strlen(uri->filename) - 4, ".ncr") == 0) {
mode = FASTCGI_SESIMOS;
} 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);
return 0;
}
struct stat statbuf;
stat(uri->filename, &statbuf);
char *last_modified = http_format_date(statbuf.st_mtime, buf, sizeof(buf));
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
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.");
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");
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);
} else if (client_transfer_encoding != NULL && strstr(client_transfer_encoding, "chunked") != NULL) {
ret = fastcgi_receive_chunked(fcgi_cnx, client);
} else {
ret = 0;
}
if (ret != 0) {
if (ret < 0) {
return -1;
} else {
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
}
res->status = http_get_status(502);
return 0;
}
fastcgi_close_stdin(fcgi_cnx);
ret = fastcgi_header(fcgi_cnx, res, err_msg);
if (ret != 0) {
return (ret < 0) ? -1 : 0;
}
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) {
ctx->custom_status.code = status_code;
strcpy(ctx->custom_status.type, "");
strcpy(ctx->custom_status.msg, status_hdr + 4);
res->status = &ctx->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");
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");
if (content_encoding == NULL &&
content_type != NULL &&
strncmp(content_type, "text/html", 9) == 0 &&
ctx->content_length != -1 &&
ctx->content_length <= sizeof(ctx->msg_content) - 1)
{
fastcgi_dump(fcgi_cnx, ctx->msg_content, sizeof(ctx->msg_content));
return 0;
}
ctx->use_fastcgi = 1;
if (ctx->content_length != -1 && ctx->content_length < 1024000) {
ctx->use_fastcgi |= FASTCGI_COMPRESS_HOLD;
}
ctx->content_length = -1;
int http_comp = http_get_compression(req, res);
if (http_comp & COMPRESS) {
if (http_comp & COMPRESS_BR) {
ctx->use_fastcgi |= FASTCGI_COMPRESS_BR;
sprintf(buf, "br");
} else if (http_comp & COMPRESS_GZ) {
ctx->use_fastcgi |= FASTCGI_COMPRESS_GZ;
sprintf(buf, "gzip");
}
http_add_header_field(&res->hdr, "Vary", "Accept-Encoding");
http_add_header_field(&res->hdr, "Content-Encoding", buf);
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");
}
return 0;
} }

View File

@ -19,6 +19,10 @@ void request_handler_func(client_ctx_t *ctx);
void responder_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 fastcgi_handler_func(client_ctx_t *ctx);
void proxy_handler_func(client_ctx_t *ctx);
#endif //SESIMOS_FUNC_H #endif //SESIMOS_FUNC_H

203
src/worker/local_handler.c Normal file
View File

@ -0,0 +1,203 @@
/**
* sesimos - secure, simple, modern web server
* @brief Local filesystem handler
* @file src/worker/local_handler.c
* @author Lorenz Stechauner
* @date 2022-12-29
*/
#include "func.h"
#include "../logger.h"
#include "../lib/utils.h"
#include "../lib/compress.h"
#include "../workers.h"
#include <string.h>
static int local_handler(client_ctx_t *ctx);
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;
}
}
static int local_handler(client_ctx_t *ctx) {
http_res *res = &ctx->res;
http_req *req = &ctx->req;
http_uri *uri = &ctx->uri;
char *err_msg = ctx->err_msg;
char buf1[1024], buf2[1024];
int accept_if_modified_since = 0;
if (strcmp(req->method, "TRACE") == 0) {
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Content-Type", "message/http");
ctx->content_length = snprintf(ctx->msg_buf, sizeof(ctx->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(ctx->msg_buf + ctx->content_length, sizeof(ctx->msg_buf) - ctx->content_length, "%s: %s\r\n", http_field_get_name(f), http_field_get_value(f));
}
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 && strstr(uri->path, "/.") != NULL) {
res->status = http_get_status(403);
sprintf(err_msg, "Parts of this URI are hidden.");
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.");
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);
sprintf(err_msg, "Listing contents of an directory is currently not implemented.");
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);
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);
sprintf(err_msg, "A GET request must not contain a payload");
return 0;
}
cache_init_uri(ctx->conf->cache, uri);
const char *last_modified = http_format_date(uri->meta->stat.st_mtime, buf1, sizeof(buf1));
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
sprintf(buf2, "%s; charset=%s", uri->meta->type, uri->meta->charset);
http_add_header_field(&res->hdr, "Content-Type", buf2);
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) {
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");
enc = COMPRESS_BR;
}
} 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");
enc = COMPRESS_GZ;
}
}
if (enc != 0) {
http_add_header_field(&res->hdr, "Vary", "Accept-Encoding");
}
}
if (uri->meta->etag[0] != 0) {
if (enc) {
sprintf(buf1, "%s-%s", uri->meta->etag, (enc & COMPRESS_BR) ? "br" : (enc & COMPRESS_GZ) ? "gzip" : "");
http_add_header_field(&res->hdr, "ETag", buf1);
} else {
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");
} else {
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) ||
(accept_if_modified_since && if_modified_since != NULL && strcmp(if_modified_since, last_modified) == 0))
{
res->status = http_get_status(304);
return 0;
}
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);
return 0;
}
range += 6;
char *ptr = strchr(range, '-');
if (ptr == NULL) {
res->status = http_get_status(416);
return 0;
}
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) {
ctx->content_length = 0;
return 0;
}
long num1 = 0;
long num2 = (long) file_len - 1;
if (ptr != range) num1 = (long) strtoul(range, NULL, 10);
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);
return 0;
}
sprintf(buf1, "bytes %li-%li/%li", num1, num2, file_len);
http_add_header_field(&res->hdr, "Content-Range", buf1);
res->status = http_get_status(206);
fseek(ctx->file, num1, SEEK_SET);
ctx->content_length = num2 - num1 + 1;
return 0;
}
if (ctx->file == NULL) {
ctx->file = fopen(uri->filename, "rb");
}
fseek(ctx->file, 0, SEEK_END);
ctx->content_length = ftell(ctx->file);
fseek(ctx->file, 0, SEEK_SET);
} else {
return 1;
}
return 0;
}

105
src/worker/proxy_handler.c Normal file
View File

@ -0,0 +1,105 @@
/**
* sesimos - secure, simple, modern web server
* @brief Proxy handler
* @file src/worker/proxy_handler.c
* @author Lorenz Stechauner
* @date 2022-12-29
*/
#include "func.h"
#include "../logger.h"
#include "../lib/utils.h"
#include "../lib/proxy.h"
#include "../lib/websocket.h"
#include "../workers.h"
#include <string.h>
static int proxy_handler(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);
respond(ctx);
}
static int proxy_handler(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];
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);
int ret = proxy_init(req, res, status, ctx->conf, client, ctx, &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 (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(status->ws_key, buf) == 0) {
ctx->use_proxy = (strcmp(buf, ws_accept) == 0) ? 2 : 1;
}
} else {
status->status = 101;
status->origin = INTERNAL;
res->status = http_get_status(501);
}
}
// Let 300 be formatted by origin server
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(ctx->msg_content) - 1) {
if (status->status != 101) {
status->status = res->status->code;
status->origin = res->status->code >= 400 ? SERVER : NONE;
}
ctx->use_proxy = 0;
proxy_dump(ctx->msg_content, content_len);
}
}
}
/*
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) {
use_proxy |= PROXY_COMPRESS_BR;
} else if (http_comp & COMPRESS_GZ) {
use_proxy |= PROXY_COMPRESS_GZ;
}
}
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);
ret = sprintf(buf, "%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", buf);
}
*/
return 0;
}

View File

@ -11,12 +11,7 @@
#include "../workers.h" #include "../workers.h"
#include "../lib/mpmc.h" #include "../lib/mpmc.h"
#include "../logger.h" #include "../logger.h"
#include "../lib/utils.h" #include "../lib/utils.h"
#include "../lib/proxy.h"
#include "../lib/fastcgi.h"
#include "../lib/compress.h"
#include "../lib/websocket.h"
#include <string.h> #include <string.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -27,33 +22,25 @@ static int request_handler(client_ctx_t *ctx);
void request_handler_func(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->s_addr, ctx->log_prefix);
if (request_handler(ctx) == 0) { switch (request_handler(ctx)) {
respond(ctx); case 0: respond(ctx); break;
} else { case 1: local_handle(ctx); break;
tcp_close(ctx); case 2: proxy_handle(ctx); break;
default: tcp_close(ctx); break;
} }
} }
static int request_handler(client_ctx_t *ctx) { static int request_handler(client_ctx_t *ctx) {
sock *client = &ctx->socket; sock *client = &ctx->socket;
char *err_msg = ctx->err_msg;
long ret; long ret;
char buf0[1024], buf1[1024]; char buf0[1024], buf1[1024];
char msg_buf[8192], err_msg[256];
char msg_content[1024];
const char *host_ptr, *hdr_connection;
msg_buf[0] = 0;
err_msg[0] = 0; err_msg[0] = 0;
msg_content[0] = 0;
int accept_if_modified_since = 0;
ctx->use_fastcgi = 0; ctx->use_fastcgi = 0;
ctx->use_proxy = 0; ctx->use_proxy = 0;
int p_len;
fastcgi_cnx_t fcgi_cnx = {.socket = 0, .req_id = 0, .ctx = ctx};
http_status custom_status;
http_res *res = &ctx->res; http_res *res = &ctx->res;
res->status = http_get_status(501); res->status = http_get_status(501);
@ -66,6 +53,11 @@ static int request_handler(client_ctx_t *ctx) {
status->origin = NONE; status->origin = NONE;
status->ws_key = NULL; 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;
clock_gettime(CLOCK_MONOTONIC, &ctx->begin); clock_gettime(CLOCK_MONOTONIC, &ctx->begin);
//ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000); //ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
@ -102,9 +94,9 @@ static int request_handler(client_ctx_t *ctx) {
return 0; return 0;
} }
hdr_connection = http_get_header_field(&req->hdr, "Connection"); const char *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)); 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"); const char *host_ptr = http_get_header_field(&req->hdr, "Host");
if (host_ptr != NULL && strlen(host_ptr) > 255) { if (host_ptr != NULL && strlen(host_ptr) > 255) {
ctx->req_host[0] = 0; ctx->req_host[0] = 0;
res->status = http_get_status(400); res->status = http_get_status(400);
@ -162,7 +154,7 @@ static int request_handler(client_ctx_t *ctx) {
res->status = http_get_status(308); res->status = http_get_status(308);
size = url_encode(uri->uri, strlen(uri->uri), buf0, sizeof(buf0)); size = url_encode(uri->uri, strlen(uri->uri), buf0, sizeof(buf0));
if (change_proto) { if (change_proto) {
p_len = snprintf(buf1, sizeof(buf1), "https://%s%s", ctx->req_host, buf0); int p_len = snprintf(buf1, sizeof(buf1), "https://%s%s", ctx->req_host, buf0);
if (p_len < 0 || p_len >= sizeof(buf1)) { 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"); error("Header field 'Location' too long");
@ -182,341 +174,9 @@ static int request_handler(client_ctx_t *ctx) {
} }
if (ctx->conf->type == CONFIG_TYPE_LOCAL) { if (ctx->conf->type == CONFIG_TYPE_LOCAL) {
if (strcmp(req->method, "TRACE") == 0) { return 1;
res->status = http_get_status(200);
http_add_header_field(&res->hdr, "Content-Type", "message/http");
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));
}
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 && strstr(uri->path, "/.") != NULL) {
res->status = http_get_status(403);
sprintf(err_msg, "Parts of this URI are hidden.");
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.");
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);
sprintf(err_msg, "Listing contents of an directory is currently not implemented.");
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);
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);
sprintf(err_msg, "A GET request must not contain a payload");
return 0;
}
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 *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) {
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");
enc = COMPRESS_BR;
}
} 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");
enc = COMPRESS_GZ;
}
}
if (enc != 0) {
http_add_header_field(&res->hdr, "Vary", "Accept-Encoding");
}
}
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);
} else {
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");
} else {
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) ||
(accept_if_modified_since && if_modified_since != NULL && strcmp(if_modified_since, last_modified) == 0))
{
res->status = http_get_status(304);
return 0;
}
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);
return 0;
}
range += 6;
char *ptr = strchr(range, '-');
if (ptr == NULL) {
res->status = http_get_status(416);
return 0;
}
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) {
ctx->content_length = 0;
return 0;
}
long num1 = 0;
long num2 = (long) file_len - 1;
if (ptr != range) num1 = (long) strtoul(range, NULL, 10);
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);
return 0;
}
sprintf(buf0, "bytes %li-%li/%li", num1, num2, file_len);
http_add_header_field(&res->hdr, "Content-Range", buf0);
res->status = http_get_status(206);
fseek(ctx->file, num1, SEEK_SET);
ctx->content_length = num2 - num1 + 1;
return 0;
}
if (ctx->file == NULL) {
ctx->file = fopen(uri->filename, "rb");
}
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) {
mode = FASTCGI_SESIMOS;
} 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);
return 0;
}
struct stat 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);
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.");
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");
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);
} else if (client_transfer_encoding != NULL && strstr(client_transfer_encoding, "chunked") != NULL) {
ret = fastcgi_receive_chunked(&fcgi_cnx, client);
} else {
ret = 0;
}
if (ret != 0) {
if (ret < 0) {
return -1;
} else {
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
}
res->status = http_get_status(502);
return 0;
}
fastcgi_close_stdin(&fcgi_cnx);
ret = fastcgi_header(&fcgi_cnx, res, err_msg);
if (ret != 0) {
return (ret < 0) ? -1 : 0;
}
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_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");
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");
if (content_encoding == NULL &&
content_type != NULL &&
strncmp(content_type, "text/html", 9) == 0 &&
ctx->content_length != -1 &&
ctx->content_length <= sizeof(msg_content) - 1)
{
fastcgi_dump(&fcgi_cnx, msg_content, sizeof(msg_content));
return 0;
}
ctx->use_fastcgi = 1;
if (ctx->content_length != -1 && ctx->content_length < 1024000) {
ctx->use_fastcgi |= FASTCGI_COMPRESS_HOLD;
}
ctx->content_length = -1;
int http_comp = http_get_compression(req, res);
if (http_comp & COMPRESS) {
if (http_comp & COMPRESS_BR) {
ctx->use_fastcgi |= FASTCGI_COMPRESS_BR;
sprintf(buf0, "br");
} else if (http_comp & 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);
}
if (http_get_header_field(&res->hdr, "Content-Length") == NULL) {
http_add_header_field(&res->hdr, "Transfer-Encoding", "chunked");
}
}
} else if (ctx->conf->type == CONFIG_TYPE_REVERSE_PROXY) { } 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); return 2;
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, 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 (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(status->ws_key, buf0) == 0) {
ctx->use_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
}
} else {
status->status = 101;
status->origin = INTERNAL;
res->status = http_get_status(501);
}
}
// Let 300 be formatted by origin server
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 (status->status != 101) {
status->status = res->status->code;
status->origin = res->status->code >= 400 ? SERVER : NONE;
}
ctx->use_proxy = 0;
proxy_dump(msg_content, content_len);
}
}
}
/*
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) {
use_proxy |= PROXY_COMPRESS_BR;
} else if (http_comp & COMPRESS_GZ) {
use_proxy |= PROXY_COMPRESS_GZ;
}
}
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);
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);
}
*/
} else { } else {
error("Unknown host type: %i", ctx->conf->type); error("Unknown host type: %i", ctx->conf->type);
res->status = http_get_status(501); res->status = http_get_status(501);

View File

@ -36,23 +36,17 @@ void responder_func(client_ctx_t *ctx) {
} }
static int responder(client_ctx_t *ctx) { static int 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_req *req = &ctx->req;
http_res *res = &ctx->res; http_res *res = &ctx->res;
http_status_ctx status = {.status = 0, .origin = NONE, .ws_key = NULL}; 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->use_proxy) {
if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_LOCAL && ctx->uri.is_static && res->status->code == 405) { if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_LOCAL && ctx->uri.is_static && res->status->code == 405) {
@ -76,45 +70,45 @@ static int responder(client_ctx_t *ctx) {
const http_doc_info *info = http_get_status_info(res->status); const http_doc_info *info = http_get_status_info(res->status);
const http_status_msg *http_msg = http_get_error_msg(res->status); const http_status_msg *http_msg = http_get_error_msg(res->status);
if (msg_content[0] == 0) { if (ctx->msg_content[0] == 0) {
if (res->status->code >= 300 && res->status->code < 400) { if (res->status->code >= 300 && res->status->code < 400) {
const char *location = http_get_header_field(&res->hdr, "Location"); const char *location = http_get_header_field(&res->hdr, "Location");
if (location != NULL) { if (location != NULL) {
snprintf(msg_content, sizeof(msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location); 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(msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(msg_content, "<html", 5) == 0) { } else if (strncmp(ctx->msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(ctx->msg_content, "<html", 5) == 0) {
msg_content[0] = 0; ctx->msg_content[0] = 0;
// TODO let relevant information pass? // TODO let relevant information pass?
} }
char *proxy_doc = ""; char *proxy_doc = "";
if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_REVERSE_PROXY) { if (ctx->conf != NULL && ctx->conf->type == CONFIG_TYPE_REVERSE_PROXY) {
const http_status *status_hdr = http_get_status(status.status); const http_status *status_hdr = http_get_status(status->status);
char stat_str[8]; char stat_str[8];
sprintf(stat_str, "%03i", status.status); sprintf(stat_str, "%03i", status->status);
sprintf(msg_pre_buf_2, http_proxy_document, sprintf(msg_pre_buf_2, http_proxy_document,
" success", " success",
(status.origin == CLIENT_REQ) ? " error" : " success", (status->origin == CLIENT_REQ) ? " error" : " success",
(status.origin == INTERNAL) ? " error" : " success", (status->origin == INTERNAL) ? " error" : " success",
(status.origin == SERVER_REQ) ? " error" : (status.status == 0 ? "" : " success"), (status->origin == SERVER_REQ) ? " error" : (status->status == 0 ? "" : " success"),
(status.origin == CLIENT_RES) ? " error" : " success", (status->origin == CLIENT_RES) ? " error" : " success",
(status.origin == SERVER) ? " error" : (status.status == 0 ? "" : " success"), (status->origin == SERVER) ? " error" : (status->status == 0 ? "" : " success"),
(status.origin == SERVER_RES) ? " error" : (status.status == 0 ? "" : " success"), (status->origin == SERVER_RES) ? " error" : (status->status == 0 ? "" : " success"),
(status.origin == INTERNAL) ? " error" : " success", (status->origin == INTERNAL) ? " error" : " success",
(status.origin == INTERNAL || status.origin == SERVER) ? " error" : " success", (status->origin == INTERNAL || status->origin == SERVER) ? " error" : " success",
res->status->code, res->status->code,
res->status->msg, res->status->msg,
(status.status == 0) ? "???" : stat_str, (status->status == 0) ? "???" : stat_str,
(status_hdr != NULL) ? status_hdr->msg : "", (status_hdr != NULL) ? status_hdr->msg : "",
ctx->req_host); ctx->req_host);
proxy_doc = msg_pre_buf_2; 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 : ""); 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, 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, res->status->msg, msg_pre_buf_1, info->mode, info->icon, info->color, ctx->req_host,
proxy_doc, msg_content[0] != 0 ? msg_content : ""); proxy_doc, ctx->msg_content[0] != 0 ? ctx->msg_content : "");
} }
if (ctx->content_length >= 0) { if (ctx->content_length >= 0) {
sprintf(buf0, "%li", ctx->content_length); sprintf(buf0, "%li", ctx->content_length);
@ -163,8 +157,8 @@ static int responder(client_ctx_t *ctx) {
// default response // default response
unsigned long snd_len = 0; unsigned long snd_len = 0;
unsigned long len; unsigned long len;
if (msg_buf[0] != 0) { if (ctx->msg_buf[0] != 0) {
ret = sock_send(client, msg_buf, ctx->content_length, 0); ret = sock_send(client, ctx->msg_buf, ctx->content_length, 0);
if (ret <= 0) { if (ret <= 0) {
error("Unable to send: %s", sock_strerror(client)); error("Unable to send: %s", sock_strerror(client));
} }
@ -187,7 +181,7 @@ static int responder(client_ctx_t *ctx) {
int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL); 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 flags = (chunked ? FASTCGI_CHUNKED : 0) | (ctx->use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
ret = fastcgi_send(&fcgi_cnx, client, flags); ret = fastcgi_send(fcgi_cnx, client, flags);
} else if (ctx->use_proxy) { } else if (ctx->use_proxy) {
const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding"); const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding");
int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL; int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
@ -217,9 +211,9 @@ static int responder(client_ctx_t *ctx) {
info("Transfer complete: %s", format_duration(micros, buf0)); info("Transfer complete: %s", format_duration(micros, buf0));
uri_free(&ctx->uri); uri_free(&ctx->uri);
if (fcgi_cnx.socket != 0) { if (fcgi_cnx->socket != 0) {
close(fcgi_cnx.socket); close(fcgi_cnx->socket);
fcgi_cnx.socket = 0; fcgi_cnx->socket = 0;
} }
http_free_req(req); http_free_req(req);
http_free_res(res); http_free_res(res);

View File

@ -11,19 +11,25 @@
#include "worker/func.h" #include "worker/func.h"
static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx, responder_ctx, fastcgi_handler_cxt; static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx, responder_ctx,
local_handler_ctx, fastcgi_handler_cxt, proxy_handler_ctx;
int workers_init(void) { int workers_init(void) {
mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp_a"); 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(&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(&request_handler_ctx, 16, 64, (void (*)(void *)) request_handler_func, "req");
mpmc_init(&responder_ctx, 16, 64, (void (*)(void *)) responder_func, "res"); 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(&fastcgi_handler_cxt, 16, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi");
mpmc_init(&proxy_handler_ctx, 16, 64, (void (*)(void *)) proxy_handler_func, "proxy");
return -1; return -1;
} }
void workers_stop(void) { void workers_stop(void) {
mpmc_stop(&tcp_acceptor_ctx); mpmc_stop(&tcp_acceptor_ctx);
mpmc_stop(&local_handler_ctx);
mpmc_stop(&fastcgi_handler_cxt);
mpmc_stop(&proxy_handler_ctx);
mpmc_stop(&request_handler_ctx); mpmc_stop(&request_handler_ctx);
mpmc_stop(&responder_ctx); mpmc_stop(&responder_ctx);
mpmc_stop(&tcp_closer_ctx); mpmc_stop(&tcp_closer_ctx);
@ -31,6 +37,9 @@ void workers_stop(void) {
void workers_destroy(void) { void workers_destroy(void) {
mpmc_destroy(&tcp_acceptor_ctx); mpmc_destroy(&tcp_acceptor_ctx);
mpmc_destroy(&local_handler_ctx);
mpmc_destroy(&fastcgi_handler_cxt);
mpmc_destroy(&proxy_handler_ctx);
mpmc_destroy(&request_handler_ctx); mpmc_destroy(&request_handler_ctx);
mpmc_destroy(&responder_ctx); mpmc_destroy(&responder_ctx);
mpmc_destroy(&tcp_closer_ctx); mpmc_destroy(&tcp_closer_ctx);
@ -52,7 +61,15 @@ int respond(client_ctx_t *ctx) {
return mpmc_queue(&responder_ctx, ctx); return mpmc_queue(&responder_ctx, ctx);
} }
int local_handle(client_ctx_t *ctx) {
return mpmc_queue(&local_handler_ctx, ctx);
}
int fastcgi_handle(client_ctx_t *ctx) { int fastcgi_handle(client_ctx_t *ctx) {
return mpmc_queue(&fastcgi_handler_cxt, ctx); return mpmc_queue(&fastcgi_handler_cxt, ctx);
} }
int proxy_handle(client_ctx_t *ctx) {
return mpmc_queue(&proxy_handler_ctx, ctx);
}

View File

@ -25,6 +25,10 @@ int handle_request(client_ctx_t *ctx);
int respond(client_ctx_t *ctx); int respond(client_ctx_t *ctx);
int local_handle(client_ctx_t *ctx);
int fastcgi_handle(client_ctx_t *ctx); int fastcgi_handle(client_ctx_t *ctx);
int proxy_handle(client_ctx_t *ctx);
#endif //SESIMOS_WORKERS_H #endif //SESIMOS_WORKERS_H