feat: first working redirect

c-web-server
Jef Roosens 2023-05-29 18:53:52 +02:00
parent 4d0e2e6c42
commit 00fa4a7c8c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 31 additions and 9 deletions

View File

@ -75,6 +75,9 @@ void trie_free(Trie *trie);
*/ */
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key); TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key);
TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key,
size_t key_len);
/** /**
* Add a string to this trie. * Add a string to this trie.
* *

View File

@ -27,10 +27,20 @@ bool lander_get_index(event_loop_conn *conn) {
bool lander_get_entry(event_loop_conn *conn) { bool lander_get_entry(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx; http_loop_ctx *ctx = conn->ctx;
size_t match_len = const char *key = &ctx->req.path[ctx->req.regex_groups[1].rm_so];
size_t key_len =
ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so; ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so;
info("matched: %.*s", match_len,
&ctx->req.path[ctx->req.regex_groups[1].rm_so]); Entry *entry;
TrieExitCode res = trie_search_len(ctx->g->trie, &entry, key, key_len);
if (res == NotFound) {
ctx->res.type = http_not_found;
} else if (entry->type == Redirect) {
ctx->res.type = http_moved_permanently;
http_loop_res_add_header(ctx, http_header_location, entry->string, false);
}
conn->state = event_loop_conn_state_res; conn->state = event_loop_conn_state_res;
return true; return true;

View File

@ -105,7 +105,7 @@ typedef struct searchresult {
TrieNode *child; TrieNode *child;
} SearchResult; } SearchResult;
SearchResult trie_search_node(Trie *trie, const char *key) { SearchResult trie_search_node_len(Trie *trie, const char *key, size_t key_len) {
SearchResult out = {NULL, NULL}; SearchResult out = {NULL, NULL};
size_t i = 0; size_t i = 0;
@ -130,15 +130,15 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
i += (*child_ptr)->string_len; i += (*child_ptr)->string_len;
if (key[i] != DELIMITER) { if (i < key_len) {
node_ptr = child_ptr; node_ptr = child_ptr;
} }
} while (key[i] != DELIMITER); } while (i < key_len);
// At this point, we've either arrived at an empty child, or traversed through // At this point, we've either arrived at an empty child, or traversed through
// the entire string. Therefore, all we have to do is check whether we're at // the entire string. Therefore, all we have to do is check whether we're at
// the end of the string and if node represents a string. // the end of the string and if node represents a string.
if (key[i] == DELIMITER && (*child_ptr)->represents) { if (i == key_len && (*child_ptr)->represents) {
out.parent = *node_ptr; out.parent = *node_ptr;
out.child = *child_ptr; out.child = *child_ptr;
} }
@ -146,6 +146,10 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
return out; return out;
} }
SearchResult trie_search_node(Trie *trie, const char *key) {
return trie_search_node_len(trie, key, strlen(key));
}
/** /**
* Returns whether the given string is present in the trie. * Returns whether the given string is present in the trie.
* *
@ -153,8 +157,9 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
* @param string string to look up * @param string string to look up
* @return true if the string is present in the trie, false otherwise * @return true if the string is present in the trie, false otherwise
*/ */
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) { TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key,
SearchResult res = trie_search_node(trie, key); size_t key_len) {
SearchResult res = trie_search_node_len(trie, key, key_len);
if (res.child == NULL) { if (res.child == NULL) {
return NotFound; return NotFound;
@ -165,6 +170,10 @@ TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) {
return Ok; return Ok;
} }
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) {
return trie_search_len(trie, entry_ptr, key, strlen(key));
}
/** /**
* Add the given string to the Trie. * Add the given string to the Trie.
* *