diff --git a/Makefile b/Makefile index c3b4497..499bf5d 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,10 @@ MASTODONT_URL = https://fossil.nekobit.net/mastodont-c # Not parallel friendly #all: $(MASTODONT_DIR) dep_build $(TARGET) +ifneq ($(strip $(SINGLE_THREADED)),) +CFLAGS += -DSINGLE_THREADED +endif + all: $(MAKE) dep_build $(MAKE) filec diff --git a/src/http.c b/src/http.c index 8ce5b10..4611e29 100644 --- a/src/http.c +++ b/src/http.c @@ -27,15 +27,14 @@ #define REDIR_HTML_END "" \ "" -void redirect(FCGX_Request* req, char* status, char* location) +void redirect(REQUEST_T req, char* status, char* location) { char* loc_str = location ? location : "/"; - FCGX_FPrintF(req->out, - "Status: %s\r\n" - "Location: %s\r\n\r\n" - REDIR_HTML_BEGIN "Redirecting to %s..." REDIR_HTML_END, - status, - loc_str, - loc_str); + FPRINTF("Status: %s\r\n" + "Location: %s\r\n\r\n" + REDIR_HTML_BEGIN "Redirecting to %s..." REDIR_HTML_END, + status, + loc_str, + loc_str); } diff --git a/src/http.h b/src/http.h index 403518c..ed6ec81 100644 --- a/src/http.h +++ b/src/http.h @@ -18,11 +18,12 @@ #ifndef HTTP_H #define HTTP_H +#include "request.h" #include #include #define REDIRECT_303 "303 See Other" -void redirect(FCGX_Request* req, char* status, char* location); +void redirect(REQUEST_T req, char* status, char* location); #endif // HTTP_H diff --git a/src/local_config_set.c b/src/local_config_set.c index b5eda48..81d5a32 100644 --- a/src/local_config_set.c +++ b/src/local_config_set.c @@ -34,9 +34,8 @@ void set_config_str(FCGX_Request* req, { if (ssn->post.set.is_set && post->is_set && page == curr_page) { - FCGX_FPrintF(req->out, - "Set-Cookie: %s=%s; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n", - cookie_name, keypstr(post)); + FPRINTF("Set-Cookie: %s=%s; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n", + cookie_name, keypstr(post)); } if ((ssn->post.set.is_set && post->is_set) || cookie->is_set) @@ -60,9 +59,8 @@ void set_config_int(FCGX_Request* req, { if (ssn->post.set.is_set && page == curr_page) { - FCGX_FPrintF(req->out, - "Set-Cookie: %s=%d; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n", - cookie_name, post_bool_intp(post)); + FPRINTF("Set-Cookie: %s=%d; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n", + cookie_name, post_bool_intp(post)); } // Checks if boolean option diff --git a/src/login.c b/src/login.c index 4c3f5fd..721296c 100644 --- a/src/login.c +++ b/src/login.c @@ -28,14 +28,15 @@ #include "http.h" #include #include +#include "request.h" #define LOGIN_SCOPE "read+write+follow+push" -void apply_access_token(FCGX_Request* req, char* token) +static void apply_access_token(REQUEST_T req, char* token) { - FCGX_FPrintF(req->out, "Set-Cookie: access_token=%s; Path=/; Max-Age=31536000\r\n", token); - FCGX_FPrintF(req->out, "Set-Cookie: logged_in=t; Path=/; Max-Age=31536000\r\n"); + FPRINTF("Set-Cookie: access_token=%s; Path=/; Max-Age=31536000\r\n", token); + PRINTF("Set-Cookie: logged_in=t; Path=/; Max-Age=31536000\r\n"); // if config_url_prefix is empty, make it root redirect(req, REDIRECT_303, config_url_prefix && config_url_prefix[0] != '\0' ? config_url_prefix : "/"); @@ -100,9 +101,9 @@ void content_login_oauth(PATH_ARGS) decode_url, encode_id, urlify_redirect_url); // Set cookie and redirect - FCGX_FPrintF(req->out, "Set-Cookie: instance_url=%s; Path=/; Max-Age=3153600\r\n", decode_url); - FCGX_FPrintF(req->out, "Set-Cookie: client_id=%s; Path=/; Max-Age=3153600\r\n", app.client_id); - FCGX_FPrintF(req->out, "Set-Cookie: client_secret=%s; Path=/; Max-Age=3153600\r\n", app.client_secret); + FPRINTF("Set-Cookie: instance_url=%s; Path=/; Max-Age=3153600\r\n", decode_url); + FPRINTF("Set-Cookie: client_id=%s; Path=/; Max-Age=3153600\r\n", app.client_id); + FPRINTF("Set-Cookie: client_secret=%s; Path=/; Max-Age=3153600\r\n", app.client_secret); redirect(req, REDIRECT_303, url); free(url); @@ -185,10 +186,10 @@ void content_login(PATH_ARGS) } else { if (url_link) - FCGX_FPrintF(req->out, "Set-Cookie: instance_url=%s; Path=/; Max-Age=31536000\r\n", url_link); + FPRINTF("Set-Cookie: instance_url=%s; Path=/; Max-Age=31536000\r\n", url_link); else // Clear - FCGX_FPrintF(req->out, "Set-Cookie: instance_url=; Path=/; Max-Age=-1\r\n"); + PRINTF("Set-Cookie: instance_url=; Path=/; Max-Age=-1\r\n"); apply_access_token(req, token.access_token); free(url_link); diff --git a/src/path.c b/src/path.c index 0b2daec..8fa63e4 100644 --- a/src/path.c +++ b/src/path.c @@ -23,7 +23,7 @@ #include "account.h" #include "error.h" -int parse_path(FCGX_Request* req, +int parse_path(REQUEST_T req, struct session* ssn, mastodont_t* api, struct path_info* path_info) @@ -101,7 +101,7 @@ breakpt: return res; } -void handle_paths(FCGX_Request* req, +void handle_paths(REQUEST_T req, struct session* ssn, mastodont_t* api, struct path_info* paths, diff --git a/src/path.h b/src/path.h index 2b0aceb..73856fe 100644 --- a/src/path.h +++ b/src/path.h @@ -35,12 +35,7 @@ struct path_info }; void handle_paths( -#ifdef SINGLE_THREADED - void* -#else - FCGX_Request* -#endif - req, + REQUEST_T req, struct session* ssn, mastodont_t* api, struct path_info* paths, diff --git a/src/query.c b/src/query.c index 5755a17..2978f23 100644 --- a/src/query.c +++ b/src/query.c @@ -143,7 +143,11 @@ char* read_post_data(REQUEST_T req, struct post_values* post) } // fread should be a macro to FCGI_fread, which is set by FCGI_Accept in previous definitions +#ifndef SINGLE_THREADED size_t len = FCGX_GetStr(post_query, content_length, req->in); +#else + size_t len = fread(post_query, 1, content_length, stdin); +#endif post_query[content_length] = '\0'; // For shifting through @@ -229,10 +233,12 @@ char* try_handle_post(REQUEST_T req, void (*call)(struct http_query_info*, void* return NULL; } #ifdef SINGLE_THREADED - read(STDIN_FILENO, post_query, content_length); + int size = read(STDIN_FILENO, post_query, content_length); #else - FCGX_GetStr(post_query, content_length, req->in); + int size = FCGX_GetStr(post_query, content_length, req->in); #endif + if (size != content_length) + return NULL; post_query[content_length] = '\0'; diff --git a/src/request.h b/src/request.h index 22b5ee5..936086a 100644 --- a/src/request.h +++ b/src/request.h @@ -20,8 +20,16 @@ #define REQUEST_H #ifdef SINGLE_THREADED +#else +#endif + +#ifdef SINGLE_THREADED +#define FPRINTF(str, ...) fprintf(str, __VA_ARGS__) +#define PRINTF(str) printf(str) #define REQUEST_T void* #else +#define FPRINTF(str, ...) FCGX_FPrintF(req->out, str, __VA_ARGS__) +#define PRINTF(str) FCGX_PrintF(req->out, str) #define REQUEST_T FCGX_Request* #endif diff --git a/src/status.c b/src/status.c index 60a9fb5..e092e38 100644 --- a/src/status.c +++ b/src/status.c @@ -221,14 +221,8 @@ void status_interact(PATH_ARGS) char* referer = GET_ENV("HTTP_REFERER", req); try_interact_status(ssn, api, data[0]); - - FCGX_FPrintF(req->out, - "Status: 303 See Other\r\n" - "Location: %s#id-%s\r\n" - "Content-Length: 14\r\n\r\n" - "Redirecting...", - referer ? referer : "/", - data[0]); + + redirect(req, REDIRECT_303, referer); } void api_status_interact(PATH_ARGS)