committed by
Sean McArthur
parent
7bd3619ece
commit
c417d6dab8
@@ -2,6 +2,7 @@ use std::fmt;
|
||||
|
||||
use futures::{Stream, Poll, Async};
|
||||
use bytes::Bytes;
|
||||
use hyper::body::Payload;
|
||||
|
||||
/// An asynchronous `Stream`.
|
||||
pub struct Body {
|
||||
@@ -20,6 +21,13 @@ impl Body {
|
||||
Inner::Reusable(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn content_length(&self) -> Option<u64> {
|
||||
match self.inner {
|
||||
Inner::Reusable(ref bytes) => Some(bytes.len() as u64),
|
||||
Inner::Hyper(ref body) => body.content_length(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for Body {
|
||||
|
||||
@@ -4,11 +4,11 @@ use std::time::Duration;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::{Async, Future, Poll};
|
||||
use hyper::client::FutureResponse;
|
||||
use hyper::header::{Headers, Location, Referer, UserAgent, Accept, Encoding,
|
||||
AcceptEncoding, Range, qitem};
|
||||
use hyper::client::ResponseFuture;
|
||||
use header::{HeaderMap, HeaderValue, LOCATION, USER_AGENT, REFERER, ACCEPT,
|
||||
ACCEPT_ENCODING, RANGE};
|
||||
use mime::{self};
|
||||
use native_tls::{TlsConnector, TlsConnectorBuilder};
|
||||
use tokio_core::reactor::Handle;
|
||||
|
||||
|
||||
use super::body;
|
||||
@@ -63,7 +63,7 @@ pub struct ClientBuilder {
|
||||
|
||||
struct Config {
|
||||
gzip: bool,
|
||||
headers: Headers,
|
||||
headers: HeaderMap,
|
||||
hostname_verification: bool,
|
||||
proxies: Vec<Proxy>,
|
||||
redirect_policy: RedirectPolicy,
|
||||
@@ -78,9 +78,9 @@ impl ClientBuilder {
|
||||
pub fn new() -> ClientBuilder {
|
||||
match TlsConnector::builder() {
|
||||
Ok(tls_connector_builder) => {
|
||||
let mut headers = Headers::with_capacity(2);
|
||||
headers.set(UserAgent::new(DEFAULT_USER_AGENT));
|
||||
headers.set(Accept::star());
|
||||
let mut headers: HeaderMap<HeaderValue> = HeaderMap::with_capacity(2);
|
||||
headers.insert(USER_AGENT, HeaderValue::from_static(DEFAULT_USER_AGENT));
|
||||
headers.insert(ACCEPT, HeaderValue::from_str(mime::STAR_STAR.as_ref()).expect("unable to parse mime"));
|
||||
|
||||
ClientBuilder {
|
||||
config: Some(Config {
|
||||
@@ -114,7 +114,7 @@ impl ClientBuilder {
|
||||
///
|
||||
/// This method consumes the internal state of the builder.
|
||||
/// Trying to use this builder again after calling `build` will panic.
|
||||
pub fn build(&mut self, handle: &Handle) -> ::Result<Client> {
|
||||
pub fn build(&mut self) -> ::Result<Client> {
|
||||
if let Some(err) = self.err.take() {
|
||||
return Err(err);
|
||||
}
|
||||
@@ -126,14 +126,13 @@ impl ClientBuilder {
|
||||
|
||||
let proxies = Arc::new(config.proxies);
|
||||
|
||||
let mut connector = Connector::new(config.dns_threads, tls, proxies.clone(), handle);
|
||||
let mut connector = Connector::new(config.dns_threads, tls, proxies.clone());
|
||||
if !config.hostname_verification {
|
||||
connector.danger_disable_hostname_verification();
|
||||
}
|
||||
|
||||
let hyper_client = ::hyper::Client::configure()
|
||||
.connector(connector)
|
||||
.build(handle);
|
||||
let hyper_client = ::hyper::Client::builder()
|
||||
.build(connector);
|
||||
|
||||
Ok(Client {
|
||||
inner: Arc::new(ClientRef {
|
||||
@@ -200,9 +199,11 @@ impl ClientBuilder {
|
||||
|
||||
/// Sets the default headers for every request.
|
||||
#[inline]
|
||||
pub fn default_headers(&mut self, headers: Headers) -> &mut ClientBuilder {
|
||||
pub fn default_headers(&mut self, headers: HeaderMap) -> &mut ClientBuilder {
|
||||
if let Some(config) = config_mut(&mut self.config, &self.err) {
|
||||
config.headers.extend(headers.iter());
|
||||
for (key, value) in headers.iter() {
|
||||
config.headers.insert(key, value.clone());
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -287,9 +288,9 @@ impl Client {
|
||||
/// initialized. Use `Client::builder()` if you wish to handle the failure
|
||||
/// as an `Error` instead of panicking.
|
||||
#[inline]
|
||||
pub fn new(handle: &Handle) -> Client {
|
||||
pub fn new() -> Client {
|
||||
ClientBuilder::new()
|
||||
.build(handle)
|
||||
.build()
|
||||
.expect("TLS failed to initialize")
|
||||
}
|
||||
|
||||
@@ -305,7 +306,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Get, url)
|
||||
self.request(Method::GET, url)
|
||||
}
|
||||
|
||||
/// Convenience method to make a `POST` request to a URL.
|
||||
@@ -314,7 +315,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Post, url)
|
||||
self.request(Method::POST, url)
|
||||
}
|
||||
|
||||
/// Convenience method to make a `PUT` request to a URL.
|
||||
@@ -323,7 +324,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Put, url)
|
||||
self.request(Method::PUT, url)
|
||||
}
|
||||
|
||||
/// Convenience method to make a `PATCH` request to a URL.
|
||||
@@ -332,7 +333,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Patch, url)
|
||||
self.request(Method::PATCH, url)
|
||||
}
|
||||
|
||||
/// Convenience method to make a `DELETE` request to a URL.
|
||||
@@ -341,7 +342,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Delete, url)
|
||||
self.request(Method::DELETE, url)
|
||||
}
|
||||
|
||||
/// Convenience method to make a `HEAD` request to a URL.
|
||||
@@ -350,7 +351,7 @@ impl Client {
|
||||
///
|
||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||
pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder {
|
||||
self.request(Method::Head, url)
|
||||
self.request(Method::HEAD, url)
|
||||
}
|
||||
|
||||
/// Start building a `Request` with the `Method` and `Url`.
|
||||
@@ -395,28 +396,35 @@ impl Client {
|
||||
) = request::pieces(req);
|
||||
|
||||
let mut headers = self.inner.headers.clone(); // default headers
|
||||
headers.extend(user_headers.iter());
|
||||
for (key, value) in user_headers.iter() {
|
||||
headers.insert(key, value.clone());
|
||||
}
|
||||
|
||||
if self.inner.gzip &&
|
||||
!headers.has::<AcceptEncoding>() &&
|
||||
!headers.has::<Range>() {
|
||||
headers.set(AcceptEncoding(vec![qitem(Encoding::Gzip)]));
|
||||
!headers.contains_key(ACCEPT_ENCODING) &&
|
||||
!headers.contains_key(RANGE) {
|
||||
headers.insert(ACCEPT_ENCODING, HeaderValue::from_static("gzip"));
|
||||
}
|
||||
|
||||
let uri = to_uri(&url);
|
||||
let mut req = ::hyper::Request::new(method.clone(), uri.clone());
|
||||
*req.headers_mut() = headers.clone();
|
||||
let body = body.map(|body| {
|
||||
let (reusable, body) = body::into_hyper(body);
|
||||
req.set_body(body);
|
||||
reusable
|
||||
});
|
||||
|
||||
if proxy::is_proxied(&self.inner.proxies, &url) {
|
||||
if uri.scheme() == Some("http") {
|
||||
req.set_proxy(true);
|
||||
let (reusable, body) = match body {
|
||||
Some(body) => {
|
||||
let (reusable, body) = body::into_hyper(body);
|
||||
(Some(reusable), body)
|
||||
},
|
||||
None => {
|
||||
(None, ::hyper::Body::empty())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut req = ::hyper::Request::builder()
|
||||
.method(method.clone())
|
||||
.uri(uri.clone())
|
||||
.body(body)
|
||||
.expect("valid request parts");
|
||||
|
||||
*req.headers_mut() = headers.clone();
|
||||
|
||||
let in_flight = self.inner.hyper.request(req);
|
||||
|
||||
@@ -425,7 +433,7 @@ impl Client {
|
||||
method: method,
|
||||
url: url,
|
||||
headers: headers,
|
||||
body: body,
|
||||
body: reusable,
|
||||
|
||||
urls: Vec::new(),
|
||||
|
||||
@@ -456,7 +464,7 @@ impl fmt::Debug for ClientBuilder {
|
||||
|
||||
struct ClientRef {
|
||||
gzip: bool,
|
||||
headers: Headers,
|
||||
headers: HeaderMap,
|
||||
hyper: HyperClient,
|
||||
proxies: Arc<Vec<Proxy>>,
|
||||
redirect_policy: RedirectPolicy,
|
||||
@@ -475,14 +483,14 @@ enum PendingInner {
|
||||
pub struct PendingRequest {
|
||||
method: Method,
|
||||
url: Url,
|
||||
headers: Headers,
|
||||
headers: HeaderMap,
|
||||
body: Option<Option<Bytes>>,
|
||||
|
||||
urls: Vec<Url>,
|
||||
|
||||
client: Arc<ClientRef>,
|
||||
|
||||
in_flight: FutureResponse,
|
||||
in_flight: ResponseFuture,
|
||||
}
|
||||
|
||||
|
||||
@@ -509,20 +517,20 @@ impl Future for PendingRequest {
|
||||
Async::NotReady => return Ok(Async::NotReady),
|
||||
};
|
||||
let should_redirect = match res.status() {
|
||||
StatusCode::MovedPermanently |
|
||||
StatusCode::Found |
|
||||
StatusCode::SeeOther => {
|
||||
StatusCode::MOVED_PERMANENTLY |
|
||||
StatusCode::FOUND |
|
||||
StatusCode::SEE_OTHER => {
|
||||
self.body = None;
|
||||
match self.method {
|
||||
Method::Get | Method::Head => {},
|
||||
Method::GET | Method::HEAD => {},
|
||||
_ => {
|
||||
self.method = Method::Get;
|
||||
self.method = Method::GET;
|
||||
}
|
||||
}
|
||||
true
|
||||
},
|
||||
StatusCode::TemporaryRedirect |
|
||||
StatusCode::PermanentRedirect => match self.body {
|
||||
StatusCode::TEMPORARY_REDIRECT |
|
||||
StatusCode::PERMANENT_REDIRECT => match self.body {
|
||||
Some(Some(_)) | None => true,
|
||||
Some(None) => false,
|
||||
},
|
||||
@@ -530,12 +538,12 @@ impl Future for PendingRequest {
|
||||
};
|
||||
if should_redirect {
|
||||
let loc = res.headers()
|
||||
.get::<Location>()
|
||||
.map(|loc| self.url.join(loc));
|
||||
.get(LOCATION)
|
||||
.map(|loc| self.url.join(loc.to_str().expect("")));
|
||||
if let Some(Ok(loc)) = loc {
|
||||
if self.client.referer {
|
||||
if let Some(referer) = make_referer(&loc, &self.url) {
|
||||
self.headers.set(referer);
|
||||
self.headers.insert(REFERER, referer);
|
||||
}
|
||||
}
|
||||
self.urls.push(self.url.clone());
|
||||
@@ -553,19 +561,17 @@ impl Future for PendingRequest {
|
||||
remove_sensitive_headers(&mut self.headers, &self.url, &self.urls);
|
||||
debug!("redirecting to {:?} '{}'", self.method, self.url);
|
||||
let uri = to_uri(&self.url);
|
||||
let mut req = ::hyper::Request::new(
|
||||
self.method.clone(),
|
||||
uri.clone()
|
||||
);
|
||||
let body = match self.body {
|
||||
Some(Some(ref body)) => ::hyper::Body::from(body.clone()),
|
||||
_ => ::hyper::Body::empty(),
|
||||
};
|
||||
let mut req = ::hyper::Request::builder()
|
||||
.method(self.method.clone())
|
||||
.uri(uri.clone())
|
||||
.body(body)
|
||||
.expect("valid request parts");
|
||||
|
||||
*req.headers_mut() = self.headers.clone();
|
||||
if let Some(Some(ref body)) = self.body {
|
||||
req.set_body(body.clone());
|
||||
}
|
||||
if proxy::is_proxied(&self.client.proxies, &self.url) {
|
||||
if uri.scheme() == Some("http") {
|
||||
req.set_proxy(true);
|
||||
}
|
||||
}
|
||||
self.in_flight = self.client.hyper.request(req);
|
||||
continue;
|
||||
},
|
||||
@@ -607,7 +613,7 @@ impl fmt::Debug for Pending {
|
||||
}
|
||||
}
|
||||
|
||||
fn make_referer(next: &Url, previous: &Url) -> Option<Referer> {
|
||||
fn make_referer(next: &Url, previous: &Url) -> Option<HeaderValue> {
|
||||
if next.scheme() == "http" && previous.scheme() == "https" {
|
||||
return None;
|
||||
}
|
||||
@@ -616,7 +622,7 @@ fn make_referer(next: &Url, previous: &Url) -> Option<Referer> {
|
||||
let _ = referer.set_username("");
|
||||
let _ = referer.set_password(None);
|
||||
referer.set_fragment(None);
|
||||
Some(Referer::new(referer.into_string()))
|
||||
referer.as_str().parse().ok()
|
||||
}
|
||||
|
||||
// pub(crate)
|
||||
|
||||
@@ -33,12 +33,12 @@ use tokio_io::AsyncRead;
|
||||
use tokio_io::io as async_io;
|
||||
use futures::{Async, Future, Poll, Stream};
|
||||
use futures::stream::Concat2;
|
||||
use hyper::StatusCode;
|
||||
use hyper::{HeaderMap, StatusCode};
|
||||
use hyper::header::{CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING, HeaderValue};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json;
|
||||
use url::Url;
|
||||
|
||||
use header::{Headers, ContentEncoding, ContentLength, Encoding, TransferEncoding};
|
||||
use super::{body, Body, Chunk};
|
||||
use error;
|
||||
|
||||
@@ -111,6 +111,13 @@ impl Decoder {
|
||||
inner: Inner::Pending(Pending::Gzip(ReadableChunks::new(body)))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn content_length(&self) -> Option<u64> {
|
||||
match self.inner {
|
||||
Inner::PlainText(ref body) => body.content_length(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for Decoder {
|
||||
@@ -407,31 +414,33 @@ impl<S> ReadableChunks<S>
|
||||
/// how to decode the content body of the request.
|
||||
///
|
||||
/// Uses the correct variant by inspecting the Content-Encoding header.
|
||||
pub fn detect(headers: &mut Headers, body: Body, check_gzip: bool) -> Decoder {
|
||||
pub fn detect(headers: &mut HeaderMap, body: Body, check_gzip: bool) -> Decoder {
|
||||
if !check_gzip {
|
||||
return Decoder::plain_text(body);
|
||||
}
|
||||
let content_encoding_gzip: bool;
|
||||
let mut is_gzip = {
|
||||
content_encoding_gzip = headers
|
||||
.get::<ContentEncoding>()
|
||||
.map_or(false, |encs| encs.contains(&Encoding::Gzip));
|
||||
.get_all(CONTENT_ENCODING)
|
||||
.iter()
|
||||
.fold(false, |acc, enc| acc || enc == HeaderValue::from_static("gzip"));
|
||||
content_encoding_gzip ||
|
||||
headers
|
||||
.get::<TransferEncoding>()
|
||||
.map_or(false, |encs| encs.contains(&Encoding::Gzip))
|
||||
.get_all(TRANSFER_ENCODING)
|
||||
.iter()
|
||||
.fold(false, |acc, enc| acc || enc == HeaderValue::from_static("gzip"))
|
||||
};
|
||||
if is_gzip {
|
||||
if let Some(content_length) = headers.get::<ContentLength>() {
|
||||
if content_length.0 == 0 {
|
||||
if let Some(content_length) = headers.get(CONTENT_LENGTH) {
|
||||
if content_length == "0" {
|
||||
warn!("GZipped response with content-length of 0");
|
||||
is_gzip = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if content_encoding_gzip {
|
||||
headers.remove::<ContentEncoding>();
|
||||
headers.remove::<ContentLength>();
|
||||
headers.remove(CONTENT_ENCODING);
|
||||
headers.remove(CONTENT_LENGTH);
|
||||
}
|
||||
if is_gzip {
|
||||
Decoder::gzip(body)
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
use std::fmt;
|
||||
|
||||
use base64::{encode};
|
||||
use mime::{self};
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
|
||||
use super::body::{self, Body};
|
||||
use super::client::{Client, Pending, pending_err};
|
||||
use header::{ContentType, Headers};
|
||||
use header::{CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue};
|
||||
use http::HttpTryFrom;
|
||||
use {Method, Url};
|
||||
|
||||
/// A request which can be executed with `Client::execute()`.
|
||||
pub struct Request {
|
||||
method: Method,
|
||||
url: Url,
|
||||
headers: Headers,
|
||||
headers: HeaderMap,
|
||||
body: Option<Body>,
|
||||
}
|
||||
|
||||
@@ -31,7 +34,7 @@ impl Request {
|
||||
Request {
|
||||
method,
|
||||
url,
|
||||
headers: Headers::new(),
|
||||
headers: HeaderMap::new(),
|
||||
body: None,
|
||||
}
|
||||
}
|
||||
@@ -62,13 +65,13 @@ impl Request {
|
||||
|
||||
/// Get the headers.
|
||||
#[inline]
|
||||
pub fn headers(&self) -> &Headers {
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.headers
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the headers.
|
||||
#[inline]
|
||||
pub fn headers_mut(&mut self) -> &mut Headers {
|
||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.headers
|
||||
}
|
||||
|
||||
@@ -87,21 +90,32 @@ impl Request {
|
||||
|
||||
impl RequestBuilder {
|
||||
/// Add a `Header` to this Request.
|
||||
pub fn header<H>(&mut self, header: H) -> &mut RequestBuilder
|
||||
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut RequestBuilder
|
||||
where
|
||||
H: ::header::Header,
|
||||
HeaderName: HttpTryFrom<K>,
|
||||
HeaderValue: HttpTryFrom<V>,
|
||||
{
|
||||
if let Some(req) = request_mut(&mut self.request, &self.err) {
|
||||
req.headers_mut().set(header);
|
||||
match <HeaderName as HttpTryFrom<K>>::try_from(key) {
|
||||
Ok(key) => {
|
||||
match <HeaderValue as HttpTryFrom<V>>::try_from(value) {
|
||||
Ok(value) => { req.headers_mut().append(key, value); }
|
||||
Err(e) => self.err = Some(::error::from(e.into())),
|
||||
}
|
||||
},
|
||||
Err(e) => self.err = Some(::error::from(e.into())),
|
||||
};
|
||||
}
|
||||
self
|
||||
}
|
||||
/// Add a set of Headers to the existing ones on this Request.
|
||||
///
|
||||
/// The headers will be merged in to any already set.
|
||||
pub fn headers(&mut self, headers: ::header::Headers) -> &mut RequestBuilder {
|
||||
pub fn headers(&mut self, headers: ::header::HeaderMap) -> &mut RequestBuilder {
|
||||
if let Some(req) = request_mut(&mut self.request, &self.err) {
|
||||
req.headers_mut().extend(headers.iter());
|
||||
for (key, value) in headers.iter() {
|
||||
req.headers_mut().insert(key, value.clone());
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -112,10 +126,10 @@ impl RequestBuilder {
|
||||
U: Into<String>,
|
||||
P: Into<String>,
|
||||
{
|
||||
self.header(::header::Authorization(::header::Basic {
|
||||
username: username.into(),
|
||||
password: password.map(|p| p.into()),
|
||||
}))
|
||||
let username = username.into();
|
||||
let password = password.map(|p| p.into()).unwrap_or(String::new());
|
||||
let header_value = format!("basic {}:{}", username, encode(&password));
|
||||
self.header(::header::AUTHORIZATION, HeaderValue::from_str(header_value.as_str()).expect(""))
|
||||
}
|
||||
|
||||
/// Set the request body.
|
||||
@@ -162,7 +176,7 @@ impl RequestBuilder {
|
||||
if let Some(req) = request_mut(&mut self.request, &self.err) {
|
||||
match serde_urlencoded::to_string(form) {
|
||||
Ok(body) => {
|
||||
req.headers_mut().set(ContentType::form_url_encoded());
|
||||
req.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_str(mime::APPLICATION_WWW_FORM_URLENCODED.as_ref()).expect(""));
|
||||
*req.body_mut() = Some(body.into());
|
||||
},
|
||||
Err(err) => self.err = Some(::error::from(err)),
|
||||
@@ -181,7 +195,7 @@ impl RequestBuilder {
|
||||
if let Some(req) = request_mut(&mut self.request, &self.err) {
|
||||
match serde_json::to_vec(json) {
|
||||
Ok(body) => {
|
||||
req.headers_mut().set(ContentType::json());
|
||||
req.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_str(mime::APPLICATION_JSON.as_ref()).expect(""));
|
||||
*req.body_mut() = Some(body.into());
|
||||
},
|
||||
Err(err) => self.err = Some(::error::from(err)),
|
||||
@@ -274,7 +288,7 @@ pub fn builder(client: Client, req: ::Result<Request>) -> RequestBuilder {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pieces(req: Request) -> (Method, Url, Headers, Option<Body>) {
|
||||
pub fn pieces(req: Request) -> (Method, Url, HeaderMap, Option<Body>) {
|
||||
(req.method, req.url, req.headers, req.body)
|
||||
}
|
||||
|
||||
@@ -282,12 +296,10 @@ pub fn pieces(req: Request) -> (Method, Url, Headers, Option<Body>) {
|
||||
mod tests {
|
||||
use super::Client;
|
||||
use std::collections::BTreeMap;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
#[test]
|
||||
fn add_query_append() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let client = Client::new(&core.handle());
|
||||
let client = Client::new();
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.get(some_url);
|
||||
|
||||
@@ -300,8 +312,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn add_query_append_same() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let client = Client::new(&core.handle());
|
||||
let client = Client::new();
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.get(some_url);
|
||||
|
||||
@@ -319,8 +330,7 @@ mod tests {
|
||||
qux: i32,
|
||||
}
|
||||
|
||||
let mut core = Core::new().unwrap();
|
||||
let client = Client::new(&core.handle());
|
||||
let client = Client::new();
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.get(some_url);
|
||||
|
||||
@@ -338,8 +348,7 @@ mod tests {
|
||||
params.insert("foo", "bar");
|
||||
params.insert("qux", "three");
|
||||
|
||||
let mut core = Core::new().unwrap();
|
||||
let client = Client::new(&core.handle());
|
||||
let client = Client::new();
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.get(some_url);
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ use serde::de::DeserializeOwned;
|
||||
use serde_json;
|
||||
use url::Url;
|
||||
|
||||
use header::{Headers, ContentEncoding, ContentLength, Encoding, TransferEncoding};
|
||||
use hyper::header::{HeaderMap, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
||||
use super::{decoder, body, Body, Chunk, Decoder};
|
||||
use error;
|
||||
|
||||
@@ -21,7 +21,7 @@ use error;
|
||||
/// A Response to a submitted `Request`.
|
||||
pub struct Response {
|
||||
status: StatusCode,
|
||||
headers: Headers,
|
||||
headers: HeaderMap,
|
||||
url: Url,
|
||||
body: Decoder,
|
||||
}
|
||||
@@ -41,13 +41,13 @@ impl Response {
|
||||
|
||||
/// Get the `Headers` of this `Response`.
|
||||
#[inline]
|
||||
pub fn headers(&self) -> &Headers {
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.headers
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the `Headers` of this `Response`.
|
||||
#[inline]
|
||||
pub fn headers_mut(&mut self) -> &mut Headers {
|
||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.headers
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ impl Response {
|
||||
/// // it could be any status between 400...599
|
||||
/// assert_eq!(
|
||||
/// err.status(),
|
||||
/// Some(reqwest::StatusCode::BadRequest)
|
||||
/// Some(reqwest::StatusCode::BAD_REQUEST)
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
@@ -152,10 +152,10 @@ impl<T> fmt::Debug for Json<T> {
|
||||
|
||||
// pub(crate)
|
||||
|
||||
pub fn new(mut res: ::hyper::client::Response, url: Url, gzip: bool) -> Response {
|
||||
pub fn new(mut res: ::hyper::Response<::hyper::Body>, url: Url, gzip: bool) -> Response {
|
||||
let status = res.status();
|
||||
let mut headers = mem::replace(res.headers_mut(), Headers::new());
|
||||
let decoder = decoder::detect(&mut headers, body::wrap(res.body()), gzip);
|
||||
let mut headers = mem::replace(res.headers_mut(), HeaderMap::new());
|
||||
let decoder = decoder::detect(&mut headers, body::wrap(res.into_body()), gzip);
|
||||
debug!("Response: '{}' for {}", status, url);
|
||||
Response {
|
||||
status: status,
|
||||
|
||||
Reference in New Issue
Block a user