mirror of https://github.com/rwf2/Rocket.git
Clean-up code style in 'lib/rocket.rs'.
This commit is contained in:
parent
aa9d9ab081
commit
ae46d27129
|
@ -43,9 +43,11 @@ impl hyper::Handler for Rocket {
|
||||||
// `dispatch` function, which knows nothing about Hyper. Because responding
|
// `dispatch` function, which knows nothing about Hyper. Because responding
|
||||||
// depends on the `HyperResponse` type, this function does the actual
|
// depends on the `HyperResponse` type, this function does the actual
|
||||||
// response processing.
|
// response processing.
|
||||||
fn handle<'h, 'k>(&self,
|
fn handle<'h, 'k>(
|
||||||
hyp_req: hyper::Request<'h, 'k>,
|
&self,
|
||||||
res: hyper::FreshResponse<'h>) {
|
hyp_req: hyper::Request<'h, 'k>,
|
||||||
|
res: hyper::FreshResponse<'h>,
|
||||||
|
) {
|
||||||
// Get all of the information from Hyper.
|
// Get all of the information from Hyper.
|
||||||
let (h_addr, h_method, h_headers, h_uri, _, h_body) = hyp_req.deconstruct();
|
let (h_addr, h_method, h_headers, h_uri, _, h_body) = hyp_req.deconstruct();
|
||||||
|
|
||||||
|
@ -111,14 +113,16 @@ impl Rocket {
|
||||||
fn issue_response(&self, response: Response, hyp_res: hyper::FreshResponse) {
|
fn issue_response(&self, response: Response, hyp_res: hyper::FreshResponse) {
|
||||||
match self.write_response(response, hyp_res) {
|
match self.write_response(response, hyp_res) {
|
||||||
Ok(_) => info_!("{}", Paint::green("Response succeeded.")),
|
Ok(_) => info_!("{}", Paint::green("Response succeeded.")),
|
||||||
Err(e) => error_!("Failed to write response: {:?}.", e)
|
Err(e) => error_!("Failed to write response: {:?}.", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write_response(&self, mut response: Response,
|
fn write_response(
|
||||||
mut hyp_res: hyper::FreshResponse) -> io::Result<()>
|
&self,
|
||||||
{
|
mut response: Response,
|
||||||
|
mut hyp_res: hyper::FreshResponse,
|
||||||
|
) -> io::Result<()> {
|
||||||
*hyp_res.status_mut() = hyper::StatusCode::from_u16(response.status().code);
|
*hyp_res.status_mut() = hyper::StatusCode::from_u16(response.status().code);
|
||||||
|
|
||||||
for header in response.headers().iter() {
|
for header in response.headers().iter() {
|
||||||
|
@ -178,9 +182,11 @@ impl Rocket {
|
||||||
if let Some(current) = req.remote() {
|
if let Some(current) = req.remote() {
|
||||||
let ip = req.headers()
|
let ip = req.headers()
|
||||||
.get_one("X-Real-IP")
|
.get_one("X-Real-IP")
|
||||||
.and_then(|ip_str| ip_str.parse().map_err(|_| {
|
.and_then(|ip| {
|
||||||
warn_!("The 'X-Real-IP' header is malformed: {}", ip_str)
|
ip.parse()
|
||||||
}).ok());
|
.map_err(|_| warn_!("'X-Real-IP' header is malformed: {}", ip))
|
||||||
|
.ok()
|
||||||
|
});
|
||||||
|
|
||||||
if let Some(ip) = ip {
|
if let Some(ip) = ip {
|
||||||
req.set_remote(SocketAddr::new(ip, current.port()));
|
req.set_remote(SocketAddr::new(ip, current.port()));
|
||||||
|
@ -195,9 +201,8 @@ impl Rocket {
|
||||||
if is_form && req.method() == Method::Post && data_len >= min_len {
|
if is_form && req.method() == Method::Post && data_len >= min_len {
|
||||||
// We're only using this for comparison and throwing it away
|
// We're only using this for comparison and throwing it away
|
||||||
// afterwards, so it doesn't matter if we have invalid UTF8.
|
// afterwards, so it doesn't matter if we have invalid UTF8.
|
||||||
let form = unsafe {
|
let form =
|
||||||
from_utf8_unchecked(&data.peek()[..min(data_len, max_len)])
|
unsafe { from_utf8_unchecked(&data.peek()[..min(data_len, max_len)]) };
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((key, value)) = FormItems::from(form).next() {
|
if let Some((key, value)) = FormItems::from(form).next() {
|
||||||
if key == "_method" {
|
if key == "_method" {
|
||||||
|
@ -210,9 +215,11 @@ impl Rocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn dispatch<'s, 'r>(&'s self,
|
pub(crate) fn dispatch<'s, 'r>(
|
||||||
request: &'r mut Request<'s>,
|
&'s self,
|
||||||
data: Data) -> Response<'r> {
|
request: &'r mut Request<'s>,
|
||||||
|
data: Data,
|
||||||
|
) -> Response<'r> {
|
||||||
info!("{}:", request);
|
info!("{}:", request);
|
||||||
|
|
||||||
// Do a bit of preprocessing before routing; run the attached fairings.
|
// Do a bit of preprocessing before routing; run the attached fairings.
|
||||||
|
@ -235,9 +242,8 @@ impl Rocket {
|
||||||
// convince it to give us another mutable reference.
|
// convince it to give us another mutable reference.
|
||||||
// TODO: Use something that is well defined, like UnsafeCell.
|
// TODO: Use something that is well defined, like UnsafeCell.
|
||||||
// But that causes variance issues...so wait for NLL.
|
// But that causes variance issues...so wait for NLL.
|
||||||
let request: &'r mut Request<'s> = unsafe {
|
let request: &'r mut Request<'s> =
|
||||||
(&mut *(request as *const _ as *mut _))
|
unsafe { (&mut *(request as *const _ as *mut _)) };
|
||||||
};
|
|
||||||
|
|
||||||
// There was no matching route.
|
// There was no matching route.
|
||||||
if request.method() == Method::Head {
|
if request.method() == Method::Head {
|
||||||
|
@ -273,9 +279,11 @@ impl Rocket {
|
||||||
// (ensuring `handler` takes an immutable borrow), any caller to `route`
|
// (ensuring `handler` takes an immutable borrow), any caller to `route`
|
||||||
// should be able to supply an `&mut` and retain an `&` after the call.
|
// should be able to supply an `&mut` and retain an `&` after the call.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn route<'s, 'r>(&'s self,
|
pub(crate) fn route<'s, 'r>(
|
||||||
request: &'r Request<'s>,
|
&'s self,
|
||||||
mut data: Data) -> handler::Outcome<'r> {
|
request: &'r Request<'s>,
|
||||||
|
mut data: Data,
|
||||||
|
) -> handler::Outcome<'r> {
|
||||||
// Go through the list of matching routes until we fail or succeed.
|
// Go through the list of matching routes until we fail or succeed.
|
||||||
let matches = self.router.route(request);
|
let matches = self.router.route(request);
|
||||||
for route in matches {
|
for route in matches {
|
||||||
|
@ -290,7 +298,7 @@ impl Rocket {
|
||||||
// to be forwarded. If it does, continue the loop to try again.
|
// to be forwarded. If it does, continue the loop to try again.
|
||||||
info_!("{} {}", Paint::white("Outcome:"), outcome);
|
info_!("{} {}", Paint::white("Outcome:"), outcome);
|
||||||
match outcome {
|
match outcome {
|
||||||
o@Outcome::Success(_) | o @Outcome::Failure(_) => return o,
|
o@Outcome::Success(_) | o@Outcome::Failure(_) => return o,
|
||||||
Outcome::Forward(unused_data) => data = unused_data,
|
Outcome::Forward(unused_data) => data = unused_data,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -420,7 +428,7 @@ impl Rocket {
|
||||||
default_catchers: catcher::defaults::get(),
|
default_catchers: catcher::defaults::get(),
|
||||||
catchers: catcher::defaults::get(),
|
catchers: catcher::defaults::get(),
|
||||||
state: Container::new(),
|
state: Container::new(),
|
||||||
fairings: Fairings::new()
|
fairings: Fairings::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -666,23 +674,23 @@ impl Rocket {
|
||||||
serve!(self, &full_addr, |server, proto| {
|
serve!(self, &full_addr, |server, proto| {
|
||||||
let mut server = match server {
|
let mut server = match server {
|
||||||
Ok(server) => server,
|
Ok(server) => server,
|
||||||
Err(e) => return LaunchError::from(e)
|
Err(e) => return LaunchError::from(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Determine the address and port we actually binded to.
|
// Determine the address and port we actually binded to.
|
||||||
match server.local_addr() {
|
match server.local_addr() {
|
||||||
Ok(server_addr) => self.config.port = server_addr.port(),
|
Ok(server_addr) => self.config.port = server_addr.port(),
|
||||||
Err(e) => return LaunchError::from(e)
|
Err(e) => return LaunchError::from(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the launch fairings.
|
// Run the launch fairings.
|
||||||
self.fairings.handle_launch(&self);
|
self.fairings.handle_launch(&self);
|
||||||
|
|
||||||
let full_addr = format!("{}:{}", self.config.address, self.config.port);
|
let full_addr = format!("{}:{}", self.config.address, self.config.port);
|
||||||
launch_info!("🚀 {} {}{}",
|
launch_info!("🚀 {} {}{}",
|
||||||
Paint::white("Rocket has launched from"),
|
Paint::white("Rocket has launched from"),
|
||||||
Paint::white(proto).bold(),
|
Paint::white(proto).bold(),
|
||||||
Paint::white(&full_addr).bold());
|
Paint::white(&full_addr).bold());
|
||||||
|
|
||||||
let threads = self.config.workers as usize;
|
let threads = self.config.workers as usize;
|
||||||
if let Err(e) = server.handle_threads(self, threads) {
|
if let Err(e) = server.handle_threads(self, threads) {
|
||||||
|
@ -727,7 +735,7 @@ impl Rocket {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn routes<'a>(&'a self) -> impl Iterator<Item=&'a Route> + 'a {
|
pub fn routes<'a>(&'a self) -> impl Iterator<Item = &'a Route> + 'a {
|
||||||
self.router.routes()
|
self.router.routes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
max_width = 85
|
max_width = 90
|
||||||
fn_call_width = 80
|
fn_call_width = 80
|
||||||
|
|
Loading…
Reference in New Issue