feat: print reponse

This commit is contained in:
2024-03-30 12:25:42 +08:00
parent 277843967b
commit f02f6bb7dd
3 changed files with 34 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -1682,6 +1682,7 @@ version = "0.1.0"
dependencies = [
"async-trait",
"base64 0.22.0",
"bytes",
"http 1.1.0",
"log",
"once_cell",

View File

@@ -20,3 +20,4 @@ p256 = "0.13.2"
p384 = "0.13.0"
p521 = "0.13.3"
once_cell = "1.19.0"
bytes = "1.6.0"

View File

@@ -1,8 +1,10 @@
use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use bytes::Bytes;
use http::HeaderName;
use pingora::{Error, ErrorType};
use pingora::http::ResponseHeader;
use pingora::prelude::{HttpPeer, ProxyHttp, Result, Session};
use super::service::HostConfig;
@@ -83,4 +85,34 @@ impl ProxyHttp for ProxyApp {
log::info!("Request:\n{}\n\n{}", req, body.unwrap_or_else(|| "<None>".into()));
Ok(false)
}
async fn response_filter(
&self,
_session: &mut Session,
upstream_response: &mut ResponseHeader,
_ctx: &mut Self::CTX,
) -> Result<()>
where Self::CTX: Send + Sync,
{
let mut resp = String::new();
resp.push_str(&format!("version: {}\n", upstream_response.status));
resp.push_str(&format!("headers: {:#?}", upstream_response.headers));
log::info!("Response: {}", resp);
Ok(())
}
fn upstream_response_body_filter(
&self,
_session: &mut Session,
body: &Option<Bytes>,
end_of_stream: bool,
_ctx: &mut Self::CTX,
) {
log::info!("Body {}: [[[{}]]] ",end_of_stream,
body.as_ref().map(|bytes|
String::from_utf8_lossy(bytes.iter().as_slice())
).unwrap_or_else(|| "<none>".into())
);
}
}