1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

clippy warnings; fmt

This commit is contained in:
Nikolay Kim 2018-04-28 22:55:47 -07:00
parent a38c3985f6
commit de49796fd1
67 changed files with 988 additions and 1866 deletions

View File

@ -1,7 +1,7 @@
max_width = 89
reorder_imports = true
reorder_imports_in_group = true
reorder_imported_names = true
#reorder_imports_in_group = true
#reorder_imported_names = true
wrap_comments = true
fn_args_density = "Compressed"
#use_small_heuristics = false
use_small_heuristics = false

View File

@ -1,6 +1,5 @@
use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::mem;
use std::rc::Rc;
use handler::Reply;
@ -74,7 +73,7 @@ impl<S: 'static> HttpApplication<S> {
if m {
let path: &'static str = unsafe {
mem::transmute(&req.path()[inner.prefix + prefix.len()..])
&*(&req.path()[inner.prefix + prefix.len()..] as *const _)
};
if path.is_empty() {
req.match_info_mut().add("tail", "");
@ -112,12 +111,7 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
let mut req = req.with_state(Rc::clone(&self.state), self.router.clone());
let tp = self.get_handler(&mut req);
let inner = Rc::clone(&self.inner);
Ok(Box::new(Pipeline::new(
req,
Rc::clone(&self.middlewares),
inner,
tp,
)))
Ok(Box::new(Pipeline::new(req, Rc::clone(&self.middlewares), inner, tp)))
} else {
Err(req)
}
@ -280,7 +274,7 @@ where
{
{
let parts: &mut ApplicationParts<S> = unsafe {
mem::transmute(self.parts.as_mut().expect("Use after finish"))
&mut *(self.parts.as_mut().expect("Use after finish") as *mut _)
};
// get resource handler
@ -455,20 +449,14 @@ where
}
let parts = self.parts.as_mut().expect("Use after finish");
parts
.handlers
.push((path, Box::new(WrapHandler::new(handler))));
parts.handlers.push((path, Box::new(WrapHandler::new(handler))));
}
self
}
/// Register a middleware.
pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> App<S> {
self.parts
.as_mut()
.expect("Use after finish")
.middlewares
.push(Box::new(mw));
self.parts.as_mut().expect("Use after finish").middlewares.push(Box::new(mw));
self
}
@ -623,9 +611,8 @@ mod tests {
#[test]
fn test_default_resource() {
let mut app = App::new()
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
.finish();
let mut app =
App::new().resource("/test", |r| r.f(|_| HttpResponse::Ok())).finish();
let req = TestRequest::with_uri("/test").finish();
let resp = app.run(req);
@ -633,20 +620,14 @@ mod tests {
let req = TestRequest::with_uri("/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let mut app = App::new()
.default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed()))
.finish();
let req = TestRequest::with_uri("/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::METHOD_NOT_ALLOWED
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::METHOD_NOT_ALLOWED);
}
#[test]
@ -660,9 +641,8 @@ mod tests {
#[test]
fn test_state() {
let mut app = App::with_state(10)
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
.finish();
let mut app =
App::with_state(10).resource("/", |r| r.f(|_| HttpResponse::Ok())).finish();
let req =
HttpRequest::default().with_state(Rc::clone(&app.state), app.router.clone());
let resp = app.run(req);
@ -694,9 +674,7 @@ mod tests {
#[test]
fn test_handler() {
let mut app = App::new()
.handler("/test", |_| HttpResponse::Ok())
.finish();
let mut app = App::new().handler("/test", |_| HttpResponse::Ok()).finish();
let req = TestRequest::with_uri("/test").finish();
let resp = app.run(req);
@ -712,24 +690,16 @@ mod tests {
let req = TestRequest::with_uri("/testapp").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}
#[test]
fn test_handler2() {
let mut app = App::new()
.handler("test", |_| HttpResponse::Ok())
.finish();
let mut app = App::new().handler("test", |_| HttpResponse::Ok()).finish();
let req = TestRequest::with_uri("/test").finish();
let resp = app.run(req);
@ -745,17 +715,11 @@ mod tests {
let req = TestRequest::with_uri("/testapp").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}
#[test]
@ -779,68 +743,41 @@ mod tests {
let req = TestRequest::with_uri("/prefix/testapp").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/prefix/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}
#[test]
fn test_route() {
let mut app = App::new()
.route("/test", Method::GET, |_: HttpRequest| {
HttpResponse::Ok()
})
.route("/test", Method::POST, |_: HttpRequest| {
HttpResponse::Created()
})
.route("/test", Method::GET, |_: HttpRequest| HttpResponse::Ok())
.route("/test", Method::POST, |_: HttpRequest| HttpResponse::Created())
.finish();
let req = TestRequest::with_uri("/test")
.method(Method::GET)
.finish();
let req = TestRequest::with_uri("/test").method(Method::GET).finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);
let req = TestRequest::with_uri("/test")
.method(Method::POST)
.finish();
let req = TestRequest::with_uri("/test").method(Method::POST).finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::CREATED
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::CREATED);
let req = TestRequest::with_uri("/test")
.method(Method::HEAD)
.finish();
let req = TestRequest::with_uri("/test").method(Method::HEAD).finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}
#[test]
fn test_handler_prefix() {
let mut app = App::new()
.prefix("/app")
.handler("/test", |_| HttpResponse::Ok())
.finish();
let mut app =
App::new().prefix("/app").handler("/test", |_| HttpResponse::Ok()).finish();
let req = TestRequest::with_uri("/test").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/app/test").finish();
let resp = app.run(req);
@ -856,16 +793,10 @@ mod tests {
let req = TestRequest::with_uri("/app/testapp").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/app/blah").finish();
let resp = app.run(req);
assert_eq!(
resp.as_response().unwrap().status(),
StatusCode::NOT_FOUND
);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}
}

View File

@ -258,9 +258,7 @@ impl Responder for Binary {
type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok()
.content_type("application/octet-stream")
.body(self))
Ok(HttpResponse::Ok().content_type("application/octet-stream").body(self))
}
}

View File

@ -94,13 +94,17 @@ pub struct Pause {
impl Pause {
/// Create message with pause duration parameter
pub fn new(time: Duration) -> Pause {
Pause { time: Some(time) }
Pause {
time: Some(time),
}
}
}
impl Default for Pause {
fn default() -> Pause {
Pause { time: None }
Pause {
time: None,
}
}
}
@ -427,8 +431,7 @@ impl ClientConnector {
} else {
0
};
self.acquired_per_host
.insert(key.clone(), per_host + 1);
self.acquired_per_host.insert(key.clone(), per_host + 1);
}
fn release_key(&mut self, key: &Key) {
@ -439,8 +442,7 @@ impl ClientConnector {
return;
};
if per_host > 1 {
self.acquired_per_host
.insert(key.clone(), per_host - 1);
self.acquired_per_host.insert(key.clone(), per_host - 1);
} else {
self.acquired_per_host.remove(key);
}
@ -516,9 +518,7 @@ impl ClientConnector {
fn collect_periodic(&mut self, ctx: &mut Context<Self>) {
self.collect(true);
// re-schedule next collect period
ctx.run_later(Duration::from_secs(1), |act, ctx| {
act.collect_periodic(ctx)
});
ctx.run_later(Duration::from_secs(1), |act, ctx| act.collect_periodic(ctx));
// send stats
let stats = mem::replace(&mut self.stats, ClientConnectorStats::default());
@ -570,7 +570,7 @@ impl ClientConnector {
}
fn wait_for(
&mut self, key: Key, wait: Duration, conn_timeout: Duration
&mut self, key: Key, wait: Duration, conn_timeout: Duration,
) -> oneshot::Receiver<Result<Connection, ClientConnectorError>> {
// connection is not available, wait
let (tx, rx) = oneshot::channel();
@ -583,10 +583,7 @@ impl ClientConnector {
wait,
conn_timeout,
};
self.waiters
.entry(key)
.or_insert_with(VecDeque::new)
.push_back(waiter);
self.waiters.entry(key).or_insert_with(VecDeque::new).push_back(waiter);
rx
}
}
@ -810,7 +807,7 @@ impl fut::ActorFuture for Maintenance {
type Actor = ClientConnector;
fn poll(
&mut self, act: &mut ClientConnector, ctx: &mut Context<ClientConnector>
&mut self, act: &mut ClientConnector, ctx: &mut Context<ClientConnector>,
) -> Poll<Self::Item, Self::Error> {
// check pause duration
let done = if let Some(Some(ref pause)) = act.paused {
@ -1105,10 +1102,7 @@ impl Pool {
if self.to_close.borrow().is_empty() {
None
} else {
Some(mem::replace(
&mut *self.to_close.borrow_mut(),
Vec::new(),
))
Some(mem::replace(&mut *self.to_close.borrow_mut(), Vec::new()))
}
}
@ -1116,10 +1110,7 @@ impl Pool {
if self.to_release.borrow().is_empty() {
None
} else {
Some(mem::replace(
&mut *self.to_release.borrow_mut(),
Vec::new(),
))
Some(mem::replace(&mut *self.to_release.borrow_mut(), Vec::new()))
}
}

View File

@ -18,9 +18,9 @@ use error::Error;
use error::PayloadError;
use header::ContentEncoding;
use httpmessage::HttpMessage;
use server::WriterState;
use server::encoding::PayloadStream;
use server::shared::SharedBytes;
use server::WriterState;
/// A set of errors that can occur during request sending and response reading
#[derive(Fail, Debug)]
@ -80,7 +80,7 @@ impl SendRequest {
}
pub(crate) fn with_connector(
req: ClientRequest, conn: Addr<Unsync, ClientConnector>
req: ClientRequest, conn: Addr<Unsync, ClientConnector>,
) -> SendRequest {
SendRequest {
req,
@ -269,11 +269,7 @@ impl Pipeline {
#[inline]
fn parse(&mut self) -> Poll<ClientResponse, HttpResponseParserError> {
if let Some(ref mut conn) = self.conn {
match self.parser
.as_mut()
.unwrap()
.parse(conn, &mut self.parser_buf)
{
match self.parser.as_mut().unwrap().parse(conn, &mut self.parser_buf) {
Ok(Async::Ready(resp)) => {
// check content-encoding
if self.should_decompress {
@ -469,9 +465,7 @@ impl Pipeline {
}
// flush io but only if we need to
match self.writer
.poll_completed(self.conn.as_mut().unwrap(), false)
{
match self.writer.poll_completed(self.conn.as_mut().unwrap(), false) {
Ok(Async::Ready(_)) => {
if self.disconnected
|| (self.body_completed && self.writer.is_completed())

View File

@ -499,10 +499,7 @@ impl ClientRequestBuilder {
jar.add(cookie.into_owned());
self.cookies = Some(jar)
} else {
self.cookies
.as_mut()
.unwrap()
.add(cookie.into_owned());
self.cookies.as_mut().unwrap().add(cookie.into_owned());
}
self
}
@ -594,11 +591,7 @@ impl ClientRequestBuilder {
if self.default_headers {
// enable br only for https
let https = if let Some(parts) = parts(&mut self.request, &self.err) {
parts
.uri
.scheme_part()
.map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true)
parts.uri.scheme_part().map(|s| s == &uri::Scheme::HTTPS).unwrap_or(true)
} else {
true
};
@ -610,9 +603,7 @@ impl ClientRequestBuilder {
}
}
let mut request = self.request
.take()
.expect("cannot reuse request builder");
let mut request = self.request.take().expect("cannot reuse request builder");
// set cookies
if let Some(ref mut jar) = self.cookies {
@ -657,9 +648,7 @@ impl ClientRequestBuilder {
S: Stream<Item = Bytes, Error = E> + 'static,
E: Into<Error>,
{
self.body(Body::Streaming(Box::new(
stream.map_err(|e| e.into()),
)))
self.body(Body::Streaming(Box::new(stream.map_err(|e| e.into()))))
}
/// Set an empty body and generate `ClientRequest`
@ -682,7 +671,7 @@ impl ClientRequestBuilder {
#[inline]
fn parts<'a>(
parts: &'a mut Option<ClientRequest>, err: &Option<HttpError>
parts: &'a mut Option<ClientRequest>, err: &Option<HttpError>,
) -> Option<&'a mut ClientRequest> {
if err.is_some() {
return None;

View File

@ -103,12 +103,7 @@ impl ClientResponse {
impl fmt::Debug for ClientResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = writeln!(
f,
"\nClientResponse {:?} {}",
self.version(),
self.status()
);
let res = writeln!(f, "\nClientResponse {:?} {}", self.version(), self.status());
let _ = writeln!(f, " headers:");
for (key, val) in self.headers().iter() {
let _ = writeln!(f, " {:?}: {:?}", key, val);
@ -138,14 +133,12 @@ mod tests {
#[test]
fn test_debug() {
let resp = ClientResponse::new(ClientMessage::default());
resp.as_mut().headers.insert(
header::COOKIE,
HeaderValue::from_static("cookie1=value1"),
);
resp.as_mut().headers.insert(
header::COOKIE,
HeaderValue::from_static("cookie2=value2"),
);
resp.as_mut()
.headers
.insert(header::COOKIE, HeaderValue::from_static("cookie1=value1"));
resp.as_mut()
.headers
.insert(header::COOKIE, HeaderValue::from_static("cookie2=value2"));
let dbg = format!("{:?}", resp);
assert!(dbg.contains("ClientResponse"));

View File

@ -114,10 +114,7 @@ impl HttpClientWriter {
self.buffer,
"{} {} {:?}\r",
msg.method(),
msg.uri()
.path_and_query()
.map(|u| u.as_str())
.unwrap_or("/"),
msg.uri().path_and_query().map(|u| u.as_str()).unwrap_or("/"),
msg.version()
)?;
@ -253,10 +250,8 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder
}
let mut b = BytesMut::new();
let _ = write!(b, "{}", bytes.len());
req.headers_mut().insert(
CONTENT_LENGTH,
HeaderValue::try_from(b.freeze()).unwrap(),
);
req.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::try_from(b.freeze()).unwrap());
TransferEncoding::eof(buf)
}
Body::Streaming(_) | Body::Actor(_) => {
@ -279,10 +274,8 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder
};
if encoding.is_compression() {
req.headers_mut().insert(
CONTENT_ENCODING,
HeaderValue::from_static(encoding.as_str()),
);
req.headers_mut()
.insert(CONTENT_ENCODING, HeaderValue::from_static(encoding.as_str()));
}
req.replace_body(body);

View File

@ -3,7 +3,6 @@ use futures::unsync::oneshot;
use futures::{Async, Future, Poll};
use smallvec::SmallVec;
use std::marker::PhantomData;
use std::mem;
use actix::dev::{ContextImpl, SyncEnvelope, ToEnvelope};
use actix::fut::ActorFuture;
@ -174,7 +173,9 @@ where
if self.stream.is_none() {
self.stream = Some(SmallVec::new());
}
self.stream.as_mut().map(|s| s.push(frame));
if let Some(s) = self.stream.as_mut() {
s.push(frame)
}
self.inner.modify();
}
@ -199,7 +200,7 @@ where
fn poll(&mut self) -> Poll<Option<SmallVec<[Frame; 4]>>, Error> {
let ctx: &mut HttpContext<A, S> =
unsafe { mem::transmute(self as &mut HttpContext<A, S>) };
unsafe { &mut *(self as &mut HttpContext<A, S> as *mut _) };
if self.inner.alive() {
match self.inner.poll(ctx) {
@ -261,7 +262,7 @@ impl<A: Actor> ActorFuture for Drain<A> {
#[inline]
fn poll(
&mut self, _: &mut A, _: &mut <Self::Actor as Actor>::Context
&mut self, _: &mut A, _: &mut <Self::Actor as Actor>::Context,
) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(|_| ())
}

View File

@ -41,7 +41,9 @@ pub struct PathDeserializer<'de, S: 'de> {
impl<'de, S: 'de> PathDeserializer<'de, S> {
pub fn new(req: &'de HttpRequest<S>) -> Self {
PathDeserializer { req }
PathDeserializer {
req,
}
}
}
@ -59,7 +61,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_struct<V>(
self, _: &'static str, _: &'static [&'static str], visitor: V
self, _: &'static str, _: &'static [&'static str], visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -75,7 +77,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_unit_struct<V>(
self, _: &'static str, visitor: V
self, _: &'static str, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -84,7 +86,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_newtype_struct<V>(
self, _: &'static str, visitor: V
self, _: &'static str, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -93,7 +95,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_tuple<V>(
self, len: usize, visitor: V
self, len: usize, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -114,7 +116,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_tuple_struct<V>(
self, _: &'static str, len: usize, visitor: V
self, _: &'static str, len: usize, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -135,7 +137,7 @@ impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
}
fn deserialize_enum<V>(
self, _: &'static str, _: &'static [&'static str], _: V
self, _: &'static str, _: &'static [&'static str], _: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -202,11 +204,12 @@ impl<'de> de::MapAccess<'de> for ParamsDeserializer<'de> {
where
K: de::DeserializeSeed<'de>,
{
self.current = self.params
.next()
.map(|&(ref k, ref v)| (k.as_ref(), v.as_ref()));
self.current =
self.params.next().map(|&(ref k, ref v)| (k.as_ref(), v.as_ref()));
match self.current {
Some((key, _)) => Ok(Some(seed.deserialize(Key { key })?)),
Some((key, _)) => Ok(Some(seed.deserialize(Key {
key,
})?)),
None => Ok(None),
}
}
@ -216,7 +219,9 @@ impl<'de> de::MapAccess<'de> for ParamsDeserializer<'de> {
V: de::DeserializeSeed<'de>,
{
if let Some((_, value)) = self.current.take() {
seed.deserialize(Value { value })
seed.deserialize(Value {
value,
})
} else {
Err(de::value::Error::custom("unexpected item"))
}
@ -301,7 +306,7 @@ impl<'de> Deserializer<'de> for Value<'de> {
}
fn deserialize_unit_struct<V>(
self, _: &'static str, visitor: V
self, _: &'static str, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -331,7 +336,7 @@ impl<'de> Deserializer<'de> for Value<'de> {
}
fn deserialize_enum<V>(
self, _: &'static str, _: &'static [&'static str], visitor: V
self, _: &'static str, _: &'static [&'static str], visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -342,7 +347,7 @@ impl<'de> Deserializer<'de> for Value<'de> {
}
fn deserialize_newtype_struct<V>(
self, _: &'static str, visitor: V
self, _: &'static str, visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -358,7 +363,7 @@ impl<'de> Deserializer<'de> for Value<'de> {
}
fn deserialize_struct<V>(
self, _: &'static str, _: &'static [&'static str], _: V
self, _: &'static str, _: &'static [&'static str], _: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
@ -367,14 +372,12 @@ impl<'de> Deserializer<'de> for Value<'de> {
}
fn deserialize_tuple_struct<V>(
self, _: &'static str, _: usize, _: V
self, _: &'static str, _: usize, _: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(de::value::Error::custom(
"unsupported type: tuple struct",
))
Err(de::value::Error::custom("unsupported type: tuple struct"))
}
unsupported_type!(deserialize_any, "any");
@ -416,7 +419,9 @@ impl<'de> de::EnumAccess<'de> for ValueEnum<'de> {
V: de::DeserializeSeed<'de>,
{
Ok((
seed.deserialize(Key { key: self.value })?,
seed.deserialize(Key {
key: self.value,
})?,
UnitVariant,
))
}
@ -446,7 +451,7 @@ impl<'de> de::VariantAccess<'de> for UnitVariant {
}
fn struct_variant<V>(
self, _: &'static [&'static str], _: V
self, _: &'static [&'static str], _: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,

View File

@ -68,12 +68,7 @@ impl fmt::Debug for Error {
if let Some(bt) = self.cause.backtrace() {
write!(f, "{:?}\n\n{:?}", &self.cause, bt)
} else {
write!(
f,
"{:?}\n\n{:?}",
&self.cause,
self.backtrace.as_ref().unwrap()
)
write!(f, "{:?}\n\n{:?}", &self.cause, self.backtrace.as_ref().unwrap())
}
}
}
@ -298,17 +293,16 @@ pub enum HttpRangeError {
/// Returned if first-byte-pos of all of the byte-range-spec
/// values is greater than the content size.
/// See `https://github.com/golang/go/commit/aa9b3d7`
#[fail(display = "First-byte-pos of all of the byte-range-spec values is greater than the content size")]
#[fail(
display = "First-byte-pos of all of the byte-range-spec values is greater than the content size"
)]
NoOverlap,
}
/// Return `BadRequest` for `HttpRangeError`
impl ResponseError for HttpRangeError {
fn error_response(&self) -> HttpResponse {
HttpResponse::with_body(
StatusCode::BAD_REQUEST,
"Invalid Range header provided",
)
HttpResponse::with_body(StatusCode::BAD_REQUEST, "Invalid Range header provided")
}
}

View File

@ -110,7 +110,9 @@ where
result(
de::Deserialize::deserialize(PathDeserializer::new(&req))
.map_err(|e| e.into())
.map(|inner| Path { inner }),
.map(|inner| Path {
inner,
}),
)
}
}
@ -246,12 +248,7 @@ where
#[inline]
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
Box::new(
UrlEncoded::new(req.clone())
.limit(cfg.limit)
.from_err()
.map(Form),
)
Box::new(UrlEncoded::new(req.clone()).limit(cfg.limit).from_err().map(Form))
}
}
@ -296,7 +293,9 @@ impl FormConfig {
impl Default for FormConfig {
fn default() -> Self {
FormConfig { limit: 262_144 }
FormConfig {
limit: 262_144,
}
}
}
@ -337,11 +336,7 @@ impl<S: 'static> FromRequest<S> for Bytes {
return Either::A(result(Err(e)));
}
Either::B(Box::new(
MessageBody::new(req.clone())
.limit(cfg.limit)
.from_err(),
))
Either::B(Box::new(MessageBody::new(req.clone()).limit(cfg.limit).from_err()))
}
}
@ -387,18 +382,14 @@ impl<S: 'static> FromRequest<S> for String {
// check charset
let encoding = match req.encoding() {
Err(_) => {
return Either::A(result(Err(ErrorBadRequest(
"Unknown request charset",
))))
return Either::A(result(Err(ErrorBadRequest("Unknown request charset"))))
}
Ok(encoding) => encoding,
};
Either::B(Box::new(
MessageBody::new(req.clone())
.limit(cfg.limit)
.from_err()
.and_then(move |body| {
MessageBody::new(req.clone()).limit(cfg.limit).from_err().and_then(
move |body| {
let enc: *const Encoding = encoding as *const Encoding;
if enc == UTF_8 {
Ok(str::from_utf8(body.as_ref())
@ -409,7 +400,8 @@ impl<S: 'static> FromRequest<S> for String {
.decode(&body, DecoderTrap::Strict)
.map_err(|_| ErrorBadRequest("Can not decode body"))?)
}
}),
},
),
))
}
}
@ -485,8 +477,7 @@ mod tests {
fn test_bytes() {
let cfg = PayloadConfig::default();
let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11").finish();
req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world"));
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
match Bytes::from_request(&req, &cfg).poll().unwrap() {
Async::Ready(s) => {
@ -500,8 +491,7 @@ mod tests {
fn test_string() {
let cfg = PayloadConfig::default();
let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11").finish();
req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world"));
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
match String::from_request(&req, &cfg).poll().unwrap() {
Async::Ready(s) => {
@ -518,8 +508,7 @@ mod tests {
"application/x-www-form-urlencoded",
).header(header::CONTENT_LENGTH, "11")
.finish();
req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world"));
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
let mut cfg = FormConfig::default();
cfg.limit(4096);
@ -573,17 +562,11 @@ mod tests {
let mut resource = ResourceHandler::<()>::default();
resource.name("index");
let mut routes = Vec::new();
routes.push((
Resource::new("index", "/{key}/{value}/"),
Some(resource),
));
routes.push((Resource::new("index", "/{key}/{value}/"), Some(resource)));
let (router, _) = Router::new("", ServerSettings::default(), routes);
assert!(router.recognize(&mut req).is_some());
match Path::<MyStruct>::from_request(&req, &())
.poll()
.unwrap()
{
match Path::<MyStruct>::from_request(&req, &()).poll().unwrap() {
Async::Ready(s) => {
assert_eq!(s.key, "name");
assert_eq!(s.value, "user1");
@ -591,10 +574,7 @@ mod tests {
_ => unreachable!(),
}
match Path::<(String, String)>::from_request(&req, &())
.poll()
.unwrap()
{
match Path::<(String, String)>::from_request(&req, &()).poll().unwrap() {
Async::Ready(s) => {
assert_eq!(s.0, "name");
assert_eq!(s.1, "user1");
@ -620,10 +600,7 @@ mod tests {
_ => unreachable!(),
}
match Path::<(String, u8)>::from_request(&req, &())
.poll()
.unwrap()
{
match Path::<(String, u8)>::from_request(&req, &()).poll().unwrap() {
Async::Ready(s) => {
assert_eq!(s.0, "name");
assert_eq!(s.1, 32);
@ -631,15 +608,9 @@ mod tests {
_ => unreachable!(),
}
match Path::<Vec<String>>::from_request(&req, &())
.poll()
.unwrap()
{
match Path::<Vec<String>>::from_request(&req, &()).poll().unwrap() {
Async::Ready(s) => {
assert_eq!(
s.into_inner(),
vec!["name".to_owned(), "32".to_owned()]
);
assert_eq!(s.into_inner(), vec!["name".to_owned(), "32".to_owned()]);
}
_ => unreachable!(),
}

110
src/fs.rs
View File

@ -202,15 +202,12 @@ impl Responder for NamedFile {
if self.status_code != StatusCode::OK {
let mut resp = HttpResponse::build(self.status_code);
resp.if_some(self.path().extension(), |ext, resp| {
resp.set(header::ContentType(get_mime_type(
&ext.to_string_lossy(),
)));
resp.set(header::ContentType(get_mime_type(&ext.to_string_lossy())));
});
let reader = ChunkedReadFile {
size: self.md.len(),
offset: 0,
cpu_pool: self.cpu_pool
.unwrap_or_else(|| req.cpu_pool().clone()),
cpu_pool: self.cpu_pool.unwrap_or_else(|| req.cpu_pool().clone()),
file: Some(self.file),
fut: None,
};
@ -253,9 +250,7 @@ impl Responder for NamedFile {
let mut resp = HttpResponse::build(self.status_code);
resp.if_some(self.path().extension(), |ext, resp| {
resp.set(header::ContentType(get_mime_type(
&ext.to_string_lossy(),
)));
resp.set(header::ContentType(get_mime_type(&ext.to_string_lossy())));
}).if_some(last_modified, |lm, resp| {
resp.set(header::LastModified(lm));
})
@ -275,8 +270,7 @@ impl Responder for NamedFile {
let reader = ChunkedReadFile {
size: self.md.len(),
offset: 0,
cpu_pool: self.cpu_pool
.unwrap_or_else(|| req.cpu_pool().clone()),
cpu_pool: self.cpu_pool.unwrap_or_else(|| req.cpu_pool().clone()),
file: Some(self.file),
fut: None,
};
@ -344,7 +338,10 @@ pub struct Directory {
impl Directory {
pub fn new(base: PathBuf, path: PathBuf) -> Directory {
Directory { base, path }
Directory {
base,
path,
}
}
fn can_list(&self, entry: &io::Result<DirEntry>) -> bool {
@ -414,9 +411,7 @@ impl Responder for Directory {
</ul></body>\n</html>",
index_of, index_of, body
);
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html))
Ok(HttpResponse::Ok().content_type("text/html; charset=utf-8").body(html))
}
}
@ -541,13 +536,12 @@ impl<S: 'static> Handler<S> for StaticFiles<S> {
if !self.accessible {
Ok(self.default.handle(req))
} else {
let relpath = match req.match_info()
.get("tail")
.map(|tail| PathBuf::from_param(tail))
{
Some(Ok(path)) => path,
_ => return Ok(self.default.handle(req)),
};
let relpath =
match req.match_info().get("tail").map(|tail| PathBuf::from_param(tail))
{
Some(Ok(path)) => path,
_ => return Ok(self.default.handle(req)),
};
// full filepath
let path = self.directory.join(&relpath).canonicalize()?;
@ -597,9 +591,8 @@ mod tests {
#[test]
fn test_named_file() {
assert!(NamedFile::open("test--").is_err());
let mut file = NamedFile::open("Cargo.toml")
.unwrap()
.set_cpu_pool(CpuPool::new(1));
let mut file =
NamedFile::open("Cargo.toml").unwrap().set_cpu_pool(CpuPool::new(1));
{
file.file();
let _f: &File = &file;
@ -609,10 +602,7 @@ mod tests {
}
let resp = file.respond_to(HttpRequest::default()).unwrap();
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/x-toml"
)
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml")
}
#[test]
@ -630,10 +620,7 @@ mod tests {
}
let resp = file.respond_to(HttpRequest::default()).unwrap();
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/x-toml"
);
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml");
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
@ -676,9 +663,7 @@ mod tests {
req.match_info_mut().add("tail", "");
st.show_index = true;
let resp = st.handle(req)
.respond_to(HttpRequest::default())
.unwrap();
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
@ -694,28 +679,18 @@ mod tests {
let mut req = HttpRequest::default();
req.match_info_mut().add("tail", "tests");
let resp = st.handle(req)
.respond_to(HttpRequest::default())
.unwrap();
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(
resp.headers().get(header::LOCATION).unwrap(),
"/tests/index.html"
);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/tests/index.html");
let mut req = HttpRequest::default();
req.match_info_mut().add("tail", "tests/");
let resp = st.handle(req)
.respond_to(HttpRequest::default())
.unwrap();
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(
resp.headers().get(header::LOCATION).unwrap(),
"/tests/index.html"
);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/tests/index.html");
}
#[test]
@ -724,9 +699,7 @@ mod tests {
let mut req = HttpRequest::default();
req.match_info_mut().add("tail", "tools/wsload");
let resp = st.handle(req)
.respond_to(HttpRequest::default())
.unwrap();
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(
@ -746,23 +719,13 @@ mod tests {
let request = srv.get().uri(srv.url("/public")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response
.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap();
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/public/Cargo.toml");
let request = srv.get().uri(srv.url("/public/")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response
.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap();
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/public/Cargo.toml");
}
@ -775,23 +738,13 @@ mod tests {
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response
.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap();
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/test/Cargo.toml");
let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response
.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap();
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/test/Cargo.toml");
}
@ -801,10 +754,7 @@ mod tests {
App::new().handler("test", StaticFiles::new(".").index_file("Cargo.toml"))
});
let request = srv.get()
.uri(srv.url("/test/%43argo.toml"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/test/%43argo.toml")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::OK);
}

View File

@ -1,5 +1,5 @@
use futures::Poll;
use futures::future::{err, ok, Future, FutureResult};
use futures::Poll;
use std::marker::PhantomData;
use std::ops::Deref;
@ -296,14 +296,13 @@ where
#[inline]
fn respond_to(self, req: HttpRequest) -> Result<Reply, Error> {
let fut = self.map_err(|e| e.into())
.then(move |r| match r.respond_to(req) {
Ok(reply) => match reply.into().0 {
ReplyItem::Message(resp) => ok(resp),
_ => panic!("Nested async replies are not supported"),
},
Err(e) => err(e),
});
let fut = self.map_err(|e| e.into()).then(move |r| match r.respond_to(req) {
Ok(reply) => match reply.into().0 {
ReplyItem::Message(resp) => ok(resp),
_ => panic!("Nested async replies are not supported"),
},
Err(e) => err(e),
});
Ok(Reply::async(fut))
}
}

View File

@ -57,7 +57,10 @@ impl EntityTag {
/// If the tag contains invalid characters.
pub fn new(weak: bool, tag: String) -> EntityTag {
assert!(check_slice_validity(&tag), "Invalid tag: {:?}", tag);
EntityTag { weak, tag }
EntityTag {
weak,
tag,
}
}
/// Constructs a new weak EntityTag.
@ -196,11 +199,7 @@ mod tests {
fn test_etag_parse_failures() {
// Expected failures
assert!("no-dquotes".parse::<EntityTag>().is_err());
assert!(
"w/\"the-first-w-is-case-sensitive\""
.parse::<EntityTag>()
.is_err()
);
assert!("w/\"the-first-w-is-case-sensitive\"".parse::<EntityTag>().is_err());
assert!("".parse::<EntityTag>().is_err());
assert!("\"unmatched-dquotes1".parse::<EntityTag>().is_err());
assert!("unmatched-dquotes2\"".parse::<EntityTag>().is_err());
@ -209,26 +208,14 @@ mod tests {
#[test]
fn test_etag_fmt() {
assert_eq!(
format!("{}", EntityTag::strong("foobar".to_owned())),
"\"foobar\""
);
assert_eq!(
format!("{}", EntityTag::strong("".to_owned())),
"\"\""
);
assert_eq!(format!("{}", EntityTag::strong("foobar".to_owned())), "\"foobar\"");
assert_eq!(format!("{}", EntityTag::strong("".to_owned())), "\"\"");
assert_eq!(
format!("{}", EntityTag::weak("weak-etag".to_owned())),
"W/\"weak-etag\""
);
assert_eq!(
format!("{}", EntityTag::weak("\u{0065}".to_owned())),
"W/\"\x65\""
);
assert_eq!(
format!("{}", EntityTag::weak("".to_owned())),
"W/\"\""
);
assert_eq!(format!("{}", EntityTag::weak("\u{0065}".to_owned())), "W/\"\x65\"");
assert_eq!(format!("{}", EntityTag::weak("".to_owned())), "W/\"\"");
}
#[test]

View File

@ -64,11 +64,7 @@ impl IntoHeaderValue for HttpDate {
fn try_into(self) -> Result<HeaderValue, Self::Error> {
let mut wrt = BytesMut::with_capacity(29).writer();
write!(wrt, "{}", self.0.rfc822()).unwrap();
unsafe {
Ok(HeaderValue::from_shared_unchecked(
wrt.get_mut().take().freeze(),
))
}
unsafe { Ok(HeaderValue::from_shared_unchecked(wrt.get_mut().take().freeze())) }
}
}
@ -104,24 +100,12 @@ mod tests {
#[test]
fn test_date() {
assert_eq!("Sun, 07 Nov 1994 08:48:37 GMT".parse::<HttpDate>().unwrap(), NOV_07);
assert_eq!(
"Sun, 07 Nov 1994 08:48:37 GMT"
.parse::<HttpDate>()
.unwrap(),
NOV_07
);
assert_eq!(
"Sunday, 07-Nov-94 08:48:37 GMT"
.parse::<HttpDate>()
.unwrap(),
NOV_07
);
assert_eq!(
"Sun Nov 7 08:48:37 1994"
.parse::<HttpDate>()
.unwrap(),
"Sunday, 07-Nov-94 08:48:37 GMT".parse::<HttpDate>().unwrap(),
NOV_07
);
assert_eq!("Sun Nov 7 08:48:37 1994".parse::<HttpDate>().unwrap(), NOV_07);
assert!("this-is-no-date".parse::<HttpDate>().is_err());
}
}

View File

@ -47,7 +47,10 @@ impl<T> QualityItem<T> {
/// The item can be of any type.
/// The quality should be a value in the range [0, 1].
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
QualityItem { item, quality }
QualityItem {
item,
quality,
}
}
}
@ -63,11 +66,7 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
match self.quality.0 {
1000 => Ok(()),
0 => f.write_str("; q=0"),
x => write!(
f,
"; q=0.{}",
format!("{:03}", x).trim_right_matches('0')
),
x => write!(f, "; q=0.{}", format!("{:03}", x).trim_right_matches('0')),
}
}
}
@ -120,10 +119,7 @@ fn from_f32(f: f32) -> Quality {
// this function is only used internally. A check that `f` is within range
// should be done before calling this method. Just in case, this
// debug_assert should catch if we were forgetful
debug_assert!(
f >= 0f32 && f <= 1f32,
"q value must be between 0.0 and 1.0"
);
debug_assert!(f >= 0f32 && f <= 1f32, "q value must be between 0.0 and 1.0");
Quality((f * 1000f32) as u16)
}
@ -156,10 +152,7 @@ mod internal {
impl IntoQuality for f32 {
fn into_quality(self) -> Quality {
assert!(
self >= 0f32 && self <= 1f32,
"float must be between 0.0 and 1.0"
);
assert!(self >= 0f32 && self <= 1f32, "float must be between 0.0 and 1.0");
super::from_f32(self)
}
}
@ -295,10 +288,6 @@ mod tests {
#[test]
fn test_fuzzing_bugs() {
assert!("99999;".parse::<QualityItem<String>>().is_err());
assert!(
"\x0d;;;=\u{d6aa}=="
.parse::<QualityItem<String>>()
.is_err()
)
assert!("\x0d;;;=\u{d6aa}==".parse::<QualityItem<String>>().is_err())
}
}

View File

@ -190,16 +190,8 @@ mod tests {
// trailing slashes
let params = vec![
("/resource1", "", StatusCode::OK),
(
"/resource1/",
"/resource1",
StatusCode::MOVED_PERMANENTLY,
),
(
"/resource2",
"/resource2/",
StatusCode::MOVED_PERMANENTLY,
),
("/resource1/", "/resource1", StatusCode::MOVED_PERMANENTLY),
("/resource2", "/resource2/", StatusCode::MOVED_PERMANENTLY),
("/resource2/", "", StatusCode::OK),
("/resource1?p1=1&p2=2", "", StatusCode::OK),
(
@ -222,11 +214,7 @@ mod tests {
if !target.is_empty() {
assert_eq!(
target,
r.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap()
r.headers().get(header::LOCATION).unwrap().to_str().unwrap()
);
}
}
@ -238,11 +226,7 @@ mod tests {
.resource("/resource1", |r| r.method(Method::GET).f(index))
.resource("/resource2/", |r| r.method(Method::GET).f(index))
.default_resource(|r| {
r.h(NormalizePath::new(
false,
true,
StatusCode::MOVED_PERMANENTLY,
))
r.h(NormalizePath::new(false, true, StatusCode::MOVED_PERMANENTLY))
})
.finish();
@ -276,46 +260,14 @@ mod tests {
// trailing slashes
let params = vec![
("/resource1/a/b", "", StatusCode::OK),
(
"/resource1/",
"/resource1",
StatusCode::MOVED_PERMANENTLY,
),
(
"/resource1//",
"/resource1",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource1//a//b",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource1//a//b/",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource1//a//b//",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"///resource1//a//b",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource1/a///b",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource1/a//b/",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
("/resource1/", "/resource1", StatusCode::MOVED_PERMANENTLY),
("/resource1//", "/resource1", StatusCode::MOVED_PERMANENTLY),
("//resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("//resource1//a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("//resource1//a//b//", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/////resource1/a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/resource1/a/b?p=1", "", StatusCode::OK),
(
"//resource1//a//b?p=1",
@ -356,11 +308,7 @@ mod tests {
if !target.is_empty() {
assert_eq!(
target,
r.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap()
r.headers().get(header::LOCATION).unwrap().to_str().unwrap()
);
}
}
@ -379,88 +327,24 @@ mod tests {
// trailing slashes
let params = vec![
("/resource1/a/b", "", StatusCode::OK),
(
"/resource1/a/b/",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource2//a//b",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource2//a//b/",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource2//a//b//",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"///resource1//a//b",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"///resource1//a//b/",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource1/a///b",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource1/a///b/",
"/resource1/a/b",
StatusCode::MOVED_PERMANENTLY,
),
(
"/resource2/a/b",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
("/resource1/a/b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("//resource2//a//b//", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("///resource1//a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/////resource1/a///b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
("/resource2/a/b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("/resource2/a/b/", "", StatusCode::OK),
(
"//resource2//a//b",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"//resource2//a//b/",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"///resource2//a//b",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"///resource2//a//b/",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource2/a///b",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
(
"/////resource2/a///b/",
"/resource2/a/b/",
StatusCode::MOVED_PERMANENTLY,
),
("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("///resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("///resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("/////resource2/a///b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("/////resource2/a///b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
("/resource1/a/b?p=1", "", StatusCode::OK),
(
"/resource1/a/b/?p=1",
"/resource1/a/b?p=1",
StatusCode::MOVED_PERMANENTLY,
),
("/resource1/a/b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
(
"//resource2//a//b?p=1",
"/resource2/a/b/?p=1",
@ -496,11 +380,7 @@ mod tests {
"/resource1/a/b?p=1",
StatusCode::MOVED_PERMANENTLY,
),
(
"/resource2/a/b?p=1",
"/resource2/a/b/?p=1",
StatusCode::MOVED_PERMANENTLY,
),
("/resource2/a/b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
(
"//resource2//a//b?p=1",
"/resource2/a/b/?p=1",
@ -540,11 +420,7 @@ mod tests {
if !target.is_empty() {
assert_eq!(
target,
r.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap()
r.headers().get(header::LOCATION).unwrap().to_str().unwrap()
);
}
}

View File

@ -14,32 +14,37 @@ pub const HttpOk: StaticResponse = StaticResponse(StatusCode::OK);
pub const HttpCreated: StaticResponse = StaticResponse(StatusCode::CREATED);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Accepted()` instead")]
pub const HttpAccepted: StaticResponse = StaticResponse(StatusCode::ACCEPTED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::pNonAuthoritativeInformation()` instead")]
#[deprecated(
since = "0.5.0",
note = "please use `HttpResponse::pNonAuthoritativeInformation()` instead"
)]
pub const HttpNonAuthoritativeInformation: StaticResponse =
StaticResponse(StatusCode::NON_AUTHORITATIVE_INFORMATION);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::NoContent()` instead")]
pub const HttpNoContent: StaticResponse = StaticResponse(StatusCode::NO_CONTENT);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::ResetContent()` instead")]
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::ResetContent()` instead")]
pub const HttpResetContent: StaticResponse = StaticResponse(StatusCode::RESET_CONTENT);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::PartialContent()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::PartialContent()` instead"
)]
pub const HttpPartialContent: StaticResponse =
StaticResponse(StatusCode::PARTIAL_CONTENT);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::MultiStatus()` instead")]
pub const HttpMultiStatus: StaticResponse = StaticResponse(StatusCode::MULTI_STATUS);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::AlreadyReported()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::AlreadyReported()` instead"
)]
pub const HttpAlreadyReported: StaticResponse =
StaticResponse(StatusCode::ALREADY_REPORTED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::MultipleChoices()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::MultipleChoices()` instead"
)]
pub const HttpMultipleChoices: StaticResponse =
StaticResponse(StatusCode::MULTIPLE_CHOICES);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::MovedPermanently()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::MovedPermanently()` instead"
)]
pub const HttpMovedPermanently: StaticResponse =
StaticResponse(StatusCode::MOVED_PERMANENTLY);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Found()` instead")]
@ -50,106 +55,125 @@ pub const HttpSeeOther: StaticResponse = StaticResponse(StatusCode::SEE_OTHER);
pub const HttpNotModified: StaticResponse = StaticResponse(StatusCode::NOT_MODIFIED);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::UseProxy()` instead")]
pub const HttpUseProxy: StaticResponse = StaticResponse(StatusCode::USE_PROXY);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::TemporaryRedirect()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::TemporaryRedirect()` instead"
)]
pub const HttpTemporaryRedirect: StaticResponse =
StaticResponse(StatusCode::TEMPORARY_REDIRECT);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::PermanentRedirect()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::PermanentRedirect()` instead"
)]
pub const HttpPermanentRedirect: StaticResponse =
StaticResponse(StatusCode::PERMANENT_REDIRECT);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::BadRequest()` instead")]
pub const HttpBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::Unauthorized()` instead")]
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Unauthorized()` instead")]
pub const HttpUnauthorized: StaticResponse = StaticResponse(StatusCode::UNAUTHORIZED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::PaymentRequired()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::PaymentRequired()` instead"
)]
pub const HttpPaymentRequired: StaticResponse =
StaticResponse(StatusCode::PAYMENT_REQUIRED);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Forbidden()` instead")]
pub const HttpForbidden: StaticResponse = StaticResponse(StatusCode::FORBIDDEN);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::NotFound()` instead")]
pub const HttpNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::MethodNotAllowed()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::MethodNotAllowed()` instead"
)]
pub const HttpMethodNotAllowed: StaticResponse =
StaticResponse(StatusCode::METHOD_NOT_ALLOWED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::NotAcceptable()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::NotAcceptable()` instead"
)]
pub const HttpNotAcceptable: StaticResponse = StaticResponse(StatusCode::NOT_ACCEPTABLE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::ProxyAuthenticationRequired()` instead")]
#[deprecated(
since = "0.5.0",
note = "please use `HttpResponse::ProxyAuthenticationRequired()` instead"
)]
pub const HttpProxyAuthenticationRequired: StaticResponse =
StaticResponse(StatusCode::PROXY_AUTHENTICATION_REQUIRED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::RequestTimeout()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::RequestTimeout()` instead"
)]
pub const HttpRequestTimeout: StaticResponse =
StaticResponse(StatusCode::REQUEST_TIMEOUT);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Conflict()` instead")]
pub const HttpConflict: StaticResponse = StaticResponse(StatusCode::CONFLICT);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::Gone()` instead")]
pub const HttpGone: StaticResponse = StaticResponse(StatusCode::GONE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::LengthRequired()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::LengthRequired()` instead"
)]
pub const HttpLengthRequired: StaticResponse =
StaticResponse(StatusCode::LENGTH_REQUIRED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::PreconditionFailed()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::PreconditionFailed()` instead"
)]
pub const HttpPreconditionFailed: StaticResponse =
StaticResponse(StatusCode::PRECONDITION_FAILED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::PayloadTooLarge()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::PayloadTooLarge()` instead"
)]
pub const HttpPayloadTooLarge: StaticResponse =
StaticResponse(StatusCode::PAYLOAD_TOO_LARGE);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::UriTooLong()` instead")]
pub const HttpUriTooLong: StaticResponse = StaticResponse(StatusCode::URI_TOO_LONG);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::UnsupportedMediaType()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::UnsupportedMediaType()` instead"
)]
pub const HttpUnsupportedMediaType: StaticResponse =
StaticResponse(StatusCode::UNSUPPORTED_MEDIA_TYPE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::RangeNotSatisfiable()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::RangeNotSatisfiable()` instead"
)]
pub const HttpRangeNotSatisfiable: StaticResponse =
StaticResponse(StatusCode::RANGE_NOT_SATISFIABLE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::ExpectationFailed()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::ExpectationFailed()` instead"
)]
pub const HttpExpectationFailed: StaticResponse =
StaticResponse(StatusCode::EXPECTATION_FAILED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::InternalServerError()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::InternalServerError()` instead"
)]
pub const HttpInternalServerError: StaticResponse =
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::NotImplemented()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::NotImplemented()` instead"
)]
pub const HttpNotImplemented: StaticResponse =
StaticResponse(StatusCode::NOT_IMPLEMENTED);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::BadGateway()` instead")]
pub const HttpBadGateway: StaticResponse = StaticResponse(StatusCode::BAD_GATEWAY);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::ServiceUnavailable()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::ServiceUnavailable()` instead"
)]
pub const HttpServiceUnavailable: StaticResponse =
StaticResponse(StatusCode::SERVICE_UNAVAILABLE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::GatewayTimeout()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::GatewayTimeout()` instead"
)]
pub const HttpGatewayTimeout: StaticResponse =
StaticResponse(StatusCode::GATEWAY_TIMEOUT);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::VersionNotSupported()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::VersionNotSupported()` instead"
)]
pub const HttpVersionNotSupported: StaticResponse =
StaticResponse(StatusCode::HTTP_VERSION_NOT_SUPPORTED);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::VariantAlsoNegotiates()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::VariantAlsoNegotiates()` instead"
)]
pub const HttpVariantAlsoNegotiates: StaticResponse =
StaticResponse(StatusCode::VARIANT_ALSO_NEGOTIATES);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::InsufficientStorage()` instead")]
#[deprecated(
since = "0.5.0", note = "please use `HttpResponse::InsufficientStorage()` instead"
)]
pub const HttpInsufficientStorage: StaticResponse =
StaticResponse(StatusCode::INSUFFICIENT_STORAGE);
#[deprecated(since = "0.5.0",
note = "please use `HttpResponse::LoopDetected()` instead")]
#[deprecated(since = "0.5.0", note = "please use `HttpResponse::LoopDetected()` instead")]
pub const HttpLoopDetected: StaticResponse = StaticResponse(StatusCode::LOOP_DETECTED);
#[deprecated(since = "0.5.0", note = "please use `HttpResponse` instead")]
@ -221,10 +245,7 @@ impl HttpResponse {
STATIC_RESP!(Ok, StatusCode::OK);
STATIC_RESP!(Created, StatusCode::CREATED);
STATIC_RESP!(Accepted, StatusCode::ACCEPTED);
STATIC_RESP!(
NonAuthoritativeInformation,
StatusCode::NON_AUTHORITATIVE_INFORMATION
);
STATIC_RESP!(NonAuthoritativeInformation, StatusCode::NON_AUTHORITATIVE_INFORMATION);
STATIC_RESP!(NoContent, StatusCode::NO_CONTENT);
STATIC_RESP!(ResetContent, StatusCode::RESET_CONTENT);
@ -249,10 +270,7 @@ impl HttpResponse {
STATIC_RESP!(Forbidden, StatusCode::FORBIDDEN);
STATIC_RESP!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED);
STATIC_RESP!(NotAcceptable, StatusCode::NOT_ACCEPTABLE);
STATIC_RESP!(
ProxyAuthenticationRequired,
StatusCode::PROXY_AUTHENTICATION_REQUIRED
);
STATIC_RESP!(ProxyAuthenticationRequired, StatusCode::PROXY_AUTHENTICATION_REQUIRED);
STATIC_RESP!(RequestTimeout, StatusCode::REQUEST_TIMEOUT);
STATIC_RESP!(Conflict, StatusCode::CONFLICT);
STATIC_RESP!(Gone, StatusCode::GONE);
@ -260,10 +278,7 @@ impl HttpResponse {
STATIC_RESP!(PreconditionFailed, StatusCode::PRECONDITION_FAILED);
STATIC_RESP!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
STATIC_RESP!(UriTooLong, StatusCode::URI_TOO_LONG);
STATIC_RESP!(
UnsupportedMediaType,
StatusCode::UNSUPPORTED_MEDIA_TYPE
);
STATIC_RESP!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
@ -272,14 +287,8 @@ impl HttpResponse {
STATIC_RESP!(BadGateway, StatusCode::BAD_GATEWAY);
STATIC_RESP!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE);
STATIC_RESP!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT);
STATIC_RESP!(
VersionNotSupported,
StatusCode::HTTP_VERSION_NOT_SUPPORTED
);
STATIC_RESP!(
VariantAlsoNegotiates,
StatusCode::VARIANT_ALSO_NEGOTIATES
);
STATIC_RESP!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
STATIC_RESP!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
STATIC_RESP!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
STATIC_RESP!(LoopDetected, StatusCode::LOOP_DETECTED);
}

View File

@ -1,8 +1,8 @@
use bytes::{Bytes, BytesMut};
use encoding::EncodingRef;
use encoding::all::UTF_8;
use encoding::label::encoding_from_whatwg_label;
use encoding::types::{DecoderTrap, Encoding};
use encoding::EncodingRef;
use futures::{Future, Poll, Stream};
use http::{header, HeaderMap};
use http_range::HttpRange;
@ -96,10 +96,8 @@ pub trait HttpMessage {
/// `size` is full size of response (file).
fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> {
if let Some(range) = self.headers().get(header::RANGE) {
HttpRange::parse(
unsafe { str::from_utf8_unchecked(range.as_bytes()) },
size,
).map_err(|e| e.into())
HttpRange::parse(unsafe { str::from_utf8_unchecked(range.as_bytes()) }, size)
.map_err(|e| e.into())
} else {
Ok(Vec::new())
}
@ -325,10 +323,7 @@ where
));
}
self.fut
.as_mut()
.expect("UrlEncoded could not be used second time")
.poll()
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
}
}
@ -385,8 +380,7 @@ where
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
return Err(UrlencodedError::ContentType);
}
let encoding = req.encoding()
.map_err(|_| UrlencodedError::ContentType)?;
let encoding = req.encoding().map_err(|_| UrlencodedError::ContentType)?;
// future
let limit = self.limit;
@ -415,18 +409,15 @@ where
self.fut = Some(Box::new(fut));
}
self.fut
.as_mut()
.expect("UrlEncoded could not be used second time")
.poll()
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
}
}
#[cfg(test)]
mod tests {
use super::*;
use encoding::Encoding;
use encoding::all::ISO_8859_2;
use encoding::Encoding;
use futures::Async;
use http::{Method, Uri, Version};
use httprequest::HttpRequest;
@ -488,19 +479,13 @@ mod tests {
#[test]
fn test_encoding_error() {
let req = TestRequest::with_header("content-type", "applicatjson").finish();
assert_eq!(
Some(ContentTypeError::ParseError),
req.encoding().err()
);
assert_eq!(Some(ContentTypeError::ParseError), req.encoding().err());
let req = TestRequest::with_header(
"content-type",
"application/json; charset=kkkttktk",
).finish();
assert_eq!(
Some(ContentTypeError::UnknownEncoding),
req.encoding().err()
);
assert_eq!(Some(ContentTypeError::UnknownEncoding), req.encoding().err());
}
#[test]
@ -621,8 +606,7 @@ mod tests {
"application/x-www-form-urlencoded",
).header(header::CONTENT_LENGTH, "11")
.finish();
req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world"));
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
let result = req.urlencoded::<Info>().poll().ok().unwrap();
assert_eq!(
@ -637,8 +621,7 @@ mod tests {
"application/x-www-form-urlencoded; charset=utf-8",
).header(header::CONTENT_LENGTH, "11")
.finish();
req.payload_mut()
.unread_data(Bytes::from_static(b"hello=world"));
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
let result = req.urlencoded().poll().ok().unwrap();
assert_eq!(
@ -664,16 +647,14 @@ mod tests {
}
let mut req = HttpRequest::default();
req.payload_mut()
.unread_data(Bytes::from_static(b"test"));
req.payload_mut().unread_data(Bytes::from_static(b"test"));
match req.body().poll().ok().unwrap() {
Async::Ready(bytes) => assert_eq!(bytes, Bytes::from_static(b"test")),
_ => unreachable!("error"),
}
let mut req = HttpRequest::default();
req.payload_mut()
.unread_data(Bytes::from_static(b"11111111111111"));
req.payload_mut().unread_data(Bytes::from_static(b"11111111111111"));
match req.body().limit(5).poll().err().unwrap() {
PayloadError::Overflow => (),
_ => unreachable!("error"),

View File

@ -1,4 +1,5 @@
//! HTTP Request message related code.
#![cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ptr))]
use bytes::Bytes;
use cookie::Cookie;
use failure;
@ -314,7 +315,7 @@ impl<S> HttpRequest<S> {
/// }
/// ```
pub fn url_for<U, I>(
&self, name: &str, elements: U
&self, name: &str, elements: U,
) -> Result<Url, UrlGenerationError>
where
U: IntoIterator<Item = I>,
@ -326,12 +327,7 @@ impl<S> HttpRequest<S> {
let path = self.router().unwrap().resource_path(name, elements)?;
if path.starts_with('/') {
let conn = self.connection_info();
Ok(Url::parse(&format!(
"{}://{}{}",
conn.scheme(),
conn.host(),
path
))?)
Ok(Url::parse(&format!("{}://{}{}", conn.scheme(), conn.host(), path))?)
} else {
Ok(Url::parse(&path)?)
}
@ -681,12 +677,8 @@ mod tests {
let mut resource = ResourceHandler::<()>::default();
resource.name("index");
let routes = vec![
(
Resource::new("index", "/user/{name}.{ext}"),
Some(resource),
),
];
let routes =
vec![(Resource::new("index", "/user/{name}.{ext}"), Some(resource))];
let (router, _) = Router::new("/", ServerSettings::default(), routes);
assert!(router.has_route("/user/test.html"));
assert!(!router.has_route("/test/unknown"));
@ -715,12 +707,8 @@ mod tests {
let mut resource = ResourceHandler::<()>::default();
resource.name("index");
let routes = vec![
(
Resource::new("index", "/user/{name}.{ext}"),
Some(resource),
),
];
let routes =
vec![(Resource::new("index", "/user/{name}.{ext}"), Some(resource))];
let (router, _) = Router::new("/prefix/", ServerSettings::default(), routes);
assert!(router.has_route("/user/test.html"));
assert!(!router.has_route("/prefix/user/test.html"));
@ -739,20 +727,15 @@ mod tests {
let mut resource = ResourceHandler::<()>::default();
resource.name("index");
let routes = vec![
(
Resource::external("youtube", "https://youtube.com/watch/{video_id}"),
None,
),
];
let routes = vec![(
Resource::external("youtube", "https://youtube.com/watch/{video_id}"),
None,
)];
let (router, _) = Router::new::<()>("", ServerSettings::default(), routes);
assert!(!router.has_route("https://youtube.com/watch/unknown"));
let req = req.with_state(Rc::new(()), router);
let url = req.url_for("youtube", &["oHg5SJYRHA0"]);
assert_eq!(
url.ok().unwrap().as_str(),
"https://youtube.com/watch/oHg5SJYRHA0"
);
assert_eq!(url.ok().unwrap().as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
}
}

View File

@ -150,10 +150,7 @@ impl HttpResponse {
if let Some(reason) = self.get_ref().reason {
reason
} else {
self.get_ref()
.status
.canonical_reason()
.unwrap_or("<unknown status code>")
self.get_ref().status.canonical_reason().unwrap_or("<unknown status code>")
}
}
@ -466,10 +463,7 @@ impl HttpResponseBuilder {
jar.add(cookie.into_owned());
self.cookies = Some(jar)
} else {
self.cookies
.as_mut()
.unwrap()
.add(cookie.into_owned());
self.cookies.as_mut().unwrap().add(cookie.into_owned());
}
self
}
@ -534,9 +528,7 @@ impl HttpResponseBuilder {
if let Some(e) = self.err.take() {
return Error::from(e).into();
}
let mut response = self.response
.take()
.expect("cannot reuse response builder");
let mut response = self.response.take().expect("cannot reuse response builder");
if let Some(ref jar) = self.cookies {
for cookie in jar.delta() {
match HeaderValue::from_str(&cookie.to_string()) {
@ -558,9 +550,7 @@ impl HttpResponseBuilder {
S: Stream<Item = Bytes, Error = E> + 'static,
E: Into<Error>,
{
self.body(Body::Streaming(Box::new(
stream.map_err(|e| e.into()),
)))
self.body(Body::Streaming(Box::new(stream.map_err(|e| e.into()))))
}
/// Set a json body and generate `HttpResponse`
@ -607,7 +597,7 @@ impl HttpResponseBuilder {
#[inline]
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn parts<'a>(
parts: &'a mut Option<Box<InnerHttpResponse>>, err: &Option<HttpError>
parts: &'a mut Option<Box<InnerHttpResponse>>, err: &Option<HttpError>,
) -> Option<&'a mut Box<InnerHttpResponse>> {
if err.is_some() {
return None;
@ -643,9 +633,7 @@ impl Responder for HttpResponseBuilder {
impl From<&'static str> for HttpResponse {
fn from(val: &'static str) -> Self {
HttpResponse::Ok()
.content_type("text/plain; charset=utf-8")
.body(val)
HttpResponse::Ok().content_type("text/plain; charset=utf-8").body(val)
}
}
@ -662,9 +650,7 @@ impl Responder for &'static str {
impl From<&'static [u8]> for HttpResponse {
fn from(val: &'static [u8]) -> Self {
HttpResponse::Ok()
.content_type("application/octet-stream")
.body(val)
HttpResponse::Ok().content_type("application/octet-stream").body(val)
}
}
@ -681,9 +667,7 @@ impl Responder for &'static [u8] {
impl From<String> for HttpResponse {
fn from(val: String) -> Self {
HttpResponse::Ok()
.content_type("text/plain; charset=utf-8")
.body(val)
HttpResponse::Ok().content_type("text/plain; charset=utf-8").body(val)
}
}
@ -719,9 +703,7 @@ impl<'a> Responder for &'a String {
impl From<Bytes> for HttpResponse {
fn from(val: Bytes) -> Self {
HttpResponse::Ok()
.content_type("application/octet-stream")
.body(val)
HttpResponse::Ok().content_type("application/octet-stream").body(val)
}
}
@ -738,9 +720,7 @@ impl Responder for Bytes {
impl From<BytesMut> for HttpResponse {
fn from(val: BytesMut) -> Self {
HttpResponse::Ok()
.content_type("application/octet-stream")
.body(val)
HttpResponse::Ok().content_type("application/octet-stream").body(val)
}
}
@ -772,9 +752,7 @@ impl<'a> From<&'a ClientResponse> for HttpResponseBuilder {
impl<'a, S> From<&'a HttpRequest<S>> for HttpResponseBuilder {
fn from(req: &'a HttpRequest<S>) -> HttpResponseBuilder {
if let Some(router) = req.router() {
router
.server_settings()
.get_response_builder(StatusCode::OK)
router.server_settings().get_response_builder(StatusCode::OK)
} else {
HttpResponse::Ok()
}
@ -822,14 +800,12 @@ thread_local!(static POOL: Rc<UnsafeCell<HttpResponsePool>> = HttpResponsePool::
impl HttpResponsePool {
pub fn pool() -> Rc<UnsafeCell<HttpResponsePool>> {
Rc::new(UnsafeCell::new(HttpResponsePool(
VecDeque::with_capacity(128),
)))
Rc::new(UnsafeCell::new(HttpResponsePool(VecDeque::with_capacity(128))))
}
#[inline]
pub fn get_builder(
pool: &Rc<UnsafeCell<HttpResponsePool>>, status: StatusCode
pool: &Rc<UnsafeCell<HttpResponsePool>>, status: StatusCode,
) -> HttpResponseBuilder {
let p = unsafe { &mut *pool.as_ref().get() };
if let Some(mut msg) = p.0.pop_front() {
@ -853,7 +829,7 @@ impl HttpResponsePool {
#[inline]
pub fn get_response(
pool: &Rc<UnsafeCell<HttpResponsePool>>, status: StatusCode, body: Body
pool: &Rc<UnsafeCell<HttpResponsePool>>, status: StatusCode, body: Body,
) -> HttpResponse {
let p = unsafe { &mut *pool.as_ref().get() };
if let Some(mut msg) = p.0.pop_front() {
@ -879,7 +855,7 @@ impl HttpResponsePool {
#[inline(always)]
#[cfg_attr(feature = "cargo-clippy", allow(boxed_local, inline_always))]
fn release(
pool: &Rc<UnsafeCell<HttpResponsePool>>, mut inner: Box<InnerHttpResponse>
pool: &Rc<UnsafeCell<HttpResponsePool>>, mut inner: Box<InnerHttpResponse>,
) {
let pool = unsafe { &mut *pool.as_ref().get() };
if pool.0.len() < 128 {
@ -975,9 +951,7 @@ mod tests {
#[test]
fn test_force_close() {
let resp = HttpResponse::build(StatusCode::OK)
.force_close()
.finish();
let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
assert!(!resp.keep_alive().unwrap())
}
@ -986,10 +960,7 @@ mod tests {
let resp = HttpResponse::build(StatusCode::OK)
.content_type("text/plain")
.body(Body::Empty);
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
"text/plain"
)
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
}
#[test]
@ -1073,10 +1044,7 @@ mod tests {
HeaderValue::from_static("application/octet-stream")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(b"test".as_ref())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref()));
let resp: HttpResponse = b"test".as_ref().respond_to(req.clone()).ok().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
@ -1085,10 +1053,7 @@ mod tests {
HeaderValue::from_static("application/octet-stream")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(b"test".as_ref())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref()));
let resp: HttpResponse = "test".to_owned().into();
assert_eq!(resp.status(), StatusCode::OK);
@ -1097,26 +1062,16 @@ mod tests {
HeaderValue::from_static("text/plain; charset=utf-8")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from("test".to_owned())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned()));
let resp: HttpResponse = "test"
.to_owned()
.respond_to(req.clone())
.ok()
.unwrap();
let resp: HttpResponse = "test".to_owned().respond_to(req.clone()).ok().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("text/plain; charset=utf-8")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from("test".to_owned())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned()));
let resp: HttpResponse = (&"test".to_owned()).into();
assert_eq!(resp.status(), StatusCode::OK);
@ -1125,25 +1080,17 @@ mod tests {
HeaderValue::from_static("text/plain; charset=utf-8")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(&"test".to_owned())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned()));
let resp: HttpResponse = (&"test".to_owned())
.respond_to(req.clone())
.ok()
.unwrap();
let resp: HttpResponse =
(&"test".to_owned()).respond_to(req.clone()).ok().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("text/plain; charset=utf-8")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(&"test".to_owned())
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned()));
let b = Bytes::from_static(b"test");
let resp: HttpResponse = b.into();
@ -1179,10 +1126,7 @@ mod tests {
HeaderValue::from_static("application/octet-stream")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(BytesMut::from("test"))
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test")));
let b = BytesMut::from("test");
let resp: HttpResponse = b.respond_to(req.clone()).ok().unwrap();
@ -1192,10 +1136,7 @@ mod tests {
HeaderValue::from_static("application/octet-stream")
);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body().binary().unwrap(),
&Binary::from(BytesMut::from("test"))
);
assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test")));
}
#[test]

View File

@ -53,8 +53,8 @@ impl<'a> ConnectionInfo<'a> {
// scheme
if scheme.is_none() {
if let Some(h) = req.headers()
.get(HeaderName::from_str(X_FORWARDED_PROTO).unwrap())
if let Some(h) =
req.headers().get(HeaderName::from_str(X_FORWARDED_PROTO).unwrap())
{
if let Ok(h) = h.to_str() {
scheme = h.split(',').next().map(|v| v.trim());
@ -74,8 +74,8 @@ impl<'a> ConnectionInfo<'a> {
// host
if host.is_none() {
if let Some(h) = req.headers()
.get(HeaderName::from_str(X_FORWARDED_HOST).unwrap())
if let Some(h) =
req.headers().get(HeaderName::from_str(X_FORWARDED_HOST).unwrap())
{
if let Ok(h) = h.to_str() {
host = h.split(',').next().map(|v| v.trim());
@ -98,8 +98,8 @@ impl<'a> ConnectionInfo<'a> {
// remote addr
if remote.is_none() {
if let Some(h) = req.headers()
.get(HeaderName::from_str(X_FORWARDED_FOR).unwrap())
if let Some(h) =
req.headers().get(HeaderName::from_str(X_FORWARDED_FOR).unwrap())
{
if let Ok(h) = h.to_str() {
remote = h.split(',').next().map(|v| v.trim());
@ -189,10 +189,8 @@ mod tests {
assert_eq!(info.remote(), Some("192.0.2.60"));
let mut req = HttpRequest::default();
req.headers_mut().insert(
header::HOST,
HeaderValue::from_static("rust-lang.org"),
);
req.headers_mut()
.insert(header::HOST, HeaderValue::from_static("rust-lang.org"));
let info = ConnectionInfo::new(&req);
assert_eq!(info.scheme(), "http");

View File

@ -6,8 +6,8 @@ use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use mime;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json;
use error::{Error, JsonPayloadError, PayloadError};
@ -308,10 +308,7 @@ where
self.fut = Some(Box::new(fut));
}
self.fut
.as_mut()
.expect("JsonBody could not be used second time")
.poll()
self.fut.as_mut().expect("JsonBody could not be used second time").poll()
}
}
@ -362,10 +359,7 @@ mod tests {
fn test_json_body() {
let req = HttpRequest::default();
let mut json = req.json::<MyObject>();
assert_eq!(
json.poll().err().unwrap(),
JsonPayloadError::ContentType
);
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
let mut req = HttpRequest::default();
req.headers_mut().insert(
@ -373,20 +367,15 @@ mod tests {
header::HeaderValue::from_static("application/text"),
);
let mut json = req.json::<MyObject>();
assert_eq!(
json.poll().err().unwrap(),
JsonPayloadError::ContentType
);
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
let mut req = HttpRequest::default();
req.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
);
req.headers_mut().insert(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("10000"),
);
req.headers_mut()
.insert(header::CONTENT_LENGTH, header::HeaderValue::from_static("10000"));
let mut json = req.json::<MyObject>().limit(100);
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::Overflow);
@ -395,12 +384,9 @@ mod tests {
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
);
req.headers_mut().insert(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("16"),
);
req.payload_mut()
.unread_data(Bytes::from_static(b"{\"name\": \"test\"}"));
req.headers_mut()
.insert(header::CONTENT_LENGTH, header::HeaderValue::from_static("16"));
req.payload_mut().unread_data(Bytes::from_static(b"{\"name\": \"test\"}"));
let mut json = req.json::<MyObject>();
assert_eq!(
json.poll().ok().unwrap(),
@ -417,12 +403,7 @@ mod tests {
let mut handler = With::new(|data: Json<MyObject>| data, cfg);
let req = HttpRequest::default();
let err = handler
.handle(req)
.as_response()
.unwrap()
.error()
.is_some();
let err = handler.handle(req).as_response().unwrap().error().is_some();
assert!(err);
let mut req = HttpRequest::default();
@ -430,18 +411,10 @@ mod tests {
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
);
req.headers_mut().insert(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("16"),
);
req.payload_mut()
.unread_data(Bytes::from_static(b"{\"name\": \"test\"}"));
let ok = handler
.handle(req)
.as_response()
.unwrap()
.error()
.is_none();
req.headers_mut()
.insert(header::CONTENT_LENGTH, header::HeaderValue::from_static("16"));
req.payload_mut().unread_data(Bytes::from_static(b"{\"name\": \"test\"}"));
let ok = handler.handle(req).as_response().unwrap().error().is_none();
assert!(ok)
}
}

View File

@ -64,26 +64,36 @@ use resource::ResourceHandler;
#[derive(Debug, Fail)]
pub enum CorsError {
/// The HTTP request header `Origin` is required but was not provided
#[fail(display = "The HTTP request header `Origin` is required but was not provided")]
#[fail(
display = "The HTTP request header `Origin` is required but was not provided"
)]
MissingOrigin,
/// The HTTP request header `Origin` could not be parsed correctly.
#[fail(display = "The HTTP request header `Origin` could not be parsed correctly.")]
BadOrigin,
/// The request header `Access-Control-Request-Method` is required but is
/// missing
#[fail(display = "The request header `Access-Control-Request-Method` is required but is missing")]
#[fail(
display = "The request header `Access-Control-Request-Method` is required but is missing"
)]
MissingRequestMethod,
/// The request header `Access-Control-Request-Method` has an invalid value
#[fail(display = "The request header `Access-Control-Request-Method` has an invalid value")]
#[fail(
display = "The request header `Access-Control-Request-Method` has an invalid value"
)]
BadRequestMethod,
/// The request header `Access-Control-Request-Headers` has an invalid
/// value
#[fail(display = "The request header `Access-Control-Request-Headers` has an invalid value")]
#[fail(
display = "The request header `Access-Control-Request-Headers` has an invalid value"
)]
BadRequestHeaders,
/// The request header `Access-Control-Request-Headers` is required but is
/// missing.
#[fail(display = "The request header `Access-Control-Request-Headers` is required but is
missing")]
#[fail(
display = "The request header `Access-Control-Request-Headers` is required but is
missing"
)]
MissingRequestHeaders,
/// Origin is not allowed to make this request
#[fail(display = "Origin is not allowed to make this request")]
@ -265,9 +275,7 @@ impl Cors {
/// `ResourceHandler::middleware()` method, but in that case *Cors*
/// middleware wont be able to handle *OPTIONS* requests.
pub fn register<S: 'static>(self, resource: &mut ResourceHandler<S>) {
resource
.method(Method::OPTIONS)
.h(|_| HttpResponse::Ok());
resource.method(Method::OPTIONS).h(|_| HttpResponse::Ok());
resource.middleware(self);
}
@ -292,11 +300,9 @@ impl Cors {
}
fn validate_allowed_method<S>(
&self, req: &mut HttpRequest<S>
&self, req: &mut HttpRequest<S>,
) -> Result<(), CorsError> {
if let Some(hdr) = req.headers()
.get(header::ACCESS_CONTROL_REQUEST_METHOD)
{
if let Some(hdr) = req.headers().get(header::ACCESS_CONTROL_REQUEST_METHOD) {
if let Ok(meth) = hdr.to_str() {
if let Ok(method) = Method::try_from(meth) {
return self.inner
@ -313,13 +319,13 @@ impl Cors {
}
fn validate_allowed_headers<S>(
&self, req: &mut HttpRequest<S>
&self, req: &mut HttpRequest<S>,
) -> Result<(), CorsError> {
match self.inner.headers {
AllOrSome::All => Ok(()),
AllOrSome::Some(ref allowed_headers) => {
if let Some(hdr) = req.headers()
.get(header::ACCESS_CONTROL_REQUEST_HEADERS)
if let Some(hdr) =
req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS)
{
if let Ok(headers) = hdr.to_str() {
let mut hdrs = HashSet::new();
@ -361,8 +367,8 @@ impl<S> Middleware<S> for Cors {
.as_str()[1..],
).unwrap(),
)
} else if let Some(hdr) = req.headers()
.get(header::ACCESS_CONTROL_REQUEST_HEADERS)
} else if let Some(hdr) =
req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS)
{
Some(hdr.clone())
} else {
@ -419,7 +425,7 @@ impl<S> Middleware<S> for Cors {
}
fn response(
&self, req: &mut HttpRequest<S>, mut resp: HttpResponse
&self, req: &mut HttpRequest<S>, mut resp: HttpResponse,
) -> Result<Response> {
match self.inner.origins {
AllOrSome::All => {
@ -506,7 +512,7 @@ pub struct CorsBuilder<S = ()> {
}
fn cors<'a>(
parts: &'a mut Option<Inner>, err: &Option<http::Error>
parts: &'a mut Option<Inner>, err: &Option<http::Error>,
) -> Option<&'a mut Inner> {
if err.is_some() {
return None;
@ -813,17 +819,13 @@ impl<S: 'static> CorsBuilder<S> {
}
if let AllOrSome::Some(ref origins) = cors.origins {
let s = origins
.iter()
.fold(String::new(), |s, v| s + &format!("{}", v));
let s = origins.iter().fold(String::new(), |s, v| s + &v.to_string());
cors.origins_str = Some(HeaderValue::try_from(s.as_str()).unwrap());
}
if !self.expose_hdrs.is_empty() {
cors.expose_hdrs = Some(
self.expose_hdrs
.iter()
.fold(String::new(), |s, v| s + v.as_str())[1..]
self.expose_hdrs.iter().fold(String::new(), |s, v| s + v.as_str())[1..]
.to_owned(),
);
}
@ -901,27 +903,19 @@ mod tests {
#[test]
#[should_panic(expected = "Credentials are allowed, but the Origin is set to")]
fn cors_validates_illegal_allow_credentials() {
Cors::build()
.supports_credentials()
.send_wildcard()
.finish();
Cors::build().supports_credentials().send_wildcard().finish();
}
#[test]
#[should_panic(expected = "No resources are registered")]
fn no_resource() {
Cors::build()
.supports_credentials()
.send_wildcard()
.register();
Cors::build().supports_credentials().send_wildcard().register();
}
#[test]
#[should_panic(expected = "Cors::for_app(app)")]
fn no_resource2() {
Cors::build()
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
.register();
Cors::build().resource("/test", |r| r.f(|_| HttpResponse::Ok())).register();
}
#[test]
@ -958,27 +952,18 @@ mod tests {
let mut req = TestRequest::with_header("Origin", "https://www.example.com")
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "POST")
.header(
header::ACCESS_CONTROL_REQUEST_HEADERS,
"AUTHORIZATION,ACCEPT",
)
.header(header::ACCESS_CONTROL_REQUEST_HEADERS, "AUTHORIZATION,ACCEPT")
.method(Method::OPTIONS)
.finish();
let resp = cors.start(&mut req).unwrap().response();
assert_eq!(
&b"*"[..],
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.unwrap()
.as_bytes()
resp.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap().as_bytes()
);
assert_eq!(
&b"3600"[..],
resp.headers()
.get(header::ACCESS_CONTROL_MAX_AGE)
.unwrap()
.as_bytes()
resp.headers().get(header::ACCESS_CONTROL_MAX_AGE).unwrap().as_bytes()
);
//assert_eq!(
// &b"authorization,accept,content-type"[..],
@ -995,9 +980,7 @@ mod tests {
#[test]
#[should_panic(expected = "MissingOrigin")]
fn test_validate_missing_origin() {
let cors = Cors::build()
.allowed_origin("https://www.example.com")
.finish();
let cors = Cors::build().allowed_origin("https://www.example.com").finish();
let mut req = HttpRequest::default();
cors.start(&mut req).unwrap();
@ -1006,9 +989,7 @@ mod tests {
#[test]
#[should_panic(expected = "OriginNotAllowed")]
fn test_validate_not_allowed_origin() {
let cors = Cors::build()
.allowed_origin("https://www.example.com")
.finish();
let cors = Cors::build().allowed_origin("https://www.example.com").finish();
let mut req = TestRequest::with_header("Origin", "https://www.unknown.com")
.method(Method::GET)
@ -1018,9 +999,7 @@ mod tests {
#[test]
fn test_validate_origin() {
let cors = Cors::build()
.allowed_origin("https://www.example.com")
.finish();
let cors = Cors::build().allowed_origin("https://www.example.com").finish();
let mut req = TestRequest::with_header("Origin", "https://www.example.com")
.method(Method::GET)
@ -1036,11 +1015,7 @@ mod tests {
let mut req = TestRequest::default().method(Method::GET).finish();
let resp: HttpResponse = HttpResponse::Ok().into();
let resp = cors.response(&mut req, resp).unwrap().response();
assert!(
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.is_none()
);
assert!(resp.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).is_none());
let mut req = TestRequest::with_header("Origin", "https://www.example.com")
.method(Method::OPTIONS)
@ -1048,10 +1023,7 @@ mod tests {
let resp = cors.response(&mut req, resp).unwrap().response();
assert_eq!(
&b"https://www.example.com"[..],
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.unwrap()
.as_bytes()
resp.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap().as_bytes()
);
}
@ -1074,19 +1046,12 @@ mod tests {
let resp = cors.response(&mut req, resp).unwrap().response();
assert_eq!(
&b"*"[..],
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.unwrap()
.as_bytes()
);
assert_eq!(
&b"Origin"[..],
resp.headers().get(header::VARY).unwrap().as_bytes()
resp.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap().as_bytes()
);
assert_eq!(&b"Origin"[..], resp.headers().get(header::VARY).unwrap().as_bytes());
let resp: HttpResponse = HttpResponse::Ok()
.header(header::VARY, "Accept")
.finish();
let resp: HttpResponse =
HttpResponse::Ok().header(header::VARY, "Accept").finish();
let resp = cors.response(&mut req, resp).unwrap().response();
assert_eq!(
&b"Accept, Origin"[..],
@ -1101,10 +1066,7 @@ mod tests {
let resp = cors.response(&mut req, resp).unwrap().response();
assert_eq!(
&b"https://www.example.com"[..],
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.unwrap()
.as_bytes()
resp.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap().as_bytes()
);
}

View File

@ -89,10 +89,7 @@ fn origin(headers: &HeaderMap) -> Option<Result<Cow<str>, CsrfError>> {
headers
.get(header::ORIGIN)
.map(|origin| {
origin
.to_str()
.map_err(|_| CsrfError::BadOrigin)
.map(|o| o.into())
origin.to_str().map_err(|_| CsrfError::BadOrigin).map(|o| o.into())
})
.or_else(|| {
headers.get(header::REFERER).map(|referer| {
@ -261,9 +258,8 @@ mod tests {
fn test_upgrade() {
let strict_csrf = CsrfFilter::new().allowed_origin("https://www.example.com");
let lax_csrf = CsrfFilter::new()
.allowed_origin("https://www.example.com")
.allow_upgrade();
let lax_csrf =
CsrfFilter::new().allowed_origin("https://www.example.com").allow_upgrade();
let mut req = TestRequest::with_header("Origin", "https://cswsh.com")
.header("Connection", "Upgrade")

View File

@ -76,7 +76,7 @@ impl DefaultHeaders {
impl<S> Middleware<S> for DefaultHeaders {
fn response(
&self, _: &mut HttpRequest<S>, mut resp: HttpResponse
&self, _: &mut HttpRequest<S>, mut resp: HttpResponse,
) -> Result<Response> {
for (key, value) in self.headers.iter() {
if !resp.headers().contains_key(key) {
@ -112,9 +112,7 @@ mod tests {
};
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
let resp = HttpResponse::Ok()
.header(CONTENT_TYPE, "0002")
.finish();
let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish();
let resp = match mw.response(&mut req, resp) {
Ok(Response::Done(resp)) => resp,
_ => panic!(),

View File

@ -69,7 +69,7 @@ impl<S> ErrorHandlers<S> {
impl<S: 'static> Middleware<S> for ErrorHandlers<S> {
fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> {
if let Some(handler) = self.handlers.get(&resp.status()) {
handler(req, resp)
@ -82,8 +82,8 @@ impl<S: 'static> Middleware<S> for ErrorHandlers<S> {
#[cfg(test)]
mod tests {
use super::*;
use http::StatusCode;
use http::header::CONTENT_TYPE;
use http::StatusCode;
fn render_500<S>(_: &mut HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
let mut builder = resp.into_builder();

View File

@ -49,8 +49,8 @@
use std::rc::Rc;
use cookie::{Cookie, CookieJar, Key};
use futures::future::{err as FutErr, ok as FutOk, FutureResult};
use futures::Future;
use futures::future::{FutureResult, err as FutErr, ok as FutOk};
use time::Duration;
use error::{Error, Result};
@ -164,7 +164,9 @@ pub struct IdentityService<T> {
impl<T> IdentityService<T> {
/// Create new identity service with specified backend.
pub fn new(backend: T) -> Self {
IdentityService { backend }
IdentityService {
backend,
}
}
}
@ -179,20 +181,18 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
let mut req = req.clone();
let fut = self.backend
.from_request(&mut req)
.then(move |res| match res {
Ok(id) => {
req.extensions().insert(IdentityBox(Box::new(id)));
FutOk(None)
}
Err(err) => FutErr(err),
});
let fut = self.backend.from_request(&mut req).then(move |res| match res {
Ok(id) => {
req.extensions().insert(IdentityBox(Box::new(id)));
FutOk(None)
}
Err(err) => FutErr(err),
});
Ok(Started::Future(Box::new(fut)))
}
fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> {
if let Some(mut id) = req.extensions().remove::<IdentityBox>() {
id.0.write(resp)

View File

@ -254,10 +254,9 @@ impl FormatText {
"-".fmt(fmt)
}
}
FormatText::RequestTime => entry_time
.strftime("[%d/%b/%Y:%H:%M:%S %z]")
.unwrap()
.fmt(fmt),
FormatText::RequestTime => {
entry_time.strftime("[%d/%b/%Y:%H:%M:%S %z]").unwrap().fmt(fmt)
}
FormatText::RequestHeader(ref name) => {
let s = if let Some(val) = req.headers().get(name) {
if let Ok(s) = val.to_str() {
@ -314,10 +313,8 @@ mod tests {
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test");
let mut headers = HeaderMap::new();
headers.insert(
header::USER_AGENT,
header::HeaderValue::from_static("ACTIX-WEB"),
);
headers
.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let mut req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
@ -354,10 +351,8 @@ mod tests {
let format = Format::default();
let mut headers = HeaderMap::new();
headers.insert(
header::USER_AGENT,
header::HeaderValue::from_static("ACTIX-WEB"),
);
headers
.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
@ -365,9 +360,7 @@ mod tests {
headers,
None,
);
let resp = HttpResponse::build(StatusCode::OK)
.force_close()
.finish();
let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
let entry_time = time::now();
let render = |fmt: &mut Formatter| {
@ -388,9 +381,7 @@ mod tests {
HeaderMap::new(),
None,
);
let resp = HttpResponse::build(StatusCode::OK)
.force_close()
.finish();
let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
let entry_time = time::now();
let render = |fmt: &mut Formatter| {

View File

@ -21,8 +21,9 @@ pub use self::logger::Logger;
#[cfg(feature = "session")]
#[doc(hidden)]
#[deprecated(since = "0.5.4",
note = "please use `actix_web::middleware::session` instead")]
#[deprecated(
since = "0.5.4", note = "please use `actix_web::middleware::session` instead"
)]
pub use self::session::{CookieSessionBackend, CookieSessionError, RequestSession,
Session, SessionBackend, SessionImpl, SessionStorage};
@ -65,7 +66,7 @@ pub trait Middleware<S>: 'static {
/// Method is called when handler returns response,
/// but before sending http message to peer.
fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> {
Ok(Response::Done(resp))
}

View File

@ -69,8 +69,8 @@ use std::rc::Rc;
use std::sync::Arc;
use cookie::{Cookie, CookieJar, Key};
use futures::future::{err as FutErr, ok as FutOk, FutureResult};
use futures::Future;
use futures::future::{FutureResult, err as FutErr, ok as FutOk};
use http::header::{self, HeaderValue};
use serde::{Deserialize, Serialize};
use serde_json;
@ -202,21 +202,18 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
let mut req = req.clone();
let fut = self.0
.from_request(&mut req)
.then(move |res| match res {
Ok(sess) => {
req.extensions()
.insert(Arc::new(SessionImplBox(Box::new(sess))));
FutOk(None)
}
Err(err) => FutErr(err),
});
let fut = self.0.from_request(&mut req).then(move |res| match res {
Ok(sess) => {
req.extensions().insert(Arc::new(SessionImplBox(Box::new(sess))));
FutOk(None)
}
Err(err) => FutErr(err),
});
Ok(Started::Future(Box::new(fut)))
}
fn response(
&self, req: &mut HttpRequest<S>, resp: HttpResponse
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> {
if let Some(s_box) = req.extensions().remove::<Arc<SessionImplBox>>() {
s_box.0.write(resp)
@ -349,7 +346,7 @@ impl CookieSessionInner {
}
fn set_cookie(
&self, resp: &mut HttpResponse, state: &HashMap<String, String>
&self, resp: &mut HttpResponse, state: &HashMap<String, String>,
) -> Result<()> {
let value =
serde_json::to_string(&state).map_err(CookieSessionError::Serialize)?;

View File

@ -7,8 +7,8 @@ use std::{cmp, fmt};
use bytes::Bytes;
use futures::task::{current as current_task, Task};
use futures::{Async, Poll, Stream};
use http::HttpTryFrom;
use http::header::{self, HeaderMap, HeaderName, HeaderValue};
use http::HttpTryFrom;
use httparse;
use mime;
@ -122,11 +122,7 @@ where
if let Some(err) = self.error.take() {
Err(err)
} else if self.safety.current() {
self.inner
.as_mut()
.unwrap()
.borrow_mut()
.poll(&self.safety)
self.inner.as_mut().unwrap().borrow_mut().poll(&self.safety)
} else {
Ok(Async::NotReady)
}
@ -168,7 +164,7 @@ where
}
fn read_boundary(
payload: &mut PayloadHelper<S>, boundary: &str
payload: &mut PayloadHelper<S>, boundary: &str,
) -> Poll<bool, MultipartError> {
// TODO: need to read epilogue
match payload.readline()? {
@ -192,7 +188,7 @@ where
}
fn skip_until_boundary(
payload: &mut PayloadHelper<S>, boundary: &str
payload: &mut PayloadHelper<S>, boundary: &str,
) -> Poll<bool, MultipartError> {
let mut eof = false;
loop {
@ -230,7 +226,7 @@ where
}
fn poll(
&mut self, safety: &Safety
&mut self, safety: &Safety,
) -> Poll<Option<MultipartItem<S>>, MultipartError> {
if self.state == InnerState::Eof {
Ok(Async::Ready(None))
@ -450,7 +446,7 @@ where
S: Stream<Item = Bytes, Error = PayloadError>,
{
fn new(
payload: PayloadRef<S>, boundary: String, headers: &HeaderMap
payload: PayloadRef<S>, boundary: String, headers: &HeaderMap,
) -> Result<InnerField<S>, PayloadError> {
let len = if let Some(len) = headers.get(header::CONTENT_LENGTH) {
if let Ok(s) = len.to_str() {
@ -477,7 +473,7 @@ where
/// Reads body part content chunk of the specified size.
/// The body part must has `Content-Length` header with proper value.
fn read_len(
payload: &mut PayloadHelper<S>, size: &mut u64
payload: &mut PayloadHelper<S>, size: &mut u64,
) -> Poll<Option<Bytes>, MultipartError> {
if *size == 0 {
Ok(Async::Ready(None))
@ -502,7 +498,7 @@ where
/// Reads content chunk of body part with unknown length.
/// The `Content-Length` header for body part is not necessary.
fn read_stream(
payload: &mut PayloadHelper<S>, boundary: &str
payload: &mut PayloadHelper<S>, boundary: &str,
) -> Poll<Option<Bytes>, MultipartError> {
match payload.read_until(b"\r")? {
Async::NotReady => Ok(Async::NotReady),
@ -675,10 +671,7 @@ mod tests {
}
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("test"),
);
headers.insert(header::CONTENT_TYPE, header::HeaderValue::from_static("test"));
match Multipart::boundary(&headers) {
Err(MultipartError::ParseContentType) => (),

View File

@ -94,8 +94,7 @@ impl<'a, 'b, 'c: 'a> Index<&'b str> for &'c Params<'a> {
type Output = str;
fn index(&self, name: &'b str) -> &str {
self.get(name)
.expect("Value for parameter is not available")
self.get(name).expect("Value for parameter is not available")
}
}
@ -202,18 +201,9 @@ mod tests {
PathBuf::from_param("/test/*tt"),
Err(UriSegmentError::BadStart('*'))
);
assert_eq!(
PathBuf::from_param("/test/tt:"),
Err(UriSegmentError::BadEnd(':'))
);
assert_eq!(
PathBuf::from_param("/test/tt<"),
Err(UriSegmentError::BadEnd('<'))
);
assert_eq!(
PathBuf::from_param("/test/tt>"),
Err(UriSegmentError::BadEnd('>'))
);
assert_eq!(PathBuf::from_param("/test/tt:"), Err(UriSegmentError::BadEnd(':')));
assert_eq!(PathBuf::from_param("/test/tt<"), Err(UriSegmentError::BadEnd('<')));
assert_eq!(PathBuf::from_param("/test/tt>"), Err(UriSegmentError::BadEnd('>')));
assert_eq!(
PathBuf::from_param("/seg1/seg2/"),
Ok(PathBuf::from_iter(vec!["seg1", "seg2"]))

View File

@ -47,7 +47,9 @@ impl Payload {
PayloadSender {
inner: Rc::downgrade(&shared),
},
Payload { inner: shared },
Payload {
inner: shared,
},
)
}
@ -534,10 +536,7 @@ mod tests {
assert_eq!(format!("{}", err.cause().unwrap()), "ParseError");
let err = PayloadError::Incomplete;
assert_eq!(
format!("{}", err),
"A payload reached EOF, but is not complete."
);
assert_eq!(format!("{}", err), "A payload reached EOF, but is not complete.");
}
#[test]
@ -671,10 +670,7 @@ mod tests {
let (mut sender, payload) = Payload::new(false);
let mut payload = PayloadHelper::new(payload);
assert_eq!(
Async::NotReady,
payload.read_until(b"ne").ok().unwrap()
);
assert_eq!(Async::NotReady, payload.read_until(b"ne").ok().unwrap());
sender.feed_data(Bytes::from("line1"));
sender.feed_data(Bytes::from("line2"));

View File

@ -67,7 +67,7 @@ impl<S: 'static, H: PipelineHandler<S>> PipelineState<S, H> {
}
struct PipelineInfo<S> {
req: HttpRequest<S>,
req: UnsafeCell<HttpRequest<S>>,
count: u16,
mws: Rc<Vec<Box<Middleware<S>>>>,
context: Option<Box<ActorHttpContext>>,
@ -79,7 +79,7 @@ struct PipelineInfo<S> {
impl<S> PipelineInfo<S> {
fn new(req: HttpRequest<S>) -> PipelineInfo<S> {
PipelineInfo {
req,
req: UnsafeCell::new(req),
count: 0,
mws: Rc::new(Vec::new()),
error: None,
@ -89,11 +89,17 @@ impl<S> PipelineInfo<S> {
}
}
#[inline]
fn req(&self) -> &HttpRequest<S> {
unsafe { &*self.req.get() }
}
#[inline]
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))]
fn req_mut(&self) -> &mut HttpRequest<S> {
#[allow(mutable_transmutes)]
unsafe {
mem::transmute(&self.req)
&mut *self.req.get()
}
}
@ -116,8 +122,8 @@ impl<S: 'static, H: PipelineHandler<S>> Pipeline<S, H> {
handler: Rc<UnsafeCell<H>>, htype: HandlerType,
) -> Pipeline<S, H> {
let mut info = PipelineInfo {
req,
mws,
req: UnsafeCell::new(req),
count: 0,
error: None,
context: None,
@ -159,7 +165,7 @@ impl<S: 'static, H: PipelineHandler<S>> HttpHandlerTask for Pipeline<S, H> {
}
fn poll_io(&mut self, io: &mut Writer) -> Poll<bool, Error> {
let info: &mut PipelineInfo<_> = unsafe { mem::transmute(&mut self.0) };
let info: &mut PipelineInfo<_> = unsafe { &mut *(&mut self.0 as *mut _) };
loop {
if self.1.is_response() {
@ -197,7 +203,7 @@ impl<S: 'static, H: PipelineHandler<S>> HttpHandlerTask for Pipeline<S, H> {
}
fn poll(&mut self) -> Poll<(), Error> {
let info: &mut PipelineInfo<_> = unsafe { mem::transmute(&mut self.0) };
let info: &mut PipelineInfo<_> = unsafe { &mut *(&mut self.0 as *mut _) };
loop {
match self.1 {
@ -228,17 +234,17 @@ struct StartMiddlewares<S, H> {
impl<S: 'static, H: PipelineHandler<S>> StartMiddlewares<S, H> {
fn init(
info: &mut PipelineInfo<S>, hnd: Rc<UnsafeCell<H>>, htype: HandlerType
info: &mut PipelineInfo<S>, hnd: Rc<UnsafeCell<H>>, htype: HandlerType,
) -> PipelineState<S, H> {
// execute middlewares, we need this stage because middlewares could be
// non-async and we can move to next state immediately
let len = info.mws.len() as u16;
loop {
if info.count == len {
let reply = unsafe { &mut *hnd.get() }.handle(info.req.clone(), htype);
let reply = unsafe { &mut *hnd.get() }.handle(info.req().clone(), htype);
return WaitingResponse::init(info, reply);
} else {
match info.mws[info.count as usize].start(&mut info.req) {
match info.mws[info.count as usize].start(info.req_mut()) {
Ok(Started::Done) => info.count += 1,
Ok(Started::Response(resp)) => {
return RunMiddlewares::init(info, resp)
@ -278,7 +284,7 @@ impl<S: 'static, H: PipelineHandler<S>> StartMiddlewares<S, H> {
}
if info.count == len {
let reply = unsafe { &mut *self.hnd.get() }
.handle(info.req.clone(), self.htype);
.handle(info.req().clone(), self.htype);
return Some(WaitingResponse::init(info, reply));
} else {
loop {
@ -462,7 +468,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
}
fn poll_io(
mut self, io: &mut Writer, info: &mut PipelineInfo<S>
mut self, io: &mut Writer, info: &mut PipelineInfo<S>,
) -> Result<PipelineState<S, H>, PipelineState<S, H>> {
loop {
if self.drain.is_none() && self.running != RunningState::Paused {
@ -482,8 +488,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
Err(err) => {
info.error = Some(err.into());
return Ok(FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
));
}
};
@ -525,8 +530,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
if let Err(err) = io.write_eof() {
info.error = Some(err.into());
return Ok(FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
));
}
break;
@ -537,8 +541,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
Err(err) => {
info.error = Some(err.into());
return Ok(FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
));
}
Ok(result) => result,
@ -572,8 +575,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
info.error = Some(err.into());
return Ok(
FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
),
);
}
@ -585,8 +587,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
info.error = Some(err.into());
return Ok(
FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
),
);
}
@ -611,8 +612,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
Err(err) => {
info.error = Some(err);
return Ok(FinishingMiddlewares::init(
info,
self.resp,
info, self.resp,
));
}
}
@ -796,18 +796,15 @@ mod tests {
.unwrap()
.run(lazy(|| {
let mut info = PipelineInfo::new(HttpRequest::default());
Completed::<(), Inner<()>>::init(&mut info)
.is_none()
.unwrap();
Completed::<(), Inner<()>>::init(&mut info).is_none().unwrap();
let req = HttpRequest::default();
let mut ctx = HttpContext::new(req.clone(), MyActor);
let addr: Addr<Unsync, _> = ctx.address();
let mut info = PipelineInfo::new(req);
info.context = Some(Box::new(ctx));
let mut state = Completed::<(), Inner<()>>::init(&mut info)
.completed()
.unwrap();
let mut state =
Completed::<(), Inner<()>>::init(&mut info).completed().unwrap();
assert!(state.poll(&mut info).is_none());
let pp = Pipeline(info, PipelineState::Completed(state));

View File

@ -171,7 +171,7 @@ pub fn Method<S: 'static>(method: http::Method) -> MethodPredicate<S> {
/// Return predicate that matches if request contains specified header and
/// value.
pub fn Header<S: 'static>(
name: &'static str, value: &'static str
name: &'static str, value: &'static str,
) -> HeaderPredicate<S> {
HeaderPredicate(
header::HeaderName::try_from(name).unwrap(),
@ -181,11 +181,7 @@ pub fn Header<S: 'static>(
}
#[doc(hidden)]
pub struct HeaderPredicate<S>(
header::HeaderName,
header::HeaderValue,
PhantomData<S>,
);
pub struct HeaderPredicate<S>(header::HeaderName, header::HeaderValue, PhantomData<S>);
impl<S: 'static> Predicate<S> for HeaderPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {

View File

@ -132,10 +132,7 @@ impl<S: 'static> ResourceHandler<S> {
/// ```
pub fn method(&mut self, method: Method) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes
.last_mut()
.unwrap()
.filter(pred::Method(method))
self.routes.last_mut().unwrap().filter(pred::Method(method))
}
/// Register a new route and add handler object.
@ -188,13 +185,11 @@ impl<S: 'static> ResourceHandler<S> {
/// This is similar to `App's` middlewares, but
/// middlewares get invoked on resource level.
pub fn middleware<M: Middleware<S>>(&mut self, mw: M) {
Rc::get_mut(&mut self.middlewares)
.unwrap()
.push(Box::new(mw));
Rc::get_mut(&mut self.middlewares).unwrap().push(Box::new(mw));
}
pub(crate) fn handle(
&mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>
&mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>,
) -> Reply {
for route in &mut self.routes {
if route.check(&mut req) {

View File

@ -50,7 +50,7 @@ impl<S: 'static> Route<S> {
#[inline]
pub(crate) fn compose(
&mut self, req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>
&mut self, req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>,
) -> Reply {
Reply::async(Compose::new(req, mws, self.handler.clone()))
}
@ -170,7 +170,7 @@ impl<S: 'static> Route<S> {
/// }
/// ```
pub fn with2<T1, T2, F, R>(
&mut self, handler: F
&mut self, handler: F,
) -> (ExtractorConfig<S, T1>, ExtractorConfig<S, T2>)
where
F: Fn(T1, T2) -> R + 'static,
@ -180,22 +180,14 @@ impl<S: 'static> Route<S> {
{
let cfg1 = ExtractorConfig::default();
let cfg2 = ExtractorConfig::default();
self.h(With2::new(
handler,
Clone::clone(&cfg1),
Clone::clone(&cfg2),
));
self.h(With2::new(handler, Clone::clone(&cfg1), Clone::clone(&cfg2)));
(cfg1, cfg2)
}
/// Set handler function, use request extractor for all paramters.
pub fn with3<T1, T2, T3, F, R>(
&mut self, handler: F
) -> (
ExtractorConfig<S, T1>,
ExtractorConfig<S, T2>,
ExtractorConfig<S, T3>,
)
&mut self, handler: F,
) -> (ExtractorConfig<S, T1>, ExtractorConfig<S, T2>, ExtractorConfig<S, T3>)
where
F: Fn(T1, T2, T3) -> R + 'static,
R: Responder + 'static,
@ -288,7 +280,7 @@ impl<S: 'static> ComposeState<S> {
impl<S: 'static> Compose<S> {
fn new(
req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>, handler: InnerHandler<S>
req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>, handler: InnerHandler<S>,
) -> Self {
let mut info = ComposeInfo {
count: 0,
@ -298,7 +290,10 @@ impl<S: 'static> Compose<S> {
};
let state = StartMiddlewares::init(&mut info);
Compose { state, info }
Compose {
state,
info,
}
}
}

View File

@ -80,7 +80,11 @@ impl Router {
return None;
}
let path: &str = unsafe { mem::transmute(&req.path()[self.0.prefix_len..]) };
let route_path = if path.is_empty() { "/" } else { path };
let route_path = if path.is_empty() {
"/"
} else {
path
};
for (idx, pattern) in self.0.patterns.iter().enumerate() {
if pattern.match_with_params(route_path, req.match_info_mut()) {
@ -98,7 +102,11 @@ impl Router {
/// following path would be recognizable `/test/name` but `has_route()` call
/// would return `false`.
pub fn has_route(&self, path: &str) -> bool {
let path = if path.is_empty() { "/" } else { path };
let path = if path.is_empty() {
"/"
} else {
path
};
for pattern in &self.0.patterns {
if pattern.is_match(path) {
@ -113,7 +121,7 @@ impl Router {
/// Check [`HttpRequest::url_for()`](../struct.HttpRequest.html#method.
/// url_for) for detailed information.
pub fn resource_path<U, I>(
&self, name: &str, elements: U
&self, name: &str, elements: U,
) -> Result<String, UrlGenerationError>
where
U: IntoIterator<Item = I>,
@ -245,7 +253,7 @@ impl Resource {
}
pub fn match_with_params<'a>(
&'a self, path: &'a str, params: &'a mut Params<'a>
&'a self, path: &'a str, params: &'a mut Params<'a>,
) -> bool {
match self.tp {
PatternType::Static(ref s) => s == path,
@ -270,7 +278,7 @@ impl Resource {
/// Build reousrce path.
pub fn resource_path<U, I>(
&self, router: &Router, elements: U
&self, router: &Router, elements: U,
) -> Result<String, UrlGenerationError>
where
U: IntoIterator<Item = I>,
@ -383,34 +391,19 @@ mod tests {
#[test]
fn test_recognizer() {
let routes = vec![
(
Resource::new("", "/name"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/name/{val}"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/name"), Some(ResourceHandler::default())),
(Resource::new("", "/name/{val}"), Some(ResourceHandler::default())),
(
Resource::new("", "/name/{val}/index.html"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/file/{file}.{ext}"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/file/{file}.{ext}"), Some(ResourceHandler::default())),
(
Resource::new("", "/v{val}/{val2}/index.html"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/v/{tail:.*}"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "{test}/index.html"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/v/{tail:.*}"), Some(ResourceHandler::default())),
(Resource::new("", "{test}/index.html"), Some(ResourceHandler::default())),
];
let (rec, _) = Router::new::<()>("", ServerSettings::default(), routes);
@ -439,10 +432,7 @@ mod tests {
let mut req = TestRequest::with_uri("/v/blah-blah/index.html").finish();
assert_eq!(rec.recognize(&mut req), Some(5));
assert_eq!(
req.match_info().get("tail").unwrap(),
"blah-blah/index.html"
);
assert_eq!(req.match_info().get("tail").unwrap(), "blah-blah/index.html");
let mut req = TestRequest::with_uri("/bbb/index.html").finish();
assert_eq!(rec.recognize(&mut req), Some(6));
@ -452,14 +442,8 @@ mod tests {
#[test]
fn test_recognizer_2() {
let routes = vec![
(
Resource::new("", "/index.json"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/{source}.json"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/index.json"), Some(ResourceHandler::default())),
(Resource::new("", "/{source}.json"), Some(ResourceHandler::default())),
];
let (rec, _) = Router::new::<()>("", ServerSettings::default(), routes);
@ -473,14 +457,8 @@ mod tests {
#[test]
fn test_recognizer_with_prefix() {
let routes = vec![
(
Resource::new("", "/name"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/name/{val}"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/name"), Some(ResourceHandler::default())),
(Resource::new("", "/name/{val}"), Some(ResourceHandler::default())),
];
let (rec, _) = Router::new::<()>("/test", ServerSettings::default(), routes);
@ -497,14 +475,8 @@ mod tests {
// same patterns
let routes = vec![
(
Resource::new("", "/name"),
Some(ResourceHandler::default()),
),
(
Resource::new("", "/name/{val}"),
Some(ResourceHandler::default()),
),
(Resource::new("", "/name"), Some(ResourceHandler::default())),
(Resource::new("", "/name/{val}"), Some(ResourceHandler::default())),
];
let (rec, _) = Router::new::<()>("/test2", ServerSettings::default(), routes);
@ -573,14 +545,8 @@ mod tests {
#[test]
fn test_request_resource() {
let routes = vec![
(
Resource::new("r1", "/index.json"),
Some(ResourceHandler::default()),
),
(
Resource::new("r2", "/test.json"),
Some(ResourceHandler::default()),
),
(Resource::new("r1", "/index.json"), Some(ResourceHandler::default())),
(Resource::new("r2", "/test.json"), Some(ResourceHandler::default())),
];
let (router, _) = Router::new::<()>("", ServerSettings::default(), routes);

View File

@ -7,7 +7,7 @@ use futures::{Async, Future, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
use super::settings::WorkerSettings;
use super::{utils, HttpHandler, IoStream, h1, h2};
use super::{h1, h2, utils, HttpHandler, IoStream};
const HTTP2_PREFACE: [u8; 14] = *b"PRI * HTTP/2.0";
@ -93,12 +93,12 @@ where
let el = self as *mut _;
self.node = Some(Node::new(el));
let _ = match self.proto {
Some(HttpProtocol::H1(ref mut h1)) => self.node
.as_ref()
.map(|n| h1.settings().head().insert(n)),
Some(HttpProtocol::H2(ref mut h2)) => self.node
.as_ref()
.map(|n| h2.settings().head().insert(n)),
Some(HttpProtocol::H1(ref mut h1)) => {
self.node.as_ref().map(|n| h1.settings().head().insert(n))
}
Some(HttpProtocol::H2(ref mut h2)) => {
self.node.as_ref().map(|n| h2.settings().head().insert(n))
}
Some(HttpProtocol::Unknown(ref mut settings, _, _, _)) => {
self.node.as_ref().map(|n| settings.head().insert(n))
}
@ -112,7 +112,9 @@ where
match result {
Ok(Async::Ready(())) | Err(_) => {
h1.settings().remove_channel();
self.node.as_mut().map(|n| n.remove());
if let Some(n) = self.node.as_mut() {
n.remove()
};
}
_ => (),
}
@ -123,7 +125,9 @@ where
match result {
Ok(Async::Ready(())) | Err(_) => {
h2.settings().remove_channel();
self.node.as_mut().map(|n| n.remove());
if let Some(n) = self.node.as_mut() {
n.remove()
};
}
_ => (),
}
@ -139,7 +143,9 @@ where
Ok(Async::Ready(0)) | Err(_) => {
debug!("Ignored premature client disconnection");
settings.remove_channel();
self.node.as_mut().map(|n| n.remove());
if let Some(n) = self.node.as_mut() {
n.remove()
};
return Err(());
}
_ => (),
@ -162,12 +168,8 @@ where
if let Some(HttpProtocol::Unknown(settings, addr, io, buf)) = self.proto.take() {
match kind {
ProtocolKind::Http1 => {
self.proto = Some(HttpProtocol::H1(h1::Http1::new(
settings,
io,
addr,
buf,
)));
self.proto =
Some(HttpProtocol::H1(h1::Http1::new(settings, io, addr, buf)));
return self.poll();
}
ProtocolKind::Http2 => {
@ -204,7 +206,8 @@ impl<T> Node<T> {
#[allow(mutable_transmutes)]
unsafe {
if let Some(ref next2) = self.next {
let n: &mut Node<()> = mem::transmute(next2.as_ref().unwrap());
let n: &mut Node<()> =
&mut *(next2.as_ref().unwrap() as *const _ as *mut _);
n.prev = Some(next as *const _ as *mut _);
}
let slf: &mut Node<T> = mem::transmute(self);
@ -275,7 +278,9 @@ where
T: AsyncRead + AsyncWrite + 'static,
{
pub fn new(io: T) -> Self {
WrapperStream { io }
WrapperStream {
io,
}
}
}

View File

@ -763,8 +763,7 @@ impl TransferEncoding {
return Ok(*remaining == 0);
}
let len = cmp::min(*remaining, msg.len() as u64);
self.buffer
.extend(msg.take().split_to(len as usize).into());
self.buffer.extend(msg.take().split_to(len as usize).into());
*remaining -= len as u64;
Ok(*remaining == 0)
@ -856,10 +855,8 @@ impl AcceptEncoding {
/// Parse a raw Accept-Encoding header value into an ordered list.
pub fn parse(raw: &str) -> ContentEncoding {
let mut encodings: Vec<_> = raw.replace(' ', "")
.split(',')
.map(|l| AcceptEncoding::new(l))
.collect();
let mut encodings: Vec<_> =
raw.replace(' ', "").split(',').map(|l| AcceptEncoding::new(l)).collect();
encodings.sort();
for enc in encodings {
@ -879,9 +876,7 @@ mod tests {
fn test_chunked_te() {
let bytes = SharedBytes::default();
let mut enc = TransferEncoding::chunked(bytes.clone());
assert!(!enc.encode(Binary::from(b"test".as_ref()))
.ok()
.unwrap());
assert!(!enc.encode(Binary::from(b"test".as_ref())).ok().unwrap());
assert!(enc.encode(Binary::from(b"".as_ref())).ok().unwrap());
assert_eq!(
bytes.get_mut().take().freeze(),

View File

@ -210,8 +210,7 @@ where
self.stream.reset();
if ready {
item.flags
.insert(EntryFlags::EOF | EntryFlags::FINISHED);
item.flags.insert(EntryFlags::EOF | EntryFlags::FINISHED);
} else {
item.flags.insert(EntryFlags::FINISHED);
}
@ -253,10 +252,7 @@ where
// cleanup finished tasks
let max = self.tasks.len() >= MAX_PIPELINED_MESSAGES;
while !self.tasks.is_empty() {
if self.tasks[0]
.flags
.contains(EntryFlags::EOF | EntryFlags::FINISHED)
{
if self.tasks[0].flags.contains(EntryFlags::EOF | EntryFlags::FINISHED) {
self.tasks.pop_front();
} else {
break;
@ -308,7 +304,10 @@ where
pub fn parse(&mut self) {
'outer: loop {
match self.decoder.decode(&mut self.buf, &self.settings) {
Ok(Some(Message::Message { msg, payload })) => {
Ok(Some(Message::Message {
msg,
payload,
})) => {
self.flags.insert(Flags::STARTED);
if payload {
@ -421,13 +420,19 @@ mod tests {
impl Message {
fn message(self) -> SharedHttpInnerMessage {
match self {
Message::Message { msg, payload: _ } => msg,
Message::Message {
msg,
payload: _,
} => msg,
_ => panic!("error"),
}
}
fn is_payload(&self) -> bool {
match *self {
Message::Message { msg: _, payload } => payload,
Message::Message {
msg: _,
payload,
} => payload,
_ => panic!("error"),
}
}
@ -623,10 +628,7 @@ mod tests {
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
assert_eq!(
req.headers().get("test").unwrap().as_bytes(),
b"value"
);
assert_eq!(req.headers().get("test").unwrap().as_bytes(), b"value");
}
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
}
@ -848,12 +850,7 @@ mod tests {
assert!(!req.keep_alive());
assert!(req.upgrade());
assert_eq!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
reader.decode(&mut buf, &settings).unwrap().unwrap().chunk().as_ref(),
b"some raw data"
);
}
@ -910,30 +907,14 @@ mod tests {
buf.extend(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n");
assert_eq!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
reader.decode(&mut buf, &settings).unwrap().unwrap().chunk().as_ref(),
b"data"
);
assert_eq!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
reader.decode(&mut buf, &settings).unwrap().unwrap().chunk().as_ref(),
b"line"
);
assert!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.eof()
);
assert!(reader.decode(&mut buf, &settings).unwrap().unwrap().eof());
}
#[test]
@ -1014,13 +995,7 @@ mod tests {
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
buf.extend(b"\r\n");
assert!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.eof()
);
assert!(reader.decode(&mut buf, &settings).unwrap().unwrap().eof());
}
#[test]
@ -1038,17 +1013,9 @@ mod tests {
assert!(req.chunked().unwrap());
buf.extend(b"4;test\r\ndata\r\n4\r\nline\r\n0\r\n\r\n"); // test: test\r\n\r\n")
let chunk = reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk();
let chunk = reader.decode(&mut buf, &settings).unwrap().unwrap().chunk();
assert_eq!(chunk, Bytes::from_static(b"data"));
let chunk = reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk();
let chunk = reader.decode(&mut buf, &settings).unwrap().unwrap().chunk();
assert_eq!(chunk, Bytes::from_static(b"line"));
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.eof());

View File

@ -41,7 +41,9 @@ impl From<io::Error> for DecoderError {
impl H1Decoder {
pub fn new() -> H1Decoder {
H1Decoder { decoder: None }
H1Decoder {
decoder: None,
}
}
pub fn decode<H>(
@ -59,9 +61,7 @@ impl H1Decoder {
}
}
match self.parse_message(src, settings)
.map_err(DecoderError::Error)?
{
match self.parse_message(src, settings).map_err(DecoderError::Error)? {
Async::Ready((msg, decoder)) => {
if let Some(decoder) = decoder {
self.decoder = Some(decoder);
@ -103,7 +103,7 @@ impl H1Decoder {
let (len, method, path, version, headers_len) = {
let b = unsafe {
let b: &[u8] = buf;
mem::transmute(b)
&*(b as *const [u8])
};
let mut req = httparse::Request::new(&mut headers);
match req.parse(b)? {
@ -415,10 +415,9 @@ impl ChunkedState {
match byte!(rdr) {
b'\n' if *size > 0 => Ok(Async::Ready(ChunkedState::Body)),
b'\n' if *size == 0 => Ok(Async::Ready(ChunkedState::EndCr)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid chunk size LF",
)),
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk size LF"))
}
}
}
@ -451,37 +450,33 @@ impl ChunkedState {
fn read_body_cr(rdr: &mut BytesMut) -> Poll<ChunkedState, io::Error> {
match byte!(rdr) {
b'\r' => Ok(Async::Ready(ChunkedState::BodyLf)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid chunk body CR",
)),
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk body CR"))
}
}
}
fn read_body_lf(rdr: &mut BytesMut) -> Poll<ChunkedState, io::Error> {
match byte!(rdr) {
b'\n' => Ok(Async::Ready(ChunkedState::Size)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid chunk body LF",
)),
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk body LF"))
}
}
}
fn read_end_cr(rdr: &mut BytesMut) -> Poll<ChunkedState, io::Error> {
match byte!(rdr) {
b'\r' => Ok(Async::Ready(ChunkedState::EndLf)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid chunk end CR",
)),
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk end CR"))
}
}
}
fn read_end_lf(rdr: &mut BytesMut) -> Poll<ChunkedState, io::Error> {
match byte!(rdr) {
b'\n' => Ok(Async::Ready(ChunkedState::End)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid chunk end LF",
)),
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk end LF"))
}
}
}
}

View File

@ -2,8 +2,8 @@
use bytes::BufMut;
use futures::{Async, Poll};
use std::io;
use std::rc::Rc;
use std::{io, mem};
use tokio_io::AsyncWrite;
use super::encoding::ContentEncoder;
@ -13,10 +13,10 @@ use super::shared::SharedBytes;
use super::{Writer, WriterState, MAX_WRITE_BUFFER_SIZE};
use body::{Binary, Body};
use header::ContentEncoding;
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE};
use http::{Method, Version};
use httprequest::HttpInnerMessage;
use httpresponse::HttpResponse;
use http::{Method, Version};
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE};
const AVERAGE_HEADER_SIZE: usize = 30; // totally scientific
@ -42,7 +42,7 @@ pub(crate) struct H1Writer<T: AsyncWrite, H: 'static> {
impl<T: AsyncWrite, H: 'static> H1Writer<T, H> {
pub fn new(
stream: T, buf: SharedBytes, settings: Rc<WorkerSettings<H>>
stream: T, buf: SharedBytes, settings: Rc<WorkerSettings<H>>,
) -> H1Writer<T, H> {
H1Writer {
flags: Flags::empty(),
@ -117,8 +117,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
let version = msg.version().unwrap_or_else(|| req.version);
if msg.upgrade() {
self.flags.insert(Flags::UPGRADE);
msg.headers_mut()
.insert(CONNECTION, HeaderValue::from_static("upgrade"));
msg.headers_mut().insert(CONNECTION, HeaderValue::from_static("upgrade"));
}
// keep-alive
else if self.flags.contains(Flags::KEEPALIVE) {
@ -127,8 +126,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
.insert(CONNECTION, HeaderValue::from_static("keep-alive"));
}
} else if version >= Version::HTTP_11 {
msg.headers_mut()
.insert(CONNECTION, HeaderValue::from_static("close"));
msg.headers_mut().insert(CONNECTION, HeaderValue::from_static("close"));
}
let body = msg.replace_body(Body::Empty);
@ -169,7 +167,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
let mut pos = 0;
let mut has_date = false;
let mut remaining = buffer.remaining_mut();
let mut buf: &mut [u8] = unsafe { mem::transmute(buffer.bytes_mut()) };
let mut buf = unsafe { &mut *(buffer.bytes_mut() as *mut [u8]) };
for (key, value) in msg.headers() {
if is_bin && key == CONTENT_LENGTH {
is_bin = false;
@ -184,7 +182,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
pos = 0;
buffer.reserve(len);
remaining = buffer.remaining_mut();
buf = unsafe { mem::transmute(buffer.bytes_mut()) };
buf = unsafe { &mut *(buffer.bytes_mut() as *mut _) };
}
buf[pos..pos + k.len()].copy_from_slice(k);
@ -272,7 +270,8 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
#[inline]
fn poll_completed(&mut self, shutdown: bool) -> Poll<(), io::Error> {
if !self.buffer.is_empty() {
let buf: &[u8] = unsafe { mem::transmute(self.buffer.as_ref()) };
let buf: &[u8] =
unsafe { &mut *(self.buffer.as_ref() as *const _ as *mut _) };
let written = self.write_data(buf)?;
let _ = self.buffer.split_to(written);
if self.buffer.len() > self.buffer_capacity {

View File

@ -61,7 +61,7 @@ where
H: HttpHandler + 'static,
{
pub fn new(
settings: Rc<WorkerSettings<H>>, io: T, addr: Option<SocketAddr>, buf: Bytes
settings: Rc<WorkerSettings<H>>, io: T, addr: Option<SocketAddr>, buf: Bytes,
) -> Self {
Http2 {
flags: Flags::empty(),

View File

@ -45,7 +45,7 @@ pub(crate) struct H2Writer<H: 'static> {
impl<H: 'static> H2Writer<H> {
pub fn new(
respond: SendResponse<Bytes>, buf: SharedBytes, settings: Rc<WorkerSettings<H>>
respond: SendResponse<Bytes>, buf: SharedBytes, settings: Rc<WorkerSettings<H>>,
) -> H2Writer<H> {
H2Writer {
respond,
@ -107,8 +107,7 @@ impl<H: 'static> Writer for H2Writer<H> {
);
}
Body::Empty => {
msg.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
msg.headers_mut().insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
}
_ => (),
}
@ -120,9 +119,7 @@ impl<H: 'static> Writer for H2Writer<H> {
resp.headers_mut().insert(key, value.clone());
}
match self.respond
.send_response(resp, self.flags.contains(Flags::EOF))
{
match self.respond.send_response(resp, self.flags.contains(Flags::EOF)) {
Ok(stream) => self.stream = Some(stream),
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "err")),
}

View File

@ -69,7 +69,7 @@ impl SharedHttpInnerMessage {
}
pub fn new(
msg: Rc<HttpInnerMessage>, pool: Rc<SharedMessagePool>
msg: Rc<HttpInnerMessage>, pool: Rc<SharedMessagePool>,
) -> SharedHttpInnerMessage {
SharedHttpInnerMessage(Some(msg), Some(pool))
}
@ -79,7 +79,7 @@ impl SharedHttpInnerMessage {
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
pub fn get_mut(&self) -> &mut HttpInnerMessage {
let r: &HttpInnerMessage = self.0.as_ref().unwrap().as_ref();
unsafe { mem::transmute(r) }
unsafe { &mut *(r as *const _ as *mut _) }
}
#[inline(always)]
@ -96,9 +96,8 @@ const DEC_DIGITS_LUT: &[u8] = b"0001020304050607080910111213141516171819\
8081828384858687888990919293949596979899";
pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesMut) {
let mut buf: [u8; 13] = [
b'H', b'T', b'T', b'P', b'/', b'1', b'.', b'1', b' ', b' ', b' ', b' ', b' '
];
let mut buf: [u8; 13] =
[b'H', b'T', b'T', b'P', b'/', b'1', b'.', b'1', b' ', b' ', b' ', b' ', b' '];
match version {
Version::HTTP_2 => buf[5] = b'2',
Version::HTTP_10 => buf[7] = b'0',
@ -251,63 +250,33 @@ mod tests {
let mut bytes = BytesMut::new();
bytes.reserve(50);
write_content_length(0, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 0\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 0\r\n"[..]);
bytes.reserve(50);
write_content_length(9, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 9\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 9\r\n"[..]);
bytes.reserve(50);
write_content_length(10, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 10\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 10\r\n"[..]);
bytes.reserve(50);
write_content_length(99, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 99\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 99\r\n"[..]);
bytes.reserve(50);
write_content_length(100, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 100\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 100\r\n"[..]);
bytes.reserve(50);
write_content_length(101, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 101\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 101\r\n"[..]);
bytes.reserve(50);
write_content_length(998, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 998\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 998\r\n"[..]);
bytes.reserve(50);
write_content_length(1000, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 1000\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 1000\r\n"[..]);
bytes.reserve(50);
write_content_length(1001, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 1001\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 1001\r\n"[..]);
bytes.reserve(50);
write_content_length(5909, &mut bytes);
assert_eq!(
bytes.take().freeze(),
b"\r\ncontent-length: 5909\r\n"[..]
);
assert_eq!(bytes.take().freeze(), b"\r\ncontent-length: 5909\r\n"[..]);
}
}

View File

@ -8,10 +8,10 @@ use std::sync::Arc;
use std::{fmt, mem, net};
use time;
use super::KeepAlive;
use super::channel::Node;
use super::helpers;
use super::shared::{SharedBytes, SharedBytesPool};
use super::KeepAlive;
use body::Body;
use httpresponse::{HttpResponse, HttpResponseBuilder, HttpResponsePool};
@ -72,7 +72,7 @@ impl Default for ServerSettings {
impl ServerSettings {
/// Crate server settings instance
pub(crate) fn new(
addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool
addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool,
) -> ServerSettings {
let host = if let Some(ref host) = *host {
host.clone()
@ -119,7 +119,7 @@ impl ServerSettings {
#[inline]
pub(crate) fn get_response_builder(
&self, status: StatusCode
&self, status: StatusCode,
) -> HttpResponseBuilder {
HttpResponsePool::get_builder(&self.responses, status)
}
@ -255,10 +255,7 @@ mod tests {
#[test]
fn test_date_len() {
assert_eq!(
DATE_VALUE_LENGTH,
"Sun, 06 Nov 1994 08:49:37 GMT".len()
);
assert_eq!(DATE_VALUE_LENGTH, "Sun, 06 Nov 1994 08:49:37 GMT".len());
}
#[test]

View File

@ -1,8 +1,8 @@
use bytes::{BufMut, BytesMut};
use std::cell::RefCell;
use std::collections::VecDeque;
use std::io;
use std::rc::Rc;
use std::{io, mem};
use body::Binary;
@ -61,7 +61,7 @@ impl SharedBytes {
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
pub(crate) fn get_mut(&self) -> &mut BytesMut {
let r: &BytesMut = self.0.as_ref().unwrap().as_ref();
unsafe { mem::transmute(r) }
unsafe { &mut *(r as *const _ as *mut _) }
}
#[inline]

View File

@ -219,10 +219,7 @@ where
if let Some(e) = err.take() {
Err(e)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Can not bind to address.",
))
Err(io::Error::new(io::ErrorKind::Other, "Can not bind to address."))
}
} else {
Ok(self)
@ -230,7 +227,7 @@ where
}
fn start_workers(
&mut self, settings: &ServerSettings, handler: &StreamHandlerType
&mut self, settings: &ServerSettings, handler: &StreamHandlerType,
) -> Vec<(usize, mpsc::UnboundedSender<Conn<net::TcpStream>>)> {
// start workers
let mut workers = Vec::new();
@ -332,9 +329,9 @@ impl<H: IntoHttpHandler> HttpServer<H> {
ctx.add_stream(rx);
self
});
signals.map(|signals| {
if let Some(signals) = signals {
signals.do_send(signal::Subscribe(addr.clone().recipient()))
});
}
addr
}
}
@ -378,10 +375,7 @@ impl<H: IntoHttpHandler> HttpServer<H> {
/// Start listening for incoming tls connections.
pub fn start_tls(mut self, acceptor: TlsAcceptor) -> io::Result<Addr<Syn, Self>> {
if self.sockets.is_empty() {
Err(io::Error::new(
io::ErrorKind::Other,
"No socket addresses are bound",
))
Err(io::Error::new(io::ErrorKind::Other, "No socket addresses are bound"))
} else {
let (tx, rx) = mpsc::unbounded();
let addrs: Vec<(net::SocketAddr, net::TcpListener)> =
@ -427,13 +421,10 @@ impl<H: IntoHttpHandler> HttpServer<H> {
///
/// This method sets alpn protocols to "h2" and "http/1.1"
pub fn start_ssl(
mut self, mut builder: SslAcceptorBuilder
mut self, mut builder: SslAcceptorBuilder,
) -> io::Result<Addr<Syn, Self>> {
if self.sockets.is_empty() {
Err(io::Error::new(
io::ErrorKind::Other,
"No socket addresses are bound",
))
Err(io::Error::new(io::ErrorKind::Other, "No socket addresses are bound"))
} else {
// alpn support
if !self.no_http2 {
@ -545,8 +536,9 @@ impl<H: IntoHttpHandler> HttpServer<H> {
}));
self
});
signals
.map(|signals| signals.do_send(signal::Subscribe(addr.clone().recipient())));
if let Some(signals) = signals {
signals.do_send(signal::Subscribe(addr.clone().recipient()))
}
addr
}
}
@ -562,17 +554,35 @@ impl<H: IntoHttpHandler> Handler<signal::Signal> for HttpServer<H> {
signal::SignalType::Int => {
info!("SIGINT received, exiting");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer { graceful: false }, ctx);
Handler::<StopServer>::handle(
self,
StopServer {
graceful: false,
},
ctx,
);
}
signal::SignalType::Term => {
info!("SIGTERM received, stopping");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer { graceful: true }, ctx);
Handler::<StopServer>::handle(
self,
StopServer {
graceful: true,
},
ctx,
);
}
signal::SignalType::Quit => {
info!("SIGQUIT received, exiting");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer { graceful: false }, ctx);
Handler::<StopServer>::handle(
self,
StopServer {
graceful: false,
},
ctx,
);
}
_ => (),
}
@ -696,7 +706,9 @@ impl<H: IntoHttpHandler> Handler<StopServer> for HttpServer<H> {
let tx2 = tx.clone();
worker
.1
.send(StopWorker { graceful: dur })
.send(StopWorker {
graceful: dur,
})
.into_actor(self)
.then(move |_, slf, ctx| {
slf.workers.pop();
@ -746,9 +758,8 @@ fn start_accept_thread(
// start accept thread
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
let _ = thread::Builder::new()
.name(format!("Accept on {}", addr))
.spawn(move || {
let _ = thread::Builder::new().name(format!("Accept on {}", addr)).spawn(
move || {
const SRV: mio::Token = mio::Token(0);
const CMD: mio::Token = mio::Token(1);
@ -773,12 +784,9 @@ fn start_accept_thread(
}
// Start listening for incoming commands
if let Err(err) = poll.register(
&reg,
CMD,
mio::Ready::readable(),
mio::PollOpt::edge(),
) {
if let Err(err) =
poll.register(&reg, CMD, mio::Ready::readable(), mio::PollOpt::edge())
{
panic!("Can not register Registration: {}", err);
}
@ -909,13 +917,14 @@ fn start_accept_thread(
}
}
}
});
},
);
(readiness, tx)
}
fn create_tcp_listener(
addr: net::SocketAddr, backlog: i32
addr: net::SocketAddr, backlog: i32,
) -> io::Result<net::TcpListener> {
let builder = match addr {
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,

View File

@ -8,7 +8,7 @@ const LW_BUFFER_SIZE: usize = 4096;
const HW_BUFFER_SIZE: usize = 32_768;
pub fn read_from_io<T: IoStream>(
io: &mut T, buf: &mut BytesMut
io: &mut T, buf: &mut BytesMut,
) -> Poll<usize, io::Error> {
unsafe {
if buf.remaining_mut() < LW_BUFFER_SIZE {

View File

@ -1,5 +1,5 @@
use futures::Future;
use futures::unsync::oneshot;
use futures::Future;
use net2::TcpStreamExt;
use std::rc::Rc;
use std::{net, time};
@ -59,7 +59,7 @@ where
impl<H: HttpHandler + 'static> Worker<H> {
pub(crate) fn new(
h: Vec<H>, handler: StreamHandlerType, keep_alive: KeepAlive
h: Vec<H>, handler: StreamHandlerType, keep_alive: KeepAlive,
) -> Worker<H> {
let tcp_ka = if let KeepAlive::Tcp(val) = keep_alive {
Some(time::Duration::new(val as u64, 0))
@ -77,13 +77,11 @@ impl<H: HttpHandler + 'static> Worker<H> {
fn update_time(&self, ctx: &mut Context<Self>) {
self.settings.update_date();
ctx.run_later(time::Duration::new(1, 0), |slf, ctx| {
slf.update_time(ctx)
});
ctx.run_later(time::Duration::new(1, 0), |slf, ctx| slf.update_time(ctx));
}
fn shutdown_timeout(
&self, ctx: &mut Context<Self>, tx: oneshot::Sender<bool>, dur: time::Duration
&self, ctx: &mut Context<Self>, tx: oneshot::Sender<bool>, dur: time::Duration,
) {
// sleep for 1 second and then check again
ctx.run_later(time::Duration::new(1, 0), move |slf, ctx| {
@ -124,8 +122,7 @@ where
if self.tcp_ka.is_some() && msg.io.set_keepalive(self.tcp_ka).is_err() {
error!("Can not set socket keep-alive option");
}
self.handler
.handle(Rc::clone(&self.settings), &self.hnd, msg);
self.handler.handle(Rc::clone(&self.settings), &self.hnd, msg);
}
}
@ -165,7 +162,7 @@ pub(crate) enum StreamHandlerType {
impl StreamHandlerType {
fn handle<H: HttpHandler>(
&mut self, h: Rc<WorkerSettings<H>>, hnd: &Handle, msg: Conn<net::TcpStream>
&mut self, h: Rc<WorkerSettings<H>>, hnd: &Handle, msg: Conn<net::TcpStream>,
) {
match *self {
StreamHandlerType::Normal => {
@ -177,60 +174,57 @@ impl StreamHandlerType {
}
#[cfg(feature = "tls")]
StreamHandlerType::Tls(ref acceptor) => {
let Conn { io, peer, http2 } = msg;
let Conn {
io,
peer,
http2,
} = msg;
let _ = io.set_nodelay(true);
let io = TcpStream::from_stream(io, hnd)
.expect("failed to associate TCP stream");
hnd.spawn(
TlsAcceptorExt::accept_async(acceptor, io).then(move |res| {
match res {
Ok(io) => Arbiter::handle().spawn(HttpChannel::new(
h,
io,
peer,
http2,
)),
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
}),
);
hnd.spawn(TlsAcceptorExt::accept_async(acceptor, io).then(move |res| {
match res {
Ok(io) => {
Arbiter::handle().spawn(HttpChannel::new(h, io, peer, http2))
}
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
}));
}
#[cfg(feature = "alpn")]
StreamHandlerType::Alpn(ref acceptor) => {
let Conn { io, peer, .. } = msg;
let Conn {
io,
peer,
..
} = msg;
let _ = io.set_nodelay(true);
let io = TcpStream::from_stream(io, hnd)
.expect("failed to associate TCP stream");
hnd.spawn(
SslAcceptorExt::accept_async(acceptor, io).then(move |res| {
match res {
Ok(io) => {
let http2 = if let Some(p) =
io.get_ref().ssl().selected_alpn_protocol()
{
p.len() == 2 && &p == b"h2"
} else {
false
};
Arbiter::handle().spawn(HttpChannel::new(
h,
io,
peer,
http2,
));
}
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
}),
);
hnd.spawn(SslAcceptorExt::accept_async(acceptor, io).then(move |res| {
match res {
Ok(io) => {
let http2 = if let Some(p) =
io.get_ref().ssl().selected_alpn_protocol()
{
p.len() == 2 && &p == b"h2"
} else {
false
};
Arbiter::handle()
.spawn(HttpChannel::new(h, io, peer, http2));
}
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
}));
}
}
}

View File

@ -170,14 +170,22 @@ impl TestServer {
if uri.starts_with('/') {
format!(
"{}://{}{}",
if self.ssl { "https" } else { "http" },
if self.ssl {
"https"
} else {
"http"
},
self.addr,
uri
)
} else {
format!(
"{}://{}/{}",
if self.ssl { "https" } else { "http" },
if self.ssl {
"https"
} else {
"http"
},
self.addr,
uri
)
@ -202,7 +210,7 @@ impl TestServer {
/// Connect to websocket server
pub fn ws(
&mut self
&mut self,
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
let url = self.url("/");
self.system.run_until_complete(
@ -350,17 +358,14 @@ pub struct TestApp<S = ()> {
impl<S: 'static> TestApp<S> {
fn new(state: S) -> TestApp<S> {
let app = App::with_state(state);
TestApp { app: Some(app) }
TestApp {
app: Some(app),
}
}
/// Register handler for "/"
pub fn handler<H: Handler<S>>(&mut self, handler: H) {
self.app = Some(
self.app
.take()
.unwrap()
.resource("/", |r| r.h(handler)),
);
self.app = Some(self.app.take().unwrap().resource("/", |r| r.h(handler)));
}
/// Register middleware
@ -594,7 +599,7 @@ impl<S> TestRequest<S> {
///
/// This method panics is handler returns actor or async result.
pub fn run<H: Handler<S>>(
self, mut h: H
self, mut h: H,
) -> Result<HttpResponse, <<H as Handler<S>>::Result as Responder>::Error> {
let req = self.finish();
let resp = h.handle(req.clone());

View File

@ -44,7 +44,10 @@ impl Url {
pub fn new(uri: Uri) -> Url {
let path = DEFAULT_QUOTER.requote(uri.path().as_bytes());
Url { uri, path }
Url {
uri,
path,
}
}
pub fn uri(&self) -> &Uri {

View File

@ -187,7 +187,7 @@ where
S: 'static,
{
pub fn new(
f: F, cfg1: ExtractorConfig<S, T1>, cfg2: ExtractorConfig<S, T2>
f: F, cfg1: ExtractorConfig<S, T1>, cfg2: ExtractorConfig<S, T2>,
) -> Self {
With2 {
hnd: Rc::new(UnsafeCell::new(f)),
@ -307,10 +307,8 @@ where
Async::Ready(item) => {
self.item = Some(item);
self.fut1.take();
self.fut2 = Some(Box::new(T2::from_request(
&self.req,
self.cfg2.as_ref(),
)));
self.fut2 =
Some(Box::new(T2::from_request(&self.req, self.cfg2.as_ref())));
}
Async::NotReady => return Ok(Async::NotReady),
}
@ -510,10 +508,8 @@ where
Async::Ready(item) => {
self.item1 = Some(item);
self.fut1.take();
self.fut2 = Some(Box::new(T2::from_request(
&self.req,
self.cfg2.as_ref(),
)));
self.fut2 =
Some(Box::new(T2::from_request(&self.req, self.cfg2.as_ref())));
}
Async::NotReady => return Ok(Async::NotReady),
}
@ -524,10 +520,8 @@ where
Async::Ready(item) => {
self.item2 = Some(item);
self.fut2.take();
self.fut3 = Some(Box::new(T3::from_request(
&self.req,
self.cfg3.as_ref(),
)));
self.fut3 =
Some(Box::new(T3::from_request(&self.req, self.cfg3.as_ref())));
}
Async::NotReady => return Ok(Async::NotReady),
}
@ -539,15 +533,13 @@ where
};
let hnd: &mut F = unsafe { &mut *self.hnd.get() };
let item = match (*hnd)(
self.item1.take().unwrap(),
self.item2.take().unwrap(),
item,
).respond_to(self.req.drop_state())
{
Ok(item) => item.into(),
Err(err) => return Err(err.into()),
};
let item =
match (*hnd)(self.item1.take().unwrap(), self.item2.take().unwrap(), item)
.respond_to(self.req.drop_state())
{
Ok(item) => item.into(),
Err(err) => return Err(err.into()),
};
match item.into() {
ReplyItem::Message(resp) => return Ok(Async::Ready(resp)),

View File

@ -121,7 +121,7 @@ impl Client {
/// Create new websocket connection with custom `ClientConnector`
pub fn with_connector<S: AsRef<str>>(
uri: S, conn: Addr<Unsync, ClientConnector>
uri: S, conn: Addr<Unsync, ClientConnector>,
) -> Client {
let mut cl = Client {
request: ClientRequest::build(),
@ -142,9 +142,8 @@ impl Client {
U: IntoIterator<Item = V> + 'static,
V: AsRef<str>,
{
let mut protos = protos
.into_iter()
.fold(String::new(), |acc, s| acc + s.as_ref() + ",");
let mut protos =
protos.into_iter().fold(String::new(), |acc, s| acc + s.as_ref() + ",");
protos.pop();
self.protocols = Some(protos);
self
@ -218,8 +217,7 @@ impl Client {
self.request.upgrade();
self.request.set_header(header::UPGRADE, "websocket");
self.request.set_header(header::CONNECTION, "upgrade");
self.request
.set_header(header::SEC_WEBSOCKET_VERSION, "13");
self.request.set_header(header::SEC_WEBSOCKET_VERSION, "13");
self.request.with_connector(self.conn.clone());
if let Some(protocols) = self.protocols.take() {
@ -394,10 +392,7 @@ impl Future for ClientHandshake {
encoded,
key
);
return Err(ClientError::InvalidChallengeResponse(
encoded,
key.clone(),
));
return Err(ClientError::InvalidChallengeResponse(encoded, key.clone()));
}
} else {
trace!("Missing SEC-WEBSOCKET-ACCEPT header");
@ -416,7 +411,9 @@ impl Future for ClientHandshake {
inner: Rc::clone(&inner),
max_size: self.max_size,
},
ClientWriter { inner },
ClientWriter {
inner,
},
)))
}
}
@ -536,23 +533,13 @@ impl ClientWriter {
/// Send ping frame
#[inline]
pub fn ping(&mut self, message: &str) {
self.write(Frame::message(
Vec::from(message),
OpCode::Ping,
true,
true,
));
self.write(Frame::message(Vec::from(message), OpCode::Ping, true, true));
}
/// Send pong frame
#[inline]
pub fn pong(&mut self, message: &str) {
self.write(Frame::message(
Vec::from(message),
OpCode::Pong,
true,
true,
));
self.write(Frame::message(Vec::from(message), OpCode::Pong, true, true));
}
/// Send close frame

View File

@ -156,23 +156,13 @@ where
/// Send ping frame
#[inline]
pub fn ping(&mut self, message: &str) {
self.write(Frame::message(
Vec::from(message),
OpCode::Ping,
true,
false,
));
self.write(Frame::message(Vec::from(message), OpCode::Ping, true, false));
}
/// Send pong frame
#[inline]
pub fn pong(&mut self, message: &str) {
self.write(Frame::message(
Vec::from(message),
OpCode::Pong,
true,
false,
));
self.write(Frame::message(Vec::from(message), OpCode::Pong, true, false));
}
/// Send close frame

View File

@ -8,9 +8,9 @@ use body::Binary;
use error::PayloadError;
use payload::PayloadHelper;
use ws::ProtocolError;
use ws::mask::apply_mask;
use ws::proto::{CloseCode, CloseReason, OpCode};
use ws::ProtocolError;
/// A struct representing a `WebSocket` frame.
#[derive(Debug)]
@ -36,7 +36,7 @@ impl Frame {
NetworkEndian::write_u16(&mut code_bytes, reason.code.into());
let mut payload = Vec::from(&code_bytes[..]);
if let Some(description) = reason.description{
if let Some(description) = reason.description {
payload.extend(description.as_bytes());
}
payload
@ -48,7 +48,7 @@ impl Frame {
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
fn read_copy_md<S>(
pl: &mut PayloadHelper<S>, server: bool, max_size: usize
pl: &mut PayloadHelper<S>, server: bool, max_size: usize,
) -> Poll<Option<(usize, bool, OpCode, usize, Option<u32>)>, ProtocolError>
where
S: Stream<Item = Bytes, Error = PayloadError>,
@ -122,17 +122,11 @@ impl Frame {
None
};
Ok(Async::Ready(Some((
idx,
finished,
opcode,
length,
mask,
))))
Ok(Async::Ready(Some((idx, finished, opcode, length, mask))))
}
fn read_chunk_md(
chunk: &[u8], server: bool, max_size: usize
chunk: &[u8], server: bool, max_size: usize,
) -> Poll<(usize, bool, OpCode, usize, Option<u32>), ProtocolError> {
let chunk_len = chunk.len();
@ -203,7 +197,7 @@ impl Frame {
/// Parse the input stream into a frame.
pub fn parse<S>(
pl: &mut PayloadHelper<S>, server: bool, max_size: usize
pl: &mut PayloadHelper<S>, server: bool, max_size: usize,
) -> Poll<Option<Frame>, ProtocolError>
where
S: Stream<Item = Bytes, Error = PayloadError>,
@ -288,7 +282,10 @@ impl Frame {
} else {
None
};
Some(CloseReason { code, description })
Some(CloseReason {
code,
description,
})
} else {
None
}
@ -296,7 +293,7 @@ impl Frame {
/// Generate binary representation
pub fn message<B: Into<Binary>>(
data: B, code: OpCode, finished: bool, genmask: bool
data: B, code: OpCode, finished: bool, genmask: bool,
) -> Binary {
let payload = data.into();
let one: u8 = if finished {

View File

@ -28,7 +28,11 @@ fn apply_mask_fast32(buf: &mut [u8], mask_u32: u32) {
// Possible first unaligned block.
let head = min(len, (8 - (ptr as usize & 0x7)) & 0x3);
let mask_u32 = if head > 0 {
let n = if head > 4 { head - 4 } else { head };
let n = if head > 4 {
head - 4
} else {
head
};
let mask_u32 = if n > 0 {
unsafe {

View File

@ -133,24 +133,24 @@ pub enum HandshakeError {
impl ResponseError for HandshakeError {
fn error_response(&self) -> HttpResponse {
match *self {
HandshakeError::GetMethodRequired => HttpResponse::MethodNotAllowed()
.header(header::ALLOW, "GET")
.finish(),
HandshakeError::GetMethodRequired => {
HttpResponse::MethodNotAllowed().header(header::ALLOW, "GET").finish()
}
HandshakeError::NoWebsocketUpgrade => HttpResponse::BadRequest()
.reason("No WebSocket UPGRADE header found")
.finish(),
HandshakeError::NoConnectionUpgrade => HttpResponse::BadRequest()
.reason("No CONNECTION upgrade")
.finish(),
HandshakeError::NoConnectionUpgrade => {
HttpResponse::BadRequest().reason("No CONNECTION upgrade").finish()
}
HandshakeError::NoVersionHeader => HttpResponse::BadRequest()
.reason("Websocket version header is required")
.finish(),
HandshakeError::UnsupportedVersion => HttpResponse::BadRequest()
.reason("Unsupported version")
.finish(),
HandshakeError::BadWebsocketKey => HttpResponse::BadRequest()
.reason("Handshake error")
.finish(),
HandshakeError::UnsupportedVersion => {
HttpResponse::BadRequest().reason("Unsupported version").finish()
}
HandshakeError::BadWebsocketKey => {
HttpResponse::BadRequest().reason("Handshake error").finish()
}
}
}
}
@ -189,7 +189,7 @@ where
// /// the returned response headers contain the first protocol in this list
// /// which the server also knows.
pub fn handshake<S>(
req: &HttpRequest<S>
req: &HttpRequest<S>,
) -> Result<HttpResponseBuilder, HandshakeError> {
// WebSocket accepts only GET
if *req.method() != Method::GET {
@ -216,9 +216,7 @@ pub fn handshake<S>(
}
// check supported version
if !req.headers()
.contains_key(header::SEC_WEBSOCKET_VERSION)
{
if !req.headers().contains_key(header::SEC_WEBSOCKET_VERSION) {
return Err(HandshakeError::NoVersionHeader);
}
let supported_ver = {
@ -355,10 +353,7 @@ mod tests {
HeaderMap::new(),
None,
);
assert_eq!(
HandshakeError::GetMethodRequired,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::GetMethodRequired, handshake(&req).err().unwrap());
let req = HttpRequest::new(
Method::GET,
@ -367,16 +362,10 @@ mod tests {
HeaderMap::new(),
None,
);
assert_eq!(
HandshakeError::NoWebsocketUpgrade,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("test"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("test"));
let req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
@ -384,16 +373,10 @@ mod tests {
headers,
None,
);
assert_eq!(
HandshakeError::NoWebsocketUpgrade,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("websocket"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
let req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
@ -401,20 +384,11 @@ mod tests {
headers,
None,
);
assert_eq!(
HandshakeError::NoConnectionUpgrade,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::NoConnectionUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("websocket"),
);
headers.insert(
header::CONNECTION,
header::HeaderValue::from_static("upgrade"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
let req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
@ -422,20 +396,11 @@ mod tests {
headers,
None,
);
assert_eq!(
HandshakeError::NoVersionHeader,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::NoVersionHeader, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("websocket"),
);
headers.insert(
header::CONNECTION,
header::HeaderValue::from_static("upgrade"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
headers.insert(
header::SEC_WEBSOCKET_VERSION,
header::HeaderValue::from_static("5"),
@ -447,20 +412,11 @@ mod tests {
headers,
None,
);
assert_eq!(
HandshakeError::UnsupportedVersion,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::UnsupportedVersion, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("websocket"),
);
headers.insert(
header::CONNECTION,
header::HeaderValue::from_static("upgrade"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
headers.insert(
header::SEC_WEBSOCKET_VERSION,
header::HeaderValue::from_static("13"),
@ -472,28 +428,17 @@ mod tests {
headers,
None,
);
assert_eq!(
HandshakeError::BadWebsocketKey,
handshake(&req).err().unwrap()
);
assert_eq!(HandshakeError::BadWebsocketKey, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new();
headers.insert(
header::UPGRADE,
header::HeaderValue::from_static("websocket"),
);
headers.insert(
header::CONNECTION,
header::HeaderValue::from_static("upgrade"),
);
headers.insert(header::UPGRADE, header::HeaderValue::from_static("websocket"));
headers.insert(header::CONNECTION, header::HeaderValue::from_static("upgrade"));
headers.insert(
header::SEC_WEBSOCKET_VERSION,
header::HeaderValue::from_static("13"),
);
headers.insert(
header::SEC_WEBSOCKET_KEY,
header::HeaderValue::from_static("13"),
);
headers
.insert(header::SEC_WEBSOCKET_KEY, header::HeaderValue::from_static("13"));
let req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),

View File

@ -194,11 +194,11 @@ impl From<CloseCode> for CloseReason {
}
}
impl <T: Into<String>> From<(CloseCode, T)> for CloseReason {
impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
fn from(info: (CloseCode, T)) -> Self {
CloseReason{
CloseReason {
code: info.0,
description: Some(info.1.into())
description: Some(info.1.into()),
}
}
}

View File

@ -9,8 +9,8 @@ use std::io::Read;
use bytes::Bytes;
use flate2::read::GzDecoder;
use futures::Future;
use futures::stream::once;
use futures::Future;
use rand::Rng;
use actix_web::*;
@ -72,10 +72,7 @@ fn test_with_query_parameter() {
})
});
let request = srv.get()
.uri(srv.url("/?qp=5").as_str())
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/?qp=5").as_str()).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -124,10 +121,8 @@ fn test_client_gzip_encoding() {
});
// client request
let request = srv.post()
.content_encoding(http::ContentEncoding::Gzip)
.body(STR)
.unwrap();
let request =
srv.post().content_encoding(http::ContentEncoding::Gzip).body(STR).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -167,10 +162,7 @@ fn test_client_gzip_encoding_large() {
#[test]
fn test_client_gzip_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(100_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(100_000).collect::<String>();
let mut srv = test::TestServer::new(|app| {
app.handler(|req: HttpRequest| {
@ -228,10 +220,7 @@ fn test_client_brotli_encoding() {
#[cfg(feature = "brotli")]
#[test]
fn test_client_brotli_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(70_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(70_000).collect::<String>();
let mut srv = test::TestServer::new(|app| {
app.handler(|req: HttpRequest| {
@ -275,10 +264,8 @@ fn test_client_deflate_encoding() {
});
// client request
let request = srv.post()
.content_encoding(http::ContentEncoding::Deflate)
.body(STR)
.unwrap();
let request =
srv.post().content_encoding(http::ContentEncoding::Deflate).body(STR).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -290,10 +277,7 @@ fn test_client_deflate_encoding() {
#[cfg(feature = "brotli")]
#[test]
fn test_client_deflate_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(70_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(70_000).collect::<String>();
let mut srv = test::TestServer::new(|app| {
app.handler(|req: HttpRequest| {
@ -338,9 +322,7 @@ fn test_client_streaming_explicit() {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
let request = srv.get()
.body(Body::Streaming(Box::new(body)))
.unwrap();
let request = srv.get().body(Body::Streaming(Box::new(body))).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -413,11 +395,8 @@ fn test_client_cookie_handling() {
})
});
let request = srv.get()
.cookie(cookie1.clone())
.cookie(cookie2.clone())
.finish()
.unwrap();
let request =
srv.get().cookie(cookie1.clone()).cookie(cookie2.clone()).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
let c1 = response.cookie("cookie1").expect("Missing cookie1");

View File

@ -26,10 +26,7 @@ fn test_path_extractor() {
});
// client request
let request = srv.get()
.uri(srv.url("/test/index.html"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/test/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -47,10 +44,7 @@ fn test_query_extractor() {
});
// client request
let request = srv.get()
.uri(srv.url("/index.html?username=test"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/index.html?username=test")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -59,10 +53,7 @@ fn test_query_extractor() {
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
// client request
let request = srv.get()
.uri(srv.url("/index.html"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
@ -78,10 +69,8 @@ fn test_path_and_query_extractor() {
});
// client request
let request = srv.get()
.uri(srv.url("/test1/index.html?username=test2"))
.finish()
.unwrap();
let request =
srv.get().uri(srv.url("/test1/index.html?username=test2")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -90,10 +79,7 @@ fn test_path_and_query_extractor() {
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
// client request
let request = srv.get()
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/test1/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
@ -102,18 +88,15 @@ fn test_path_and_query_extractor() {
fn test_path_and_query_extractor2() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route()
.with3(|_: HttpRequest, p: Path<PParam>, q: Query<PParam>| {
format!("Welcome {} - {}!", p.username, q.username)
})
r.route().with3(|_: HttpRequest, p: Path<PParam>, q: Query<PParam>| {
format!("Welcome {} - {}!", p.username, q.username)
})
});
});
// client request
let request = srv.get()
.uri(srv.url("/test1/index.html?username=test2"))
.finish()
.unwrap();
let request =
srv.get().uri(srv.url("/test1/index.html?username=test2")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -122,10 +105,7 @@ fn test_path_and_query_extractor2() {
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
// client request
let request = srv.get()
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/test1/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
@ -137,10 +117,7 @@ fn test_non_ascii_route() {
});
// client request
let request = srv.get()
.uri(srv.url("/中文/index.html"))
.finish()
.unwrap();
let request = srv.get().uri(srv.url("/中文/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -158,17 +135,12 @@ fn test_unsafe_path_route() {
});
// client request
let request = srv.get()
.uri(srv.url("/test/http%3A%2F%2Fexample.com"))
.finish()
.unwrap();
let request =
srv.get().uri(srv.url("/test/http%3A%2F%2Fexample.com")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(
bytes,
Bytes::from_static(b"success: http:%2F%2Fexample.com")
);
assert_eq!(bytes, Bytes::from_static(b"success: http:%2F%2Fexample.com"));
}

View File

@ -14,9 +14,9 @@ extern crate brotli2;
#[cfg(feature = "brotli")]
use brotli2::write::{BrotliDecoder, BrotliEncoder};
use bytes::{Bytes, BytesMut};
use flate2::Compression;
use flate2::read::GzDecoder;
use flate2::write::{DeflateDecoder, DeflateEncoder, GzEncoder};
use flate2::Compression;
use futures::stream::once;
use futures::{future, Future, Stream};
use h2::client as h2client;
@ -62,11 +62,9 @@ fn test_start() {
thread::spawn(move || {
let sys = System::new("test");
let srv = server::new(|| {
vec![
App::new().resource("/", |r| {
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
}),
]
vec![App::new().resource("/", |r| {
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
})]
});
let srv = srv.bind("127.0.0.1:0").unwrap();
@ -113,11 +111,9 @@ fn test_shutdown() {
thread::spawn(move || {
let sys = System::new("test");
let srv = server::new(|| {
vec![
App::new().resource("/", |r| {
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
}),
]
vec![App::new().resource("/", |r| {
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
})]
});
let srv = srv.bind("127.0.0.1:0").unwrap();
@ -135,7 +131,9 @@ fn test_shutdown() {
.finish()
.unwrap();
let response = sys.run_until_complete(req.send()).unwrap();
srv_addr.do_send(server::StopServer { graceful: true });
srv_addr.do_send(server::StopServer {
graceful: true,
});
assert!(response.status().is_success());
}
@ -208,9 +206,7 @@ fn test_body() {
fn test_body_gzip() {
let mut srv = test::TestServer::new(|app| {
app.handler(|_| {
HttpResponse::Ok()
.content_encoding(http::ContentEncoding::Gzip)
.body(STR)
HttpResponse::Ok().content_encoding(http::ContentEncoding::Gzip).body(STR)
})
});
@ -258,10 +254,7 @@ fn test_body_gzip_large() {
#[test]
fn test_body_gzip_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(70_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(70_000).collect::<String>();
let srv_data = Arc::new(data.clone());
let mut srv = test::TestServer::new(move |app| {
@ -342,11 +335,7 @@ fn test_body_br_streaming() {
#[test]
fn test_head_empty() {
let mut srv = test::TestServer::new(|app| {
app.handler(|_| {
HttpResponse::Ok()
.content_length(STR.len() as u64)
.finish()
})
app.handler(|_| HttpResponse::Ok().content_length(STR.len() as u64).finish())
});
let request = srv.head().finish().unwrap();
@ -354,10 +343,7 @@ fn test_head_empty() {
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
let len = response.headers().get(http::header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
@ -382,10 +368,7 @@ fn test_head_binary() {
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
let len = response.headers().get(http::header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
@ -409,10 +392,7 @@ fn test_head_binary2() {
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
let len = response.headers().get(http::header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
}
@ -468,9 +448,7 @@ fn test_body_chunked_explicit() {
fn test_body_deflate() {
let mut srv = test::TestServer::new(|app| {
app.handler(|_| {
HttpResponse::Ok()
.content_encoding(http::ContentEncoding::Deflate)
.body(STR)
HttpResponse::Ok().content_encoding(http::ContentEncoding::Deflate).body(STR)
})
});
@ -494,9 +472,7 @@ fn test_body_deflate() {
fn test_body_brotli() {
let mut srv = test::TestServer::new(|app| {
app.handler(|_| {
HttpResponse::Ok()
.content_encoding(http::ContentEncoding::Br)
.body(STR)
HttpResponse::Ok().content_encoding(http::ContentEncoding::Br).body(STR)
})
});
@ -580,10 +556,7 @@ fn test_gzip_encoding_large() {
#[test]
fn test_reading_gzip_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(60_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(60_000).collect::<String>();
let mut srv = test::TestServer::new(|app| {
app.handler(|req: HttpRequest| {
@ -634,10 +607,8 @@ fn test_reading_deflate_encoding() {
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(http::header::CONTENT_ENCODING, "deflate")
.body(enc)
.unwrap();
let request =
srv.post().header(http::header::CONTENT_ENCODING, "deflate").body(enc).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -666,10 +637,8 @@ fn test_reading_deflate_encoding_large() {
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(http::header::CONTENT_ENCODING, "deflate")
.body(enc)
.unwrap();
let request =
srv.post().header(http::header::CONTENT_ENCODING, "deflate").body(enc).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -680,10 +649,7 @@ fn test_reading_deflate_encoding_large() {
#[test]
fn test_reading_deflate_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(160_000)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(160_000).collect::<String>();
let mut srv = test::TestServer::new(|app| {
app.handler(|req: HttpRequest| {
@ -702,10 +668,8 @@ fn test_reading_deflate_encoding_large_random() {
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(http::header::CONTENT_ENCODING, "deflate")
.body(enc)
.unwrap();
let request =
srv.post().header(http::header::CONTENT_ENCODING, "deflate").body(enc).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -735,10 +699,8 @@ fn test_brotli_encoding() {
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(http::header::CONTENT_ENCODING, "br")
.body(enc)
.unwrap();
let request =
srv.post().header(http::header::CONTENT_ENCODING, "br").body(enc).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -768,10 +730,8 @@ fn test_brotli_encoding_large() {
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(http::header::CONTENT_ENCODING, "br")
.body(enc)
.unwrap();
let request =
srv.post().header(http::header::CONTENT_ENCODING, "br").body(enc).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
@ -789,30 +749,29 @@ fn test_h2() {
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = tcp.then(|res| h2client::handshake(res.unwrap()))
.then(move |res| {
let (mut client, h2) = res.unwrap();
let tcp = tcp.then(|res| h2client::handshake(res.unwrap())).then(move |res| {
let (mut client, h2) = res.unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response.and_then(|response| {
assert_eq!(response.status(), http::StatusCode::OK);
response.and_then(|response| {
assert_eq!(response.status(), http::StatusCode::OK);
let (_, body) = response.into_parts();
let (_, body) = response.into_parts();
body.fold(BytesMut::new(), |mut b, c| -> Result<_, h2::Error> {
b.extend(c);
Ok(b)
})
body.fold(BytesMut::new(), |mut b, c| -> Result<_, h2::Error> {
b.extend(c);
Ok(b)
})
});
})
});
let _res = core.run(tcp);
// assert_eq!(res.unwrap(), Bytes::from_static(STR.as_ref()));
}
@ -836,28 +795,20 @@ struct MiddlewareTest {
impl<S> middleware::Middleware<S> for MiddlewareTest {
fn start(&self, _: &mut HttpRequest<S>) -> Result<middleware::Started> {
self.start.store(
self.start.load(Ordering::Relaxed) + 1,
Ordering::Relaxed,
);
self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
Ok(middleware::Started::Done)
}
fn response(
&self, _: &mut HttpRequest<S>, resp: HttpResponse
&self, _: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<middleware::Response> {
self.response.store(
self.response.load(Ordering::Relaxed) + 1,
Ordering::Relaxed,
);
self.response
.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
Ok(middleware::Response::Done(resp))
}
fn finish(&self, _: &mut HttpRequest<S>, _: &HttpResponse) -> middleware::Finished {
self.finish.store(
self.finish.load(Ordering::Relaxed) + 1,
Ordering::Relaxed,
);
self.finish.store(self.finish.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
middleware::Finished::Done
}
}

View File

@ -44,12 +44,7 @@ fn test_simple() {
writer.binary(b"text".as_ref());
let (item, reader) = srv.execute(reader.into_future()).unwrap();
assert_eq!(
item,
Some(ws::Message::Binary(
Bytes::from_static(b"text").into()
))
);
assert_eq!(item, Some(ws::Message::Binary(Bytes::from_static(b"text").into())));
writer.ping("ping");
let (item, reader) = srv.execute(reader.into_future()).unwrap();
@ -75,7 +70,8 @@ fn test_close_description() {
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
let (reader, mut writer) = srv.ws().unwrap();
let close_reason:ws::CloseReason = (ws::CloseCode::Normal, "close description").into();
let close_reason: ws::CloseReason =
(ws::CloseCode::Normal, "close description").into();
writer.close(Some(close_reason.clone()));
let (item, _) = srv.execute(reader.into_future()).unwrap();
assert_eq!(item, Some(ws::Message::Close(Some(close_reason))));
@ -83,10 +79,7 @@ fn test_close_description() {
#[test]
fn test_large_text() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(65_536)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(65_536).collect::<String>();
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
let (mut reader, mut writer) = srv.ws().unwrap();
@ -101,10 +94,7 @@ fn test_large_text() {
#[test]
fn test_large_bin() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(65_536)
.collect::<String>();
let data = rand::thread_rng().gen_ascii_chars().take(65_536).collect::<String>();
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
let (mut reader, mut writer) = srv.ws().unwrap();
@ -113,10 +103,7 @@ fn test_large_bin() {
writer.binary(data.clone());
let (item, r) = srv.execute(reader.into_future()).unwrap();
reader = r;
assert_eq!(
item,
Some(ws::Message::Binary(Binary::from(data.clone())))
);
assert_eq!(item, Some(ws::Message::Binary(Binary::from(data.clone()))));
}
}
@ -220,26 +207,20 @@ fn test_ws_server_ssl() {
// load ssl keys
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("tests/key.pem", SslFiletype::PEM)
.unwrap();
builder
.set_certificate_chain_file("tests/cert.pem")
.unwrap();
builder.set_private_key_file("tests/key.pem", SslFiletype::PEM).unwrap();
builder.set_certificate_chain_file("tests/cert.pem").unwrap();
let mut srv = test::TestServer::build()
.ssl(builder.build())
.start(|app| {
app.handler(|req| {
ws::start(
req,
Ws2 {
count: 0,
bin: false,
},
)
})
});
let mut srv = test::TestServer::build().ssl(builder.build()).start(|app| {
app.handler(|req| {
ws::start(
req,
Ws2 {
count: 0,
bin: false,
},
)
})
});
let (mut reader, _writer) = srv.ws().unwrap();
let data = Some(ws::Message::Text("0".repeat(65_536)));

View File

@ -14,8 +14,8 @@ extern crate url;
use futures::Future;
use rand::{thread_rng, Rng};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use actix::prelude::*;
@ -61,12 +61,8 @@ fn main() {
let sample_rate = parse_u64_default(matches.value_of("sample-rate"), 1) as usize;
let perf_counters = Arc::new(PerfCounters::new());
let payload = Arc::new(
thread_rng()
.gen_ascii_chars()
.take(payload_size)
.collect::<String>(),
);
let payload =
Arc::new(thread_rng().gen_ascii_chars().take(payload_size).collect::<String>());
let sys = actix::System::new("ws-client");
@ -82,43 +78,40 @@ fn main() {
let perf = perf_counters.clone();
let addr = Arbiter::new(format!("test {}", t));
addr.do_send(actix::msgs::Execute::new(
move || -> Result<(), ()> {
for _ in 0..concurrency {
let pl2 = pl.clone();
let perf2 = perf.clone();
let ws2 = ws.clone();
addr.do_send(actix::msgs::Execute::new(move || -> Result<(), ()> {
for _ in 0..concurrency {
let pl2 = pl.clone();
let perf2 = perf.clone();
let ws2 = ws.clone();
Arbiter::handle().spawn(
ws::Client::new(&ws)
.write_buffer_capacity(0)
.connect()
.map_err(|e| {
println!("Error: {}", e);
//Arbiter::system().do_send(actix::msgs::SystemExit(0));
()
})
.map(move |(reader, writer)| {
let addr: Addr<Syn, _> =
ChatClient::create(move |ctx| {
ChatClient::add_stream(reader, ctx);
ChatClient {
url: ws2,
conn: writer,
payload: pl2,
bin: bin,
ts: time::precise_time_ns(),
perf_counters: perf2,
sent: 0,
max_payload_size: max_payload_size,
}
});
}),
);
}
Ok(())
},
));
Arbiter::handle().spawn(
ws::Client::new(&ws)
.write_buffer_capacity(0)
.connect()
.map_err(|e| {
println!("Error: {}", e);
//Arbiter::system().do_send(actix::msgs::SystemExit(0));
()
})
.map(move |(reader, writer)| {
let addr: Addr<Syn, _> = ChatClient::create(move |ctx| {
ChatClient::add_stream(reader, ctx);
ChatClient {
url: ws2,
conn: writer,
payload: pl2,
bin: bin,
ts: time::precise_time_ns(),
perf_counters: perf2,
sent: 0,
max_payload_size: max_payload_size,
}
});
}),
);
}
Ok(())
}));
}
let res = sys.run();
@ -126,10 +119,7 @@ fn main() {
fn parse_u64_default(input: Option<&str>, default: u64) -> u64 {
input
.map(|v| {
v.parse()
.expect(&format!("not a valid number: {}", v))
})
.map(|v| v.parse().expect(&format!("not a valid number: {}", v)))
.unwrap_or(default)
}
@ -149,15 +139,13 @@ impl Actor for Perf {
impl Perf {
fn sample_rate(&self, ctx: &mut Context<Self>) {
ctx.run_later(
Duration::new(self.sample_rate_secs as u64, 0),
|act, ctx| {
let req_count = act.counters.pull_request_count();
if req_count != 0 {
let conns = act.counters.pull_connections_count();
let latency = act.counters.pull_latency_ns();
let latency_max = act.counters.pull_latency_max_ns();
println!(
ctx.run_later(Duration::new(self.sample_rate_secs as u64, 0), |act, ctx| {
let req_count = act.counters.pull_request_count();
if req_count != 0 {
let conns = act.counters.pull_connections_count();
let latency = act.counters.pull_latency_ns();
let latency_max = act.counters.pull_latency_max_ns();
println!(
"rate: {}, conns: {}, throughput: {:?} kb, latency: {}, latency max: {}",
req_count / act.sample_rate_secs,
conns / act.sample_rate_secs,
@ -166,11 +154,10 @@ impl Perf {
time::Duration::nanoseconds((latency / req_count as u64) as i64),
time::Duration::nanoseconds(latency_max as i64)
);
}
}
act.sample_rate(ctx);
},
);
act.sample_rate(ctx);
});
}
}
@ -314,8 +301,7 @@ impl PerfCounters {
loop {
let current = self.lat_max.load(Ordering::SeqCst);
if current >= nanos
|| self.lat_max
.compare_and_swap(current, nanos, Ordering::SeqCst)
|| self.lat_max.compare_and_swap(current, nanos, Ordering::SeqCst)
== current
{
break;