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:
@@ -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,
|
||||
>,
|
||||
|
||||
Reference in New Issue
Block a user