Refactor parser

FossilOrigin-Name: f3cd880740b95d2fbe3b61e4f8b9cdfd503f62720da1755ba14d387b95e0938c
This commit is contained in:
nekobit 2022-06-29 03:37:38 +00:00
parent c3e8af299f
commit 1cec6bb4b5
9 changed files with 57 additions and 75 deletions

View file

@ -73,6 +73,7 @@ int main(void)
{ "/@:/media", content_account_media },
{ "/@:/following", content_account_following },
{ "/@:/followers", content_account_followers },
{ "/@:/statuses", content_account_statuses },
{ "/@:", content_account_statuses },
{ "/status/:/react/:", content_status_react },
{ "/status/:/react", status_emoji },

View file

@ -38,84 +38,58 @@ int parse_path(struct session* ssn,
enum path_state state = PARSE_NEUTRAL;
char* p = path_info->path + 1;
char* p2 = getenv("PATH_INFO") + 1;
size_t p2_len = strlen(getenv("PATH_INFO")) - 1;
// Stored into data
int str_size = 0;
char* tmp = NULL;
char** data = NULL;
size_t size = 0;
char* after_str = 0;
size_t read_len;
for (int i = 0, j = 0;;)
{
for (int i = 0, j = 0; p[j] || p2[i]; (++i, ++j))
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;
}
else if (state == PARSE_NEUTRAL && fin != 1) {
if (p[j] == p2[i])
break;
else {
fail = 1;
goto breakpt;
}
}
if (fin) goto breakpt;
break;
case ':':
state = PARSE_READ;
/* Abort early */
if (p2[j] == '\0')
// TODO null check
if (p[j+1] == '\0')
{
after_str = strchr(p2 + i, '/');
if (!after_str)
after_str = p2+i + strlen(p2+i);
fin = 1;
}
else
after_str = strstr(p2 + i, "/");
if (!after_str)
{
fail = 1;
goto breakpt;
}
// fall
read_len = (size_t)after_str - (size_t)(p2 + j);
// Copy in new data from the string we just read
tmp = malloc(read_len+1);
strncpy(tmp, after_str - read_len, read_len);
tmp[read_len] = '\0';
// Add our new string
data = realloc(data, ++size * sizeof(tmp));
data[size-1] = tmp;
// Move ahead (-1 because we move again)
i += read_len - 1;
if (fin) goto breakpt;
break;
default:
if (state == PARSE_NEUTRAL)
if (p2[i] == '/' && p[j] == '\0') goto breakpt;
if (p[j] != p2[i])
{
if (p[j] == p2[i])
break;
else {
fail = 1;
goto breakpt;
}
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)
{
@ -127,11 +101,11 @@ breakpt:
}
// Cleanup
/* for (size_t i = 0; i < size; ++i) */
/* { */
/* free(data[i]); */
/* } */
/* if (data) free(data); */
for (size_t i = 0; i < size; ++i)
{
free(data[i]);
}
if (data) free(data);
return res;
}

View file

@ -26,7 +26,7 @@
char* read_get_data(struct get_values* query)
{
struct http_query_info info;
struct http_query_info info = { 0 };
char* query_string = getenv("QUERY_STRING");
char* get_query = NULL, *g_query_read;
@ -189,7 +189,7 @@ char* read_post_data(struct post_values* post)
char* parse_query(char* begin, struct http_query_info* info)
{
int end = 0;
char* val_begin;
char* val_begin = NULL;
info->key = begin;
for (; *begin != '&' && *begin != '\0'; ++begin)
{

View file

@ -505,6 +505,7 @@ char* make_mentions_local(char* content)
char* url_format;
int error;
PCRE2_SIZE erroffset;
int substitute_success = 0;
// Initial size, will be increased by 30% if pcre2_substitute cannot fit into the size
// ...why can't pcre2 just allocate a string with the size for us? Thanks...
size_t res_len = 1024;
@ -555,11 +556,16 @@ char* make_mentions_local(char* content)
goto out;
}
}
else
substitute_success = 1;
}
out:
if (!substitute_success)
free(res);
free(url_format);
return res && rc ? res : content;
pcre2_code_free(re);
return substitute_success ? res : content;
}
char* greentextify(char* content)

View file

@ -112,6 +112,7 @@ void content_timeline(struct session* ssn,
mstdnt_cleanup_statuses(statuses, statuses_len);
free(status_format);
free(post_box);
free(timeline_options);
free(navigation_box);
free(output);
}

View file

@ -3,18 +3,15 @@
<tr>
<td>
<a target="_parent" href="{{%s:prefix}}/status/{{%s:status_id}}/reply#{{%s:status_id}}" class="pointer statbtn reply-btn">
{{ %s : reply_btn }}
{{ %s : reply_btn }}
<span class="count">{{%s:reply_count}}</span>
</label>
</form>
</a>
</td>
<td>
<form action="{{%s:prefix}}/status/{{%s:status_id}}/interact" method="post">
<input class="itype" type="hidden" name="itype" value="{{%s:unrepeat}}repeat">
<label class="repeat-btn pointer statbtn">
{{ %s : repeat_btn }}
<span class="count">{{%s:repeats_count}}</span>
<input class="hidden" type="submit" value="{{%s:repeat_text}}">
</label>

View file

@ -1 +1,2 @@
<svg class="like {{%s:favourite_active}}" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon></svg>
<svg class="like {{%s:favourite_active}}" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon></svg>

Before

Width:  |  Height:  |  Size: 355 B

After

Width:  |  Height:  |  Size: 344 B

View file

@ -1 +1,2 @@
<svg class="repeat {{%s:repeat_active}}" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2.1l4 4-4 4"/><path d="M3 12.2v-2a4 4 0 0 1 4-4h12.8M7 21.9l-4-4 4-4"/><path d="M21 11.8v2a4 4 0 0 1-4 4H4.2"/></svg>
<svg class="repeat {{%s:repeat_active}}" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2.1l4 4-4 4"/><path d="M3 12.2v-2a4 4 0 0 1 4-4h12.8M7 21.9l-4-4 4-4"/><path d="M21 11.8v2a4 4 0 0 1-4 4H4.2"/></svg>

Before

Width:  |  Height:  |  Size: 342 B

After

Width:  |  Height:  |  Size: 344 B

View file

@ -1 +1,2 @@
<svg class="reply" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 9l6 6-6 6"/><path d="M4 4v7a4 4 0 0 0 4 4h11"/></svg>
<svg class="reply" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 9l6 6-6 6"/><path d="M4 4v7a4 4 0 0 0 4 4h11"/></svg>

Before

Width:  |  Height:  |  Size: 256 B

After

Width:  |  Height:  |  Size: 258 B