Skip to content

Commit e106ffb

Browse files
committed
index redirect with query string
1 parent 211cff0 commit e106ffb

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

docs/develop/change-log.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Change Log
2+
### Version 2.2.3
3+
- fix: sub_path '' => '/', like GitHub pages
24

35
### Version 2.2.2
46

server/src/file_cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ impl FileCache {
242242
.or_else(|| result.get("index.htm"))
243243
.cloned();
244244
if let Some(v) = index_opt {
245-
// result.insert("".to_string(), v.clone());
246-
result.insert("/".to_string(), v);
245+
result.insert("".to_string(), v.clone());
246+
//result.insert("/".to_string(), v);
247247
}
248248
}
249249
}

server/src/service.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ pub async fn create_service(
6464
if !is_https {
6565
if let Some(port) = service_config.http_redirect_to_https {
6666
let mut resp = Response::default();
67-
let port = if port == 443 {
68-
"".to_string()
67+
68+
let redirect_path = if port != 443 {
69+
format!("https://{host}{port}{uri}")
6970
} else {
70-
format!(":{}", port)
71+
format!("https://{host}{uri}")
7172
};
72-
let redirect_path = format!("https://{host}{port}{}", uri);
7373
resp.headers_mut()
7474
.insert(LOCATION, redirect_path.parse().unwrap());
7575
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
@@ -80,8 +80,14 @@ pub async fn create_service(
8080
let path = uri.path();
8181
if domain_storage.check_if_empty_index(host, path) {
8282
let mut resp = Response::default();
83+
let mut path = format!("{path}/");
84+
if let Some(query) = uri.query() {
85+
path.push('?');
86+
path.push_str(query);
87+
}
88+
let path = path.parse().unwrap();
8389
resp.headers_mut()
84-
.insert(LOCATION, format!("{path}/").parse().unwrap());
90+
.insert(LOCATION, path);
8591
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
8692
return Ok(resp);
8793
}

test/config.test.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ http {
55
}
66

77
# directory to store static web files. if you use docker, please mount a persistence volume for it.
8-
file_dir = "./test/data/"
8+
file_dir = "./tests/data/web"
99

1010
# enable cors, default is false, its implementation is simple now.
1111
# Access-Control-Allow-Origin: $ORIGIN

tests/tests/common.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use reqwest::header::CACHE_CONTROL;
22
use reqwest::redirect::Policy;
3-
use reqwest::{ClientBuilder, StatusCode};
3+
use reqwest::{ClientBuilder, StatusCode, Url};
44
use spa_client::api::API;
55
use std::path::{Path, PathBuf};
66
use std::{env, fs, io};
@@ -140,6 +140,29 @@ pub async fn assert_files(
140140
);
141141
}
142142
}
143+
}
144+
pub async fn assert_index_redirect_correct(request_prefix: &str) {
145+
let client = ClientBuilder::new()
146+
.danger_accept_invalid_certs(true)
147+
.redirect(Policy::none())
148+
.build()
149+
.unwrap();
150+
let query = [("lang", "rust"), ("browser", "servo"), ("zh", "转义字符")];
151+
let url = Url::parse_with_params(request_prefix,
152+
&query).unwrap();
153+
println!("{}", url);
154+
let path = url.path();
155+
let query = url.query().unwrap();
156+
let response= client.get(url.clone()).send().await.unwrap();
157+
assert_eq!(response.status(),StatusCode::MOVED_PERMANENTLY);
158+
assert_eq!(response.headers().get("location").unwrap().to_str().unwrap(),
159+
format!("{path}/?{query}")
160+
);
161+
162+
163+
164+
165+
143166
}
144167
pub async fn assert_files_no_exists(request_prefix: &str, check_path: Vec<&'static str>) {
145168
for file in check_path {

tests/tests/starter.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ async fn start_server_and_client_upload_file() {
1818

1919
run_server();
2020

21-
tokio::time::sleep(Duration::from_secs(2)).await;
21+
tokio::time::sleep(Duration::from_secs(1)).await;
2222

2323
upload_file_and_check(domain, request_prefix, 1, vec!["", "index.html"]).await;
24+
assert_index_redirect_correct(request_prefix).await;
2425

2526
assert_expired(
2627
request_prefix,
@@ -58,6 +59,7 @@ async fn start_server_with_single_domain() {
5859

5960
tokio::time::sleep(Duration::from_secs(2)).await;
6061

62+
// assert_index_redirect_correct(request_prefix).await; // http client would auto patch / to http://www.example.com => http://www.example.com/
6163
upload_file_and_check(domain, request_prefix, 1, vec!["", "index.html"]).await;
6264

6365
assert_expired(

0 commit comments

Comments
 (0)