Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the route as soon as the URL is parsed #499

Merged
merged 2 commits into from
Jul 22, 2022
Merged

Find the route as soon as the URL is parsed #499

merged 2 commits into from
Jul 22, 2022

Conversation

The-EDev
Copy link
Member

the TL;DR is that this PR makes it so that a request is routed as soon as the URL is parsed.

The long version is that it makes multiple changes to multiple parts of the framework, these changes are as follows:

  • Common
    • replaced the tuples in the router with using routing_search_result and using routing_handle_result.
  • Parser
    • Moves URL parameter handling to the on_url callback. (from the on_message_complete callback)
    • Adds new on_method callback that executes when the method is parsed
    • Replaces the to_request() method by immediately assigning any parsed data to a request object owned by the parser. (this is fine since the parser is owned by and exist for the lifetime of the HTTP connection)
    • Makes parser variables (except req) private.
    • Adds process_url() function to call a connection's handle_url(). (explained below)
  • Connection
    • Alters the request object to be a reference the Parser's request object.
    • Adapts the functionality to the changes made in the Parser
    • Adds handle_url() function to call the router and behave according to the result. (i.e returning a response in case of a 404 or 405)
    • Adds a routing_handle_result property in order to check whether or not a route was found. (could be a good idea to replace with a boolean returned from handle_url() in the application)
  • Router
    • Improves the way a Trie is printed out. (I needed that for debugging)
    • Splits the handle() method into 2 separate methods, one that deals with only finding a route and another that does the actual handling.

The other changes are either documentation related or only there to adapt to the changes above.

This PR closes #493 and make an implementation for #397 possible.

@crow-clang-format
Copy link

--- include/crow/routing.h	(before formatting)
+++ include/crow/routing.h	(after formatting)
@@ -1626,7 +1626,7 @@
             else // Every request that isn't a HEAD or OPTIONS request
             {
                 search_result = per_methods_[static_cast<int>(method_actual)].trie.find(req.url);
-// TODO(EDev): maybe ending the else here would allow the requests coming from above (after removing the return statement) to be checked on whether they actually point to a route
+                // TODO(EDev): maybe ending the else here would allow the requests coming from above (after removing the return statement) to be checked on whether they actually point to a route
                 if (!std::get<0>(search_result))
                 {
                     for (auto& per_method : per_methods_)

include/crow/common.h Outdated Show resolved Hide resolved
@The-EDev The-EDev merged commit ac7fb15 into master Jul 22, 2022
@The-EDev The-EDev deleted the early_routing branch July 22, 2022 15:13
@andrew-guz
Copy link

andrew-guz commented Apr 6, 2024

After that changes, CORS is not working for WebKit browser (for example, Epiphany). Can not parse request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pass a request to the router as soon as the URL is parsed
4 participants