Skip to content

Commit 55ae2b9

Browse files
committed
Clean up, preparing to merge in other changes.
1 parent 70d6444 commit 55ae2b9

File tree

5 files changed

+38
-107
lines changed

5 files changed

+38
-107
lines changed

core/http/src/hyper.rs

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,28 @@
1818
#[doc(hidden)] pub use http::status::StatusCode;
1919
#[doc(hidden)] pub use http::uri::Uri;
2020

21-
/// Type alias to `hyper::Response<'a, hyper::net::Fresh>`.
22-
// TODO #[doc(hidden)] pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;
23-
24-
/// Reexported Hyper header types.
21+
/// Reexported http header types.
2522
pub mod header {
26-
use crate::Header;
27-
28-
macro_rules! import_hyper_items {
29-
($($item:ident),*) => ($(pub use hyper::header::$item;)*)
30-
}
31-
32-
macro_rules! import_hyper_headers {
23+
macro_rules! import_http_headers {
3324
($($name:ident),*) => ($(
3425
pub use http::header::$name as $name;
3526
)*)
3627
}
3728

38-
// import_hyper_items! {
39-
// Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, AcceptRanges,
40-
// AccessControlAllowCredentials, AccessControlAllowHeaders,
41-
// AccessControlAllowMethods, AccessControlExposeHeaders,
42-
// AccessControlMaxAge, AccessControlRequestHeaders,
43-
// AccessControlRequestMethod, Allow, Authorization, Basic, Bearer,
44-
// CacheControl, Connection, ContentDisposition, ContentEncoding,
45-
// ContentLanguage, ContentLength, ContentRange, ContentType, Date, ETag,
46-
// EntityTag, Expires, From, Headers, Host, HttpDate, IfModifiedSince,
47-
// IfUnmodifiedSince, LastModified, Location, Origin, Prefer,
48-
// PreferenceApplied, Protocol, Quality, QualityItem, Referer,
49-
// StrictTransportSecurity, TransferEncoding, Upgrade, UserAgent,
50-
// AccessControlAllowOrigin, ByteRangeSpec, CacheDirective, Charset,
51-
// ConnectionOption, ContentRangeSpec, DispositionParam, DispositionType,
52-
// Encoding, Expect, IfMatch, IfNoneMatch, IfRange, Pragma, Preference,
53-
// ProtocolName, Range, RangeUnit, ReferrerPolicy, Vary, Scheme, q, qitem
54-
// }
55-
//
56-
import_hyper_headers! {
57-
ACCEPT, ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
29+
import_http_headers! {
30+
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
31+
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
5832
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
5933
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
60-
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, ACCEPT_CHARSET,
61-
ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES, ALLOW, CACHE_CONTROL,
62-
CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE,
63-
CONTENT_LENGTH, CONTENT_RANGE, DATE, ETAG, EXPECT, EXPIRES, HOST, IF_MATCH,
64-
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
65-
LOCATION, ORIGIN, PRAGMA, RANGE, REFERER,
66-
REFERRER_POLICY, STRICT_TRANSPORT_SECURITY, TRANSFER_ENCODING, UPGRADE,
67-
USER_AGENT, VARY
34+
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, ALLOW,
35+
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION,
36+
CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION,
37+
CONTENT_RANGE, CONTENT_SECURITY_POLICY,
38+
CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, DATE, ETAG, EXPECT,
39+
EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE,
40+
IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK,
41+
LOCATION, ORIGIN, PRAGMA, RANGE, REFERER, REFERRER_POLICY, REFRESH,
42+
STRICT_TRANSPORT_SECURITY, TE, TRANSFER_ENCODING, UPGRADE, USER_AGENT,
43+
VARY
6844
}
6945
}

core/http/src/method.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
extern crate http;
2-
31
use std::fmt;
42
use std::str::FromStr;
53

6-
use crate::{hyper, uncased::uncased_eq};
4+
use crate::uncased::uncased_eq;
75

86
use self::Method::*;
97

core/lib/src/catcher.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use yansi::Color::*;
5959
///
6060
/// A function decorated with `catch` must take exactly zero or one arguments.
6161
/// If the catcher takes an argument, it must be of type [`&Request`](Request).
62-
#[derive(Clone)]
6362
pub struct Catcher {
6463
/// The HTTP status code to match against.
6564
pub code: u16,

core/lib/src/request/request.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use std::cell::{Cell, RefCell};
33
use std::net::{IpAddr, SocketAddr};
44
use std::fmt;
55
use std::str;
6-
use std::str::FromStr;
7-
use std::sync::Arc;
86

97
use yansi::Paint;
108
use state::{Container, Storage};
@@ -15,7 +13,7 @@ use crate::request::{FromFormValue, FormItems, FormItem};
1513
use crate::rocket::Rocket;
1614
use crate::router::Route;
1715
use crate::config::{Config, Limits};
18-
use crate::http::{hyper, uri::{Origin, Segments, Uri}};
16+
use crate::http::{hyper, uri::{Origin, Segments}};
1917
use crate::http::{Method, Header, HeaderMap, Cookies};
2018
use crate::http::{RawStr, ContentType, Accept, MediaType};
2119
use crate::http::private::{Indexed, SmallVec, CookieJar};
@@ -820,7 +818,7 @@ impl<'r> Request<'r> {
820818
// TODO if cookie_headers.peek().is_some() {
821819
let mut cookie_jar = CookieJar::new();
822820
for header in cookie_headers {
823-
let raw_str = match ::std::str::from_utf8(header.as_bytes()) {
821+
let raw_str = match std::str::from_utf8(header.as_bytes()) {
824822
Ok(string) => string,
825823
Err(_) => continue
826824
};

core/lib/src/rocket.rs

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use std::collections::HashMap;
22
use std::convert::From;
33
use std::str::{from_utf8, FromStr};
44
use std::cmp::min;
5-
use std::io::{self, Write};
6-
use std::time::Duration;
75
use std::mem;
8-
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
6+
use std::net::ToSocketAddrs;
97
use std::sync::Arc;
108

119
use futures::{Future, Stream};
@@ -19,7 +17,6 @@ use tokio::prelude::{Future as _, Stream as _};
1917
#[cfg(feature = "tls")] use crate::http::tls::TlsAcceptor;
2018

2119
use crate::{logger, handler};
22-
use crate::ext::ReadExt;
2320
use crate::config::{self, Config, LoggedValue};
2421
use crate::request::{Request, FormItems};
2522
use crate::data::Data;
@@ -45,16 +42,20 @@ pub struct Rocket {
4542
fairings: Fairings,
4643
}
4744

48-
struct RocketArcs {
49-
config: Arc<Config>,
50-
router: Arc<Router>,
51-
default_catchers: Arc<HashMap<u16, Catcher>>,
52-
catchers: Arc<HashMap<u16, Catcher>>,
53-
state: Arc<Container>,
54-
fairings: Arc<Fairings>,
45+
#[derive(Clone)]
46+
struct RocketHyperService {
47+
rocket: Arc<Rocket>,
48+
}
49+
50+
impl std::ops::Deref for RocketHyperService {
51+
type Target = Rocket;
52+
53+
fn deref(&self) -> &Self::Target {
54+
&*self.rocket
55+
}
5556
}
5657

57-
impl<Ctx> hyper::MakeService<Ctx> for RocketArcs {
58+
impl<Ctx> hyper::MakeService<Ctx> for RocketHyperService {
5859
type ReqBody = hyper::Body;
5960
type ResBody = hyper::Body;
6061
type Error = hyper::Error;
@@ -63,27 +64,16 @@ impl<Ctx> hyper::MakeService<Ctx> for RocketArcs {
6364
type MakeError = Self::Error;
6465

6566
fn make_service(&mut self, _: Ctx) -> Self::Future {
66-
future::ok(RocketHyperService::new(self))
67+
future::ok(RocketHyperService { rocket: self.rocket.clone() })
6768
}
6869
}
6970

70-
#[derive(Clone)]
71-
pub struct RocketHyperService {
72-
config: Arc<Config>,
73-
router: Arc<Router>,
74-
default_catchers: Arc<HashMap<u16, Catcher>>,
75-
catchers: Arc<HashMap<u16, Catcher>>,
76-
state: Arc<Container>,
77-
fairings: Arc<Fairings>,
78-
}
79-
8071
#[doc(hidden)]
8172
impl hyper::Service for RocketHyperService {
8273
type ReqBody = hyper::Body;
8374
type ResBody = hyper::Body;
8475
type Error = hyper::Error;
85-
//type Future = FutureResult<hyper::Response<Self::ResBody>, Self::Error>;
86-
type Future = Box<future::Future<Item = hyper::Response<Self::ResBody>, Error = Self::Error> + Send>;
76+
type Future = Box<dyn future::Future<Item = hyper::Response<Self::ResBody>, Error = Self::Error> + Send>;
8777

8878
// This function tries to hide all of the Hyper-ness from Rocket. It
8979
// essentially converts Hyper types into Rocket types, then calls the
@@ -132,19 +122,6 @@ impl hyper::Service for RocketHyperService {
132122
}
133123

134124
impl RocketHyperService {
135-
136-
#[inline]
137-
fn new(rocket: &RocketArcs) -> RocketHyperService {
138-
RocketHyperService {
139-
config: rocket.config.clone(),
140-
router: rocket.router.clone(),
141-
default_catchers: rocket.default_catchers.clone(),
142-
catchers: rocket.catchers.clone(),
143-
state: rocket.state.clone(),
144-
fairings: rocket.fairings.clone(),
145-
}
146-
}
147-
148125
/// Preprocess the request for Rocket things. Currently, this means:
149126
///
150127
/// * Rewriting the method in the request if _method form field exists.
@@ -322,7 +299,7 @@ impl Rocket {
322299
pub(crate) fn handle_error<'r>(
323300
&self,
324301
status: Status,
325-
req: &'r Request
302+
req: &'r Request<'_>
326303
) -> Response<'r> {
327304
unimplemented!("TODO")
328305
}
@@ -553,20 +530,16 @@ impl Rocket {
553530
pub fn register(mut self, catchers: Vec<Catcher>) -> Self {
554531
info!("{}{}", Paint::masked("👾 "), Paint::magenta("Catchers:"));
555532

556-
let mut current_catchers = self.catchers.clone();
557-
558533
for c in catchers {
559534
if self.catchers.get(&c.code).map_or(false, |e| !e.is_default) {
560535
info_!("{} {}", c, Paint::yellow("(warning: duplicate catcher!)"));
561536
} else {
562537
info_!("{}", c);
563538
}
564539

565-
current_catchers.insert(c.code, c);
540+
self.catchers.insert(c.code, c);
566541
}
567542

568-
self.catchers = current_catchers;
569-
570543
self
571544
}
572545

@@ -763,12 +736,12 @@ impl Rocket {
763736
// Restore the log level back to what it originally was.
764737
logger::pop_max_level();
765738

766-
let arcs = RocketArcs::from(self);
739+
let service = RocketHyperService { rocket: Arc::new(self) };
767740

768741
// NB: executor must be passed manually here, see hyperium/hyper#1537
769742
let server = hyper::Server::builder(incoming)
770743
.executor(runtime.executor())
771-
.serve(arcs);
744+
.serve(service);
772745

773746
// TODO.async: Use with_graceful_shutdown, and let launch() return a Result<(), Error>
774747
runtime.block_on(server).expect("TODO.async handle error");
@@ -859,22 +832,9 @@ impl Rocket {
859832
}
860833
}
861834

862-
impl From<Rocket> for RocketArcs {
863-
fn from(mut rocket: Rocket) -> Self {
864-
RocketArcs {
865-
config: Arc::new(rocket.config),
866-
router: Arc::new(rocket.router),
867-
default_catchers: Arc::new(rocket.default_catchers),
868-
catchers: Arc::new(rocket.catchers),
869-
state: Arc::new(rocket.state),
870-
fairings: Arc::new(rocket.fairings),
871-
}
872-
}
873-
}
874-
875835
// TODO: consider try_from here?
876836
impl<'a> From<Response<'a>> for hyper::Response<hyper::Body> {
877-
fn from(mut response: Response) -> Self {
837+
fn from(mut response: Response<'_>) -> Self {
878838

879839
let mut builder = hyper::Response::builder();
880840
builder.status(hyper::StatusCode::from_u16(response.status().code).expect(""));

0 commit comments

Comments
 (0)