diff --git a/include/trie.h b/include/trie.h index 322ad53..37d5341 100644 --- a/include/trie.h +++ b/include/trie.h @@ -75,6 +75,9 @@ void trie_free(Trie *trie); */ 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. * diff --git a/src/main.c b/src/main.c index d3a840c..aead5a8 100644 --- a/src/main.c +++ b/src/main.c @@ -27,10 +27,20 @@ bool lander_get_index(event_loop_conn *conn) { bool lander_get_entry(event_loop_conn *conn) { 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; - 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; return true; diff --git a/src/trie/trie.c b/src/trie/trie.c index 727d3da..926e37c 100644 --- a/src/trie/trie.c +++ b/src/trie/trie.c @@ -105,7 +105,7 @@ typedef struct searchresult { TrieNode *child; } 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}; size_t i = 0; @@ -130,15 +130,15 @@ SearchResult trie_search_node(Trie *trie, const char *key) { i += (*child_ptr)->string_len; - if (key[i] != DELIMITER) { + if (i < key_len) { 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 // 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. - if (key[i] == DELIMITER && (*child_ptr)->represents) { + if (i == key_len && (*child_ptr)->represents) { out.parent = *node_ptr; out.child = *child_ptr; } @@ -146,6 +146,10 @@ SearchResult trie_search_node(Trie *trie, const char *key) { 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. * @@ -153,8 +157,9 @@ SearchResult trie_search_node(Trie *trie, const char *key) { * @param string string to look up * @return true if the string is present in the trie, false otherwise */ -TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) { - SearchResult res = trie_search_node(trie, key); +TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key, + size_t key_len) { + SearchResult res = trie_search_node_len(trie, key, key_len); if (res.child == NULL) { return NotFound; @@ -165,6 +170,10 @@ TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) { 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. *