diff --git a/src/client/request.rs b/src/client/request.rs index 42682a30c..a1f61d8d1 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -11,7 +11,7 @@ use serde::Serialize; use body::Body; use error::Error; -use headers::ContentEncoding; +use header::{ContentEncoding, Header, IntoHeaderValue}; use super::pipeline::SendRequest; use super::connector::{Connection, ClientConnector}; @@ -267,6 +267,14 @@ impl ClientRequestBuilder { self } + /// Set HTTP method of this request. + #[inline] + pub fn get_method(&mut self) -> &Method { + let parts = parts(&mut self.request, &self.err) + .expect("cannot reuse request builder"); + &parts.method + } + /// Set HTTP version of this request. /// /// By default requests's http version depends on network stream @@ -278,7 +286,36 @@ impl ClientRequestBuilder { self } - /// Add a header. + /// Set a header. + /// + /// ```rust + /// # extern crate mime; + /// # extern crate actix_web; + /// # use actix_web::*; + /// # use actix_web::httpcodes::*; + /// # + /// use actix_web::header; + /// + /// fn main() { + /// let req = ClientRequest::build() + /// .set(header::Date::now()) + /// .set(header::ContentType(mime::TEXT_HTML) + /// .finish().unwrap(); + /// } + /// ``` + #[doc(hidden)] + pub fn set(&mut self, hdr: H) -> &mut Self + { + if let Some(parts) = parts(&mut self.request, &self.err) { + match hdr.try_into() { + Ok(value) => { parts.headers.insert(H::name(), value); } + Err(e) => self.err = Some(e.into()), + } + } + self + } + + /// Append a header. /// /// Header get appended to existing header. /// To override header use `set_header()` method. @@ -298,12 +335,12 @@ impl ClientRequestBuilder { /// } /// ``` pub fn header(&mut self, key: K, value: V) -> &mut Self - where HeaderName: HttpTryFrom, HeaderValue: HttpTryFrom + where HeaderName: HttpTryFrom, V: IntoHeaderValue { if let Some(parts) = parts(&mut self.request, &self.err) { match HeaderName::try_from(key) { Ok(key) => { - match HeaderValue::try_from(value) { + match value.try_into() { Ok(value) => { parts.headers.append(key, value); } Err(e) => self.err = Some(e.into()), } @@ -314,14 +351,14 @@ impl ClientRequestBuilder { self } - /// Replace a header. + /// Set a header. pub fn set_header(&mut self, key: K, value: V) -> &mut Self - where HeaderName: HttpTryFrom, HeaderValue: HttpTryFrom + where HeaderName: HttpTryFrom, V: IntoHeaderValue { if let Some(parts) = parts(&mut self.request, &self.err) { match HeaderName::try_from(key) { Ok(key) => { - match HeaderValue::try_from(value) { + match value.try_into() { Ok(value) => { parts.headers.insert(key, value); } Err(e) => self.err = Some(e.into()), } diff --git a/src/header/common/date.rs b/src/header/common/date.rs index 33333f446..c2e0c8d7f 100644 --- a/src/header/common/date.rs +++ b/src/header/common/date.rs @@ -1,5 +1,7 @@ +use std::time::SystemTime; use header::{http, HttpDate}; + header! { /// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2) /// @@ -32,3 +34,9 @@ header! { test_header!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]); } } + +impl Date { + pub fn now() -> Date { + Date(SystemTime::now().into()) + } +} diff --git a/src/ws/client.rs b/src/ws/client.rs index c8fdec0ff..42dacc847 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -18,6 +18,7 @@ use actix::prelude::*; use body::{Body, Binary}; use error::UrlParseError; +use header::IntoHeaderValue; use payload::PayloadHelper; use httpmessage::HttpMessage; @@ -193,7 +194,7 @@ impl Client { /// Set request header pub fn header(mut self, key: K, value: V) -> Self - where HeaderName: HttpTryFrom, HeaderValue: HttpTryFrom + where HeaderName: HttpTryFrom, V: IntoHeaderValue { self.request.header(key, value); self