diff --git a/examples/hello.rs b/examples/hello.rs
index 3a1d865a35..60d5e6a3a8 100644
--- a/examples/hello.rs
+++ b/examples/hello.rs
@@ -2,29 +2,23 @@
extern crate hyper;
extern crate pretty_env_logger;
-use hyper::{Body, Response, Server};
+use hyper::{Body, Request, Response, Server};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};
-static PHRASE: &'static [u8] = b"Hello World!";
-
fn main() {
pretty_env_logger::init();
let addr = ([127, 0, 0, 1], 3000).into();
- // new_service is run for each connection, creating a 'service'
- // to handle requests for that specific connection.
- let new_service = || {
- // This is the `Service` that will handle the connection.
- // `service_fn_ok` is a helper to convert a function that
- // returns a Response into a `Service`.
- service_fn_ok(|_| {
- Response::new(Body::from(PHRASE))
- })
- };
-
let server = Server::bind(&addr)
- .serve(new_service)
+ .serve(|| {
+ // This is the `Service` that will handle the connection.
+ // `service_fn_ok` is a helper to convert a function that
+ // returns a Response into a `Service`.
+ service_fn_ok(move |_: Request
| {
+ Response::new(Body::from("Hello World!"))
+ })
+ })
.map_err(|e| eprintln!("server error: {}", e));
println!("Listening on http://{}", addr);
diff --git a/src/server/conn.rs b/src/server/conn.rs
index e4aa424ffc..2902e7cd02 100644
--- a/src/server/conn.rs
+++ b/src/server/conn.rs
@@ -25,15 +25,16 @@ use common::exec::{Exec, H2Exec, NewSvcExec};
use common::io::Rewind;
use error::{Kind, Parse};
use proto;
-use service::{NewService, Service};
+use service::Service;
use upgrade::Upgraded;
+pub(super) use self::make_service::MakeServiceRef;
pub(super) use self::spawn_all::NoopWatcher;
use self::spawn_all::NewSvcTask;
pub(super) use self::spawn_all::Watcher;
pub(super) use self::upgrades::UpgradeableConnection;
-#[cfg(feature = "runtime")] pub use super::tcp::AddrIncoming;
+#[cfg(feature = "runtime")] pub use super::tcp::{AddrIncoming, AddrStream};
/// A lower-level configuration of the HTTP protocol.
///
@@ -69,13 +70,13 @@ enum ConnectionMode {
#[derive(Debug)]
pub struct Serve {
incoming: I,
- new_service: S,
+ make_service: S,
protocol: Http,
}
/// A future building a new `Service` to a `Connection`.
///
-/// Wraps the future returned from `NewService` into one that returns
+/// Wraps the future returned from `MakeService` into one that returns
/// a `Connection`.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
@@ -349,12 +350,16 @@ impl Http {
///
/// This method will bind the `addr` provided with a new TCP listener ready
/// to accept connections. Each connection will be processed with the
- /// `new_service` object provided, creating a new service per
+ /// `make_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
- pub fn serve_addr(&self, addr: &SocketAddr, new_service: S) -> ::Result>
+ pub fn serve_addr(&self, addr: &SocketAddr, make_service: S) -> ::Result>
where
- S: NewService,
+ S: MakeServiceRef<
+ AddrStream,
+ ReqBody=Body,
+ ResBody=Bd,
+ >,
S::Error: Into>,
Bd: Payload,
E: H2Exec<::Future, Bd>,
@@ -363,19 +368,23 @@ impl Http {
if self.keep_alive {
incoming.set_keepalive(Some(Duration::from_secs(90)));
}
- Ok(self.serve_incoming(incoming, new_service))
+ Ok(self.serve_incoming(incoming, make_service))
}
/// Bind the provided `addr` with the `Handle` and return a [`Serve`](Serve)
///
/// This method will bind the `addr` provided with a new TCP listener ready
/// to accept connections. Each connection will be processed with the
- /// `new_service` object provided, creating a new service per
+ /// `make_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
- pub fn serve_addr_handle(&self, addr: &SocketAddr, handle: &Handle, new_service: S) -> ::Result>
+ pub fn serve_addr_handle(&self, addr: &SocketAddr, handle: &Handle, make_service: S) -> ::Result>
where
- S: NewService,
+ S: MakeServiceRef<
+ AddrStream,
+ ReqBody=Body,
+ ResBody=Bd,
+ >,
S::Error: Into>,
Bd: Payload,
E: H2Exec<::Future, Bd>,
@@ -384,23 +393,27 @@ impl Http {
if self.keep_alive {
incoming.set_keepalive(Some(Duration::from_secs(90)));
}
- Ok(self.serve_incoming(incoming, new_service))
+ Ok(self.serve_incoming(incoming, make_service))
}
- /// Bind the provided stream of incoming IO objects with a `NewService`.
- pub fn serve_incoming(&self, incoming: I, new_service: S) -> Serve
+ /// Bind the provided stream of incoming IO objects with a `MakeService`.
+ pub fn serve_incoming(&self, incoming: I, make_service: S) -> Serve
where
I: Stream,
I::Error: Into>,
I::Item: AsyncRead + AsyncWrite,
- S: NewService,
+ S: MakeServiceRef<
+ I::Item,
+ ReqBody=Body,
+ ResBody=Bd,
+ >,
S::Error: Into>,
Bd: Payload,
E: H2Exec<::Future, Bd>,
{
Serve {
- incoming: incoming,
- new_service: new_service,
+ incoming,
+ make_service,
protocol: self.clone(),
}
}
@@ -604,8 +617,9 @@ where
I: Stream,
I::Item: AsyncRead + AsyncWrite,
I::Error: Into>,
- S: NewService,
- S::Error: Into>,
+ S: MakeServiceRef,
+ //S::Error2: Into>,
+ //SME: Into>,
B: Payload,
E: H2Exec<::Future, B>,
{
@@ -614,7 +628,7 @@ where
fn poll(&mut self) -> Poll