1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +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.
#[inline]
pub fn version(&self) -> Version {
self.head().version.clone().unwrap()
self.head().version
}
/// Get the status from the server.

View File

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

View File

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

View File

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

View File

@ -53,7 +53,7 @@ impl Head for RequestHead {
}
pub struct ResponseHead {
pub version: Option<Version>,
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();
}

View File

@ -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<Version> {
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("<unknown status code>")
}
@ -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);
}