Global cookies

FossilOrigin-Name: 4e93c56d4bbe301f634eb42dc0d4976faba0100cba2c8e1a1822249e197b27c8
This commit is contained in:
me@ow.nekobit.net 2022-02-20 05:59:55 +00:00
parent 762ad3dc00
commit 4877ef15ae
4 changed files with 61 additions and 27 deletions

View file

@ -31,34 +31,16 @@ void render_base_page(struct base_page* page)
{
char* cookie = getenv("HTTP_COOKIE");
enum l10n_locale locale = page->locale;
char* cookie_read = cookie;
struct http_cookie_info info = { 0 };
char* login_string = "<a href=\"login\" id=\"login-header\">Login / Register</a>";
/*
* Since getenv() returns a pointer to the env variables,
* we're going to overwrite that data. It saves us some copying
* time, since this is /very likely/ the last time we will ever
* read HTTP_COOKIE
*/
if (!g_config.changed && cookie)
while (1)
{
cookie_read = parse_cookies(cookie_read, &info);
{
if (!(info.key && info.val)) break;
if (strcmp(info.key, "theme") == 0)
{
g_config.theme = info.val;
}
else if (strcmp(info.key, "logged_in") == 0)
{
if (strcmp(info.val, "t") == 0)
login_string = "";
}
if (!cookie_read) break;
}
if (cookies.theme)
g_config.theme = cookies.theme;
if (cookies.logged_in && strcmp(cookies.logged_in, "t") == 0)
login_string = "";
}
char* data;
int len = easprintf(&data, data_index_html,
@ -83,7 +65,7 @@ void render_base_page(struct base_page* page)
return;
}
printf("Content-Length: %d\r\n\r\n", len + 1);
printf("Content-Length: %d\r\n\r\n%s", len + 1, data);
puts(data);
free(data);

View file

@ -19,6 +19,7 @@
#include <string.h>
#include <stdio.h>
#include "cookie.h"
#include <stdlib.h>
enum cookie_state
{
@ -28,8 +29,56 @@ enum cookie_state
STATE_V_START,
};
struct cookie_value_refs
{
char* key;
char** val;
};
struct cookie_values cookies = { 0 };
char* read_cookies_env()
{
struct http_cookie_info info;
char* cookies_env = getenv("HTTP_COOKIE");
// Is it even work bothering with?
if (!cookies_env)
return NULL;
char* cookies_str = malloc(strlen(cookies_env));
if (!cookies_str)
{
perror("malloc");
return NULL;
}
strcpy(cookies_str, cookies_env);
char* cookies_read = cookies_str;
// Will loop through these
struct cookie_value_refs refs[] = {
{ "access_token", &(cookies.access_token) },
{ "logged_in", &(cookies.logged_in) },
{ "theme", &(cookies.theme) }
};
do
{
cookies_read = parse_cookies(cookies_read, &info);
if (!(info.key && info.val)) break;
for (int i = 0; i < (sizeof(refs)/sizeof(refs[0])); ++i)
{
if (strcmp(info.key, refs[i].key) == 0)
*(refs[i].val) = info.val;
}
}
while (cookies_read);
// User is responsible for freeing when done!
return cookies_str;
}
char* parse_cookies(char* begin, struct http_cookie_info* info)
{
int keydone = 0;

View file

@ -38,6 +38,7 @@ extern struct cookie_values cookies;
// Stupidly fast simple cookie parser
char* parse_cookies(char* begin, struct http_cookie_info* info);
char* read_cookies_env();
int cookie_get_val(char* src, char* key, struct http_cookie_info* info);
#endif // COOKIE_H

View file

@ -65,8 +65,9 @@ int main(void)
api.url = config_instance_url;
mastodont_init(&api);
// Used if the user is authenticated
load_auth_token(&api);
// Load cookies
char* cookies_str = read_cookies_env();
api.token = cookies.access_token; // Load token now
// Config defaults
g_config.theme = "ratfe20";
@ -83,6 +84,7 @@ int main(void)
handle_paths(&api, paths, sizeof(paths)/sizeof(paths[0]));
// Cleanup
if (cookies_str) free(cookies_str);
mastodont_free(&api);
mastodont_global_curl_cleanup();
}