From afb81b6b8faa3e5195b0f8ed6f4005eee3e0fce5 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 21 Mar 2018 19:54:21 -0700 Subject: [PATCH] add convinience ClientRequest::build_from() from HttpRequest --- src/client/request.rs | 22 ++++++++++++++++++++++ src/httpresponse.rs | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/src/client/request.rs b/src/client/request.rs index f446c8845..d7e634736 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -16,6 +16,8 @@ use percent_encoding::{USERINFO_ENCODE_SET, percent_encode}; use body::Body; use error::Error; use header::{ContentEncoding, Header, IntoHeaderValue}; +use httpmessage::HttpMessage; +use httprequest::HttpRequest; use super::pipeline::SendRequest; use super::connector::{Connection, ClientConnector}; @@ -111,6 +113,11 @@ impl ClientRequest { } } + /// Create client request builder + pub fn build_from>(source: T) -> ClientRequestBuilder { + source.into() + } + /// Get the request uri #[inline] pub fn uri(&self) -> &Uri { @@ -645,3 +652,18 @@ impl fmt::Debug for ClientRequestBuilder { } } } + +/// Create `ClientRequestBuilder` from `HttpRequest` +/// +/// It is useful for proxy requests. This implementation +/// copies all request's headers and method. +impl<'a, S: 'static> From<&'a HttpRequest> for ClientRequestBuilder { + fn from(req: &'a HttpRequest) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + for (key, value) in req.headers() { + builder.header(key.clone(), value.clone()); + } + builder.method(req.method().clone()); + builder + } +} diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 7bfaac7be..ab06adfe7 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -71,6 +71,12 @@ impl HttpResponse { } } + /// Create http response builder + #[inline] + pub fn build_from>(source: T) -> HttpResponseBuilder { + source.into() + } + /// Constructs a response #[inline] pub fn new(status: StatusCode, body: Body) -> HttpResponse {