From 6177d86d976c35314a40823faca386602874a930 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 29 Nov 2017 09:17:00 -0800 Subject: [PATCH] refactor handler rtype handling --- src/application.rs | 5 ++--- src/httpresponse.rs | 37 +++++++++++++++++++------------------ src/middlewares/session.rs | 13 ++----------- src/resource.rs | 5 ++--- src/route.rs | 11 +++++------ src/task.rs | 7 ++++--- 6 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/application.rs b/src/application.rs index 642bdd63..cbe92525 100644 --- a/src/application.rs +++ b/src/application.rs @@ -2,11 +2,10 @@ use std::rc::Rc; use std::collections::HashMap; use task::Task; -use route::{RouteHandler, FnHandler}; +use route::{RouteHandler, FnHandler, Reply}; use resource::Resource; use recognizer::{RouteRecognizer, check_pattern}; use httprequest::HttpRequest; -use httpresponse::HttpResponse; use channel::HttpHandler; use pipeline::Pipeline; use middlewares::Middleware; @@ -204,7 +203,7 @@ impl ApplicationBuilder where S: 'static { /// ``` pub fn handler(&mut self, path: P, handler: F) -> &mut Self where F: Fn(HttpRequest) -> R + 'static, - R: Into + 'static, + R: Into + 'static, P: Into, { self.parts.as_mut().expect("Use after finish") diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 459c9ba7..84e28e77 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -205,12 +205,6 @@ impl HttpResponse { } } -impl From for Frame { - fn from(resp: HttpResponse) -> Frame { - Frame::Message(resp) - } -} - impl fmt::Debug for HttpResponse { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let res = write!(f, "\nHttpResponse {:?} {}{}\n", @@ -229,6 +223,13 @@ impl fmt::Debug for HttpResponse { } } +// TODO: remove +impl From for Frame { + fn from(resp: HttpResponse) -> Frame { + Frame::Message(resp) + } +} + #[derive(Debug)] struct Parts { version: Option, @@ -441,12 +442,6 @@ impl HttpResponseBuilder { } } -impl From for HttpResponse { - fn from(mut builder: HttpResponseBuilder) -> Self { - builder.finish().into() - } -} - fn parts<'a>(parts: &'a mut Option, err: &Option) -> Option<&'a mut Parts> { if err.is_some() { @@ -465,8 +460,14 @@ impl, E: Into> From> for HttpResponse } } +impl From for HttpResponse { + fn from(mut builder: HttpResponseBuilder) -> Self { + builder.finish().into() + } +} + impl From<&'static str> for HttpResponse { - fn from(val: &'static str) -> HttpResponse { + fn from(val: &'static str) -> Self { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(val) @@ -475,7 +476,7 @@ impl From<&'static str> for HttpResponse { } impl From<&'static [u8]> for HttpResponse { - fn from(val: &'static [u8]) -> HttpResponse { + fn from(val: &'static [u8]) -> Self { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(val) @@ -484,7 +485,7 @@ impl From<&'static [u8]> for HttpResponse { } impl From for HttpResponse { - fn from(val: String) -> HttpResponse { + fn from(val: String) -> Self { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(val) @@ -493,7 +494,7 @@ impl From for HttpResponse { } impl<'a> From<&'a String> for HttpResponse { - fn from(val: &'a String) -> HttpResponse { + fn from(val: &'a String) -> Self { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(val) @@ -502,7 +503,7 @@ impl<'a> From<&'a String> for HttpResponse { } impl From for HttpResponse { - fn from(val: Bytes) -> HttpResponse { + fn from(val: Bytes) -> Self { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(val) @@ -511,7 +512,7 @@ impl From for HttpResponse { } impl From for HttpResponse { - fn from(val: BytesMut) -> HttpResponse { + fn from(val: BytesMut) -> Self { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(val) diff --git a/src/middlewares/session.rs b/src/middlewares/session.rs index 8e7315af..24ad9fef 100644 --- a/src/middlewares/session.rs +++ b/src/middlewares/session.rs @@ -218,7 +218,6 @@ struct CookieSessionInner { path: String, domain: Option, secure: bool, - http_only: bool, } impl CookieSessionInner { @@ -229,8 +228,7 @@ impl CookieSessionInner { name: "actix_session".to_owned(), path: "/".to_owned(), domain: None, - secure: true, - http_only: true } + secure: true } } fn set_cookie(&self, resp: &mut HttpResponse, state: &HashMap) -> Result<()> { @@ -243,7 +241,7 @@ impl CookieSessionInner { let mut cookie = Cookie::new(self.name.clone(), value); cookie.set_path(self.path.clone()); cookie.set_secure(self.secure); - cookie.set_http_only(self.http_only); + cookie.set_http_only(true); if let Some(ref domain) = self.domain { cookie.set_domain(domain.clone()); @@ -354,7 +352,6 @@ impl SessionBackend for CookieSessionBackend { /// .domain("www.rust-lang.org") /// .path("/") /// .secure(true) -/// .http_only(true) /// .finish(); /// # } /// ``` @@ -384,12 +381,6 @@ impl CookieSessionBackendBuilder { self } - /// Sets the `http_only` field in the session cookie being built. - pub fn http_only(mut self, value: bool) -> CookieSessionBackendBuilder { - self.0.http_only = value; - self - } - /// Finishes building and returns the built `CookieSessionBackend`. pub fn finish(self) -> CookieSessionBackend { CookieSessionBackend(Rc::new(self.0)) diff --git a/src/resource.rs b/src/resource.rs index f9549714..a7cdece1 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -7,10 +7,9 @@ use futures::Stream; use task::Task; use error::Error; -use route::{Route, RouteHandler, Frame, FnHandler, StreamHandler}; +use route::{Reply, Route, RouteHandler, Frame, FnHandler, StreamHandler}; use context::HttpContext; use httprequest::HttpRequest; -use httpresponse::HttpResponse; use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed}; /// Http resource @@ -64,7 +63,7 @@ impl Resource where S: 'static { /// Register handler for specified method. pub fn handler(&mut self, method: Method, handler: F) where F: Fn(HttpRequest) -> R + 'static, - R: Into + 'static, + R: Into + 'static, { self.routes.insert(method, Box::new(FnHandler::new(handler))); } diff --git a/src/route.rs b/src/route.rs index 08e64d1a..98701375 100644 --- a/src/route.rs +++ b/src/route.rs @@ -110,7 +110,7 @@ impl RouteHandler for RouteFactory pub(crate) struct FnHandler where F: Fn(HttpRequest) -> R + 'static, - R: Into, + R: Into, S: 'static, { f: Box, @@ -119,7 +119,7 @@ struct FnHandler impl FnHandler where F: Fn(HttpRequest) -> R + 'static, - R: Into + 'static, + R: Into + 'static, S: 'static, { pub fn new(f: F) -> Self { @@ -129,11 +129,11 @@ impl FnHandler impl RouteHandler for FnHandler where F: Fn(HttpRequest) -> R + 'static, - R: Into + 'static, + R: Into + 'static, S: 'static, { fn handle(&self, req: HttpRequest, task: &mut Task) { - task.reply((self.f)(req).into()) + (self.f)(req).into().into(task) } } @@ -212,8 +212,7 @@ impl Reply } } -impl From for Reply - where T: Into +impl> From for Reply { fn from(item: T) -> Self { Reply(ReplyItem::Message(item.into())) diff --git a/src/task.rs b/src/task.rs index f3a4b1cc..2d0545a7 100644 --- a/src/task.rs +++ b/src/task.rs @@ -114,6 +114,7 @@ pub struct Task { middlewares: Option, } +#[doc(hidden)] impl Default for Task { fn default() -> Task { @@ -130,7 +131,7 @@ impl Default for Task { impl Task { - pub fn from_response>(response: R) -> Task { + pub(crate) fn from_response>(response: R) -> Task { let mut frames = VecDeque::new(); frames.push_back(Frame::Message(response.into())); frames.push_back(Frame::Payload(None)); @@ -145,7 +146,7 @@ impl Task { middlewares: None } } - pub fn from_error>(err: E) -> Task { + pub(crate) fn from_error>(err: E) -> Task { Task::from_response(err.into()) } @@ -163,7 +164,7 @@ impl Task { self.stream = TaskStream::Context(ctx); } - pub(crate) fn stream(&mut self, stream: S) + pub fn stream(&mut self, stream: S) where S: Stream + 'static { self.stream = TaskStream::Stream(Box::new(stream));