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

refactor handler rtype handling

This commit is contained in:
Nikolay Kim 2017-11-29 09:17:00 -08:00
parent e9bfab8012
commit 6177d86d97
6 changed files with 34 additions and 44 deletions

View File

@ -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<S> ApplicationBuilder<S> where S: 'static {
/// ```
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Into<HttpResponse> + 'static,
R: Into<Reply> + 'static,
P: Into<String>,
{
self.parts.as_mut().expect("Use after finish")

View File

@ -205,12 +205,6 @@ impl HttpResponse {
}
}
impl From<HttpResponse> 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<HttpResponse> for Frame {
fn from(resp: HttpResponse) -> Frame {
Frame::Message(resp)
}
}
#[derive(Debug)]
struct Parts {
version: Option<Version>,
@ -441,12 +442,6 @@ impl HttpResponseBuilder {
}
}
impl From<HttpResponseBuilder> for HttpResponse {
fn from(mut builder: HttpResponseBuilder) -> Self {
builder.finish().into()
}
}
fn parts<'a>(parts: &'a mut Option<Parts>, err: &Option<HttpError>) -> Option<&'a mut Parts>
{
if err.is_some() {
@ -465,8 +460,14 @@ impl<I: Into<HttpResponse>, E: Into<Error>> From<Result<I, E>> for HttpResponse
}
}
impl From<HttpResponseBuilder> 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<String> 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<String> 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<Bytes> 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<Bytes> for HttpResponse {
}
impl From<BytesMut> for HttpResponse {
fn from(val: BytesMut) -> HttpResponse {
fn from(val: BytesMut) -> Self {
HttpResponse::build(StatusCode::OK)
.content_type("application/octet-stream")
.body(val)

View File

@ -218,7 +218,6 @@ struct CookieSessionInner {
path: String,
domain: Option<String>,
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<String, String>) -> 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))

View File

@ -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<S> Resource<S> where S: 'static {
/// Register handler for specified method.
pub fn handler<F, R>(&mut self, method: Method, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Into<HttpResponse> + 'static,
R: Into<Reply> + 'static,
{
self.routes.insert(method, Box::new(FnHandler::new(handler)));
}

View File

@ -110,7 +110,7 @@ impl<A, S> RouteHandler<S> for RouteFactory<A, S>
pub(crate)
struct FnHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Into<HttpResponse>,
R: Into<Reply>,
S: 'static,
{
f: Box<F>,
@ -119,7 +119,7 @@ struct FnHandler<S, R, F>
impl<S, R, F> FnHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Into<HttpResponse> + 'static,
R: Into<Reply> + 'static,
S: 'static,
{
pub fn new(f: F) -> Self {
@ -129,11 +129,11 @@ impl<S, R, F> FnHandler<S, R, F>
impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Into<HttpResponse> + 'static,
R: Into<Reply> + 'static,
S: 'static,
{
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
task.reply((self.f)(req).into())
(self.f)(req).into().into(task)
}
}
@ -212,8 +212,7 @@ impl Reply
}
}
impl<T> From<T> for Reply
where T: Into<HttpResponse>
impl<T: Into<HttpResponse>> From<T> for Reply
{
fn from(item: T) -> Self {
Reply(ReplyItem::Message(item.into()))

View File

@ -114,6 +114,7 @@ pub struct Task {
middlewares: Option<MiddlewaresResponse>,
}
#[doc(hidden)]
impl Default for Task {
fn default() -> Task {
@ -130,7 +131,7 @@ impl Default for Task {
impl Task {
pub fn from_response<R: Into<HttpResponse>>(response: R) -> Task {
pub(crate) fn from_response<R: Into<HttpResponse>>(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<E: Into<Error>>(err: E) -> Task {
pub(crate) fn from_error<E: Into<Error>>(err: E) -> Task {
Task::from_response(err.into())
}
@ -163,7 +164,7 @@ impl Task {
self.stream = TaskStream::Context(ctx);
}
pub(crate) fn stream<S>(&mut self, stream: S)
pub fn stream<S>(&mut self, stream: S)
where S: Stream<Item=Frame, Error=Error> + 'static
{
self.stream = TaskStream::Stream(Box::new(stream));