feat: join queries

This commit is contained in:
Christof Weickhardt 2022-04-12 20:03:26 +00:00 committed by Felipe Noronha
parent 51e2a0d05b
commit 2563ad1289

View File

@ -208,24 +208,77 @@ fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> {
} }
fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String { fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String {
if let Some(query) = req.uri().query() { let split_url = forward_url.split("?").collect::<Vec<&str>>();
let mut forwarding_uri =
String::with_capacity(forward_url.len() + req.uri().path().len() + query.len() + 1);
forwarding_uri.push_str(forward_url); let mut base_url: &str = split_url.get(0).unwrap_or(&"");
forwarding_uri.push_str(req.uri().path()); let forward_url_query: &str = split_url.get(1).unwrap_or(&"");
forwarding_uri.push('?');
forwarding_uri.push_str(query);
forwarding_uri let path2 = req.uri().path();
} else {
let mut forwarding_uri = String::with_capacity(forward_url.len() + req.uri().path().len());
forwarding_uri.push_str(forward_url); if base_url.ends_with("/") {
forwarding_uri.push_str(req.uri().path()); let mut path1_chars = base_url.chars();
path1_chars.next();
forwarding_uri base_url = path1_chars.as_str();
} }
let total_length = base_url.len()
+ path2.len()
+ 1
+ forward_url_query.len()
+ req.uri().query().map(|e| e.len()).unwrap_or(0);
let mut url = String::with_capacity(total_length);
url.push_str(base_url);
url.push_str(path2);
if !forward_url_query.is_empty() || req.uri().query().map(|e| !e.is_empty()).unwrap_or(false) {
url.push('?');
url.push_str(forward_url_query);
if forward_url_query.is_empty() {
url.push_str(req.uri().query().unwrap_or(""));
} else {
let forward_query_items = forward_url_query
.split("&")
.map(|el| {
let parts = el.split("=").collect::<Vec<&str>>();
parts[0]
})
.collect::<Vec<_>>();
let request_query_items = req
.uri()
.query()
.unwrap_or("")
.split("&")
.collect::<Vec<&str>>()
.iter()
.map(|el| {
let parts = el.split("=").collect::<Vec<&str>>();
(parts[0], if parts.len() > 1 { parts[1] } else { "" })
})
.collect::<Vec<(&str, &str)>>();
for (key, value) in request_query_items.iter() {
if !forward_query_items.contains(&key) {
url.push_str(key);
url.push('=');
url.push_str(value);
}
}
if url.ends_with("&") {
let mut parts = url.chars();
parts.next_back();
url = parts.as_str().to_string();
}
}
}
url.parse().unwrap()
} }
fn create_proxied_request<B>( fn create_proxied_request<B>(