aboutsummaryrefslogtreecommitdiffhomepage
path: root/pingora-proxy
diff options
context:
space:
mode:
Diffstat (limited to 'pingora-proxy')
-rw-r--r--pingora-proxy/examples/gateway.rs7
-rw-r--r--pingora-proxy/src/lib.rs25
-rw-r--r--pingora-proxy/src/proxy_cache.rs8
-rw-r--r--pingora-proxy/src/proxy_trait.rs5
4 files changed, 30 insertions, 15 deletions
diff --git a/pingora-proxy/examples/gateway.rs b/pingora-proxy/examples/gateway.rs
index 78f4aae..d4a5cee 100644
--- a/pingora-proxy/examples/gateway.rs
+++ b/pingora-proxy/examples/gateway.rs
@@ -13,6 +13,7 @@
// limitations under the License.
use async_trait::async_trait;
+use bytes::Bytes;
use clap::Parser;
use log::info;
use prometheus::register_int_counter;
@@ -42,7 +43,9 @@ impl ProxyHttp for MyGateway {
if session.req_header().uri.path().starts_with("/login")
&& !check_login(session.req_header())
{
- let _ = session.respond_error(403).await;
+ let _ = session
+ .respond_error_with_body(403, Bytes::from_static(b"no way!"))
+ .await;
// true: early return as the response is already written
return Ok(true);
}
@@ -103,7 +106,7 @@ impl ProxyHttp for MyGateway {
}
}
-// RUST_LOG=INFO cargo run --example load_balancer
+// RUST_LOG=INFO cargo run --example gateway
// curl 127.0.0.1:6191 -H "Host: one.one.one.one"
// curl 127.0.0.1:6190/family/ -H "Host: one.one.one.one"
// curl 127.0.0.1:6191/login/ -H "Host: one.one.one.one" -I -H "Authorization: password"
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
diff --git a/pingora-proxy/src/proxy_cache.rs b/pingora-proxy/src/proxy_cache.rs
index 8957bc7..c720575 100644
--- a/pingora-proxy/src/proxy_cache.rs
+++ b/pingora-proxy/src/proxy_cache.rs
@@ -295,7 +295,13 @@ impl<SV> HttpProxy<SV> {
}
Err(e) => {
// TODO: more logging and error handling
- session.as_mut().respond_error(500).await;
+ session
+ .as_mut()
+ .respond_error(500)
+ .await
+ .unwrap_or_else(|e| {
+ error!("failed to send error response to downstream: {e}");
+ });
// we have not write anything dirty to downstream, it is still reusable
return (true, Some(e));
}
diff --git a/pingora-proxy/src/proxy_trait.rs b/pingora-proxy/src/proxy_trait.rs
index 4017789..6970ec0 100644
--- a/pingora-proxy/src/proxy_trait.rs
+++ b/pingora-proxy/src/proxy_trait.rs
@@ -372,7 +372,6 @@ pub trait ProxyHttp {
where
Self::CTX: Send + Sync,
{
- let server_session = session.as_mut();
let code = match e.etype() {
HTTPStatus(code) => *code,
_ => {
@@ -392,7 +391,9 @@ pub trait ProxyHttp {
}
};
if code > 0 {
- server_session.respond_error(code).await
+ session.respond_error(code).await.unwrap_or_else(|e| {
+ error!("failed to send error response to downstream: {e}");
+ });
}
code
}