From b7b1005d2a0d636029629513890314114df3f903 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 7 Aug 2024 01:57:39 +0100 Subject: [PATCH] chore: distinguish reqwest types from actix types --- .vscode/settings.json | 2 ++ http-proxy/src/main.rs | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7bdd3b5..1ed1a32 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cSpell.words": [ "actix", + "addrs", "apalis", "askama", "autoclean", @@ -18,6 +19,7 @@ "pemfile", "prost", "protobuf", + "reqwest", "rustls", "rustup", "sparklepost", diff --git a/http-proxy/src/main.rs b/http-proxy/src/main.rs index 113d306..c981a19 100644 --- a/http-proxy/src/main.rs +++ b/http-proxy/src/main.rs @@ -1,8 +1,7 @@ -use std::net::ToSocketAddrs; +use std::{io, net::ToSocketAddrs as _}; use actix_web::{ - dev::PeerAddr, error, http::Method, middleware, web, App, Error, HttpRequest, HttpResponse, - HttpServer, + dev::PeerAddr, error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, }; use awc::Client; use clap::Parser; @@ -57,7 +56,7 @@ async fn forward( async fn forward_reqwest( req: HttpRequest, mut payload: web::Payload, - method: Method, + method: actix_web::http::Method, peer_addr: Option, url: web::Data, client: web::Data, @@ -81,7 +80,10 @@ async fn forward_reqwest( }); let forwarded_req = client - .request(method, new_url) + .request( + reqwest::Method::from_bytes(method.as_str().as_bytes()).unwrap(), + new_url, + ) .body(reqwest::Body::wrap_stream(UnboundedReceiverStream::new(rx))); // TODO: This forwarded implementation is incomplete as it only handles the unofficial @@ -96,11 +98,16 @@ async fn forward_reqwest( .await .map_err(error::ErrorInternalServerError)?; - let mut client_resp = HttpResponse::build(res.status()); + let mut client_resp = + HttpResponse::build(actix_web::http::StatusCode::from_u16(res.status().as_u16()).unwrap()); + // Remove `Connection` as per // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives for (header_name, header_value) in res.headers().iter().filter(|(h, _)| *h != "connection") { - client_resp.insert_header((header_name.clone(), header_value.clone())); + client_resp.insert_header(( + actix_web::http::header::HeaderName::from_bytes(header_name.as_ref()).unwrap(), + actix_web::http::header::HeaderValue::from_bytes(header_value.as_ref()).unwrap(), + )); } Ok(client_resp.streaming(res.bytes_stream())) @@ -115,7 +122,7 @@ struct CliArguments { } #[actix_web::main] -async fn main() -> std::io::Result<()> { +async fn main() -> io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); let args = CliArguments::parse();