From 699c3c07b10b0a279e94291ac8791b9bf61c1e1d Mon Sep 17 00:00:00 2001 From: Udit Karode Date: Sun, 7 Aug 2022 17:01:23 +0530 Subject: [PATCH] fix: add Content-Type --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 34 ++++++++++++++++++++++++---------- src/utils.rs | 18 ++++++++---------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 386cb41..79f9b69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,6 +45,7 @@ dependencies = [ "bytes", "curl", "tokio", + "unicase", "warp", ] diff --git a/Cargo.toml b/Cargo.toml index 620444f..d24bf5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" bytes = "1.2.1" curl = "0.4.44" tokio = { version = "1", features = ["full"] } +unicase = "2.6.0" warp = "0.3" diff --git a/src/main.rs b/src/main.rs index ee54be6..c2d6fdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,16 @@ -#![feature(slice_pattern)] - pub mod utils; -use core::slice::SlicePattern; -use curl::easy::Easy; +use curl::easy::{Easy, List}; use std::env::var; use std::io::Read; use std::str::from_utf8; use std::{error::Error, net::SocketAddr}; -use utils::QueryParameters; +use unicase::Ascii; +use warp::hyper::HeaderMap; use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter}; +const FOR_COPY: [&'static str; 1] = ["Content-Type"]; + const BASE: &str = "https://www.bankofmaldives.com.mv/internetbanking/api/"; const ERR: &str = "{ code: 407, message: \"Proxy failed\" }"; @@ -24,15 +24,29 @@ fn get_base_url() -> String { fn get_proxied_body( uri: &String, method: &Method, + headers: &HeaderMap, body: &Bytes, base: &String, ) -> Result> { let mut handle = Easy::new(); handle.url(format!("{}{}", base, uri).as_str())?; + let mut custom_headers = List::new(); + for (name, value) in headers.iter() { + if FOR_COPY.iter().any(|v| Ascii::new(v) == &name) { + match value.to_str() { + Ok(val) => custom_headers.append(format!("{}: {}", name.as_str(), val).as_str())?, + Err(_) => (), + }; + } + } + handle.http_headers(custom_headers)?; + + let body = std::str::from_utf8(body)?; + let mut body_bytes = body.as_bytes(); + if method == Method::POST { - handle.post(true)?; - handle.post_field_size(body.len() as u64)?; + handle.post_field_size(body_bytes.len() as u64)?; } let mut buf = Vec::new(); @@ -41,7 +55,7 @@ fn get_proxied_body( if method == Method::POST { transfer.read_function(|into| { - let read = body.as_slice().read(into).unwrap_or(0); + let read = body_bytes.read(into).unwrap_or(0); Ok(read) })?; } @@ -62,7 +76,7 @@ fn get_proxied_body( fn handler( uri: FullPath, - _: QueryParameters, + headers: HeaderMap, method: Method, body: Bytes, ip: Option, @@ -76,7 +90,7 @@ fn handler( println!("({}) {}", ip_str, uri); - let proxy_response = get_proxied_body(&uri, &method, &body, &base); + let proxy_response = get_proxied_body(&uri, &method, &headers, &body, &base); let res = match proxy_response { Ok(v) => v, diff --git a/src/utils.rs b/src/utils.rs index d0c12d9..2137e29 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,19 +1,17 @@ -use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter}; +use warp::{ + http::Method, + hyper::{body::Bytes, HeaderMap}, + path::FullPath, + Filter, +}; pub type QueryParameters = Option; -pub type Request = (FullPath, QueryParameters, Method, Bytes); - -pub fn query_params_filter( -) -> impl Filter + Clone { - warp::query::raw() - .map(Some) - .or_else(|_| async { Ok::<(QueryParameters,), std::convert::Infallible>((None,)) }) -} +pub type Request = (FullPath, HeaderMap, Method, Bytes); pub fn extract_request_data_filter( ) -> impl Filter + Clone { warp::path::full() - .and(query_params_filter()) + .and(warp::header::headers_cloned()) .and(warp::method()) .and(warp::body::bytes()) }