1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-10 20:22:07 +00:00
actix-web/src/context.rs

266 lines
6.2 KiB
Rust
Raw Normal View History

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