Skip to content

Commit

Permalink
fix: the development server was unable to correctly consume the "publ…
Browse files Browse the repository at this point in the history
…icPath" configuration (#1334)
  • Loading branch information
huanyu.why committed Jul 10, 2024
1 parent e55756d commit 8630d60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
12 changes: 9 additions & 3 deletions crates/mako/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ impl DevServer {
) -> Result<hyper::Response<Body>> {
let mut path = req.uri().path().to_string();
let public_path = &context.config.public_path;
// public_path should be ended with "/", so we can replace it with "/"
// and we don't handle the public_path which start with http protocol
if !public_path.is_empty() {
path = process_req_url(public_path, &path);
path = match process_req_url(public_path, &path) {
Ok(p) => p,
Err(_) => {
return Ok(hyper::Response::builder()
.status(hyper::StatusCode::BAD_REQUEST)
.body(hyper::Body::from("Bad Request"))
.unwrap());
}
};
}
let path_without_slash_start = path.trim_start_matches('/');
let not_found_response = || {
Expand Down
33 changes: 15 additions & 18 deletions crates/mako/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ impl ParseRegex for Option<String> {
}
}

pub fn process_req_url(public_path: &str, req_url: &str) -> String {
let mut public_path = public_path.to_string();
if !public_path.starts_with('/') {
public_path = format!("/{}", public_path);
}
if !public_path.ends_with('/') {
public_path.push('/');
}
pub fn process_req_url(public_path: &str, req_url: &str) -> Result<String> {
let public_path = format!("/{}/", public_path.trim_matches('/'));
if req_url.starts_with(&public_path) {
return req_url[public_path.len() - 1..].to_string();
return Ok(req_url[public_path.len() - 1..].to_string());
}
req_url.to_string()
Ok(req_url.to_string())
}

#[cfg(test)]
Expand All @@ -53,29 +47,32 @@ mod tests {
#[test]
fn test_process_req_url() {
assert_eq!(
process_req_url("/public/", "/public/index.html"),
process_req_url("/public/", "/public/index.html").unwrap(),
"/index.html"
);
assert_eq!(
process_req_url("public/", "/public/index.html").unwrap(),
"/index.html"
);
assert_eq!(
process_req_url("public/", "/public/index.html"),
process_req_url("/public/foo/", "/public/foo/index.html").unwrap(),
"/index.html"
);
assert_eq!(
process_req_url("/public/foo/", "/public/foo/index.html"),
process_req_url("public/foo/", "/public/foo/index.html").unwrap(),
"/index.html"
);
assert_eq!(process_req_url("/", "/index.html").unwrap(), "/index.html");
assert_eq!(
process_req_url("public/foo/", "/public/foo/index.html"),
process_req_url("/#/", "/#/index.html").unwrap(),
"/index.html"
);
assert_eq!(process_req_url("/", "/index.html"), "/index.html");
assert_eq!(process_req_url("/#/", "/#/index.html"), "/index.html");
assert_eq!(
process_req_url("/公共路径/", "/公共路径/index.html"),
process_req_url("/公共路径/", "/公共路径/index.html").unwrap(),
"/index.html"
);
assert_eq!(
process_req_url("公共路径/", "/公共路径/index.html"),
process_req_url("公共路径/", "/公共路径/index.html").unwrap(),
"/index.html"
);
}
Expand Down

0 comments on commit 8630d60

Please sign in to comment.