-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Passing the logic to the router and performance corrections
- Loading branch information
Showing
5 changed files
with
70 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,41 +288,49 @@ impl Rocket { | |
mut data: Data, | ||
) -> handler::Outcome<'r> { | ||
// Go through the list of matching routes until we fail or succeed. | ||
let matches = self.router.route(request); | ||
let mut counter: usize = 0; | ||
for route in &matches { | ||
counter += 1; | ||
|
||
// Must pass HEAD requests foward | ||
if (&request.method() != &Method::Head) && (&route.method != &request.method()) { | ||
// Must make sure it consumed all list before fail | ||
if &counter == &matches.len() { | ||
error_!("No matching routes for {}.", request); | ||
info_!( | ||
"{} {}", | ||
Paint::yellow("A similar route exists:").bold(), | ||
route | ||
); | ||
return Outcome::Failure(Status::MethodNotAllowed); | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
// Retrieve and set the requests parameters. | ||
info_!("Matched: {}", route); | ||
|
||
request.set_route(route); | ||
|
||
// Dispatch the request to the handler. | ||
let outcome = route.handler.handle(request, data); | ||
let method_matches = self.router.route(request, true); | ||
if method_matches.len() > 0 { | ||
for route in &method_matches { | ||
// Retrieve and set the requests parameters. | ||
info_!("Matched: {}", route); | ||
|
||
request.set_route(route); | ||
|
||
// Dispatch the request to the handler. | ||
let outcome = route.handler.handle(request, data); | ||
|
||
// Check if the request processing completed or if the request needs | ||
// to be forwarded. If it does, continue the loop to try again. | ||
info_!("{} {}", Paint::default("Outcome:").bold(), outcome); | ||
match outcome { | ||
o @ Outcome::Success(_) | o @ Outcome::Failure(_) => return o, | ||
Outcome::Forward(unused_data) => data = unused_data, | ||
}; | ||
} | ||
} | ||
|
||
// Check if the request processing completed or if the request needs | ||
// to be forwarded. If it does, continue the loop to try again. | ||
info_!("{} {}", Paint::default("Outcome:").bold(), outcome); | ||
match outcome { | ||
o @ Outcome::Success(_) | o @ Outcome::Failure(_) => return o, | ||
Outcome::Forward(unused_data) => data = unused_data, | ||
}; | ||
let match_any = self.router.route(request, false); | ||
if match_any.len() > 0 { | ||
|
||
let mut counter: usize = 0; | ||
for route in &match_any { | ||
counter += 1; | ||
|
||
// Must pass HEAD requests foward | ||
if (&request.method() != &Method::Head) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
// Must make sure it consumed all list before fail | ||
if &counter == &match_any.len() && !method_matches.iter().any(|item| route.collides_with(item)){ | ||
This comment has been minimized.
Sorry, something went wrong.
igalic
|
||
error_!("No matching routes for {}.", request); | ||
info_!( | ||
"{} {}", | ||
Paint::yellow("A similar route exists:").bold(), | ||
route | ||
); | ||
return Outcome::Failure(Status::MethodNotAllowed); | ||
} else { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if we invert this condition, we can continue in the if, and then we don't need to nest a very complicated if in here