1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-01-03 13:58:44 +00:00

add 431 and 451 status codes

This commit is contained in:
Rob Ede 2021-02-11 22:58:35 +00:00
parent 77efc09362
commit c1af5089b9
No known key found for this signature in database
GPG key ID: C2A3B36E841A91E6
10 changed files with 35 additions and 28 deletions

View file

@ -199,7 +199,7 @@ mod tests {
use http::Method; use http::Method;
use super::*; use super::*;
use crate::httpmessage::HttpMessage; use crate::HttpMessage;
#[test] #[test]
fn test_http_request_chunked_payload_and_next_message() { fn test_http_request_chunked_payload_and_next_message() {

View file

@ -652,7 +652,7 @@ mod tests {
use super::*; use super::*;
use crate::error::ParseError; use crate::error::ParseError;
use crate::http::header::{HeaderName, SET_COOKIE}; use crate::http::header::{HeaderName, SET_COOKIE};
use crate::httpmessage::HttpMessage; use crate::HttpMessage;
impl PayloadType { impl PayloadType {
fn unwrap(self) -> PayloadDecoder { fn unwrap(self) -> PayloadDecoder {

View file

@ -382,8 +382,8 @@ where
} }
// send service call error as response // send service call error as response
Poll::Ready(Err(e)) => { Poll::Ready(Err(err)) => {
let res: Response = e.into().into(); let res: Response = err.into().into();
let (res, body) = res.replace_body(()); let (res, body) = res.replace_body(());
self.as_mut().send_response(res, body.into_body())?; self.as_mut().send_response(res, body.into_body())?;
} }
@ -421,8 +421,8 @@ where
continue 'res; continue 'res;
} }
Poll::Ready(Some(Err(e))) => { Poll::Ready(Some(Err(err))) => {
return Err(DispatchError::Service(e)) return Err(DispatchError::Service(err))
} }
Poll::Pending => return Ok(PollResponse::DoNothing), Poll::Pending => return Ok(PollResponse::DoNothing),
@ -443,8 +443,8 @@ where
this.state.set(State::ServiceCall(fut)); this.state.set(State::ServiceCall(fut));
} }
// send expect error as response // send expect error as response
Poll::Ready(Err(e)) => { Poll::Ready(Err(err)) => {
let res: Response = e.into().into(); let res: Response = err.into().into();
let (res, body) = res.replace_body(()); let (res, body) = res.replace_body(());
self.as_mut().send_response(res, body.into_body())?; self.as_mut().send_response(res, body.into_body())?;
} }
@ -492,9 +492,9 @@ where
// future is error. send response and return a result. On success // future is error. send response and return a result. On success
// to notify the dispatcher a new state is set and the outer loop // to notify the dispatcher a new state is set and the outer loop
// should be continue. // should be continue.
Poll::Ready(Err(e)) => { Poll::Ready(Err(err)) => {
let e = e.into(); let err = err.into();
let res: Response = e.into(); let res: Response = err.into();
let (res, body) = res.replace_body(()); let (res, body) = res.replace_body(());
return self.send_response(res, body.into_body()); return self.send_response(res, body.into_body());
} }
@ -512,9 +512,9 @@ where
} }
// see the comment on ExpectCall state branch's Pending. // see the comment on ExpectCall state branch's Pending.
Poll::Pending => Ok(()), Poll::Pending => Ok(()),
// see the comment on ExpectCall state branch's Ready(Err(e)). // see the comment on ExpectCall state branch's Ready(Err(err)).
Poll::Ready(Err(e)) => { Poll::Ready(Err(err)) => {
let res: Response = e.into().into(); let res: Response = err.into().into();
let (res, body) = res.replace_body(()); let (res, body) = res.replace_body(());
self.send_response(res, body.into_body()) self.send_response(res, body.into_body())
} }
@ -606,25 +606,25 @@ where
// decode is partial and buffer is not full yet. // decode is partial and buffer is not full yet.
// break and wait for more read. // break and wait for more read.
Ok(None) => break, Ok(None) => break,
Err(ParseError::Io(e)) => { Err(ParseError::Io(err)) => {
self.as_mut().client_disconnected(); self.as_mut().client_disconnected();
this = self.as_mut().project(); this = self.as_mut().project();
*this.error = Some(DispatchError::Io(e)); *this.error = Some(DispatchError::Io(err));
break; break;
} }
Err(ParseError::TooLarge) => { Err(ParseError::TooLarge) => {
if let Some(mut payload) = this.payload.take() { if let Some(mut payload) = this.payload.take() {
payload.set_error(PayloadError::Overflow); payload.set_error(PayloadError::Overflow);
} }
// Requests overflow buffer size should be responded with 413 // Requests overflow buffer size should be responded with 431
this.messages.push_back(DispatcherMessage::Error( this.messages.push_back(DispatcherMessage::Error(
Response::PayloadTooLarge().finish().drop_body(), Response::RequestHeaderFieldsTooLarge().finish().drop_body(),
)); ));
this.flags.insert(Flags::READ_DISCONNECT); this.flags.insert(Flags::READ_DISCONNECT);
*this.error = Some(ParseError::TooLarge.into()); *this.error = Some(ParseError::TooLarge.into());
break; break;
} }
Err(e) => { Err(err) => {
if let Some(mut payload) = this.payload.take() { if let Some(mut payload) = this.payload.take() {
payload.set_error(PayloadError::EncodingCorrupted); payload.set_error(PayloadError::EncodingCorrupted);
} }
@ -634,7 +634,7 @@ where
Response::BadRequest().finish().drop_body(), Response::BadRequest().finish().drop_body(),
)); ));
this.flags.insert(Flags::READ_DISCONNECT); this.flags.insert(Flags::READ_DISCONNECT);
*this.error = Some(e.into()); *this.error = Some(err.into());
break; break;
} }
} }

View file

@ -5,7 +5,7 @@ use crate::header::{
self, from_one_raw_str, EntityTag, Header, HeaderName, HeaderValue, HttpDate, self, from_one_raw_str, EntityTag, Header, HeaderName, HeaderValue, HttpDate,
IntoHeaderValue, InvalidHeaderValue, Writer, IntoHeaderValue, InvalidHeaderValue, Writer,
}; };
use crate::httpmessage::HttpMessage; use crate::HttpMessage;
/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2) /// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
/// ///

View file

@ -9,7 +9,7 @@ use percent_encoding::{AsciiSet, CONTROLS};
pub use http::header::*; pub use http::header::*;
use crate::error::ParseError; use crate::error::ParseError;
use crate::httpmessage::HttpMessage; use crate::HttpMessage;
mod as_name; mod as_name;
mod into_pair; mod into_pair;

View file

@ -67,6 +67,14 @@ impl Response {
static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED); static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY); static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS); static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS);
static_resp!(
RequestHeaderFieldsTooLarge,
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE
);
static_resp!(
UnavailableForLegalReasons,
StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS
);
static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR); static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED); static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED);

View file

@ -1,6 +1,6 @@
//! HTTP primitives for the Actix ecosystem. //! HTTP primitives for the Actix ecosystem.
#![deny(rust_2018_idioms)] #![deny(rust_2018_idioms, nonstandard_style)]
#![allow( #![allow(
clippy::type_complexity, clippy::type_complexity,
clippy::too_many_arguments, clippy::too_many_arguments,
@ -26,7 +26,7 @@ mod extensions;
mod header; mod header;
mod helpers; mod helpers;
mod http_codes; mod http_codes;
mod httpmessage; mod http_message;
mod message; mod message;
mod payload; mod payload;
mod request; mod request;
@ -45,7 +45,7 @@ pub use self::builder::HttpServiceBuilder;
pub use self::config::{KeepAlive, ServiceConfig}; pub use self::config::{KeepAlive, ServiceConfig};
pub use self::error::{Error, ResponseError, Result}; pub use self::error::{Error, ResponseError, Result};
pub use self::extensions::Extensions; pub use self::extensions::Extensions;
pub use self::httpmessage::HttpMessage; pub use self::http_message::HttpMessage;
pub use self::message::{Message, RequestHead, RequestHeadType, ResponseHead}; pub use self::message::{Message, RequestHead, RequestHeadType, ResponseHead};
pub use self::payload::{Payload, PayloadStream}; pub use self::payload::{Payload, PayloadStream};
pub use self::request::Request; pub use self::request::Request;

View file

@ -9,7 +9,7 @@ use http::{header, Method, Uri, Version};
use crate::extensions::Extensions; use crate::extensions::Extensions;
use crate::header::HeaderMap; use crate::header::HeaderMap;
use crate::httpmessage::HttpMessage; use crate::HttpMessage;
use crate::message::{Message, RequestHead}; use crate::message::{Message, RequestHead};
use crate::payload::{Payload, PayloadStream}; use crate::payload::{Payload, PayloadStream};

View file

@ -867,6 +867,7 @@ mod tests {
use super::*; use super::*;
use crate::body::Body; use crate::body::Body;
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE, SET_COOKIE}; use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE, SET_COOKIE};
use crate::HttpMessage;
#[test] #[test]
fn test_debug() { fn test_debug() {
@ -880,8 +881,6 @@ mod tests {
#[test] #[test]
fn test_response_cookies() { fn test_response_cookies() {
use crate::httpmessage::HttpMessage;
let req = crate::test::TestRequest::default() let req = crate::test::TestRequest::default()
.append_header((COOKIE, "cookie1=value1")) .append_header((COOKIE, "cookie1=value1"))
.append_header((COOKIE, "cookie2=value2")) .append_header((COOKIE, "cookie2=value2"))