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:
parent
e9bfab8012
commit
6177d86d97
@ -2,11 +2,10 @@ use std::rc::Rc;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use task::Task;
|
use task::Task;
|
||||||
use route::{RouteHandler, FnHandler};
|
use route::{RouteHandler, FnHandler, Reply};
|
||||||
use resource::Resource;
|
use resource::Resource;
|
||||||
use recognizer::{RouteRecognizer, check_pattern};
|
use recognizer::{RouteRecognizer, check_pattern};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
|
||||||
use channel::HttpHandler;
|
use channel::HttpHandler;
|
||||||
use pipeline::Pipeline;
|
use pipeline::Pipeline;
|
||||||
use middlewares::Middleware;
|
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
|
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<HttpResponse> + 'static,
|
R: Into<Reply> + 'static,
|
||||||
P: Into<String>,
|
P: Into<String>,
|
||||||
{
|
{
|
||||||
self.parts.as_mut().expect("Use after finish")
|
self.parts.as_mut().expect("Use after finish")
|
||||||
|
@ -205,12 +205,6 @@ impl HttpResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<HttpResponse> for Frame {
|
|
||||||
fn from(resp: HttpResponse) -> Frame {
|
|
||||||
Frame::Message(resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for HttpResponse {
|
impl fmt::Debug for HttpResponse {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let res = write!(f, "\nHttpResponse {:?} {}{}\n",
|
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)]
|
#[derive(Debug)]
|
||||||
struct Parts {
|
struct Parts {
|
||||||
version: Option<Version>,
|
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>
|
fn parts<'a>(parts: &'a mut Option<Parts>, err: &Option<HttpError>) -> Option<&'a mut Parts>
|
||||||
{
|
{
|
||||||
if err.is_some() {
|
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 {
|
impl From<&'static str> for HttpResponse {
|
||||||
fn from(val: &'static str) -> HttpResponse {
|
fn from(val: &'static str) -> Self {
|
||||||
HttpResponse::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("text/plain; charset=utf-8")
|
.content_type("text/plain; charset=utf-8")
|
||||||
.body(val)
|
.body(val)
|
||||||
@ -475,7 +476,7 @@ impl From<&'static str> for HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static [u8]> for HttpResponse {
|
impl From<&'static [u8]> for HttpResponse {
|
||||||
fn from(val: &'static [u8]) -> HttpResponse {
|
fn from(val: &'static [u8]) -> Self {
|
||||||
HttpResponse::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("application/octet-stream")
|
.content_type("application/octet-stream")
|
||||||
.body(val)
|
.body(val)
|
||||||
@ -484,7 +485,7 @@ impl From<&'static [u8]> for HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for HttpResponse {
|
impl From<String> for HttpResponse {
|
||||||
fn from(val: String) -> HttpResponse {
|
fn from(val: String) -> Self {
|
||||||
HttpResponse::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("text/plain; charset=utf-8")
|
.content_type("text/plain; charset=utf-8")
|
||||||
.body(val)
|
.body(val)
|
||||||
@ -493,7 +494,7 @@ impl From<String> for HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a 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)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("text/plain; charset=utf-8")
|
.content_type("text/plain; charset=utf-8")
|
||||||
.body(val)
|
.body(val)
|
||||||
@ -502,7 +503,7 @@ impl<'a> From<&'a String> for HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<Bytes> for HttpResponse {
|
impl From<Bytes> for HttpResponse {
|
||||||
fn from(val: Bytes) -> HttpResponse {
|
fn from(val: Bytes) -> Self {
|
||||||
HttpResponse::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("application/octet-stream")
|
.content_type("application/octet-stream")
|
||||||
.body(val)
|
.body(val)
|
||||||
@ -511,7 +512,7 @@ impl From<Bytes> for HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<BytesMut> for HttpResponse {
|
impl From<BytesMut> for HttpResponse {
|
||||||
fn from(val: BytesMut) -> HttpResponse {
|
fn from(val: BytesMut) -> Self {
|
||||||
HttpResponse::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("application/octet-stream")
|
.content_type("application/octet-stream")
|
||||||
.body(val)
|
.body(val)
|
||||||
|
@ -218,7 +218,6 @@ struct CookieSessionInner {
|
|||||||
path: String,
|
path: String,
|
||||||
domain: Option<String>,
|
domain: Option<String>,
|
||||||
secure: bool,
|
secure: bool,
|
||||||
http_only: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CookieSessionInner {
|
impl CookieSessionInner {
|
||||||
@ -229,8 +228,7 @@ impl CookieSessionInner {
|
|||||||
name: "actix_session".to_owned(),
|
name: "actix_session".to_owned(),
|
||||||
path: "/".to_owned(),
|
path: "/".to_owned(),
|
||||||
domain: None,
|
domain: None,
|
||||||
secure: true,
|
secure: true }
|
||||||
http_only: true }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cookie(&self, resp: &mut HttpResponse, state: &HashMap<String, String>) -> Result<()> {
|
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);
|
let mut cookie = Cookie::new(self.name.clone(), value);
|
||||||
cookie.set_path(self.path.clone());
|
cookie.set_path(self.path.clone());
|
||||||
cookie.set_secure(self.secure);
|
cookie.set_secure(self.secure);
|
||||||
cookie.set_http_only(self.http_only);
|
cookie.set_http_only(true);
|
||||||
|
|
||||||
if let Some(ref domain) = self.domain {
|
if let Some(ref domain) = self.domain {
|
||||||
cookie.set_domain(domain.clone());
|
cookie.set_domain(domain.clone());
|
||||||
@ -354,7 +352,6 @@ impl SessionBackend for CookieSessionBackend {
|
|||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
/// .path("/")
|
/// .path("/")
|
||||||
/// .secure(true)
|
/// .secure(true)
|
||||||
/// .http_only(true)
|
|
||||||
/// .finish();
|
/// .finish();
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
@ -384,12 +381,6 @@ impl CookieSessionBackendBuilder {
|
|||||||
self
|
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`.
|
/// Finishes building and returns the built `CookieSessionBackend`.
|
||||||
pub fn finish(self) -> CookieSessionBackend {
|
pub fn finish(self) -> CookieSessionBackend {
|
||||||
CookieSessionBackend(Rc::new(self.0))
|
CookieSessionBackend(Rc::new(self.0))
|
||||||
|
@ -7,10 +7,9 @@ use futures::Stream;
|
|||||||
|
|
||||||
use task::Task;
|
use task::Task;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use route::{Route, RouteHandler, Frame, FnHandler, StreamHandler};
|
use route::{Reply, Route, RouteHandler, Frame, FnHandler, StreamHandler};
|
||||||
use context::HttpContext;
|
use context::HttpContext;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
|
||||||
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
||||||
|
|
||||||
/// Http resource
|
/// Http resource
|
||||||
@ -64,7 +63,7 @@ impl<S> Resource<S> where S: 'static {
|
|||||||
/// Register handler for specified method.
|
/// Register handler for specified method.
|
||||||
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<HttpResponse> + 'static,
|
R: Into<Reply> + 'static,
|
||||||
{
|
{
|
||||||
self.routes.insert(method, Box::new(FnHandler::new(handler)));
|
self.routes.insert(method, Box::new(FnHandler::new(handler)));
|
||||||
}
|
}
|
||||||
|
11
src/route.rs
11
src/route.rs
@ -110,7 +110,7 @@ impl<A, S> RouteHandler<S> for RouteFactory<A, S>
|
|||||||
pub(crate)
|
pub(crate)
|
||||||
struct FnHandler<S, R, F>
|
struct FnHandler<S, R, F>
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<HttpResponse>,
|
R: Into<Reply>,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
f: Box<F>,
|
f: Box<F>,
|
||||||
@ -119,7 +119,7 @@ struct FnHandler<S, R, F>
|
|||||||
|
|
||||||
impl<S, R, F> FnHandler<S, R, F>
|
impl<S, R, F> FnHandler<S, R, F>
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<HttpResponse> + 'static,
|
R: Into<Reply> + 'static,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
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>
|
impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F>
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<HttpResponse> + 'static,
|
R: Into<Reply> + 'static,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
|
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
|
impl<T: Into<HttpResponse>> From<T> for Reply
|
||||||
where T: Into<HttpResponse>
|
|
||||||
{
|
{
|
||||||
fn from(item: T) -> Self {
|
fn from(item: T) -> Self {
|
||||||
Reply(ReplyItem::Message(item.into()))
|
Reply(ReplyItem::Message(item.into()))
|
||||||
|
@ -114,6 +114,7 @@ pub struct Task {
|
|||||||
middlewares: Option<MiddlewaresResponse>,
|
middlewares: Option<MiddlewaresResponse>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
impl Default for Task {
|
impl Default for Task {
|
||||||
|
|
||||||
fn default() -> Task {
|
fn default() -> Task {
|
||||||
@ -130,7 +131,7 @@ impl Default for Task {
|
|||||||
|
|
||||||
impl 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();
|
let mut frames = VecDeque::new();
|
||||||
frames.push_back(Frame::Message(response.into()));
|
frames.push_back(Frame::Message(response.into()));
|
||||||
frames.push_back(Frame::Payload(None));
|
frames.push_back(Frame::Payload(None));
|
||||||
@ -145,7 +146,7 @@ impl Task {
|
|||||||
middlewares: None }
|
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())
|
Task::from_response(err.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +164,7 @@ impl Task {
|
|||||||
self.stream = TaskStream::Context(ctx);
|
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
|
where S: Stream<Item=Frame, Error=Error> + 'static
|
||||||
{
|
{
|
||||||
self.stream = TaskStream::Stream(Box::new(stream));
|
self.stream = TaskStream::Stream(Box::new(stream));
|
||||||
|
Loading…
Reference in New Issue
Block a user