From e73a97884af8d0c738aaf32cb6142b703620d46f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 17 Nov 2018 09:03:35 -0800 Subject: [PATCH] do not allow to set server response version --- src/client/response.rs | 2 +- src/h1/codec.rs | 7 +++---- src/h1/decoder.rs | 2 +- src/h1/encoder.rs | 2 -- src/message.rs | 5 ++--- src/response.rs | 43 ++++++++++++++++-------------------------- 6 files changed, 23 insertions(+), 38 deletions(-) diff --git a/src/client/response.rs b/src/client/response.rs index 797c88df8..41c18562a 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -57,7 +57,7 @@ impl ClientResponse { /// Read the Request Version. #[inline] pub fn version(&self) -> Version { - self.head().version.clone().unwrap() + self.head().version } /// Get the status from the server. diff --git a/src/h1/codec.rs b/src/h1/codec.rs index a6f39242b..bb8afa710 100644 --- a/src/h1/codec.rs +++ b/src/h1/codec.rs @@ -113,7 +113,6 @@ impl Codec { .unwrap_or_else(|| self.flags.contains(Flags::KEEPALIVE)); // Connection upgrade - let version = msg.version().unwrap_or_else(|| self.version); if msg.upgrade() { self.flags.insert(Flags::UPGRADE); self.flags.remove(Flags::KEEPALIVE); @@ -123,11 +122,11 @@ impl Codec { // keep-alive else if ka { self.flags.insert(Flags::KEEPALIVE); - if version < Version::HTTP_11 { + if self.version < Version::HTTP_11 { msg.headers_mut() .insert(CONNECTION, HeaderValue::from_static("keep-alive")); } - } else if version >= Version::HTTP_11 { + } else if self.version >= Version::HTTP_11 { self.flags.remove(Flags::KEEPALIVE); msg.headers_mut() .insert(CONNECTION, HeaderValue::from_static("close")); @@ -149,7 +148,7 @@ impl Codec { } // status line - helpers::write_status_line(version, msg.status().as_u16(), buffer); + helpers::write_status_line(self.version, msg.status().as_u16(), buffer); buffer.extend_from_slice(reason); // content length diff --git a/src/h1/decoder.rs b/src/h1/decoder.rs index 61cab7ad8..26154ef1e 100644 --- a/src/h1/decoder.rs +++ b/src/h1/decoder.rs @@ -290,7 +290,7 @@ impl MessageTypeDecoder for ClientResponse { }; msg.head.status = status; - msg.head.version = Some(ver); + msg.head.version = ver; Ok(Some((msg, decoder))) } diff --git a/src/h1/encoder.rs b/src/h1/encoder.rs index 5cfdd01b9..421d0b961 100644 --- a/src/h1/encoder.rs +++ b/src/h1/encoder.rs @@ -45,8 +45,6 @@ impl ResponseEncoder { pub fn update(&mut self, resp: &mut Response, head: bool, version: Version) { self.head = head; - - let version = resp.version().unwrap_or_else(|| version); let mut len = 0; let has_body = match resp.body() { diff --git a/src/message.rs b/src/message.rs index f0275093f..3dcb203de 100644 --- a/src/message.rs +++ b/src/message.rs @@ -53,7 +53,7 @@ impl Head for RequestHead { } pub struct ResponseHead { - pub version: Option, + pub version: Version, pub status: StatusCode, pub headers: HeaderMap, pub reason: Option<&'static str>, @@ -63,7 +63,7 @@ pub struct ResponseHead { impl Default for ResponseHead { fn default() -> ResponseHead { ResponseHead { - version: None, + version: Version::default(), status: StatusCode::OK, headers: HeaderMap::with_capacity(16), reason: None, @@ -75,7 +75,6 @@ impl Default for ResponseHead { impl Head for ResponseHead { fn clear(&mut self) { self.reason = None; - self.version = None; self.headers.clear(); self.flags = MessageFlags::empty(); } diff --git a/src/response.rs b/src/response.rs index 0b20f41b8..f96facbae 100644 --- a/src/response.rs +++ b/src/response.rs @@ -12,10 +12,10 @@ use http::{Error as HttpError, HeaderMap, HttpTryFrom, StatusCode, Version}; use serde::Serialize; use serde_json; -use message::{Head, ResponseHead, MessageFlags}; use body::Body; use error::Error; use header::{ContentEncoding, Header, IntoHeaderValue}; +use message::{Head, MessageFlags, ResponseHead}; /// max write buffer size 64k pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536; @@ -105,12 +105,6 @@ impl Response { self.get_ref().error.as_ref() } - /// Get the HTTP version of this response - #[inline] - pub fn version(&self) -> Option { - self.get_ref().head.version - } - /// Get the headers from the response #[inline] pub fn headers(&self) -> &HeaderMap { @@ -127,7 +121,12 @@ impl Response { #[inline] pub fn cookies(&self) -> CookieIter { CookieIter { - iter: self.get_ref().head.headers.get_all(header::SET_COOKIE).iter(), + iter: self + .get_ref() + .head + .headers + .get_all(header::SET_COOKIE) + .iter(), } } @@ -187,7 +186,8 @@ impl Response { reason } else { self.get_ref() - .head.status + .head + .status .canonical_reason() .unwrap_or("") } @@ -347,17 +347,6 @@ impl ResponseBuilder { self } - /// Set HTTP version of this response. - /// - /// By default response's http version depends on request's version. - #[inline] - pub fn version(&mut self, version: Version) -> &mut Self { - if let Some(parts) = parts(&mut self.response, &self.err) { - parts.head.version = Some(version); - } - self - } - /// Set a header. /// /// ```rust,ignore @@ -782,11 +771,15 @@ pub(crate) struct ResponseParts { impl InnerResponse { #[inline] - fn new(status: StatusCode, body: Body, pool: &'static ResponsePool) -> InnerResponse { + fn new( + status: StatusCode, + body: Body, + pool: &'static ResponsePool, + ) -> InnerResponse { InnerResponse { head: ResponseHead { status, - version: None, + version: Version::default(), headers: HeaderMap::with_capacity(16), reason: None, flags: MessageFlags::empty(), @@ -999,11 +992,7 @@ mod tests { #[test] fn test_basic_builder() { - let resp = Response::Ok() - .header("X-TEST", "value") - .version(Version::HTTP_10) - .finish(); - assert_eq!(resp.version(), Some(Version::HTTP_10)); + let resp = Response::Ok().header("X-TEST", "value").finish(); assert_eq!(resp.status(), StatusCode::OK); }