1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

use IntoHeaderValue and Header for client request

This commit is contained in:
Nikolay Kim 2018-03-06 15:18:04 -08:00
parent ceb97cd6b9
commit 14a511bdad
3 changed files with 54 additions and 8 deletions

View File

@ -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<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.
/// To override header use `set_header()` method.
@ -298,12 +335,12 @@ impl ClientRequestBuilder {
/// }
/// ```
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) {
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<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) {
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()),
}

View File

@ -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())
}
}

View File

@ -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<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