Skip to content

Commit ba4e5a0

Browse files
AlexandruCihodaruluminitavoicu
authored andcommitted
Added payload too large response
Signed-off-by: AlexandruCihodaru <[email protected]> Suggested-by: George Pisaltu <[email protected]>
1 parent c1a38b5 commit ba4e5a0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/common/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ pub enum RequestError {
8484
Overflow,
8585
/// Underflow occurred when parsing a request.
8686
Underflow,
87+
/// Payload too large.
88+
SizeLimitExceeded(usize, usize),
8789
}
8890

8991
impl Display for RequestError {
@@ -104,6 +106,12 @@ impl Display for RequestError {
104106
Self::InvalidUri(inner) => write!(f, "Invalid URI: {}", inner),
105107
Self::Overflow => write!(f, "Overflow occurred when parsing a request."),
106108
Self::Underflow => write!(f, "Underflow occurred when parsing a request."),
109+
Self::SizeLimitExceeded(limit, size) => write!(
110+
f,
111+
"Request payload with size {} is larger than the limit of {} \
112+
allowed by server.",
113+
size, limit
114+
),
107115
}
108116
}
109117
}
@@ -418,6 +426,10 @@ mod tests {
418426
format!("{}", RequestError::Underflow),
419427
"Underflow occurred when parsing a request."
420428
);
429+
assert_eq!(
430+
format!("{}", RequestError::SizeLimitExceeded(4, 10)),
431+
"Request payload with size 10 is larger than the limit of 4 allowed by server."
432+
);
421433
}
422434

423435
#[test]

src/response.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub enum StatusCode {
2828
NotFound,
2929
/// 405, Method Not Allowed
3030
MethodNotAllowed,
31+
/// 413, Payload Too Large
32+
PayloadTooLarge,
3133
/// 500, Internal Server Error
3234
InternalServerError,
3335
/// 501, Not Implemented
@@ -47,6 +49,7 @@ impl StatusCode {
4749
Self::Unauthorized => b"401",
4850
Self::NotFound => b"404",
4951
Self::MethodNotAllowed => b"405",
52+
Self::PayloadTooLarge => b"413",
5053
Self::InternalServerError => b"500",
5154
Self::NotImplemented => b"501",
5255
Self::ServiceUnavailable => b"503",
@@ -375,6 +378,7 @@ mod tests {
375378
assert_eq!(StatusCode::Unauthorized.raw(), b"401");
376379
assert_eq!(StatusCode::NotFound.raw(), b"404");
377380
assert_eq!(StatusCode::MethodNotAllowed.raw(), b"405");
381+
assert_eq!(StatusCode::PayloadTooLarge.raw(), b"413");
378382
assert_eq!(StatusCode::InternalServerError.raw(), b"500");
379383
assert_eq!(StatusCode::NotImplemented.raw(), b"501");
380384
assert_eq!(StatusCode::ServiceUnavailable.raw(), b"503");

0 commit comments

Comments
 (0)