1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00

do not allow to set server response version

This commit is contained in:
Nikolay Kim 2018-11-17 09:03:35 -08:00
parent f0bd4d868e
commit e73a97884a
6 changed files with 23 additions and 38 deletions

View File

@ -57,7 +57,7 @@ impl ClientResponse {
/// Read the Request Version. /// Read the Request Version.
#[inline] #[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.head().version.clone().unwrap() self.head().version
} }
/// Get the status from the server. /// Get the status from the server.

View File

@ -113,7 +113,6 @@ impl Codec {
.unwrap_or_else(|| self.flags.contains(Flags::KEEPALIVE)); .unwrap_or_else(|| self.flags.contains(Flags::KEEPALIVE));
// Connection upgrade // Connection upgrade
let version = msg.version().unwrap_or_else(|| self.version);
if msg.upgrade() { if msg.upgrade() {
self.flags.insert(Flags::UPGRADE); self.flags.insert(Flags::UPGRADE);
self.flags.remove(Flags::KEEPALIVE); self.flags.remove(Flags::KEEPALIVE);
@ -123,11 +122,11 @@ impl Codec {
// keep-alive // keep-alive
else if ka { else if ka {
self.flags.insert(Flags::KEEPALIVE); self.flags.insert(Flags::KEEPALIVE);
if version < Version::HTTP_11 { if self.version < Version::HTTP_11 {
msg.headers_mut() msg.headers_mut()
.insert(CONNECTION, HeaderValue::from_static("keep-alive")); .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); self.flags.remove(Flags::KEEPALIVE);
msg.headers_mut() msg.headers_mut()
.insert(CONNECTION, HeaderValue::from_static("close")); .insert(CONNECTION, HeaderValue::from_static("close"));
@ -149,7 +148,7 @@ impl Codec {
} }
// status line // 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); buffer.extend_from_slice(reason);
// content length // content length

View File

@ -290,7 +290,7 @@ impl MessageTypeDecoder for ClientResponse {
}; };
msg.head.status = status; msg.head.status = status;
msg.head.version = Some(ver); msg.head.version = ver;
Ok(Some((msg, decoder))) Ok(Some((msg, decoder)))
} }

View File

@ -45,8 +45,6 @@ impl ResponseEncoder {
pub fn update(&mut self, resp: &mut Response, head: bool, version: Version) { pub fn update(&mut self, resp: &mut Response, head: bool, version: Version) {
self.head = head; self.head = head;
let version = resp.version().unwrap_or_else(|| version);
let mut len = 0; let mut len = 0;
let has_body = match resp.body() { let has_body = match resp.body() {

View File

@ -53,7 +53,7 @@ impl Head for RequestHead {
} }
pub struct ResponseHead { pub struct ResponseHead {
pub version: Option<Version>, pub version: Version,
pub status: StatusCode, pub status: StatusCode,
pub headers: HeaderMap, pub headers: HeaderMap,
pub reason: Option<&'static str>, pub reason: Option<&'static str>,
@ -63,7 +63,7 @@ pub struct ResponseHead {
impl Default for ResponseHead { impl Default for ResponseHead {
fn default() -> ResponseHead { fn default() -> ResponseHead {
ResponseHead { ResponseHead {
version: None, version: Version::default(),
status: StatusCode::OK, status: StatusCode::OK,
headers: HeaderMap::with_capacity(16), headers: HeaderMap::with_capacity(16),
reason: None, reason: None,
@ -75,7 +75,6 @@ impl Default for ResponseHead {
impl Head for ResponseHead { impl Head for ResponseHead {
fn clear(&mut self) { fn clear(&mut self) {
self.reason = None; self.reason = None;
self.version = None;
self.headers.clear(); self.headers.clear();
self.flags = MessageFlags::empty(); self.flags = MessageFlags::empty();
} }

View File

@ -12,10 +12,10 @@ use http::{Error as HttpError, HeaderMap, HttpTryFrom, StatusCode, Version};
use serde::Serialize; use serde::Serialize;
use serde_json; use serde_json;
use message::{Head, ResponseHead, MessageFlags};
use body::Body; use body::Body;
use error::Error; use error::Error;
use header::{ContentEncoding, Header, IntoHeaderValue}; use header::{ContentEncoding, Header, IntoHeaderValue};
use message::{Head, MessageFlags, ResponseHead};
/// max write buffer size 64k /// max write buffer size 64k
pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536; pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536;
@ -105,12 +105,6 @@ impl Response {
self.get_ref().error.as_ref() self.get_ref().error.as_ref()
} }
/// Get the HTTP version of this response
#[inline]
pub fn version(&self) -> Option<Version> {
self.get_ref().head.version
}
/// Get the headers from the response /// Get the headers from the response
#[inline] #[inline]
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
@ -127,7 +121,12 @@ impl Response {
#[inline] #[inline]
pub fn cookies(&self) -> CookieIter { pub fn cookies(&self) -> CookieIter {
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 reason
} else { } else {
self.get_ref() self.get_ref()
.head.status .head
.status
.canonical_reason() .canonical_reason()
.unwrap_or("<unknown status code>") .unwrap_or("<unknown status code>")
} }
@ -347,17 +347,6 @@ impl ResponseBuilder {
self 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. /// Set a header.
/// ///
/// ```rust,ignore /// ```rust,ignore
@ -782,11 +771,15 @@ pub(crate) struct ResponseParts {
impl InnerResponse { impl InnerResponse {
#[inline] #[inline]
fn new(status: StatusCode, body: Body, pool: &'static ResponsePool) -> InnerResponse { fn new(
status: StatusCode,
body: Body,
pool: &'static ResponsePool,
) -> InnerResponse {
InnerResponse { InnerResponse {
head: ResponseHead { head: ResponseHead {
status, status,
version: None, version: Version::default(),
headers: HeaderMap::with_capacity(16), headers: HeaderMap::with_capacity(16),
reason: None, reason: None,
flags: MessageFlags::empty(), flags: MessageFlags::empty(),
@ -999,11 +992,7 @@ mod tests {
#[test] #[test]
fn test_basic_builder() { fn test_basic_builder() {
let resp = Response::Ok() let resp = Response::Ok().header("X-TEST", "value").finish();
.header("X-TEST", "value")
.version(Version::HTTP_10)
.finish();
assert_eq!(resp.version(), Some(Version::HTTP_10));
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
} }