fix: add Content-Type

This commit is contained in:
Udit Karode 2022-08-07 17:01:23 +05:30
parent 4142aa289c
commit 699c3c07b1
No known key found for this signature in database
GPG Key ID: 864BAA48465205F0
4 changed files with 34 additions and 20 deletions

1
Cargo.lock generated
View File

@ -45,6 +45,7 @@ dependencies = [
"bytes",
"curl",
"tokio",
"unicase",
"warp",
]

View File

@ -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"

View File

@ -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<String, Box<dyn Error>> {
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<SocketAddr>,
@ -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,

View File

@ -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<String>;
pub type Request = (FullPath, QueryParameters, Method, Bytes);
pub fn query_params_filter(
) -> impl Filter<Extract = (QueryParameters,), Error = std::convert::Infallible> + 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<Extract = Request, Error = warp::Rejection> + Clone {
warp::path::full()
.and(query_params_filter())
.and(warp::header::headers_cloned())
.and(warp::method())
.and(warp::body::bytes())
}