More single threaded macros

FossilOrigin-Name: 78ff7ba472d155fba3fe238e59193053b96dd2792b1b6ff7012ffe15cd4aec0e
This commit is contained in:
nekobit 2022-08-24 01:49:02 +00:00
parent 67a2f90c8b
commit 8d9d537030
10 changed files with 47 additions and 41 deletions

View file

@ -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

View file

@ -27,15 +27,14 @@
#define REDIR_HTML_END "</body>" \
"</html>"
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 <a href=\"\">%s</a>..." REDIR_HTML_END,
status,
loc_str,
loc_str);
FPRINTF("Status: %s\r\n"
"Location: %s\r\n\r\n"
REDIR_HTML_BEGIN "Redirecting to <a href=\"\">%s</a>..." REDIR_HTML_END,
status,
loc_str,
loc_str);
}

View file

@ -18,11 +18,12 @@
#ifndef HTTP_H
#define HTTP_H
#include "request.h"
#include <fcgi_stdio.h>
#include <fcgiapp.h>
#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

View file

@ -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

View file

@ -28,14 +28,15 @@
#include "http.h"
#include <curl/curl.h>
#include <fcgi_stdio.h>
#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);

View file

@ -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,

View file

@ -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,

View file

@ -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';

View file

@ -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

View file

@ -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)