feat(lib): replace types with those from http crate

BREAKING CHANGE: `Method`, `Request`, `Response`, `StatusCode`,
  `Version`, and `Uri` have been replaced with types from the `http`
  crate. The `hyper::header` module is gone for now.

  Removed `Client::get`, since it needed to construct a `Request<B>`
  with an empty body. Just use `Client::request` instead.

  Removed `compat` cargo feature, and `compat` related API.
This commit is contained in:
Sean McArthur
2018-02-28 16:37:17 -08:00
parent a37e6b59e6
commit 3cd48b45fb
109 changed files with 1004 additions and 14411 deletions

View File

@@ -1,84 +0,0 @@
//! Wrappers to build compatibility with the `http` crate.
use std::io::{Error as IoError};
use futures::{Future, Poll};
use http;
use tokio_service::{NewService, Service};
use error::Error;
use proto::Body;
use proto::request::Request;
use proto::response::Response;
/// Wraps a `Future` returning an `http::Response` into
/// a `Future` returning a `hyper::server::Response`.
#[derive(Debug)]
pub struct CompatFuture<F> {
future: F
}
impl<F, Bd> Future for CompatFuture<F>
where F: Future<Item=http::Response<Bd>, Error=Error>
{
type Item = Response<Bd>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.future.poll()
.map(|a| a.map(|res| res.into()))
}
}
/// Wraps a `Service` taking an `http::Request` and returning
/// an `http::Response` into a `Service` taking a `hyper::server::Request`,
/// and returning a `hyper::server::Response`.
#[derive(Debug)]
pub struct CompatService<S> {
service: S
}
pub(super) fn service<S>(service: S) -> CompatService<S> {
CompatService { service: service }
}
impl<S, Bd> Service for CompatService<S>
where S: Service<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Future = CompatFuture<S::Future>;
fn call(&self, req: Self::Request) -> Self::Future {
CompatFuture {
future: self.service.call(req.into())
}
}
}
/// Wraps a `NewService` taking an `http::Request` and returning
/// an `http::Response` into a `NewService` taking a `hyper::server::Request`,
/// and returning a `hyper::server::Response`.
#[derive(Debug)]
pub struct NewCompatService<S> {
new_service: S
}
pub(super) fn new_service<S>(new_service: S) -> NewCompatService<S> {
NewCompatService { new_service: new_service }
}
impl<S, Bd> NewService for NewCompatService<S>
where S: NewService<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Instance = CompatService<S::Instance>;
fn new_service(&self) -> Result<Self::Instance, IoError> {
self.new_service.new_service()
.map(service)
}
}

View File

@@ -14,7 +14,7 @@ use bytes::Bytes;
use futures::{Future, Poll, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
use proto;
use proto::{self, Body};
use super::{HyperService, Request, Response, Service};
/// A future binding a connection with a Service.
@@ -59,7 +59,7 @@ pub struct Parts<T> {
// ===== impl Connection =====
impl<I, B, S> Connection<I, S>
where S: Service<Request = Request, Response = Response<B>, Error = ::Error> + 'static,
where S: Service<Request = Request<Body>, Response = Response<B>, Error = ::Error> + 'static,
I: AsyncRead + AsyncWrite + 'static,
B: Stream<Error=::Error> + 'static,
B::Item: AsRef<[u8]>,
@@ -97,7 +97,7 @@ where S: Service<Request = Request, Response = Response<B>, Error = ::Error> + '
}
impl<I, B, S> Future for Connection<I, S>
where S: Service<Request = Request, Response = Response<B>, Error = ::Error> + 'static,
where S: Service<Request = Request<Body>, Response = Response<B>, Error = ::Error> + 'static,
I: AsyncRead + AsyncWrite + 'static,
B: Stream<Error=::Error> + 'static,
B::Item: AsRef<[u8]>,

View File

@@ -3,8 +3,6 @@
//! A `Server` is created to listen on a port, parse HTTP requests, and hand
//! them off to a `Service`.
#[cfg(feature = "compat")]
pub mod compat;
pub mod conn;
mod service;
@@ -19,24 +17,16 @@ use std::time::Duration;
use futures::task::{self, Task};
use futures::future::{self};
use futures::{Future, Stream, Poll, Async};
#[cfg(feature = "compat")]
use http;
use http::{Request, Response};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio::reactor::{Core, Handle, Timeout};
use tokio::net::TcpListener;
pub use tokio_service::{NewService, Service};
use proto;
#[cfg(feature = "compat")]
use proto::Body;
use proto::{self, Body};
use self::addr_stream::AddrStream;
use self::hyper_service::HyperService;
pub use proto::response::Response;
pub use proto::request::Request;
pub use self::conn::Connection;
pub use self::service::{const_service, service_fn};
@@ -163,7 +153,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
/// The returned `Server` contains one method, `run`, which is used to
/// actually run the server.
pub fn bind<S, Bd>(&self, addr: &SocketAddr, new_service: S) -> ::Result<Server<S, Bd>>
where S: NewService<Request = Request, Response = Response<Bd>, Error = ::Error> + 'static,
where S: NewService<Request = Request<Body>, Response = Response<Bd>, Error = ::Error> + 'static,
Bd: Stream<Item=B, Error=::Error>,
{
let core = try!(Core::new());
@@ -179,19 +169,6 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
})
}
/// Bind a `NewService` using types from the `http` crate.
///
/// See `Http::bind`.
#[cfg(feature = "compat")]
pub fn bind_compat<S, Bd>(&self, addr: &SocketAddr, new_service: S) -> ::Result<Server<compat::NewCompatService<S>, Bd>>
where S: NewService<Request = http::Request<Body>, Response = http::Response<Bd>, Error = ::Error> +
Send + Sync + 'static,
Bd: Stream<Item=B, Error=::Error>,
{
self.bind(addr, self::compat::new_service(new_service))
}
/// Bind the provided `addr` and return a server with a shared `Core`.
///
/// This method allows the ability to share a `Core` with multiple servers.
@@ -201,7 +178,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
/// `new_service` object provided as well, creating a new service per
/// connection.
pub fn serve_addr_handle<S, Bd>(&self, addr: &SocketAddr, handle: &Handle, new_service: S) -> ::Result<Serve<AddrIncoming, S>>
where S: NewService<Request = Request, Response = Response<Bd>, Error = ::Error>,
where S: NewService<Request = Request<Body>, Response = Response<Bd>, Error = ::Error>,
Bd: Stream<Item=B, Error=::Error>,
{
let listener = TcpListener::bind(addr, &handle)?;
@@ -218,7 +195,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
pub fn serve_incoming<I, S, Bd>(&self, incoming: I, new_service: S) -> Serve<I, S>
where I: Stream<Error=::std::io::Error>,
I::Item: AsyncRead + AsyncWrite,
S: NewService<Request = Request, Response = Response<Bd>, Error = ::Error>,
S: NewService<Request = Request<Body>, Response = Response<Bd>, Error = ::Error>,
Bd: Stream<Item=B, Error=::Error>,
{
Serve {
@@ -247,13 +224,14 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
/// # extern crate tokio_core;
/// # extern crate tokio_io;
/// # use futures::Future;
/// # use hyper::server::{Http, Request, Response, Service};
/// # use hyper::{Body, Request, Response};
/// # use hyper::server::{Http, Service};
/// # use tokio_io::{AsyncRead, AsyncWrite};
/// # use tokio_core::reactor::Handle;
/// # fn run<I, S>(some_io: I, some_service: S, some_handle: &Handle)
/// # where
/// # I: AsyncRead + AsyncWrite + 'static,
/// # S: Service<Request=Request, Response=Response, Error=hyper::Error> + 'static,
/// # S: Service<Request=Request<Body>, Response=Response<Body>, Error=hyper::Error> + 'static,
/// # {
/// let http = Http::<hyper::Chunk>::new();
/// let conn = http.serve_connection(some_io, some_service);
@@ -267,7 +245,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
/// # fn main() {}
/// ```
pub fn serve_connection<S, I, Bd>(&self, io: I, service: S) -> Connection<I, S>
where S: Service<Request = Request, Response = Response<Bd>, Error = ::Error>,
where S: Service<Request = Request<Body>, Response = Response<Bd>, Error = ::Error>,
Bd: Stream<Error=::Error>,
Bd::Item: AsRef<[u8]>,
I: AsyncRead + AsyncWrite,
@@ -311,7 +289,7 @@ impl<B> fmt::Debug for Http<B> {
// ===== impl Server =====
impl<S, B> Server<S, B>
where S: NewService<Request = Request, Response = Response<B>, Error = ::Error> + 'static,
where S: NewService<Request = Request<Body>, Response = Response<B>, Error = ::Error> + 'static,
B: Stream<Error=::Error> + 'static,
B::Item: AsRef<[u8]>,
{
@@ -384,9 +362,9 @@ impl<S, B> Server<S, B>
let addr = socket.remote_addr;
debug!("accepted new connection ({})", addr);
let addr_service = SocketAddrService::new(addr, new_service.new_service()?);
let service = new_service.new_service()?;
let s = NotifyService {
inner: addr_service,
inner: service,
info: Rc::downgrade(&info),
};
info.borrow_mut().active += 1;
@@ -466,7 +444,7 @@ impl<I, S, B> Stream for Serve<I, S>
where
I: Stream<Error=io::Error>,
I::Item: AsyncRead + AsyncWrite,
S: NewService<Request=Request, Response=Response<B>, Error=::Error>,
S: NewService<Request=Request<Body>, Response=Response<B>, Error=::Error>,
B: Stream<Error=::Error>,
B::Item: AsRef<[u8]>,
{
@@ -490,7 +468,7 @@ impl<I, S, E> Future for SpawnAll<I, S, E>
where
I: Stream<Error=io::Error>,
I::Item: AsyncRead + AsyncWrite,
S: NewService<Request=Request, Response=Response<B>, Error=::Error>,
S: NewService<Request=Request<Body>, Response=Response<B>, Error=::Error>,
B: Stream<Error=::Error>,
B::Item: AsRef<[u8]>,
//E: Executor<Connection<I::Item, S::Instance>>,
@@ -683,39 +661,6 @@ mod addr_stream {
}
}
// ===== SocketAddrService
// This is used from `Server::run`, which captures the remote address
// in this service, and then injects it into each `Request`.
struct SocketAddrService<S> {
addr: SocketAddr,
inner: S,
}
impl<S> SocketAddrService<S> {
fn new(addr: SocketAddr, service: S) -> SocketAddrService<S> {
SocketAddrService {
addr: addr,
inner: service,
}
}
}
impl<S> Service for SocketAddrService<S>
where
S: Service<Request=Request>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn call(&self, mut req: Self::Request) -> Self::Future {
proto::request::addr(&mut req, self.addr);
self.inner.call(req)
}
}
// ===== NotifyService =====
struct NotifyService<S> {
@@ -775,7 +720,7 @@ impl Future for WaitUntilZero {
}
mod hyper_service {
use super::{Request, Response, Service, Stream};
use super::{Body, Request, Response, Service, Stream};
/// A "trait alias" for any type that implements `Service` with hyper's
/// Request, Response, and Error types, and a streaming body.
///
@@ -802,7 +747,7 @@ mod hyper_service {
impl<S, B> Sealed for S
where
S: Service<
Request=Request,
Request=Request<Body>,
Response=Response<B>,
Error=::Error,
>,
@@ -813,7 +758,7 @@ mod hyper_service {
impl<S, B> HyperService for S
where
S: Service<
Request=Request,
Request=Request<Body>,
Response=Response<B>,
Error=::Error,
>,