feat(main): also relay headers

This commit is contained in:
Udit Karode 2022-08-08 00:42:24 +05:30
parent 7a493cdce3
commit 9a8ab6c3e1
No known key found for this signature in database
GPG Key ID: 864BAA48465205F0

View File

@ -7,7 +7,7 @@ use std::str::from_utf8;
use std::{error::Error, net::SocketAddr}; use std::{error::Error, net::SocketAddr};
use unicase::Ascii; use unicase::Ascii;
use warp::hyper::HeaderMap; use warp::hyper::HeaderMap;
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter}; use warp::{http::Method, http::Response, hyper::body::Bytes, path::FullPath, Filter};
const FOR_COPY: [&'static str; 1] = ["Content-Type"]; const FOR_COPY: [&'static str; 1] = ["Content-Type"];
@ -27,7 +27,7 @@ fn get_proxied_body(
headers: &HeaderMap, headers: &HeaderMap,
body: &Bytes, body: &Bytes,
base: &String, base: &String,
) -> Result<String, Box<dyn Error>> { ) -> Result<(String, Vec<String>), Box<dyn Error>> {
let mut handle = Easy::new(); let mut handle = Easy::new();
handle.url(format!("{}{}", base, uri).as_str())?; handle.url(format!("{}{}", base, uri).as_str())?;
@ -49,7 +49,9 @@ fn get_proxied_body(
handle.post_field_size(body_bytes.len() as u64)?; handle.post_field_size(body_bytes.len() as u64)?;
} }
let mut buf = Vec::new(); let mut body_buffer = Vec::new();
let mut headers = Vec::new();
{ {
let mut transfer = handle.transfer(); let mut transfer = handle.transfer();
@ -61,17 +63,26 @@ fn get_proxied_body(
} }
transfer.write_function(|data| { transfer.write_function(|data| {
buf.extend_from_slice(data); body_buffer.extend_from_slice(data);
Ok(data.len()) Ok(data.len())
})?; })?;
transfer.header_function(|header| {
match from_utf8(header) {
Ok(v) => headers.push(v.to_string()),
Err(_) => (),
}
true
})?;
transfer.perform()?; transfer.perform()?;
} }
let cloned_buf = buf.clone(); let cloned_buf = body_buffer.clone();
let s = from_utf8(&cloned_buf)?; let s = from_utf8(&cloned_buf)?;
Ok(s.to_string()) Ok((s.to_string(), headers))
} }
fn handler( fn handler(
@ -81,7 +92,7 @@ fn handler(
body: Bytes, body: Bytes,
ip: Option<SocketAddr>, ip: Option<SocketAddr>,
base: String, base: String,
) -> String { ) -> Result<Response<String>, warp::http::Error> {
let uri = uri.as_str().clone().to_string(); let uri = uri.as_str().clone().to_string();
let ip_str = match ip { let ip_str = match ip {
Some(v) => format!("{}", v), Some(v) => format!("{}", v),
@ -92,15 +103,24 @@ fn handler(
let proxy_response = get_proxied_body(&uri, &method, &headers, &body, &base); let proxy_response = get_proxied_body(&uri, &method, &headers, &body, &base);
let res = match proxy_response { let (res, headers) = match proxy_response {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
println!("error: {}", e); println!("error: {}", e);
ERR.to_string() (ERR.to_string(), vec![])
} }
}; };
return res; let mut builder = Response::builder();
for header in headers.iter() {
let split: Vec<&str> = header.split(": ").collect();
if split.len() == 2 {
builder = builder.header(split[0], split[1].replace("\r\n", ""));
}
}
builder.body(res)
} }
#[tokio::main] #[tokio::main]