From 5ff35f5b99e4bda221a3937dece60f75b864ff53 Mon Sep 17 00:00:00 2001
From: Nikolay Kim <fafhrd91@gmail.com>
Date: Fri, 5 Jan 2018 13:30:21 -0800
Subject: [PATCH] upgrade to actix 0.4

---
 Cargo.toml                         |  7 +---
 examples/basics/Cargo.toml         |  4 +-
 examples/basics/src/main.rs        |  8 +---
 examples/diesel/Cargo.toml         |  4 +-
 examples/hello-world/Cargo.toml    |  2 +-
 examples/json/Cargo.toml           |  4 +-
 examples/multipart/Cargo.toml      |  4 +-
 examples/signals/Cargo.toml        |  4 +-
 examples/state/Cargo.toml          |  4 +-
 examples/template_tera/Cargo.toml  |  4 +-
 examples/tls/Cargo.toml            |  4 +-
 examples/websocket-chat/Cargo.toml |  4 +-
 examples/websocket/Cargo.toml      |  4 +-
 guide/src/qs_3_5.md                |  9 +---
 guide/src/qs_9.md                  |  6 +--
 src/context.rs                     |  9 ++--
 src/server.rs                      | 66 +++++++++++++++---------------
 src/worker.rs                      |  9 ++--
 src/ws.rs                          |  9 ++--
 19 files changed, 75 insertions(+), 90 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 5ba819c4f..d81057fcc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,9 +32,6 @@ tls = ["native-tls", "tokio-tls"]
 # openssl
 alpn = ["openssl", "openssl/v102", "openssl/v110", "tokio-openssl"]
 
-# signals
-signal = ["actix/signal"]
-
 [dependencies]
 log = "0.3"
 failure = "0.1"
@@ -77,9 +74,7 @@ tokio-tls = { version="0.1", optional = true }
 tokio-openssl = { version="0.1", optional = true }
 
 [dependencies.actix]
-version = "^0.3.5"
-default-features = false
-features = []
+version = "0.4"
 
 [dependencies.openssl]
 version = "0.9"
diff --git a/examples/basics/Cargo.toml b/examples/basics/Cargo.toml
index b5eefd0f3..88b5f61e0 100644
--- a/examples/basics/Cargo.toml
+++ b/examples/basics/Cargo.toml
@@ -7,5 +7,5 @@ workspace = "../.."
 [dependencies]
 futures = "*"
 env_logger = "0.4"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
diff --git a/examples/basics/src/main.rs b/examples/basics/src/main.rs
index 64c6809c9..c1d4902e2 100644
--- a/examples/basics/src/main.rs
+++ b/examples/basics/src/main.rs
@@ -11,7 +11,6 @@ use actix::*;
 use actix_web::*;
 use actix_web::middleware::RequestSession;
 use futures::future::{FutureResult, result};
-#[cfg(unix)]
 use actix::actors::signal::{ProcessSignals, Subscribe};
 
 /// simple handler
@@ -97,11 +96,8 @@ fn main() {
         .start();
 
     // Subscribe to unix signals
-    #[cfg(unix)]
-    {
-        let signals = actix::Arbiter::system_registry().get::<ProcessSignals>();
-        signals.send(Subscribe(addr.subscriber()));
-    }
+    let signals = actix::Arbiter::system_registry().get::<ProcessSignals>();
+    signals.send(Subscribe(addr.subscriber()));
 
     println!("Starting http server: 127.0.0.1:8080");
     let _ = sys.run();
diff --git a/examples/diesel/Cargo.toml b/examples/diesel/Cargo.toml
index 31c8c4068..eb6628375 100644
--- a/examples/diesel/Cargo.toml
+++ b/examples/diesel/Cargo.toml
@@ -6,8 +6,8 @@ workspace = "../.."
 
 [dependencies]
 env_logger = "0.4"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
 
 futures = "0.1"
 uuid = { version = "0.5", features = ["serde", "v4"] }
diff --git a/examples/hello-world/Cargo.toml b/examples/hello-world/Cargo.toml
index 63bb6f6a4..4cb1f70f7 100644
--- a/examples/hello-world/Cargo.toml
+++ b/examples/hello-world/Cargo.toml
@@ -6,5 +6,5 @@ workspace = "../.."
 
 [dependencies]
 env_logger = "0.4"
-actix = "^0.3.5"
+actix = "0.4"
 actix-web = { path = "../../" }
diff --git a/examples/json/Cargo.toml b/examples/json/Cargo.toml
index e4d7ed8fc..681b63450 100644
--- a/examples/json/Cargo.toml
+++ b/examples/json/Cargo.toml
@@ -14,5 +14,5 @@ serde_json = "1.0"
 serde_derive = "1.0"
 json = "*"
 
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
diff --git a/examples/multipart/Cargo.toml b/examples/multipart/Cargo.toml
index 7a92f465c..32edbea61 100644
--- a/examples/multipart/Cargo.toml
+++ b/examples/multipart/Cargo.toml
@@ -11,5 +11,5 @@ path = "src/main.rs"
 [dependencies]
 env_logger = "*"
 futures = "0.1"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
diff --git a/examples/signals/Cargo.toml b/examples/signals/Cargo.toml
index 984cf8e3a..1e4006e35 100644
--- a/examples/signals/Cargo.toml
+++ b/examples/signals/Cargo.toml
@@ -11,5 +11,5 @@ path = "src/main.rs"
 [dependencies]
 env_logger = "*"
 futures = "0.1"
-actix = "^0.3.5"
-actix-web = { path = "../../", features=["signal"] }
+actix = "0.4"
+actix-web = { path = "../../" }
diff --git a/examples/state/Cargo.toml b/examples/state/Cargo.toml
index 7e4c7d3dd..149e8128e 100644
--- a/examples/state/Cargo.toml
+++ b/examples/state/Cargo.toml
@@ -7,5 +7,5 @@ workspace = "../.."
 [dependencies]
 futures = "*"
 env_logger = "0.4"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
\ No newline at end of file
diff --git a/examples/template_tera/Cargo.toml b/examples/template_tera/Cargo.toml
index 791934d09..3862fb803 100644
--- a/examples/template_tera/Cargo.toml
+++ b/examples/template_tera/Cargo.toml
@@ -6,6 +6,6 @@ workspace = "../.."
 
 [dependencies]
 env_logger = "0.4"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
 tera = "*"
diff --git a/examples/tls/Cargo.toml b/examples/tls/Cargo.toml
index a659bc36b..e6d39742d 100644
--- a/examples/tls/Cargo.toml
+++ b/examples/tls/Cargo.toml
@@ -10,5 +10,5 @@ path = "src/main.rs"
 
 [dependencies]
 env_logger = "0.4"
-actix = { version = "^0.3.5" }
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal", "alpn"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web", features=["alpn"] }
diff --git a/examples/websocket-chat/Cargo.toml b/examples/websocket-chat/Cargo.toml
index 517cf8163..f5534f9be 100644
--- a/examples/websocket-chat/Cargo.toml
+++ b/examples/websocket-chat/Cargo.toml
@@ -25,5 +25,5 @@ serde = "1.0"
 serde_json = "1.0"
 serde_derive = "1.0"
 
-actix = { version = "^0.3.5" }
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web" }
diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml
index 3168601a2..5c6bd9889 100644
--- a/examples/websocket/Cargo.toml
+++ b/examples/websocket/Cargo.toml
@@ -10,5 +10,5 @@ path = "src/main.rs"
 [dependencies]
 env_logger = "*"
 futures = "0.1"
-actix = "^0.3.5"
-actix-web = { git = "https://github.com/actix/actix-web.git", features=["signal"] }
+actix = "0.4"
+actix-web = { git = "https://github.com/actix/actix-web.git" }
diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md
index 98a5d8c39..b9f002c87 100644
--- a/guide/src/qs_3_5.md
+++ b/guide/src/qs_3_5.md
@@ -195,13 +195,8 @@ fn main() {
 }
 ```
 
-It is possible to use unix signals on compatible OSs. "signal" feature needs to be enabled
-in *Cargo.toml* for *actix-web* dependency.
-
-```toml
-[dependencies]
-actix-web = { git = "https://github.com/actix/actix-web", features=["signal"] }
-```
+It is possible to use signals. *CTRL-C* is available on all OSs, other signals are 
+available on unix systems.
 
 Then you can subscribe your server to unix signals. Http server handles three signals:
 
diff --git a/guide/src/qs_9.md b/guide/src/qs_9.md
index 7feb7d94b..0c291237b 100644
--- a/guide/src/qs_9.md
+++ b/guide/src/qs_9.md
@@ -23,15 +23,15 @@ impl Actor for Ws {
 /// Define Handler for ws::Message message
 # impl StreamHandler<ws::Message> for Ws {}
 impl Handler<ws::Message> for Ws {
-    fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) -> Response<Self, ws::Message>
-    {
+    type Result=();
+    
+    fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) {
         match msg {
             ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg),
             ws::Message::Text(text) => ws::WsWriter::text(ctx, &text),
             ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
             _ => (),
         }
-        Self::empty()
     }
 }
 
diff --git a/src/context.rs b/src/context.rs
index dff9344a2..d6261a3a0 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -6,9 +6,9 @@ use futures::sync::oneshot::Sender;
 use futures::unsync::oneshot;
 
 use actix::{Actor, ActorState, ActorContext, AsyncContext,
-            Handler, Subscriber, ResponseType};
+            Handler, Subscriber, ResponseType, SpawnHandle};
 use actix::fut::ActorFuture;
-use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell, SpawnHandle,
+use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell,
                  Envelope, ToEnvelope, RemoteEnvelope};
 
 use body::{Body, Binary};
@@ -290,13 +290,14 @@ impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>,
 impl<A, S> ToEnvelope<A> for HttpContext<A, S>
     where A: Actor<Context=HttpContext<A, S>>,
 {
-    fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>) -> Envelope<A>
+    fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
+               channel_on_drop: bool) -> Envelope<A>
         where A: Handler<M>,
               M: ResponseType + Send + 'static,
               M::Item: Send,
               M::Error: Send
     {
-        RemoteEnvelope::new(msg, tx).into()
+        RemoteEnvelope::new(msg, tx, channel_on_drop).into()
     }
 }
 
diff --git a/src/server.rs b/src/server.rs
index d602e2769..45d158c51 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -5,12 +5,12 @@ use std::time::Duration;
 use std::marker::PhantomData;
 use std::collections::HashMap;
 
-use actix::dev::*;
-use actix::System;
+use actix::prelude::*;
 use futures::{Future, Sink, Stream};
 use futures::sync::mpsc;
 use tokio_io::{AsyncRead, AsyncWrite};
 use tokio_core::net::TcpStream;
+use actix::actors::signal;
 use mio;
 use num_cpus;
 use net2::TcpBuilder;
@@ -27,9 +27,6 @@ use openssl::pkcs12::ParsedPkcs12;
 #[cfg(feature="alpn")]
 use tokio_openssl::SslStream;
 
-#[cfg(feature="signal")]
-use actix::actors::signal;
-
 use helpers;
 use channel::{HttpChannel, HttpHandler, IntoHttpHandler, IoStream, WrapperStream};
 use worker::{Conn, Worker, WorkerSettings, StreamHandlerType, StopWorker};
@@ -202,7 +199,6 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
         self
     }
 
-    #[cfg(feature="signal")]
     /// Send `SystemExit` message to actix system
     ///
     /// `SystemExit` message stops currently running system arbiter and all
@@ -271,7 +267,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
                 let apps: Vec<_> = (*factory)()
                     .into_iter()
                     .map(|h| h.into_handler(s.clone())).collect();
-                ctx.add_stream(rx);
+                ctx.add_message_stream(rx);
                 Worker::new(apps, h, ka)
             });
             workers.push(tx);
@@ -494,8 +490,7 @@ impl<T, A, H, U, V> HttpServer<WrapperStream<T>, A, H, U>
     }
 }
 
-#[cfg(feature="signal")]
-/// Unix Signals support
+/// Signals support
 /// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
 /// message to `System` actor.
 impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
@@ -504,9 +499,9 @@ impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
           U: 'static,
           A: 'static,
 {
-    fn handle(&mut self, msg: signal::Signal, ctx: &mut Context<Self>)
-              -> Response<Self, signal::Signal>
-    {
+    type Result = ();
+
+    fn handle(&mut self, msg: signal::Signal, ctx: &mut Context<Self>) {
         match msg.0 {
             signal::SignalType::Int => {
                 info!("SIGINT received, exiting");
@@ -524,32 +519,33 @@ impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
                 Handler::<StopServer>::handle(self, StopServer{graceful: false}, ctx);
             }
             _ => (),
-        };
-        Self::empty()
+        }
     }
 }
 
-impl<T, A, H, U> StreamHandler<Conn<T>, io::Error> for HttpServer<T, A, H, U>
+impl<T, A, H, U> StreamHandler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
     where T: IoStream,
           H: HttpHandler + 'static,
           U: 'static,
           A: 'static {}
 
-impl<T, A, H, U> Handler<Conn<T>, io::Error> for HttpServer<T, A, H, U>
+impl<T, A, H, U> Handler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
     where T: IoStream,
           H: HttpHandler + 'static,
           U: 'static,
           A: 'static,
 {
-    fn error(&mut self, err: io::Error, _: &mut Context<Self>) {
-        debug!("Error handling request: {}", err)
-    }
+    type Result = ();
 
-    fn handle(&mut self, msg: Conn<T>, _: &mut Context<Self>) -> Response<Self, Conn<T>>
-    {
-        Arbiter::handle().spawn(
-            HttpChannel::new(Rc::clone(self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2));
-        Self::empty()
+    fn handle(&mut self, msg: io::Result<Conn<T>>, _: &mut Context<Self>) -> Self::Result {
+        match msg {
+            Ok(msg) =>
+                Arbiter::handle().spawn(
+                    HttpChannel::new(
+                        Rc::clone(self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2)),
+            Err(err) =>
+                debug!("Error handling request: {}", err),
+        }
     }
 }
 
@@ -578,13 +574,14 @@ impl<T, A, H, U> Handler<PauseServer> for HttpServer<T, A, H, U>
           U: 'static,
           A: 'static,
 {
-    fn handle(&mut self, _: PauseServer, _: &mut Context<Self>) -> Response<Self, PauseServer>
+    type Result = ();
+
+    fn handle(&mut self, _: PauseServer, _: &mut Context<Self>)
     {
         for item in &self.accept {
             let _ = item.1.send(Command::Pause);
             let _ = item.0.set_readiness(mio::Ready::readable());
         }
-        Self::empty()
     }
 }
 
@@ -594,13 +591,13 @@ impl<T, A, H, U> Handler<ResumeServer> for HttpServer<T, A, H, U>
           U: 'static,
           A: 'static,
 {
-    fn handle(&mut self, _: ResumeServer, _: &mut Context<Self>) -> Response<Self, ResumeServer>
-    {
+    type Result = ();
+
+    fn handle(&mut self, _: ResumeServer, _: &mut Context<Self>) {
         for item in &self.accept {
             let _ = item.1.send(Command::Resume);
             let _ = item.0.set_readiness(mio::Ready::readable());
         }
-        Self::empty()
     }
 }
 
@@ -610,8 +607,9 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
           U: 'static,
           A: 'static,
 {
-    fn handle(&mut self, msg: StopServer, ctx: &mut Context<Self>) -> Response<Self, StopServer>
-    {
+    type Result = actix::Response<Self, StopServer>;
+
+    fn handle(&mut self, msg: StopServer, ctx: &mut Context<Self>) -> Self::Result {
         // stop accept threads
         for item in &self.accept {
             let _ = item.1.send(Command::Stop);
@@ -636,10 +634,10 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
 
                     // we need to stop system if server was spawned
                     if slf.exit {
-                        Arbiter::system().send(msgs::SystemExit(0))
+                        Arbiter::system().send(actix::msgs::SystemExit(0))
                     }
                 }
-                fut::ok(())
+                actix::fut::ok(())
             }).spawn(ctx);
         }
 
@@ -649,7 +647,7 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
         } else {
             // we need to stop system if server was spawned
             if self.exit {
-                Arbiter::system().send(msgs::SystemExit(0))
+                Arbiter::system().send(actix::msgs::SystemExit(0))
             }
             Self::empty()
         }
diff --git a/src/worker.rs b/src/worker.rs
index 188678a7f..d3bcf88c1 100644
--- a/src/worker.rs
+++ b/src/worker.rs
@@ -157,8 +157,9 @@ impl<H> StreamHandler<Conn<net::TcpStream>> for Worker<H>
 impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
     where H: HttpHandler + 'static,
 {
+    type Result = ();
+
     fn handle(&mut self, msg: Conn<net::TcpStream>, _: &mut Context<Self>)
-              -> Response<Self, Conn<net::TcpStream>>
     {
         if !self.settings.keep_alive_enabled() &&
             msg.io.set_keepalive(Some(time::Duration::new(75, 0))).is_err()
@@ -166,7 +167,6 @@ impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
             error!("Can not set socket keep-alive option");
         }
         self.handler.handle(Rc::clone(&self.settings), &self.hnd, msg);
-        Self::empty()
     }
 }
 
@@ -174,8 +174,9 @@ impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
 impl<H> Handler<StopWorker> for Worker<H>
     where H: HttpHandler + 'static,
 {
-    fn handle(&mut self, msg: StopWorker, ctx: &mut Context<Self>) -> Response<Self, StopWorker>
-    {
+    type Result = Response<Self, StopWorker>;
+
+    fn handle(&mut self, msg: StopWorker, ctx: &mut Context<Self>) -> Self::Result {
         let num = self.settings.channels.get();
         if num == 0 {
             info!("Shutting down http worker, 0 connections");
diff --git a/src/ws.rs b/src/ws.rs
index 69e74aadb..cd83ea2f7 100644
--- a/src/ws.rs
+++ b/src/ws.rs
@@ -26,16 +26,15 @@
 //! # impl StreamHandler<ws::Message> for Ws {}
 //! #
 //! impl Handler<ws::Message> for Ws {
-//!     fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
-//!               -> Response<Self, ws::Message>
-//!     {
+//!     type Result = ();
+//!
+//!     fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) {
 //!         match msg {
 //!             ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg),
 //!             ws::Message::Text(text) => ws::WsWriter::text(ctx, &text),
 //!             ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
 //!             _ => (),
 //!         }
-//!         Self::empty()
 //!     }
 //! }
 //! #
@@ -94,7 +93,7 @@ pub fn start<A, S>(mut req: HttpRequest<S>, actor: A) -> Result<HttpResponse, Er
     let stream = WsStream::new(req.payload_mut().readany());
 
     let mut ctx = HttpContext::new(req, actor);
-    ctx.add_stream(stream);
+    ctx.add_message_stream(stream);
 
     Ok(resp.body(ctx)?)
 }