From f24191362082cfdbdea6e885a44cde45cc7175ad Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Thu, 29 Dec 2022 17:03:50 +0100
Subject: [PATCH] rename fastcgi_conn to fastcgi_cnx_t

---
 src/lib/fastcgi.c            | 16 ++++++++--------
 src/lib/fastcgi.h            | 18 +++++++++---------
 src/worker/fastcgi_handler.c | 37 ++++++++++++++++++++++++++++++++++++
 src/worker/fastcgi_handler.h | 22 +++++++++++++++++++++
 src/worker/request_handler.c | 22 ++++++++++-----------
 5 files changed, 87 insertions(+), 28 deletions(-)
 create mode 100644 src/worker/fastcgi_handler.c
 create mode 100644 src/worker/fastcgi_handler.h

diff --git a/src/lib/fastcgi.c b/src/lib/fastcgi.c
index 918fe38..46c2e8d 100644
--- a/src/lib/fastcgi.c
+++ b/src/lib/fastcgi.c
@@ -53,7 +53,7 @@ char *fastcgi_add_param(char *buf, const char *key, const char *value) {
     return ptr;
 }
 
-int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client, const http_req *req, const http_uri *uri) {
+int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client, const http_req *req, const http_uri *uri) {
     unsigned short req_id = (client_num & 0xFFF) << 4;
     if (client_num == 0) {
         req_id |= (req_num + 1) & 0xF;
@@ -201,7 +201,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
     return 0;
 }
 
-int fastcgi_close_stdin(fastcgi_conn *conn) {
+int fastcgi_close_stdin(fastcgi_cnx_t *conn) {
     FCGI_Header header = {
             .version = FCGI_VERSION_1,
             .type = FCGI_STDIN,
@@ -221,7 +221,7 @@ int fastcgi_close_stdin(fastcgi_conn *conn) {
     return 0;
 }
 
-int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, char *err_msg) {
+int fastcgi_php_error(const fastcgi_cnx_t *conn, const char *msg, int msg_len, char *err_msg) {
     char *msg_str = malloc(msg_len + 1);
     char *ptr0 = msg_str;
     memcpy(msg_str, msg, msg_len);
@@ -288,7 +288,7 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
     return err;
 }
 
-int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
+int fastcgi_header(fastcgi_cnx_t *conn, http_res *res, char *err_msg) {
     FCGI_Header header;
     char *content;
     unsigned short content_len, req_id;
@@ -399,7 +399,7 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
     return 0;
 }
 
-int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
+int fastcgi_send(fastcgi_cnx_t *conn, sock *client, int flags) {
     FCGI_Header header;
     long ret;
     char buf0[256];
@@ -517,7 +517,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
     }
 }
 
-int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
+int fastcgi_dump(fastcgi_cnx_t *conn, char *buf, long len) {
     FCGI_Header header;
     long ret;
     char buf0[256];
@@ -582,7 +582,7 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
     }
 }
 
-int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
+int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
     unsigned long rcv_len = 0;
     char *buf[16384];
     long ret;
@@ -617,7 +617,7 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
     return 0;
 }
 
-int fastcgi_receive_chunked(fastcgi_conn *conn, sock *client) {
+int fastcgi_receive_chunked(fastcgi_cnx_t *conn, sock *client) {
     long ret;
     unsigned long next_len;
 
diff --git a/src/lib/fastcgi.h b/src/lib/fastcgi.h
index 6b4fd84..592e10f 100644
--- a/src/lib/fastcgi.h
+++ b/src/lib/fastcgi.h
@@ -38,25 +38,25 @@ typedef struct {
     unsigned short out_len;
     unsigned short out_off;
     client_ctx_t *ctx;
-} fastcgi_conn;
+} fastcgi_cnx_t;
 
 char *fastcgi_add_param(char *buf, const char *key, const char *value);
 
-int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client,
+int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client,
                  const http_req *req, const http_uri *uri);
 
-int fastcgi_close_stdin(fastcgi_conn *conn);
+int fastcgi_close_stdin(fastcgi_cnx_t *conn);
 
-int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, char *err_msg);
+int fastcgi_php_error(const fastcgi_cnx_t *conn, const char *msg, int msg_len, char *err_msg);
 
-int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg);
+int fastcgi_header(fastcgi_cnx_t *conn, http_res *res, char *err_msg);
 
-int fastcgi_send(fastcgi_conn *conn, sock *client, int flags);
+int fastcgi_send(fastcgi_cnx_t *conn, sock *client, int flags);
 
-int fastcgi_dump(fastcgi_conn *conn, char *buf, long len);
+int fastcgi_dump(fastcgi_cnx_t *conn, char *buf, long len);
 
-int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len);
+int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len);
 
-int fastcgi_receive_chunked(fastcgi_conn *conn, sock *client);
+int fastcgi_receive_chunked(fastcgi_cnx_t *conn, sock *client);
 
 #endif //SESIMOS_FASTCGI_H
diff --git a/src/worker/fastcgi_handler.c b/src/worker/fastcgi_handler.c
new file mode 100644
index 0000000..20c43a2
--- /dev/null
+++ b/src/worker/fastcgi_handler.c
@@ -0,0 +1,37 @@
+/**
+ * sesimos - secure, simple, modern web server
+ * @brief TCP closer
+ * @file src/worker/fastcgi_handler.c
+ * @author Lorenz Stechauner
+ * @date 2022-12-28
+ */
+
+#include "fastcgi_handler.h"
+#include "../logger.h"
+#include "../lib/mpmc.h"
+#include "../lib/utils.h"
+
+static mpmc_t mpmc_ctx;
+
+static void fastcgi_handler_func(client_ctx_t *ctx);
+
+int fastcgi_handler_init(int n_workers, int buf_size) {
+    return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) fastcgi_handler_func, "fcgi");
+}
+
+int fastcgi_handle(client_ctx_t *ctx) {
+    return mpmc_queue(&mpmc_ctx, ctx);
+}
+
+void fastcgi_handler_stop(void) {
+    mpmc_stop(&mpmc_ctx);
+}
+
+void fastcgi_handler_destroy(void) {
+    mpmc_destroy(&mpmc_ctx);
+}
+
+static 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);
+    // TODO
+}
diff --git a/src/worker/fastcgi_handler.h b/src/worker/fastcgi_handler.h
new file mode 100644
index 0000000..bcbe14d
--- /dev/null
+++ b/src/worker/fastcgi_handler.h
@@ -0,0 +1,22 @@
+/**
+ * sesimos - secure, simple, modern web server
+ * @brief TCP closer (header file)
+ * @file src/worker/fastcgi_handler.h
+ * @author Lorenz Stechauner
+ * @date 2022-12-29
+ */
+
+#ifndef SESIMOS_FASTCGI_HANDLER_H
+#define SESIMOS_FASTCGI_HANDLER_H
+
+#include "../server.h"
+
+int fastcgi_handler_init(int n_workers, int buf_size);
+
+int fastcgi_handle(client_ctx_t *ctx);
+
+void fastcgi_handler_stop(void);
+
+void fastcgi_handler_destroy(void);
+
+#endif //SESIMOS_FASTCGI_HANDLER_H
diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c
index a35b0bd..be96886 100644
--- a/src/worker/request_handler.c
+++ b/src/worker/request_handler.c
@@ -87,7 +87,7 @@ static void request_handler(client_ctx_t *cctx) {
     int use_proxy = 0;
     int p_len;
 
-    fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0, .ctx = cctx};
+    fastcgi_cnx_t fcgi_cnx = {.socket = 0, .req_id = 0, .ctx = cctx};
     http_status custom_status;
 
     http_res res = {.version = "1.1", .status = http_get_status(501), .hdr.field_num = 0, .hdr.last_field_num = -1};
@@ -388,7 +388,7 @@ static void request_handler(client_ctx_t *cctx) {
             http_add_header_field(&res.hdr, "Last-Modified", last_modified);
 
             res.status = http_get_status(200);
-            if (fastcgi_init(&fcgi_conn, mode, 0 /* TODO */, cctx->req_num, client, &req, &uri) != 0) {
+            if (fastcgi_init(&fcgi_cnx, mode, 0 /* TODO */, cctx->req_num, client, &req, &uri) != 0) {
                 res.status = http_get_status(503);
                 sprintf(err_msg, "Unable to communicate with FastCGI socket.");
                 goto respond;
@@ -398,9 +398,9 @@ static void request_handler(client_ctx_t *cctx) {
             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_conn, client, client_content_len);
+                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_conn, client);
+                ret = fastcgi_receive_chunked(&fcgi_cnx, client);
             } else {
                 ret = 0;
             }
@@ -413,9 +413,9 @@ static void request_handler(client_ctx_t *cctx) {
                 res.status = http_get_status(502);
                 goto respond;
             }
-            fastcgi_close_stdin(&fcgi_conn);
+            fastcgi_close_stdin(&fcgi_cnx);
 
-            ret = fastcgi_header(&fcgi_conn, &res, err_msg);
+            ret = fastcgi_header(&fcgi_cnx, &res, err_msg);
             if (ret != 0) {
                 if (ret < 0) goto abort;
                 goto respond;
@@ -449,7 +449,7 @@ static void request_handler(client_ctx_t *cctx) {
                 content_length != -1 &&
                 content_length <= sizeof(msg_content) - 1)
             {
-                fastcgi_dump(&fcgi_conn, msg_content, sizeof(msg_content));
+                fastcgi_dump(&fcgi_cnx, msg_content, sizeof(msg_content));
                 goto respond;
             }
 
@@ -685,7 +685,7 @@ static void request_handler(client_ctx_t *cctx) {
             int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL);
 
             int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
-            ret = fastcgi_send(&fcgi_conn, client, flags);
+            ret = fastcgi_send(&fcgi_cnx, client, flags);
         } else if (use_proxy) {
             const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
             int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL;
@@ -716,9 +716,9 @@ static void request_handler(client_ctx_t *cctx) {
 
     uri_free(&uri);
     abort:
-    if (fcgi_conn.socket != 0) {
-        close(fcgi_conn.socket);
-        fcgi_conn.socket = 0;
+    if (fcgi_cnx.socket != 0) {
+        close(fcgi_cnx.socket);
+        fcgi_cnx.socket = 0;
     }
     http_free_req(&req);
     http_free_res(&res);