1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 08:31:09 +00:00

add convinience ClientRequest::build_from() from HttpRequest

This commit is contained in:
Nikolay Kim 2018-03-21 19:54:21 -07:00
parent 4866a26578
commit afb81b6b8f
2 changed files with 28 additions and 0 deletions

View file

@ -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<T: Into<ClientRequestBuilder>>(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<S>> for ClientRequestBuilder {
fn from(req: &'a HttpRequest<S>) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
for (key, value) in req.headers() {
builder.header(key.clone(), value.clone());
}
builder.method(req.method().clone());
builder
}
}

View file

@ -71,6 +71,12 @@ impl HttpResponse {
}
}
/// Create http response builder
#[inline]
pub fn build_from<T: Into<HttpResponseBuilder>>(source: T) -> HttpResponseBuilder {
source.into()
}
/// Constructs a response
#[inline]
pub fn new(status: StatusCode, body: Body) -> HttpResponse {