1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-18 01:43:58 +01:00

use const header values where possible

This commit is contained in:
Rob Ede 2021-12-27 16:15:20 +00:00
parent 554ae7a868
commit 2308f8afa4
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 26 additions and 19 deletions

View File

@ -288,9 +288,11 @@ fn prepare_response(
let _ = match size { let _ = match size {
BodySize::None | BodySize::Stream => None, BodySize::None | BodySize::Stream => None,
BodySize::Sized(0) => res BodySize::Sized(0) => {
.headers_mut() #[allow(clippy::declare_interior_mutable_const)]
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")), const HV_ZERO: HeaderValue = HeaderValue::from_static("0");
res.headers_mut().insert(CONTENT_LENGTH, HV_ZERO)
}
BodySize::Sized(len) => { BodySize::Sized(len) => {
let mut buf = itoa::Buffer::new(); let mut buf = itoa::Buffer::new();

View File

@ -45,13 +45,13 @@ pub enum ContentEncoding {
impl ContentEncoding { impl ContentEncoding {
/// Is the content compressed? /// Is the content compressed?
#[inline] #[inline]
pub fn is_compression(self) -> bool { pub const fn is_compression(self) -> bool {
matches!(self, ContentEncoding::Identity | ContentEncoding::Auto) matches!(self, ContentEncoding::Identity | ContentEncoding::Auto)
} }
/// Convert content encoding to string. /// Convert content encoding to string.
#[inline] #[inline]
pub fn as_str(self) -> &'static str { pub const fn as_str(self) -> &'static str {
match self { match self {
ContentEncoding::Br => "br", ContentEncoding::Br => "br",
ContentEncoding::Gzip => "gzip", ContentEncoding::Gzip => "gzip",

View File

@ -99,8 +99,9 @@ impl From<HandshakeError> for Response<BoxBody> {
match err { match err {
HandshakeError::GetMethodRequired => { HandshakeError::GetMethodRequired => {
let mut res = Response::new(StatusCode::METHOD_NOT_ALLOWED); let mut res = Response::new(StatusCode::METHOD_NOT_ALLOWED);
res.headers_mut() #[allow(clippy::declare_interior_mutable_const)]
.insert(header::ALLOW, HeaderValue::from_static("GET")); const HV_GET: HeaderValue = HeaderValue::from_static("GET");
res.headers_mut().insert(header::ALLOW, HV_GET);
res res
} }

View File

@ -52,9 +52,11 @@ where
let _ = match length { let _ = match length {
BodySize::None => None, BodySize::None => None,
BodySize::Sized(0) => req BodySize::Sized(0) => {
.headers_mut() #[allow(clippy::declare_interior_mutable_const)]
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")), const HV_ZERO: HeaderValue = HeaderValue::from_static("0");
req.headers_mut().insert(CONTENT_LENGTH, HV_ZERO)
}
BodySize::Sized(len) => { BodySize::Sized(len) => {
let mut buf = itoa::Buffer::new(); let mut buf = itoa::Buffer::new();

View File

@ -300,13 +300,16 @@ impl WebsocketsRequest {
} }
self.head.set_connection_type(ConnectionType::Upgrade); self.head.set_connection_type(ConnectionType::Upgrade);
#[allow(clippy::declare_interior_mutable_const)]
const HV_WEBSOCKET: HeaderValue = HeaderValue::from_static("websocket");
self.head.headers.insert(header::UPGRADE, HV_WEBSOCKET);
#[allow(clippy::declare_interior_mutable_const)]
const HV_THIRTEEN: HeaderValue = HeaderValue::from_static("13");
self.head self.head
.headers .headers
.insert(header::UPGRADE, HeaderValue::from_static("websocket")); .insert(header::SEC_WEBSOCKET_VERSION, HV_THIRTEEN);
self.head.headers.insert(
header::SEC_WEBSOCKET_VERSION,
HeaderValue::from_static("13"),
);
if let Some(protocols) = self.protocols.take() { if let Some(protocols) = self.protocols.take() {
self.head.headers.insert( self.head.headers.insert(

View File

@ -100,10 +100,9 @@ impl DefaultHeaders {
/// ///
/// Default is `application/octet-stream`. /// Default is `application/octet-stream`.
pub fn add_content_type(self) -> Self { pub fn add_content_type(self) -> Self {
self.add(( #[allow(clippy::declare_interior_mutable_const)]
CONTENT_TYPE, const HV_MIME: HeaderValue = HeaderValue::from_static("application/octet-stream");
HeaderValue::from_static("application/octet-stream"), self.add((CONTENT_TYPE, HV_MIME))
))
} }
} }