diff options
author | Andrew Hauck <[email protected]> | 2024-11-08 14:34:14 -0800 |
---|---|---|
committer | Yuchen Wu <[email protected]> | 2024-12-13 17:27:40 -0800 |
commit | a8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2 (patch) | |
tree | c5eb5e7b4e8b4c267de63f1c338365a09847a584 /pingora-http/src/lib.rs | |
parent | e309436319ed5cbc3aaf53221070a1fd070b8bcf (diff) | |
download | pingora-a8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2.tar.gz pingora-a8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2.zip |
Improve support for sending custom response headers and bodies for error messages
Diffstat (limited to 'pingora-http/src/lib.rs')
-rw-r--r-- | pingora-http/src/lib.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/pingora-http/src/lib.rs b/pingora-http/src/lib.rs index d57998d..d5cf8a8 100644 --- a/pingora-http/src/lib.rs +++ b/pingora-http/src/lib.rs @@ -492,6 +492,11 @@ impl ResponseHeader { pub fn as_owned_parts(&self) -> RespParts { clone_resp_parts(&self.base) } + + /// Helper function to set the HTTP content length on the response header. + pub fn set_content_length(&mut self, len: usize) -> Result<()> { + self.insert_header(http::header::CONTENT_LENGTH, len) + } } fn clone_req_parts(me: &ReqParts) -> ReqParts { @@ -754,4 +759,18 @@ mod tests { // Some(false) assert!(!req.send_end_stream().unwrap()); } + + #[test] + fn set_test_set_content_length() { + let mut resp = ResponseHeader::new(None); + resp.set_content_length(10).unwrap(); + + assert_eq!( + b"10", + resp.headers + .get(http::header::CONTENT_LENGTH) + .map(|d| d.as_bytes()) + .unwrap() + ); + } } |