1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00

Merge branch 'master' into feature/allow_connection_timeout_to_be_set

This commit is contained in:
Nikolay Kim 2018-03-06 15:18:25 -08:00 committed by GitHub
commit 6a3c5c4ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 8 deletions

View File

@ -12,7 +12,7 @@ use serde::Serialize;
use body::Body; use body::Body;
use error::Error; use error::Error;
use headers::ContentEncoding; use header::{ContentEncoding, Header, IntoHeaderValue};
use super::pipeline::SendRequest; use super::pipeline::SendRequest;
use super::connector::{Connection, ClientConnector}; use super::connector::{Connection, ClientConnector};
@ -275,6 +275,14 @@ impl ClientRequestBuilder {
self 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. /// Set HTTP version of this request.
/// ///
/// By default requests's http version depends on network stream /// By default requests's http version depends on network stream
@ -286,7 +294,36 @@ impl ClientRequestBuilder {
self 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<H: Header>(&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. /// Header get appended to existing header.
/// To override header use `set_header()` method. /// To override header use `set_header()` method.
@ -306,12 +343,12 @@ impl ClientRequestBuilder {
/// } /// }
/// ``` /// ```
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
where HeaderName: HttpTryFrom<K>, HeaderValue: HttpTryFrom<V> where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue
{ {
if let Some(parts) = parts(&mut self.request, &self.err) { if let Some(parts) = parts(&mut self.request, &self.err) {
match HeaderName::try_from(key) { match HeaderName::try_from(key) {
Ok(key) => { Ok(key) => {
match HeaderValue::try_from(value) { match value.try_into() {
Ok(value) => { parts.headers.append(key, value); } Ok(value) => { parts.headers.append(key, value); }
Err(e) => self.err = Some(e.into()), Err(e) => self.err = Some(e.into()),
} }
@ -322,14 +359,14 @@ impl ClientRequestBuilder {
self self
} }
/// Replace a header. /// Set a header.
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
where HeaderName: HttpTryFrom<K>, HeaderValue: HttpTryFrom<V> where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue
{ {
if let Some(parts) = parts(&mut self.request, &self.err) { if let Some(parts) = parts(&mut self.request, &self.err) {
match HeaderName::try_from(key) { match HeaderName::try_from(key) {
Ok(key) => { Ok(key) => {
match HeaderValue::try_from(value) { match value.try_into() {
Ok(value) => { parts.headers.insert(key, value); } Ok(value) => { parts.headers.insert(key, value); }
Err(e) => self.err = Some(e.into()), Err(e) => self.err = Some(e.into()),
} }

View File

@ -1,5 +1,7 @@
use std::time::SystemTime;
use header::{http, HttpDate}; use header::{http, HttpDate};
header! { header! {
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2) /// `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"]); test_header!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
} }
} }
impl Date {
pub fn now() -> Date {
Date(SystemTime::now().into())
}
}

View File

@ -18,6 +18,7 @@ use actix::prelude::*;
use body::{Body, Binary}; use body::{Body, Binary};
use error::UrlParseError; use error::UrlParseError;
use header::IntoHeaderValue;
use payload::PayloadHelper; use payload::PayloadHelper;
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
@ -193,7 +194,7 @@ impl Client {
/// Set request header /// Set request header
pub fn header<K, V>(mut self, key: K, value: V) -> Self pub fn header<K, V>(mut self, key: K, value: V) -> Self
where HeaderName: HttpTryFrom<K>, HeaderValue: HttpTryFrom<V> where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue
{ {
self.request.header(key, value); self.request.header(key, value);
self self