Skip to content

Commit 2d74c0f

Browse files
jebrosenSergioBenitez
authored andcommitted
Clean up, preparing to merge in other changes.
1 parent b31d523 commit 2d74c0f

File tree

6 files changed

+40
-109
lines changed

6 files changed

+40
-109
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;
@@ -46,16 +43,20 @@ pub struct Rocket {
4643
fairings: Fairings,
4744
}
4845

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

58-
impl<Ctx> hyper::MakeService<Ctx> for RocketArcs {
59+
impl<Ctx> hyper::MakeService<Ctx> for RocketHyperService {
5960
type ReqBody = hyper::Body;
6061
type ResBody = hyper::Body;
6162
type Error = hyper::Error;
@@ -64,27 +65,16 @@ impl<Ctx> hyper::MakeService<Ctx> for RocketArcs {
6465
type MakeError = Self::Error;
6566

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

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

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

135125
impl RocketHyperService {
136-
137-
#[inline]
138-
fn new(rocket: &RocketArcs) -> RocketHyperService {
139-
RocketHyperService {
140-
config: rocket.config.clone(),
141-
router: rocket.router.clone(),
142-
default_catchers: rocket.default_catchers.clone(),
143-
catchers: rocket.catchers.clone(),
144-
state: rocket.state.clone(),
145-
fairings: rocket.fairings.clone(),
146-
}
147-
}
148-
149126
/// Preprocess the request for Rocket things. Currently, this means:
150127
///
151128
/// * Rewriting the method in the request if _method form field exists.
@@ -332,7 +309,7 @@ impl Rocket {
332309
pub(crate) fn handle_error<'r>(
333310
&self,
334311
status: Status,
335-
req: &'r Request
312+
req: &'r Request<'_>
336313
) -> Response<'r> {
337314
unimplemented!("TODO")
338315
}
@@ -563,20 +540,16 @@ impl Rocket {
563540
pub fn register(mut self, catchers: Vec<Catcher>) -> Self {
564541
info!("{}{}", Paint::emoji("👾 "), Paint::magenta("Catchers:"));
565542

566-
let mut current_catchers = self.catchers.clone();
567-
568543
for c in catchers {
569544
if self.catchers.get(&c.code).map_or(false, |e| !e.is_default) {
570545
info_!("{} {}", c, Paint::yellow("(warning: duplicate catcher!)"));
571546
} else {
572547
info_!("{}", c);
573548
}
574549

575-
current_catchers.insert(c.code, c);
550+
self.catchers.insert(c.code, c);
576551
}
577552

578-
self.catchers = current_catchers;
579-
580553
self
581554
}
582555

@@ -773,12 +746,12 @@ impl Rocket {
773746
// Restore the log level back to what it originally was.
774747
logger::pop_max_level();
775748

776-
let arcs = RocketArcs::from(self);
749+
let service = RocketHyperService { rocket: Arc::new(self) };
777750

778751
// NB: executor must be passed manually here, see hyperium/hyper#1537
779752
let server = hyper::Server::builder(incoming)
780753
.executor(runtime.executor())
781-
.serve(arcs);
754+
.serve(service);
782755

783756
// TODO.async: Use with_graceful_shutdown, and let launch() return a Result<(), Error>
784757
runtime.block_on(server).expect("TODO.async handle error");
@@ -869,22 +842,9 @@ impl Rocket {
869842
}
870843
}
871844

872-
impl From<Rocket> for RocketArcs {
873-
fn from(mut rocket: Rocket) -> Self {
874-
RocketArcs {
875-
config: Arc::new(rocket.config),
876-
router: Arc::new(rocket.router),
877-
default_catchers: Arc::new(rocket.default_catchers),
878-
catchers: Arc::new(rocket.catchers),
879-
state: Arc::new(rocket.state),
880-
fairings: Arc::new(rocket.fairings),
881-
}
882-
}
883-
}
884-
885845
// TODO: consider try_from here?
886846
impl<'a> From<Response<'a>> for hyper::Response<hyper::Body> {
887-
fn from(mut response: Response) -> Self {
847+
fn from(mut response: Response<'_>) -> Self {
888848

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

site/tests/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#![feature(external_doc)]
1+
// #![feature(external_doc)]
22

3-
rocket::rocket_internal_guide_tests!("../guide/*.md");
3+
// rocket::rocket_internal_guide_tests!("../guide/*.md");

0 commit comments

Comments
 (0)