Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions service-rs/httpgate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async-trait = "0.1"
futures = "0.3"

# Utilities
bytes = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
regex = "1"
Expand Down
48 changes: 44 additions & 4 deletions service-rs/httpgate/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use async_trait::async_trait;
use bytes::Bytes;
use pingora_core::upstreams::peer::{HttpPeer, ALPN};
use pingora_core::Result;
use pingora_core::{Error, ErrorSource, ErrorType, Result};
use pingora_http::{RequestHeader, ResponseHeader, Version};
use pingora_proxy::{ProxyHttp, Session};
use pingora_proxy::{FailToProxy, ProxyHttp, Session};
use regex::Regex;
use tracing::{debug, info, warn};
use tracing::{debug, error, info, warn};

use crate::registry::DevboxRegistry;

Expand Down Expand Up @@ -218,7 +219,7 @@ impl ProxyHttp for DevboxProxy {
.headers
.get("content-type")
.and_then(|ct| ct.to_str().ok())
.map(|ct| ct.to_ascii_lowercase())
.map(str::to_ascii_lowercase)
.is_some_and(|ct| ct.starts_with("application/grpc"));

let protocol = if is_h2 && is_grpc_content_type {
Expand Down Expand Up @@ -300,6 +301,45 @@ impl ProxyHttp for DevboxProxy {

Ok(())
}

/// Handle fatal proxy errors by returning an error response with a message body.
///
/// This overrides the default behavior which returns empty 502 responses.
async fn fail_to_proxy(
&self,
session: &mut Session,
e: &Error,
_ctx: &mut Self::CTX,
) -> FailToProxy {
let code = match e.etype() {
ErrorType::HTTPStatus(code) => *code,
_ => match e.esource() {
ErrorSource::Upstream => 502,
ErrorSource::Downstream => match e.etype() {
ErrorType::WriteError | ErrorType::ReadError | ErrorType::ConnectionClosed => {
/* conn already dead */
0
}
_ => 400,
},
ErrorSource::Internal | ErrorSource::Unset => 500,
},
};

if code > 0 {
// Return the error message with httpgate prefix
let body = Bytes::from(format!("httpgate: {e}"));

if let Err(send_err) = session.respond_error_with_body(code, body).await {
error!("Failed to send error response to downstream: {send_err}");
}
}

FailToProxy {
error_code: code,
can_reuse_downstream: false,
}
}
}

#[cfg(test)]
Expand Down
Loading