mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
better naming
This commit is contained in:
parent
a505be9321
commit
ce4aea46c3
@ -20,7 +20,7 @@ pub const HTTPMethodNotAllowed: StaticResponse = StaticResponse(StatusCode::METH
|
||||
impl<S> RouteHandler<S> for StaticResponse {
|
||||
fn handle(&self, req: HttpRequest, _: Option<Payload>, _: Rc<S>) -> Task
|
||||
{
|
||||
Task::reply(HttpResponse::new(req, self.0, Body::Empty), None)
|
||||
Task::reply(HttpResponse::new(req, self.0, Body::Empty))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,9 +25,9 @@ impl Route for MyRoute {
|
||||
{
|
||||
if let Some(pl) = payload {
|
||||
ctx.add_stream(pl);
|
||||
HttpMessage::Stream(MyRoute{req: Some(req)})
|
||||
Self::stream(MyRoute{req: Some(req)})
|
||||
} else {
|
||||
HttpMessage::Reply(req, httpcodes::HTTPOk)
|
||||
Self::reply(req, httpcodes::HTTPOk)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ use std::marker::PhantomData;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix::Actor;
|
||||
use bytes::Bytes;
|
||||
use http::Method;
|
||||
|
||||
use task::Task;
|
||||
@ -88,7 +87,7 @@ impl<S: 'static> RouteHandler<S> for HttpResource<S> {
|
||||
|
||||
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
|
||||
enum HttpMessageItem<A> where A: Actor<Context=HttpContext<A>> + Route {
|
||||
Message(HttpResponse, Option<Bytes>),
|
||||
Message(HttpResponse),
|
||||
Actor(A),
|
||||
}
|
||||
|
||||
@ -97,27 +96,21 @@ pub struct HttpMessage<A: Actor<Context=HttpContext<A>> + Route> (HttpMessageIte
|
||||
impl<A> HttpMessage<A> where A: Actor<Context=HttpContext<A>> + Route
|
||||
{
|
||||
/// Create async response
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Stream(act: A) -> Self {
|
||||
pub fn stream(act: A) -> Self {
|
||||
HttpMessage(HttpMessageItem::Actor(act))
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Reply<I>(req: HttpRequest, msg: I) -> Self
|
||||
/// Create response with empty body
|
||||
pub fn reply<I>(req: HttpRequest, msg: I) -> Self
|
||||
where I: IntoHttpResponse
|
||||
{
|
||||
HttpMessage(HttpMessageItem::Message(msg.into_response(req), None))
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn ReplyMessage(msg: HttpResponse, body: Option<Bytes>) -> Self {
|
||||
HttpMessage(HttpMessageItem::Message(msg, body))
|
||||
HttpMessage(HttpMessageItem::Message(msg.into_response(req)))
|
||||
}
|
||||
|
||||
pub(crate) fn into(self, mut ctx: HttpContext<A>) -> Task {
|
||||
match self.0 {
|
||||
HttpMessageItem::Message(msg, body) => {
|
||||
Task::reply(msg, body)
|
||||
HttpMessageItem::Message(msg) => {
|
||||
Task::reply(msg)
|
||||
},
|
||||
HttpMessageItem::Actor(act) => {
|
||||
ctx.set_actor(act);
|
||||
|
14
src/route.rs
14
src/route.rs
@ -8,7 +8,7 @@ use futures::unsync::mpsc::Receiver;
|
||||
use task::Task;
|
||||
use context::HttpContext;
|
||||
use resource::HttpMessage;
|
||||
use httpmessage::{HttpRequest, HttpResponse};
|
||||
use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse};
|
||||
|
||||
/// Stream of `PayloadItem`'s
|
||||
pub type Payload = Receiver<PayloadItem>;
|
||||
@ -60,6 +60,18 @@ pub trait Route: Actor<Context=HttpContext<Self>> {
|
||||
fn factory() -> RouteFactory<Self, Self::State> {
|
||||
RouteFactory(PhantomData)
|
||||
}
|
||||
|
||||
/// Create async response
|
||||
fn stream(act: Self) -> HttpMessage<Self> {
|
||||
HttpMessage::stream(act)
|
||||
}
|
||||
|
||||
/// Create response
|
||||
fn reply<I>(req: HttpRequest, msg: I) -> HttpMessage<Self>
|
||||
where I: IntoHttpResponse
|
||||
{
|
||||
HttpMessage::reply(req, msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -91,7 +91,7 @@ impl Router {
|
||||
}
|
||||
}
|
||||
|
||||
Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req), None)
|
||||
Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
src/task.rs
13
src/task.rs
@ -4,7 +4,7 @@ use std::fmt::Write;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use http::{StatusCode, Version};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use futures::{Async, Future, Poll, Stream};
|
||||
use tokio_core::net::TcpStream;
|
||||
|
||||
@ -57,15 +57,10 @@ pub struct Task {
|
||||
|
||||
impl Task {
|
||||
|
||||
pub(crate) fn reply(msg: HttpResponse, body: Option<Bytes>) -> Self {
|
||||
pub(crate) fn reply(msg: HttpResponse) -> Self {
|
||||
let mut frames = VecDeque::new();
|
||||
if let Some(body) = body {
|
||||
frames.push_back(Frame::Message(msg));
|
||||
frames.push_back(Frame::Payload(Some(body)));
|
||||
frames.push_back(Frame::Payload(None));
|
||||
} else {
|
||||
frames.push_back(Frame::Message(msg));
|
||||
}
|
||||
frames.push_back(Frame::Message(msg));
|
||||
frames.push_back(Frame::Payload(None));
|
||||
|
||||
Task {
|
||||
state: TaskRunningState::Running,
|
||||
|
Loading…
Reference in New Issue
Block a user