aboutsummaryrefslogtreecommitdiffhomepage
path: root/pingora-proxy/src/lib.rs
diff options
context:
space:
mode:
authorAndrew Hauck <[email protected]>2024-11-08 14:34:14 -0800
committerYuchen Wu <[email protected]>2024-12-13 17:27:40 -0800
commita8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2 (patch)
treec5eb5e7b4e8b4c267de63f1c338365a09847a584 /pingora-proxy/src/lib.rs
parente309436319ed5cbc3aaf53221070a1fd070b8bcf (diff)
downloadpingora-a8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2.tar.gz
pingora-a8a6e77eef2c0f4d2a45f00c5b0e316dd373f2f2.zip
Improve support for sending custom response headers and bodies for error messages
Diffstat (limited to 'pingora-proxy/src/lib.rs')
-rw-r--r--pingora-proxy/src/lib.rs25
1 files changed, 15 insertions, 10 deletions
diff --git a/pingora-proxy/src/lib.rs b/pingora-proxy/src/lib.rs
index 731cdb4..fa639f2 100644
--- a/pingora-proxy/src/lib.rs
+++ b/pingora-proxy/src/lib.rs
@@ -143,9 +143,14 @@ impl<SV> HttpProxy<SV> {
}
Err(mut e) => {
e.as_down();
- error!("Fail to proxy: {}", e);
+ error!("Fail to proxy: {e}");
if matches!(e.etype, InvalidHTTPHeader) {
- downstream_session.respond_error(400).await;
+ downstream_session
+ .respond_error(400)
+ .await
+ .unwrap_or_else(|e| {
+ error!("failed to send error response to downstream: {e}");
+ });
} // otherwise the connection must be broken, no need to send anything
downstream_session.shutdown().await;
return None;
@@ -344,16 +349,16 @@ impl Session {
&self.downstream_session
}
- /// Write HTTP response with the given error code to the downstream
+ /// Write HTTP response with the given error code to the downstream.
pub async fn respond_error(&mut self, error: u16) -> Result<()> {
- let resp = HttpSession::generate_error(error);
- self.write_response_header(Box::new(resp), true)
+ self.as_downstream_mut().respond_error(error).await
+ }
+
+ /// Write HTTP response with the given error code to the downstream with a body.
+ pub async fn respond_error_with_body(&mut self, error: u16, body: Bytes) -> Result<()> {
+ self.as_downstream_mut()
+ .respond_error_with_body(error, body)
.await
- .unwrap_or_else(|e| {
- self.downstream_session.set_keepalive(None);
- error!("failed to send error response to downstream: {e}");
- });
- Ok(())
}
/// Write the given HTTP response header to the downstream