2017-10-07 06:48:14 +02:00
|
|
|
use std;
|
2018-01-01 02:26:32 +01:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use futures::{Async, Future, Poll};
|
2017-10-21 02:16:17 +02:00
|
|
|
use futures::sync::oneshot::Sender;
|
2017-12-24 20:58:09 +01:00
|
|
|
use futures::unsync::oneshot;
|
2018-01-14 01:17:33 +01:00
|
|
|
use smallvec::SmallVec;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-21 02:16:17 +02:00
|
|
|
use actix::{Actor, ActorState, ActorContext, AsyncContext,
|
2018-01-08 02:13:49 +01:00
|
|
|
Address, SyncAddress, Handler, Subscriber, ResponseType, SpawnHandle};
|
2017-10-07 06:48:14 +02:00
|
|
|
use actix::fut::ActorFuture;
|
2018-01-08 02:13:49 +01:00
|
|
|
use actix::dev::{queue, AsyncContextApi,
|
|
|
|
ContextImpl, ContextProtocol, Envelope, ToEnvelope, RemoteEnvelope};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-30 23:42:20 +01:00
|
|
|
use body::{Body, Binary};
|
2018-01-07 07:59:39 +01:00
|
|
|
use error::{Error, Result, ErrorInternalServerError};
|
2017-11-29 19:31:24 +01:00
|
|
|
use httprequest::HttpRequest;
|
2017-12-24 20:58:09 +01:00
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-01-01 02:26:32 +01:00
|
|
|
pub trait ActorHttpContext: 'static {
|
2017-12-02 00:45:15 +01:00
|
|
|
fn disconnected(&mut self);
|
2018-01-25 05:12:49 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<SmallVec<[Frame; 4]>>, Error>;
|
2017-12-02 00:45:15 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-30 23:42:20 +01:00
|
|
|
#[derive(Debug)]
|
2018-01-01 02:26:32 +01:00
|
|
|
pub enum Frame {
|
2018-01-11 05:08:13 +01:00
|
|
|
Chunk(Option<Binary>),
|
2017-12-24 20:58:09 +01:00
|
|
|
Drain(oneshot::Sender<()>),
|
2017-11-30 23:42:20 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 00:35:39 +01:00
|
|
|
impl Frame {
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
match *self {
|
|
|
|
Frame::Chunk(Some(ref bin)) => bin.len(),
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:08:54 +01:00
|
|
|
/// Http actor execution context
|
2017-11-29 19:31:24 +01:00
|
|
|
pub struct HttpContext<A, S=()> where A: Actor<Context=HttpContext<A, S>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2018-01-07 07:59:39 +01:00
|
|
|
inner: ContextImpl<A>,
|
2018-01-25 05:12:49 +01:00
|
|
|
stream: Option<SmallVec<[Frame; 4]>>,
|
2017-11-29 19:31:24 +01:00
|
|
|
request: HttpRequest<S>,
|
2017-10-26 01:25:26 +02:00
|
|
|
disconnected: bool,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> ActorContext for HttpContext<A, S> where A: Actor<Context=Self>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
|
|
|
fn stop(&mut self) {
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.stop();
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
fn terminate(&mut self) {
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.terminate()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
fn state(&self) -> ActorState {
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.state()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> AsyncContext<A> for HttpContext<A, S> where A: Actor<Context=Self>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2018-01-23 18:42:04 +01:00
|
|
|
#[inline]
|
2017-10-09 19:44:11 +02:00
|
|
|
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
2017-10-07 06:48:14 +02:00
|
|
|
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
|
|
|
|
{
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.spawn(fut)
|
2017-10-09 19:44:11 +02:00
|
|
|
}
|
2018-01-23 18:42:04 +01:00
|
|
|
#[inline]
|
2017-10-12 04:20:05 +02:00
|
|
|
fn wait<F>(&mut self, fut: F)
|
|
|
|
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
|
|
|
|
{
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.wait(fut)
|
2017-10-12 04:20:05 +02:00
|
|
|
}
|
2018-01-23 18:42:04 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
fn waiting(&self) -> bool {
|
|
|
|
self.inner.wating()
|
|
|
|
}
|
|
|
|
#[inline]
|
2017-10-09 19:44:11 +02:00
|
|
|
fn cancel_future(&mut self, handle: SpawnHandle) -> bool {
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.cancel_future(handle)
|
2017-10-30 22:49:20 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
#[doc(hidden)]
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> AsyncContextApi<A> for HttpContext<A, S> where A: Actor<Context=Self> {
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
|
|
|
fn unsync_sender(&mut self) -> queue::unsync::UnboundedSender<ContextProtocol<A>> {
|
|
|
|
self.inner.unsync_sender()
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn unsync_address(&mut self) -> Address<A> {
|
|
|
|
self.inner.unsync_address()
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn sync_address(&mut self) -> SyncAddress<A> {
|
|
|
|
self.inner.sync_address()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 02:26:32 +01:00
|
|
|
impl<A, S: 'static> HttpContext<A, S> where A: Actor<Context=Self> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2018-01-01 02:26:32 +01:00
|
|
|
pub fn new(req: HttpRequest<S>, actor: A) -> HttpContext<A, S> {
|
|
|
|
HttpContext::from_request(req).actor(actor)
|
|
|
|
}
|
|
|
|
pub fn from_request(req: HttpRequest<S>) -> HttpContext<A, S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
HttpContext {
|
2018-01-07 07:59:39 +01:00
|
|
|
inner: ContextImpl::new(None),
|
2018-01-14 01:17:33 +01:00
|
|
|
stream: None,
|
2017-11-29 19:31:24 +01:00
|
|
|
request: req,
|
2017-10-26 01:25:26 +02:00
|
|
|
disconnected: false,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2018-01-01 02:26:32 +01:00
|
|
|
pub fn actor(mut self, actor: A) -> HttpContext<A, S> {
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.set_actor(actor);
|
2018-01-01 02:26:32 +01:00
|
|
|
self
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-27 06:47:33 +01:00
|
|
|
/// Shared application state
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-11-29 19:31:24 +01:00
|
|
|
pub fn state(&self) -> &S {
|
|
|
|
self.request.state()
|
2017-11-27 06:47:33 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
/// Incoming request
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-11-29 19:31:24 +01:00
|
|
|
pub fn request(&mut self) -> &mut HttpRequest<S> {
|
|
|
|
&mut self.request
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
/// Write payload
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-11-10 22:42:32 +01:00
|
|
|
pub fn write<B: Into<Binary>>(&mut self, data: B) {
|
2018-01-01 02:26:32 +01:00
|
|
|
if !self.disconnected {
|
2018-01-14 01:17:33 +01:00
|
|
|
self.add_frame(Frame::Chunk(Some(data.into())));
|
2017-11-30 23:42:20 +01:00
|
|
|
} else {
|
2018-01-01 02:26:32 +01:00
|
|
|
warn!("Trying to write to disconnected response");
|
2017-11-30 23:42:20 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-26 01:25:26 +02:00
|
|
|
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Indicate end of streaming payload. Also this method calls `Self::close`.
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-12-02 00:45:15 +01:00
|
|
|
pub fn write_eof(&mut self) {
|
2018-01-14 01:17:33 +01:00
|
|
|
self.add_frame(Frame::Chunk(None));
|
2017-12-02 00:45:15 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 14:05:31 +01:00
|
|
|
/// Returns drain future
|
2018-01-01 02:26:32 +01:00
|
|
|
pub fn drain(&mut self) -> Drain<A> {
|
2017-12-24 20:58:09 +01:00
|
|
|
let (tx, rx) = oneshot::channel();
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.modify();
|
2018-01-14 01:17:33 +01:00
|
|
|
self.add_frame(Frame::Drain(tx));
|
2018-01-01 02:26:32 +01:00
|
|
|
Drain::new(rx)
|
2017-10-29 14:05:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 01:25:26 +02:00
|
|
|
/// Check if connection still open
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-10-26 01:25:26 +02:00
|
|
|
pub fn connected(&self) -> bool {
|
|
|
|
!self.disconnected
|
|
|
|
}
|
2018-01-14 01:17:33 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn add_frame(&mut self, frame: Frame) {
|
|
|
|
if self.stream.is_none() {
|
|
|
|
self.stream = Some(SmallVec::new());
|
|
|
|
}
|
|
|
|
self.stream.as_mut().map(|s| s.push(frame));
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
2017-10-21 02:16:17 +02:00
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-10-21 02:16:17 +02:00
|
|
|
#[doc(hidden)]
|
2017-10-22 00:21:16 +02:00
|
|
|
pub fn subscriber<M>(&mut self) -> Box<Subscriber<M>>
|
2018-01-07 07:59:39 +01:00
|
|
|
where A: Handler<M>, M: ResponseType + 'static
|
2017-10-21 02:16:17 +02:00
|
|
|
{
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.subscriber()
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-10-21 02:16:17 +02:00
|
|
|
#[doc(hidden)]
|
2017-10-22 00:21:16 +02:00
|
|
|
pub fn sync_subscriber<M>(&mut self) -> Box<Subscriber<M> + Send>
|
2017-10-21 02:16:17 +02:00
|
|
|
where A: Handler<M>,
|
2018-01-07 07:59:39 +01:00
|
|
|
M: ResponseType + Send + 'static, M::Item: Send, M::Error: Send,
|
2017-10-21 02:16:17 +02:00
|
|
|
{
|
2018-01-07 07:59:39 +01:00
|
|
|
self.inner.sync_subscriber()
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 02:26:32 +01:00
|
|
|
impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>, S: 'static {
|
2017-11-30 23:42:20 +01:00
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2017-11-30 23:42:20 +01:00
|
|
|
fn disconnected(&mut self) {
|
|
|
|
self.disconnected = true;
|
2018-01-07 07:59:39 +01:00
|
|
|
self.stop();
|
2017-11-30 23:42:20 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-01-25 05:12:49 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<SmallVec<[Frame; 4]>>, Error> {
|
2017-11-29 19:31:24 +01:00
|
|
|
let ctx: &mut HttpContext<A, S> = unsafe {
|
|
|
|
std::mem::transmute(self as &mut HttpContext<A, S>)
|
2017-10-07 06:48:14 +02:00
|
|
|
};
|
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
if self.inner.alive() {
|
|
|
|
match self.inner.poll(ctx) {
|
|
|
|
Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
|
|
|
|
Err(_) => return Err(ErrorInternalServerError("error").into()),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-01-08 02:13:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// frames
|
2018-01-14 01:17:33 +01:00
|
|
|
if let Some(data) = self.stream.take() {
|
|
|
|
Ok(Async::Ready(Some(data)))
|
2018-01-08 02:13:49 +01:00
|
|
|
} else if self.inner.alive() {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(None))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 02:16:17 +02:00
|
|
|
|
2017-11-29 19:31:24 +01:00
|
|
|
impl<A, S> ToEnvelope<A> for HttpContext<A, S>
|
|
|
|
where A: Actor<Context=HttpContext<A, S>>,
|
2017-10-21 02:16:17 +02:00
|
|
|
{
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2018-01-05 22:30:21 +01:00
|
|
|
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
|
|
|
|
channel_on_drop: bool) -> Envelope<A>
|
2017-10-25 05:24:14 +02:00
|
|
|
where A: Handler<M>,
|
2018-01-10 19:12:34 +01:00
|
|
|
M: ResponseType + Send + 'static, M::Item: Send, M::Error: Send
|
2017-10-21 02:16:17 +02:00
|
|
|
{
|
2018-01-05 22:30:21 +01:00
|
|
|
RemoteEnvelope::new(msg, tx, channel_on_drop).into()
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-01 02:26:32 +01:00
|
|
|
|
|
|
|
impl<A, S> From<HttpContext<A, S>> for Body
|
|
|
|
where A: Actor<Context=HttpContext<A, S>>,
|
|
|
|
S: 'static
|
|
|
|
{
|
|
|
|
fn from(ctx: HttpContext<A, S>) -> Body {
|
|
|
|
Body::Actor(Box::new(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Drain<A> {
|
|
|
|
fut: oneshot::Receiver<()>,
|
|
|
|
_a: PhantomData<A>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> Drain<A> {
|
2018-01-10 19:12:34 +01:00
|
|
|
pub fn new(fut: oneshot::Receiver<()>) -> Self {
|
2018-01-01 02:26:32 +01:00
|
|
|
Drain {
|
|
|
|
fut: fut,
|
|
|
|
_a: PhantomData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Actor> ActorFuture for Drain<A> {
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
type Actor = A;
|
|
|
|
|
2018-01-08 02:13:49 +01:00
|
|
|
#[inline]
|
2018-01-01 02:26:32 +01:00
|
|
|
fn poll(&mut self,
|
|
|
|
_: &mut A,
|
|
|
|
_: &mut <Self::Actor as Actor>::Context) -> Poll<Self::Item, Self::Error>
|
|
|
|
{
|
|
|
|
self.fut.poll().map_err(|_| ())
|
|
|
|
}
|
|
|
|
}
|