feat(lib): implement compatibility with http crate

This commit is contained in:
Sam Rijs
2017-09-23 05:07:57 +10:00
committed by Sean McArthur
parent 92595e84a2
commit 0c7d375ba3
17 changed files with 535 additions and 1 deletions

View File

@@ -1,11 +1,16 @@
use std::fmt;
#[cfg(feature = "compat")]
use std::mem::replace;
use std::net::SocketAddr;
#[cfg(feature = "compat")]
use http_types;
use header::Headers;
use http::{Body, MessageHead, RequestHead, RequestLine};
use method::Method;
use uri::{self, Uri};
use version::HttpVersion;
use std::net::SocketAddr;
/// An HTTP Request
pub struct Request<B = Body> {
@@ -132,6 +137,36 @@ impl<B> fmt::Debug for Request<B> {
}
}
#[cfg(feature = "compat")]
impl From<Request> for http_types::Request<Body> {
fn from(from_req: Request) -> http_types::Request<Body> {
let (m, u, v, h, b) = from_req.deconstruct();
let to_req = http_types::Request::new(());
let (mut to_parts, _) = to_req.into_parts();
to_parts.method = m.into();
to_parts.uri = u.into();
to_parts.version = v.into();
to_parts.headers = h.into();
http_types::Request::from_parts(to_parts, b)
}
}
#[cfg(feature = "compat")]
impl<B> From<http_types::Request<B>> for Request<B> {
fn from(from_req: http_types::Request<B>) -> Request<B> {
let (from_parts, body) = from_req.into_parts();
let mut to_req = Request::new(from_parts.method.into(), from_parts.uri.into());
to_req.set_version(from_parts.version.into());
replace(to_req.headers_mut(), from_parts.headers.into());
to_req.set_body(body);
to_req
}
}
/// Constructs a request using a received ResponseHead and optional body
pub fn from_wire<B>(addr: Option<SocketAddr>, incoming: RequestHead, body: B) -> Request<B> {
let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming;

View File

@@ -1,4 +1,9 @@
use std::fmt;
#[cfg(feature = "compat")]
use std::mem::replace;
#[cfg(feature = "compat")]
use http_types;
use header::{Header, Headers};
use http::{MessageHead, ResponseHead, Body};
@@ -142,6 +147,30 @@ impl fmt::Debug for Response {
}
}
#[cfg(feature = "compat")]
impl<B> From<http_types::Response<B>> for Response<B> {
fn from(from_res: http_types::Response<B>) -> Response<B> {
let (from_parts, body) = from_res.into_parts();
let mut to_res = Response::new();
to_res.version = from_parts.version.into();
to_res.set_status(from_parts.status.into());
replace(to_res.headers_mut(), from_parts.headers.into());
to_res.with_body(body)
}
}
#[cfg(feature = "compat")]
impl From<Response> for http_types::Response<Body> {
fn from(mut from_res: Response) -> http_types::Response<Body> {
let (mut to_parts, ()) = http_types::Response::new(()).into_parts();
to_parts.version = from_res.version().into();
to_parts.status = from_res.status().into();
let from_headers = replace(from_res.headers_mut(), Headers::new());
to_parts.headers = from_headers.into();
http_types::Response::from_parts(to_parts, from_res.body())
}
}
/// Constructs a response using a received ResponseHead and optional body
#[inline]
#[cfg(not(feature = "raw_status"))]