Skip to content

Commit a9b4cf2

Browse files
authored
fix(core): use entire request URL on dev server proxy (#5819)
1 parent 8f47825 commit a9b4cf2

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Properly proxy dev server requests with query strings and fragments.

core/tauri/src/manager.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -886,18 +886,26 @@ impl<R: Runtime> WindowManager<R> {
886886
) -> Box<dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync>
887887
{
888888
#[cfg(dev)]
889-
let url = self.get_url().into_owned();
889+
let url = {
890+
let mut url = self.get_url().as_str().to_string();
891+
if url.ends_with('/') {
892+
url.pop();
893+
}
894+
url
895+
};
890896
#[cfg(not(dev))]
891897
let manager = self.clone();
892898
let window_origin = window_origin.to_string();
893899

894900
Box::new(move |request| {
895-
let path = request
896-
.uri()
897-
.split(&['?', '#'][..])
898-
// ignore query string and fragment
899-
.next()
900-
.unwrap()
901+
// use the entire URI as we are going to proxy the request
902+
#[cfg(dev)]
903+
let path = request.uri();
904+
// ignore query string and fragment
905+
#[cfg(not(dev))]
906+
let path = request.uri().split(&['?', '#'][..]).next().unwrap();
907+
908+
let path = path
901909
.strip_prefix("tauri://localhost")
902910
.map(|p| p.to_string())
903911
// the `strip_prefix` only returns None when a request is made to `https://tauri.$P` on Windows
@@ -910,9 +918,12 @@ impl<R: Runtime> WindowManager<R> {
910918
#[cfg(dev)]
911919
let mut response = {
912920
use attohttpc::StatusCode;
913-
let mut url = url.clone();
914-
url.set_path(&path);
915-
let mut proxy_builder = attohttpc::get(url.as_str()).danger_accept_invalid_certs(true);
921+
let decoded_path = percent_encoding::percent_decode(path.as_bytes())
922+
.decode_utf8_lossy()
923+
.to_string();
924+
let url = format!("{url}{decoded_path}");
925+
println!("request url {url}, original path {path}, decoded {decoded_path}");
926+
let mut proxy_builder = attohttpc::get(&url).danger_accept_invalid_certs(true);
916927
for (name, value) in request.headers() {
917928
proxy_builder = proxy_builder.header(name, value);
918929
}

0 commit comments

Comments
 (0)