treebird/src/path.c
me@ow.nekobit.net 81c12a35e6 Fix broken path parsing
FossilOrigin-Name: 5040e8d660047072b4b829e4da564e744b8c44c0233ef85ee5a6c7c82e1a2918
2022-02-24 02:58:47 +00:00

132 lines
3.3 KiB
C

/*
* RatFE - 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 <string.h>
#include <stdlib.h>
#include "path.h"
#include "index.h"
#include "account.h"
enum path_state
{
PARSE_NEUTRAL,
PARSE_READ,
};
int parse_path(mastodont_t* api, struct path_info* path_info)
{
int fail = 0, fin = 0;
enum path_state state = PARSE_NEUTRAL;
char* p = path_info->path + 1;
char* p2 = getenv("PATH_INFO") + 1;
// Stored into data
int str_size = 0;
char* tmp = NULL;
char** data = NULL;
size_t size = 0;
for (int i = 0, j = 0;;)
{
switch (p[j])
{
case '\0':
fin = 1;
// fall
case '/':
if (state == PARSE_READ)
{
state = PARSE_NEUTRAL;
// Set value and move on
data = realloc(data, ++size * sizeof(tmp));
data[size-1] = tmp;
tmp = NULL;
str_size = 0;
}
if (fin) goto breakpt;
break;
case ':':
state = PARSE_READ;
// fall
default:
if (state == PARSE_NEUTRAL)
{
if (p[j] == p2[i])
break;
else {
fail = 1;
goto breakpt;
}
}
else {
// Don't realloc, we already have a space for our final character
if (p2[i] == '\0' || p2[i] == '/')
{
tmp[str_size] = '\0';
++j;
// Move --i back one to counter the upcoming ++i
// If we don't, then we are one step too far
--i;
}
else {
tmp = realloc(tmp, ++str_size + 1);
tmp[str_size-1] = p2[i];
}
}
break;
}
if (state == PARSE_NEUTRAL) ++j; // Used for p
++i; // Used for p2
}
breakpt:
if (fail)
return 1;
path_info->callback(api, data, size);
// Cleanup
for (size_t i = 0; i < size; ++i)
{
free(data[i]);
}
if (data) free(data);
return 0;
}
void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len)
{
char* path = getenv("PATH_INFO");
// "default" path
if (path == NULL || (path && strcmp(path, "/") == 0))
{
content_index(api);
}
else { // Generic path
for (size_t i = 0; i < paths_len; ++i)
{
if (parse_path(api, paths + i) == 0)
return;
}
}
}