Async stuff

FossilOrigin-Name: 65fba45d6d6e5cb9318b881b0b137be84030afe64200f793fb7f5cf55d41b48c
This commit is contained in:
nekobit 2022-10-31 18:08:54 +00:00
parent 8c34f90655
commit bf3af231c8
7 changed files with 116 additions and 27 deletions

View File

@ -133,7 +133,7 @@ static int application(mastodont_t* api, REQUEST_T req)
propagate_memory();
// Default config
struct session ssn = {
static struct session ssn_def = {
.config = {
.theme = "treebird20",
.lang = L10N_EN_US,
@ -160,34 +160,28 @@ static int application(mastodont_t* api, REQUEST_T req)
.acct_storage = { 0 },
.logged_in = 0,
};
// Make our own from copy
struct session* ssn = malloc(sizeof(struct session));
memcpy(ssn, &ssn_def, sizeof(struct session));
// Load cookies
char* cookies_str = read_cookies_env(req, &(ssn.cookies));
char* post_str = read_post_data(req, &(ssn.post));
char* get_str = read_get_data(req, &(ssn.query));
ssn->cookies_str = read_cookies_env(req, &(ssn->cookies));
ssn->post_str = read_post_data(req, &(ssn->post));
ssn->get_str = read_get_data(req, &(ssn->query));
// Read config options
enum config_page page = CONFIG_GENERAL;
char* path_info = GET_ENV("PATH_INFO", req);
if (path_info && strcmp(path_info, "/config/appearance") == 0)
page = CONFIG_APPEARANCE;
struct mstdnt_storage* attachments = load_config(req, &ssn, api, page);
struct mstdnt_storage* attachments = load_config(req, ssn, api, page);
// Load current account information
get_account_info(api, &ssn);
get_account_info(api, ssn);
rc = handle_paths(req, &ssn, api, paths, sizeof(paths)/sizeof(paths[0]));
rc = handle_paths(req, ssn, api, paths, sizeof(paths)/sizeof(paths[0]));
// Cleanup
if (cookies_str) tb_free(cookies_str);
if (post_str) tb_free(post_str);
if (get_str) tb_free(get_str);
free_files(&(keyfile(ssn.post.files)));
if (ssn.logged_in) mstdnt_cleanup_account(&(ssn.acct));
mstdnt_storage_cleanup(&(ssn.acct_storage));
if (attachments)
cleanup_media_storages(&ssn, attachments);
return rc;
}
@ -195,30 +189,32 @@ static int application(mastodont_t* api, REQUEST_T req)
static void fcgi_start(mastodont_t* api)
{
int rc;
FCGX_Request req;
FCGX_Request* req;
while (1)
{
FCGX_InitRequest(&req, 0, 0);
req = malloc(sizeof(FCGX_Request));
FCGX_InitRequest(req, 0, 0);
struct mstdnt_fd fds[] = {
{
// The docs says not to use this directly, but we don't care
// what the docs say
.fd = req.listen_sock,
.fd = req->listen_sock,
.events = MSTDNT_POLLIN,
}
};
// Will poll until we get a request
mstdnt_await(api, 0, fds, sizeof(fds)/sizeof(fds[0]));
mstdnt_poll(api, 0, fds, sizeof(fds)/sizeof(fds[0]));
rc = FCGX_Accept_r(&req);
rc = FCGX_Accept_r(req);
if (rc < 0) break;
rc = application(api, &req);
rc = application(api, req);
if (rc != 1)
FCGX_Finish_r(&req);
FCGX_Finish_r(req);
}
}
#else

View File

@ -18,4 +18,3 @@
#include "request.h"
pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;

View File

@ -50,3 +50,14 @@ HV* perlify_session(struct session* ssn)
return ssn_hv;
}
void session_cleanup(struct session* ssn)
{
tb_free(ssn->cookies_str);
tb_free(ssn->post_str);
tb_free(ssn->get_str);
free_files(&(keyfile(ssn->post.files)));
if (ssn->logged_in) mstdnt_cleanup_account(&(ssn->acct));
mstdnt_storage_cleanup(&(ssn->acct_storage));
if (attachments)
cleanup_media_storages(ssn, attachments);
}

View File

@ -33,10 +33,16 @@ struct session
int logged_in;
struct mstdnt_account acct;
struct mstdnt_storage acct_storage;
char* cookies_str;
char* post_str;
char* get_str;
};
const char* const get_instance(struct session* ssn);
const char* const get_token(struct session* ssn);
HV* perlify_session(struct session* ssn);
void session_cleanup(struct session* ssn);
#endif // SESSION_H

View File

@ -29,6 +29,7 @@
#include "query.h"
#include "error.h"
#include "string_helpers.h"
#include "types.h"
void content_timeline(REQUEST_T req,
struct session* ssn,
@ -103,6 +104,7 @@ int tl_home(REQUEST_T req, struct session* ssn, mastodont_t* api, int local)
};
try_post_status(ssn, api);
mstdnt_timeline_home(api, &m_args, NULL, NULL, &args, &storage, &statuses, &statuses_len);
@ -140,7 +142,6 @@ int tl_direct(REQUEST_T req, struct session* ssn, mastodont_t* api)
static void request_cb_tl_public(mstdnt_request_cb_data_t data, void* args)
{
free(-123);
}
int tl_public(REQUEST_T req, struct session* ssn, mastodont_t* api, int local, enum base_category cat)
@ -168,6 +169,7 @@ int tl_public(REQUEST_T req, struct session* ssn, mastodont_t* api, int local, e
try_post_status(ssn, api);
struct request_args cb_args
mstdnt_timeline_public(api, &m_args, request_cb_tl_public, ssn, &args, &storage, &statuses, &statuses_len);
return 1;

40
src/types.c Normal file
View File

@ -0,0 +1,40 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "types.h"
#include <fcgiapp.h>
struct request_args*
request_args_create(REQUEST_T req, struct session* ssn)
{
struct request_args* args = malloc(sizeof(struct request_args));
if (!args)
perror("request_args_create: malloc");
args->req = req;
args->ssn = ssn;
return args;
}
void request_args_cleanup(struct request_args* args)
{
FCGX_Finish_r(args->req);
free(args->req);
free(args->ssn);
}

35
src/types.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TB_TYPES_H
#define TB_TYPES_H
#include "types.h"
#include "session.h"
struct request_args
{
REQUEST_T req;
struct session* ssn;
};
struct request_args*
request_args_create(REQUEST_T req, struct session* ssn);
void request_args_cleanup(struct request_args* req);
#endif // TB_TYPES_H