Skip to content
Closed

Linting #1114

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
2a694ef
Upgrade to hyper 0.12:
schrieveslaach May 19, 2019
f478221
Clean up, preparing to merge in other changes.
jebrosen Jun 30, 2019
52ec70c
Convert core to async and add support for async routes.
jebrosen Mar 11, 2019
cf68c9b
Add async_await feature to examples to make them more check-able.
jebrosen May 17, 2019
6660fee
Use AddrIncoming/AddrStream.
jebrosen Jul 11, 2019
52db364
Stream body data instead of buffering it.
jebrosen Jul 12, 2019
37c0c69
Update core lib tests for async.
jebrosen Jul 13, 2019
c0a0f77
Make response fairings async.
jebrosen Jul 24, 2019
b1bdbba
Make respond_to async.
jebrosen Jul 25, 2019
50e3609
Update helmet in contrib.
jebrosen Jul 27, 2019
21fae46
Update rocket_contrib::json and json example for async.
jebrosen Jul 27, 2019
e33078f
Upgrade 'msgpack' for async in contrib and examples.
Redrield Aug 2, 2019
1d863f5
Update 'rocket_contrib::templates' and examples that use it for async.
jebrosen Aug 4, 2019
2edee38
Fix 'rocket_contrib::json' tests for async.
jebrosen Aug 4, 2019
d278d48
Fix 'rocket_contrib::helmet' tests for async.
jebrosen Aug 4, 2019
6675bb5
Use 'body_string_wait' in all example tests.
jebrosen Aug 4, 2019
c82e24d
Update 'fairings' example for async.
jebrosen Aug 4, 2019
e85c733
Fix 'rocket_contrib::databases' tests for async.
jebrosen Aug 4, 2019
676ea96
Fix a few 'unused use' warnings.
jebrosen Aug 4, 2019
5b1125b
Update 'rocket_contrib::serve' for async.
Redrield Aug 3, 2019
22622d1
Update many doc tests in 'core' for async.
jebrosen Aug 7, 2019
215a4e1
Disable some known-failing tests for now.
jebrosen Aug 7, 2019
f2aaab0
Convert two internal functions returning `Pin<Box<_>>` to `async fn`.
jhpratt Aug 10, 2019
72ccbbd
Use read_to_string (from futures-preview 0.3.0-alpha.18) to more clos…
jebrosen Aug 13, 2019
de84686
Fix some of the broken doc tests in core.
jebrosen Aug 14, 2019
5a182aa
Fix or ignore remaining tests in core.
jebrosen Aug 14, 2019
ad70f11
Fix 'static_files' and 'serve' tests.
jebrosen Aug 14, 2019
95648dd
Use futures 0.3-compatible hyper and tokio.
jebrosen Aug 20, 2019
17d7c76
Use the tokio runtime instead of the futures-rs executor for blocking…
jebrosen Aug 20, 2019
b511666
Update hyper dependency and new API for hyper::service::Service.
jebrosen Aug 21, 2019
02a98fe
Remove stabilized 'async_await' feature gate.
jhpratt Aug 20, 2019
1920e58
Update minimum nightly to 2019-08-20 for removal of the 'async_await'…
jebrosen Aug 22, 2019
70ca157
Add 'Rocket::spawn_on' to spawn a server on a user-provided (tokio) r…
jhpratt Aug 18, 2019
fea6dff
Revamp testing system for async.
jebrosen Aug 24, 2019
95b6e95
Use '#[rocket::async_test]' for tests in 'codegen'.
jebrosen Aug 24, 2019
f018f8d
Use '#[rocket::async_test]' for tests in 'contrib'.
jebrosen Aug 24, 2019
bc0c46c
Use '#[rocket::async_test]' and rocket::async_test() for tests in exa…
jebrosen Aug 24, 2019
96f7594
Update 'test.sh' to reflect the tests that should be passing.
jebrosen Aug 24, 2019
b57ae4f
Update remaining examples for async.
jebrosen Aug 27, 2019
2606c64
Use 'File' from 'async_std' instead of from 'tokio'.
jhpratt Aug 29, 2019
b27c0d1
Fix and re-enable 'static_files' tests.
jebrosen Aug 29, 2019
6042c17
Update pinned hyper and tokio versions.
jebrosen Aug 30, 2019
755666f
Remove `self` from path where possible
jhpratt Sep 2, 2019
302a8a3
Restrict visibility where not externally visible
jhpratt Sep 2, 2019
17788dc
Remove identity function
jhpratt Sep 2, 2019
3c872a7
Use struct field shorthand where possible
jhpratt Sep 2, 2019
fb63b4b
Remove `use foo`, which is a noop
jhpratt Sep 2, 2019
425ab60
Use shorter path where already imported
jhpratt Sep 2, 2019
759b934
Remove unnecessary braces on import
jhpratt Sep 2, 2019
c2b210b
Use path over ident in match arm
jhpratt Sep 2, 2019
192e7ae
Revert needless path addition
jhpratt Sep 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ members = [
"examples/tls",
"examples/fairings",
"examples/hello_2018",
"examples/hello_2015",
]
1 change: 1 addition & 0 deletions contrib/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ memcache_pool = ["databases", "memcache", "r2d2-memcache"]

[dependencies]
# Global dependencies.
futures-preview = { version = "0.3.0-alpha.18" }
rocket_contrib_codegen = { version = "0.5.0-dev", path = "../codegen", optional = true }
rocket = { version = "0.5.0-dev", path = "../../core/lib/", default-features = false }
log = "0.4"
Expand Down
6 changes: 4 additions & 2 deletions contrib/lib/src/helmet/helmet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ impl Fairing for SpaceHelmet {
}
}

fn on_response(&self, _request: &Request<'_>, response: &mut Response<'_>) {
self.apply(response);
fn on_response<'a>(&'a self, _request: &'a Request<'_>, response: &'a mut Response<'_>) -> std::pin::Pin<Box<dyn std::future::Future<Output=()> + Send + 'a>> {
Box::pin(async move {
self.apply(response);
})
}

fn on_launch(&self, rocket: &Rocket) {
Expand Down
67 changes: 38 additions & 29 deletions contrib/lib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
//! ```

use std::ops::{Deref, DerefMut};
use std::io::{self, Read};
use std::io;
use std::iter::FromIterator;

use futures::io::AsyncReadExt;

use rocket::request::Request;
use rocket::outcome::Outcome::*;
use rocket::data::{Outcome, Transform, Transform::*, Transformed, Data, FromData};
use rocket::data::{Transform::*, Transformed, Data, FromData, TransformFuture, FromDataFuture};
use rocket::response::{self, Responder, content};
use rocket::http::Status;
use rocket::AsyncReadExt as _;

use serde::{Serialize, Serializer};
use serde::de::{Deserialize, Deserializer};
Expand Down Expand Up @@ -133,42 +136,48 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for Json<T> {
type Owned = String;
type Borrowed = str;

fn transform(r: &Request<'_>, d: Data) -> Transform<Outcome<Self::Owned, Self::Error>> {
fn transform(r: &Request<'_>, d: Data) -> TransformFuture<'a, Self::Owned, Self::Error> {
let size_limit = r.limits().get("json").unwrap_or(LIMIT);
let mut s = String::with_capacity(512);
match d.open().take(size_limit).read_to_string(&mut s) {
Ok(_) => Borrowed(Success(s)),
Err(e) => Borrowed(Failure((Status::BadRequest, JsonError::Io(e))))
}
Box::pin(async move {
let mut s = String::with_capacity(512);
let mut reader = d.open().take(size_limit);
match reader.read_to_string(&mut s).await {
Ok(_) => Borrowed(Success(s)),
Err(e) => Borrowed(Failure((Status::BadRequest, JsonError::Io(e))))
}
})
}

fn from_data(_: &Request<'_>, o: Transformed<'a, Self>) -> Outcome<Self, Self::Error> {
let string = o.borrowed()?;
match serde_json::from_str(&string) {
Ok(v) => Success(Json(v)),
Err(e) => {
error_!("Couldn't parse JSON body: {:?}", e);
if e.is_data() {
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
} else {
Failure((Status::BadRequest, JsonError::Parse(string, e)))
fn from_data(_: &Request<'_>, o: Transformed<'a, Self>) -> FromDataFuture<'a, Self, Self::Error> {
Box::pin(async move {
let string = o.borrowed()?;
match serde_json::from_str(&string) {
Ok(v) => Success(Json(v)),
Err(e) => {
error_!("Couldn't parse JSON body: {:?}", e);
if e.is_data() {
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
} else {
Failure((Status::BadRequest, JsonError::Parse(string, e)))
}
}
}
}
})
}
}

/// Serializes the wrapped value into JSON. Returns a response with Content-Type
/// JSON and a fixed-size body with the serialized value. If serialization
/// fails, an `Err` of `Status::InternalServerError` is returned.
impl<'a, T: Serialize> Responder<'a> for Json<T> {
fn respond_to(self, req: &Request<'_>) -> response::Result<'a> {
serde_json::to_string(&self.0).map(|string| {
content::Json(string).respond_to(req).unwrap()
}).map_err(|e| {
error_!("JSON failed to serialize: {:?}", e);
Status::InternalServerError
})
impl<'r, T: Serialize> Responder<'r> for Json<T> {
fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
match serde_json::to_string(&self.0) {
Ok(string) => Box::pin(async move { Ok(content::Json(string).respond_to(req).await.unwrap()) }),
Err(e) => Box::pin(async move {
error_!("JSON failed to serialize: {:?}", e);
Err(Status::InternalServerError)
})
}
}
}

Expand Down Expand Up @@ -283,9 +292,9 @@ impl<T> FromIterator<T> for JsonValue where serde_json::Value: FromIterator<T> {

/// Serializes the value into JSON. Returns a response with Content-Type JSON
/// and a fixed-size body with the serialized value.
impl<'a> Responder<'a> for JsonValue {
impl<'r> Responder<'r> for JsonValue {
#[inline]
fn respond_to(self, req: &Request<'_>) -> response::Result<'a> {
fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
content::Json(self.0.to_string()).respond_to(req)
}
}
Expand Down
65 changes: 37 additions & 28 deletions contrib/lib/src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
//! features = ["msgpack"]
//! ```

use std::io::Read;
use std::ops::{Deref, DerefMut};

use futures::io::AsyncReadExt;

use rocket::request::Request;
use rocket::outcome::Outcome::*;
use rocket::data::{Outcome, Transform, Transform::*, Transformed, Data, FromData};
use rocket::response::{self, Responder, content};
use rocket::data::{Data, FromData, FromDataFuture, Transform::*, TransformFuture, Transformed};
use rocket::http::Status;
use rocket::response::{self, content, Responder};
use rocket::AsyncReadExt as _;

use serde::Serialize;
use serde::de::Deserialize;
Expand Down Expand Up @@ -119,45 +121,52 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for MsgPack<T> {
type Owned = Vec<u8>;
type Borrowed = [u8];

fn transform(r: &Request<'_>, d: Data) -> Transform<Outcome<Self::Owned, Self::Error>> {
let mut buf = Vec::new();
fn transform(r: &Request<'_>, d: Data) -> TransformFuture<'a, Self::Owned, Self::Error> {
let size_limit = r.limits().get("msgpack").unwrap_or(LIMIT);
match d.open().take(size_limit).read_to_end(&mut buf) {
Ok(_) => Borrowed(Success(buf)),
Err(e) => Borrowed(Failure((Status::BadRequest, Error::InvalidDataRead(e))))
}

Box::pin(async move {
let mut buf = Vec::new();
let mut reader = d.open().take(size_limit);
match reader.read_to_end(&mut buf).await {
Ok(_) => Borrowed(Success(buf)),
Err(e) => Borrowed(Failure((Status::BadRequest, Error::InvalidDataRead(e)))),
}
})
}

fn from_data(_: &Request<'_>, o: Transformed<'a, Self>) -> Outcome<Self, Self::Error> {
fn from_data(_: &Request<'_>, o: Transformed<'a, Self>) -> FromDataFuture<'a, Self, Self::Error> {
use self::Error::*;

let buf = o.borrowed()?;
match rmp_serde::from_slice(&buf) {
Ok(val) => Success(MsgPack(val)),
Err(e) => {
error_!("Couldn't parse MessagePack body: {:?}", e);
match e {
TypeMismatch(_) | OutOfRange | LengthMismatch(_) => {
Failure((Status::UnprocessableEntity, e))
Box::pin(async move {
let buf = o.borrowed()?;
match rmp_serde::from_slice(&buf) {
Ok(val) => Success(MsgPack(val)),
Err(e) => {
error_!("Couldn't parse MessagePack body: {:?}", e);
match e {
TypeMismatch(_) | OutOfRange | LengthMismatch(_) => {
Failure((Status::UnprocessableEntity, e))
}
_ => Failure((Status::BadRequest, e)),
}
_ => Failure((Status::BadRequest, e))
}
}
}
})
}
}

/// Serializes the wrapped value into MessagePack. Returns a response with
/// Content-Type `MsgPack` and a fixed-size body with the serialization. If
/// serialization fails, an `Err` of `Status::InternalServerError` is returned.
impl<T: Serialize> Responder<'static> for MsgPack<T> {
fn respond_to(self, req: &Request<'_>) -> response::Result<'static> {
rmp_serde::to_vec(&self.0).map_err(|e| {
error_!("MsgPack failed to serialize: {:?}", e);
Status::InternalServerError
}).and_then(|buf| {
content::MsgPack(buf).respond_to(req)
})
impl<'r, T: Serialize> Responder<'r> for MsgPack<T> {
fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
match rmp_serde::to_vec(&self.0) {
Ok(buf) => content::MsgPack(buf).respond_to(req),
Err(e) => Box::pin(async move {
error_!("MsgPack failed to serialize: {:?}", e);
Err(Status::InternalServerError)
}),
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions contrib/lib/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::{PathBuf, Path};

use rocket::{Request, Data, Route};
use rocket::http::{Method, uri::Segments};
use rocket::handler::{Handler, Outcome};
use rocket::handler::{Handler, HandlerFuture, Outcome};
use rocket::response::NamedFile;

/// A bitset representing configurable options for the [`StaticFiles`] handler.
Expand Down Expand Up @@ -273,10 +273,10 @@ impl Into<Vec<Route>> for StaticFiles {
}

impl Handler for StaticFiles {
fn handle<'r>(&self, req: &'r Request<'_>, data: Data) -> Outcome<'r> {
fn handle_dir<'r>(opt: Options, r: &'r Request<'_>, d: Data, path: &Path) -> Outcome<'r> {
fn handle<'r>(&self, req: &'r Request<'_>, data: Data) -> HandlerFuture<'r> {
fn handle_dir<'r>(opt: Options, r: &'r Request<'_>, d: Data, path: &Path) -> HandlerFuture<'r> {
if !opt.contains(Options::Index) {
return Outcome::forward(d);
return Box::pin(async move { Outcome::forward(d) });
}

let file = NamedFile::open(path.join("index.html")).ok();
Expand All @@ -302,7 +302,7 @@ impl Handler for StaticFiles {
match &path {
Some(path) if path.is_dir() => handle_dir(self.options, req, data, path),
Some(path) => Outcome::from_or_forward(req, data, NamedFile::open(path).ok()),
None => Outcome::forward(data)
None => Box::pin(async move { Outcome::forward(data) }),
}
}
}
25 changes: 15 additions & 10 deletions contrib/lib/src/templates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,21 @@ impl Template {
/// Returns a response with the Content-Type derived from the template's
/// extension and a fixed-size body containing the rendered template. If
/// rendering fails, an `Err` of `Status::InternalServerError` is returned.
impl Responder<'static> for Template {
fn respond_to(self, req: &Request<'_>) -> response::Result<'static> {
let ctxt = req.guard::<State<'_, ContextManager>>().succeeded().ok_or_else(|| {
error_!("Uninitialized template context: missing fairing.");
info_!("To use templates, you must attach `Template::fairing()`.");
info_!("See the `Template` documentation for more information.");
Status::InternalServerError
})?.inner().context();
impl<'r> Responder<'r> for Template {
fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
Box::pin(async move {
let (render, content_type) = {
let ctxt = req.guard::<State<'_, ContextManager>>().succeeded().ok_or_else(|| {
error_!("Uninitialized template context: missing fairing.");
info_!("To use templates, you must attach `Template::fairing()`.");
info_!("See the `Template` documentation for more information.");
Status::InternalServerError
})?.inner().context();

self.finalize(&ctxt)?
};

let (render, content_type) = self.finalize(&ctxt)?;
Content(content_type, render).respond_to(req)
Content(content_type, render).respond_to(req).await
})
}
}
18 changes: 9 additions & 9 deletions contrib/lib/tests/helmet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ mod helmet_tests {
($helmet:expr, $closure:expr) => {{
let rocket = rocket::ignite().mount("/", routes![hello]).attach($helmet);
let client = Client::new(rocket).unwrap();
let response = client.get("/").dispatch();
let response = client.get("/").dispatch().await;
assert_eq!(response.status(), Status::Ok);
$closure(response)
}}
}

#[test]
fn default_headers_test() {
#[rocket::async_test]
async fn default_headers_test() {
dispatch!(SpaceHelmet::default(), |response: LocalResponse<'_>| {
assert_header!(response, "X-XSS-Protection", "1");
assert_header!(response, "X-Frame-Options", "SAMEORIGIN");
assert_header!(response, "X-Content-Type-Options", "nosniff");
})
}

#[test]
fn disable_headers_test() {
#[rocket::async_test]
async fn disable_headers_test() {
let helmet = SpaceHelmet::default().disable::<XssFilter>();
dispatch!(helmet, |response: LocalResponse<'_>| {
assert_header!(response, "X-Frame-Options", "SAMEORIGIN");
Expand Down Expand Up @@ -84,8 +84,8 @@ mod helmet_tests {
});
}

#[test]
fn additional_headers_test() {
#[rocket::async_test]
async fn additional_headers_test() {
let helmet = SpaceHelmet::default()
.enable(Hsts::default())
.enable(ExpectCt::default())
Expand All @@ -108,8 +108,8 @@ mod helmet_tests {
})
}

#[test]
fn uri_test() {
#[rocket::async_test]
async fn uri_test() {
let allow_uri = Uri::parse("https://www.google.com").unwrap();
let report_uri = Uri::parse("https://www.google.com").unwrap();
let enforce_uri = Uri::parse("https://www.google.com").unwrap();
Expand Down
Loading