From 12cfbafc2882bb30c86bdffa108d213870aaf4ec Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Fri, 6 Jul 2018 22:30:30 -0400 Subject: [PATCH] Add support for getting HTTP version of a response --- src/async_impl/response.rs | 11 ++++++++++- src/lib.rs | 2 +- src/response.rs | 8 +++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index 9627045..14d7111 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -8,7 +8,7 @@ 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::{StatusCode, Version}; use serde::de::DeserializeOwned; use serde_json; use url::Url; @@ -24,6 +24,7 @@ pub struct Response { headers: HeaderMap, url: Url, body: Decoder, + version: Version, } impl Response { @@ -71,6 +72,12 @@ impl Response { &mut self.body } + /// Get the `Version` of this `Response`. + #[inline] + pub fn version(&self) -> &Version { + &self.version + } + /// Try to deserialize the response body as JSON using `serde`. #[inline] @@ -154,6 +161,7 @@ impl fmt::Debug for Json { pub fn new(mut res: ::hyper::Response<::hyper::Body>, url: Url, gzip: bool) -> Response { let status = res.status(); + let version = res.version(); 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); @@ -162,5 +170,6 @@ pub fn new(mut res: ::hyper::Response<::hyper::Body>, url: Url, gzip: bool) -> R headers: headers, url: url, body: decoder, + version: version, } } diff --git a/src/lib.rs b/src/lib.rs index 399f6e5..899cae5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,7 +152,7 @@ extern crate uuid; pub use hyper::header; pub use hyper::Method; -pub use hyper::StatusCode; +pub use hyper::{StatusCode, Version}; pub use url::Url; pub use url::ParseError as UrlError; diff --git a/src/response.rs b/src/response.rs index 79c6c9a..15952e9 100644 --- a/src/response.rs +++ b/src/response.rs @@ -12,7 +12,7 @@ use serde_json; use client::KeepCoreThreadAlive; use hyper::header::HeaderMap; -use {async_impl, StatusCode, Url, wait}; +use {async_impl, StatusCode, Url, Version, wait}; /// A Response to a submitted `Request`. pub struct Response { @@ -122,6 +122,12 @@ impl Response { self.inner.headers() } + /// Get the `Version` of this `Response`. + #[inline] + pub fn version(&self) -> &Version { + self.inner.version() + } + /// Try and deserialize the response body as JSON using `serde`. /// /// # Examples