diff --git a/Cargo.toml b/Cargo.toml
index 4d750d6630..90380e9fae 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -34,7 +34,6 @@ net2 = "0.2.32"
time = "0.1"
tokio = "0.1.5"
tokio-executor = "0.1.0"
-tokio-service = "0.1"
tokio-io = "0.1"
want = "0.0.3"
diff --git a/benches/end_to_end.rs b/benches/end_to_end.rs
index 58f14a2b0e..f62ce86a70 100644
--- a/benches/end_to_end.rs
+++ b/benches/end_to_end.rs
@@ -71,20 +71,20 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
static PHRASE: &'static [u8] = include_bytes!("../CHANGELOG.md"); //b"Hello, World!";
fn spawn_hello(rt: &mut Runtime) -> SocketAddr {
- use hyper::server::{const_service, service_fn, NewService};
+ use hyper::service::{service_fn};
let addr = "127.0.0.1:0".parse().unwrap();
let listener = TcpListener::bind(&addr).unwrap();
let addr = listener.local_addr().unwrap();
let http = Http::new();
- let service = const_service(service_fn(|req: Request
| {
+ let service = service_fn(|req: Request| {
req.into_body()
.concat2()
.map(|_| {
Response::new(Body::from(PHRASE))
})
- }));
+ });
// Specifically only accept 1 connection.
let srv = listener.incoming()
@@ -92,8 +92,7 @@ fn spawn_hello(rt: &mut Runtime) -> SocketAddr {
.map_err(|(e, _inc)| panic!("accept error: {}", e))
.and_then(move |(accepted, _inc)| {
let socket = accepted.expect("accepted socket");
- http.serve_connection(socket, service.new_service().expect("new_service"))
- .map(|_| ())
+ http.serve_connection(socket, service)
.map_err(|_| ())
});
rt.spawn(srv);
diff --git a/benches/server.rs b/benches/server.rs
index 858171099f..c58314bd71 100644
--- a/benches/server.rs
+++ b/benches/server.rs
@@ -11,11 +11,11 @@ use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc;
-use futures::{future, stream, Future, Stream};
+use futures::{stream, Future, Stream};
use futures::sync::oneshot;
-use hyper::{Body, Request, Response, Server};
-use hyper::server::Service;
+use hyper::{Body, Response, Server};
+use hyper::service::service_fn_ok;
macro_rules! bench_server {
($b:ident, $header:expr, $body:expr) => ({
@@ -26,10 +26,17 @@ macro_rules! bench_server {
::std::thread::spawn(move || {
let addr = "127.0.0.1:0".parse().unwrap();
let srv = Server::bind(&addr)
- .serve(|| Ok(BenchPayload {
- header: $header,
- body: $body,
- }));
+ .serve(|| {
+ let header = $header;
+ let body = $body;
+ service_fn_ok(move |_| {
+ Response::builder()
+ .header(header.0, header.1)
+ .header("content-type", "text/plain")
+ .body(body())
+ .unwrap()
+ })
+ });
addr_tx.send(srv.local_addr()).unwrap();
let fut = srv
.map_err(|e| panic!("server error: {}", e))
@@ -182,26 +189,3 @@ fn raw_tcp_throughput_large_payload(b: &mut test::Bencher) {
tx.send(()).unwrap();
}
-struct BenchPayload {
- header: (&'static str, &'static str),
- body: F,
-}
-
-impl Service for BenchPayload
-where
- F: Fn() -> B,
-{
- type Request = Request;
- type Response = Response;
- type Error = hyper::Error;
- type Future = future::FutureResult;
- fn call(&self, _req: Self::Request) -> Self::Future {
- future::ok(
- Response::builder()
- .header(self.header.0, self.header.1)
- .header("content-type", "text/plain")
- .body((self.body)())
- .unwrap()
- )
- }
-}
diff --git a/examples/hello.rs b/examples/hello.rs
index f3c476cd91..302f7bdc2a 100644
--- a/examples/hello.rs
+++ b/examples/hello.rs
@@ -6,8 +6,8 @@ extern crate tokio;
use futures::Future;
-use hyper::{Body, Response};
-use hyper::server::{Server, const_service, service_fn};
+use hyper::{Body, Response, Server};
+use hyper::service::service_fn_ok;
static PHRASE: &'static [u8] = b"Hello World!";
@@ -16,10 +16,16 @@ fn main() {
let addr = ([127, 0, 0, 1], 3000).into();
- let new_service = const_service(service_fn(|_| {
- //TODO: when `!` is stable, replace error type
- Ok::<_, hyper::Error>(Response::new(Body::from(PHRASE)))
- }));
+ // 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)
diff --git a/examples/multi_server.rs b/examples/multi_server.rs
index f8989fa334..3988561cac 100644
--- a/examples/multi_server.rs
+++ b/examples/multi_server.rs
@@ -5,39 +5,14 @@ extern crate pretty_env_logger;
extern crate tokio;
use futures::{Future};
-use futures::future::{FutureResult, lazy};
+use futures::future::{lazy};
-use hyper::{Body, Method, Request, Response, StatusCode};
-use hyper::server::{Server, Service};
+use hyper::{Body, Response, Server};
+use hyper::service::service_fn_ok;
static INDEX1: &'static [u8] = b"The 1st service!";
static INDEX2: &'static [u8] = b"The 2nd service!";
-struct Srv(&'static [u8]);
-
-impl Service for Srv {
- type Request = Request;
- type Response = Response;
- type Error = hyper::Error;
- type Future = FutureResult, hyper::Error>;
-
- fn call(&self, req: Request) -> Self::Future {
- futures::future::ok(match (req.method(), req.uri().path()) {
- (&Method::GET, "/") => {
- Response::new(self.0.into())
- },
- _ => {
- Response::builder()
- .status(StatusCode::NOT_FOUND)
- .body(Body::empty())
- .unwrap()
- }
- })
- }
-
-}
-
-
fn main() {
pretty_env_logger::init();
@@ -46,11 +21,11 @@ fn main() {
tokio::run(lazy(move || {
let srv1 = Server::bind(&addr1)
- .serve(|| Ok(Srv(INDEX1)))
+ .serve(|| service_fn_ok(|_| Response::new(Body::from(INDEX1))))
.map_err(|e| eprintln!("server 1 error: {}", e));
let srv2 = Server::bind(&addr2)
- .serve(|| Ok(Srv(INDEX2)))
+ .serve(|| service_fn_ok(|_| Response::new(Body::from(INDEX2))))
.map_err(|e| eprintln!("server 2 error: {}", e));
println!("Listening on http://{} and http://{}", addr1, addr2);
diff --git a/examples/params.rs b/examples/params.rs
index 05987a3d57..e3704f70ed 100644
--- a/examples/params.rs
+++ b/examples/params.rs
@@ -5,10 +5,10 @@ extern crate pretty_env_logger;
extern crate tokio;
extern crate url;
-use futures::{Future, Stream};
+use futures::{future, Future, Stream};
-use hyper::{Body, Method, Request, Response, StatusCode};
-use hyper::server::{Server, Service};
+use hyper::{Body, Method, Request, Response, Server, StatusCode};
+use hyper::service::service_fn;
use std::collections::HashMap;
use url::form_urlencoded;
@@ -17,89 +17,80 @@ static INDEX: &[u8] = b"