diff --git a/src/base_page.c b/src/base_page.c index 604913f..b2e2c56 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -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 = "Login / Register"; - /* - * 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); diff --git a/src/cookie.c b/src/cookie.c index 6700130..64a9a00 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -19,6 +19,7 @@ #include #include #include "cookie.h" +#include 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; diff --git a/src/cookie.h b/src/cookie.h index 4ef980b..b9515b3 100644 --- a/src/cookie.h +++ b/src/cookie.h @@ -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 diff --git a/src/main.c b/src/main.c index 0e0c081..e726275 100644 --- a/src/main.c +++ b/src/main.c @@ -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(); }