From 1cec6bb4b5b558a78df02bb68a75fdaa1b936746 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 29 Jun 2022 03:37:38 +0000 Subject: [PATCH] Refactor parser FossilOrigin-Name: f3cd880740b95d2fbe3b61e4f8b9cdfd503f62720da1755ba14d387b95e0938c --- src/main.c | 1 + src/path.c | 102 ++++++++++++-------------------- src/query.c | 4 +- src/status.c | 8 ++- src/timeline.c | 1 + static/interaction_buttons.tmpl | 7 +-- static/like_btn.tmpl | 3 +- static/repeat_btn.tmpl | 3 +- static/reply_btn.tmpl | 3 +- 9 files changed, 57 insertions(+), 75 deletions(-) diff --git a/src/main.c b/src/main.c index fc5cc79..4bc3d95 100644 --- a/src/main.c +++ b/src/main.c @@ -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 }, diff --git a/src/path.c b/src/path.c index ed4af0c..aef0f53 100644 --- a/src/path.c +++ b/src/path.c @@ -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; } diff --git a/src/query.c b/src/query.c index 501344c..af3d8a8 100644 --- a/src/query.c +++ b/src/query.c @@ -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) { diff --git a/src/status.c b/src/status.c index 06b4eaa..eb1674b 100644 --- a/src/status.c +++ b/src/status.c @@ -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) diff --git a/src/timeline.c b/src/timeline.c index b226628..7f5c3d1 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -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); } diff --git a/static/interaction_buttons.tmpl b/static/interaction_buttons.tmpl index 097a861..d23f257 100644 --- a/static/interaction_buttons.tmpl +++ b/static/interaction_buttons.tmpl @@ -3,18 +3,15 @@ - {{ %s : reply_btn }} - + {{ %s : reply_btn }} {{%s:reply_count}} - - +
diff --git a/static/like_btn.tmpl b/static/like_btn.tmpl index 3707a83..ce973f2 100644 --- a/static/like_btn.tmpl +++ b/static/like_btn.tmpl @@ -1 +1,2 @@ - + + diff --git a/static/repeat_btn.tmpl b/static/repeat_btn.tmpl index 3148f92..68ab520 100644 --- a/static/repeat_btn.tmpl +++ b/static/repeat_btn.tmpl @@ -1 +1,2 @@ - \ No newline at end of file + + diff --git a/static/reply_btn.tmpl b/static/reply_btn.tmpl index d191f95..0dd62bd 100644 --- a/static/reply_btn.tmpl +++ b/static/reply_btn.tmpl @@ -1 +1,2 @@ - \ No newline at end of file + +