Skip to content

Commit 211cff0

Browse files
committed
fix: sub_domain '' => '/', like Github pages
1 parent c501833 commit 211cff0

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

server/src/domain_storage.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl DomainStorage {
407407
.filter_map(|version_entity| {
408408
let version_entity = version_entity.ok()?;
409409
let version = version_entity.file_name().to_str()?.parse::<u32>();
410-
Some(version.ok()?)
410+
version.ok()
411411
})
412412
.collect();
413413
if versions.is_empty() {
@@ -535,6 +535,21 @@ impl DomainStorage {
535535
Ok(Vec::new())
536536
}
537537
}
538+
pub fn check_if_empty_index(&self, host:&str, path:&str) -> bool {
539+
self.meta.get(host).map(|v| {
540+
match v.value() {
541+
DomainMeta::OneWeb{..} => path.is_empty(),
542+
DomainMeta::MultipleWeb(map) => {
543+
if path.len() > 1 {
544+
map.contains_key(&path[1..])
545+
} else {
546+
map.contains_key(path)
547+
}
548+
549+
}
550+
}
551+
}).unwrap_or(false)
552+
}
538553

539554
pub fn save_file(
540555
&self,

server/src/file_cache.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl FileCache {
146146
) -> anyhow::Result<HashMap<String, Arc<CacheItem>>> {
147147
let prefix = path
148148
.to_str()
149-
.map(|x| Ok(format!("{}/", x.to_string())))
149+
.map(|x| Ok(format!("{x}/")))
150150
.unwrap_or(Err(anyhow!("can not parse path")))?;
151151
tracing::info!("prepare to cache_dir: {}", &prefix);
152152
let conf = self.conf.get_domain_cache_config(domain);
@@ -233,7 +233,7 @@ impl FileCache {
233233
.cloned();
234234
if let Some(v) = index_opt {
235235
result.insert(format!("{key_prefix}/"), v.clone());
236-
result.insert(key_prefix.to_string(), v);
236+
// result.insert(key_prefix.to_string(), v);
237237
}
238238
}
239239
None => {
@@ -242,7 +242,7 @@ 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());
245+
// result.insert("".to_string(), v.clone());
246246
result.insert("/".to_string(), v);
247247
}
248248
}

server/src/service.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub async fn create_service(
3535
domain_storage: Arc<DomainStorage>,
3636
is_https: bool,
3737
) -> Result<Response<Body>, Infallible> {
38-
let from_uri = req.uri().authority().cloned();
38+
let uri = req.uri();
39+
let from_uri = uri.authority().cloned();
3940
// trick, need more check
4041
let authority_opt = from_uri.or_else(|| {
4142
req.headers()
@@ -52,6 +53,7 @@ pub async fn create_service(
5253

5354
if let Some(authority) = authority_opt {
5455
let host = authority.host();
56+
5557
let service_config = service_config.get_domain_service_config(host);
5658
// cors
5759
let origin_opt = match resp_cors_request(req.method(), req.headers(), service_config.cors) {
@@ -67,15 +69,24 @@ pub async fn create_service(
6769
} else {
6870
format!(":{}", port)
6971
};
70-
let redirect_path = format!("https://{host}{port}{}", req.uri());
72+
let redirect_path = format!("https://{host}{port}{}", uri);
7173
resp.headers_mut()
7274
.insert(LOCATION, redirect_path.parse().unwrap());
7375
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
7476
return Ok(resp);
7577
}
7678
}
79+
// path: "" => "/"
80+
let path = uri.path();
81+
if domain_storage.check_if_empty_index(host, path) {
82+
let mut resp = Response::default();
83+
resp.headers_mut()
84+
.insert(LOCATION, format!("{path}/").parse().unwrap());
85+
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
86+
return Ok(resp);
87+
}
7788
// static file
78-
let mut resp = match get_cache_file(req.uri().path(), host, domain_storage).await {
89+
let mut resp = match get_cache_file(path, host, domain_storage).await {
7990
Ok(item) => {
8091
let headers = req.headers();
8192
let conditionals = Conditionals {

server/src/web_server.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ impl Server {
7272
http_redirect_to_https: domain
7373
.https
7474
.as_ref()
75-
.map(|x| x.http_redirect_to_https)
76-
.flatten()
75+
.and_then(|x| x.http_redirect_to_https)
7776
.or(default_http_redirect_to_https),
7877
};
7978

tests/tests/common.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,20 @@ pub async fn assert_files(
124124
get_file_text(domain, version, file).unwrap()
125125
);
126126
if file.is_empty() {
127-
println!("begin to check: {request_prefix}, version:{version}");
127+
println!("begin to check: {request_prefix}/, version:{version}");
128128
let result = client.get(request_prefix).send().await.unwrap();
129129
assert_eq!(result.status(), StatusCode::OK);
130130
assert_eq!(
131131
result.text().await.unwrap(),
132132
get_file_text(domain, version, file).unwrap()
133133
);
134+
println!("begin to check: {request_prefix}/, version:{version}");
135+
let result = client.get(format!("{request_prefix}/")).send().await.unwrap();
136+
assert_eq!(result.status(), StatusCode::OK);
137+
assert_eq!(
138+
result.text().await.unwrap(),
139+
get_file_text(domain, version, file).unwrap()
140+
);
134141
}
135142
}
136143
}

0 commit comments

Comments
 (0)