Skip to content

Commit

Permalink
implement custom not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Termack committed Jun 10, 2024
1 parent ad6bb43 commit 6e01248
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ tls_active: true
ssl_certificate: jequi/test/leaf-cert.pem
ssl_key: jequi/test/leaf-cert.key
static_files_path: "test/"
not_found_file_path: test/notfound
go_library_path: "./jequi_go.so"
host:
jequi.com:
Expand Down
1 change: 1 addition & 0 deletions plugins/jequi_proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl Config {
let client = Client::builder().build::<_, Body>(https);
let request = request_builder.body(body).unwrap();
let response = client.request(request).await.unwrap();
resp.status = response.status().as_u16() as usize;
resp.headers = response.headers().clone();
resp.write_body(&body::to_bytes(response.into_body()).await.unwrap())
.unwrap();
Expand Down
9 changes: 8 additions & 1 deletion plugins/jequi_serve_static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub struct Config {
pub static_files_path: Option<PathKind>,
#[derivative(Default(value = "true"))]
pub infer_content_type: bool,
pub not_found_file_path: Option<PathBuf>,
config_path: Option<String>,
}

Expand Down Expand Up @@ -126,6 +127,11 @@ impl Config {
}
Err(_) => {
resp.status = 404;
if let Some(not_found_path) = self.not_found_file_path.as_ref() {
if let Ok(content) = std::fs::read(not_found_path) {
resp.write_body(&content).unwrap();
}
}
return;
}
};
Expand Down Expand Up @@ -209,6 +215,7 @@ mod tests {

let mut conf = Config {
static_files_path: Some(PathKind::Dir(TEST_PATH.into())),
not_found_file_path: Some(format!("{}notfound.html", TEST_PATH).into()),
..Default::default()
};

Expand Down Expand Up @@ -239,7 +246,7 @@ mod tests {
test_handle_request(&conf, &mut http, "/noperm", 403, b"");

// Notfound test
test_handle_request(&conf, &mut http, "/notfound", 404, b"");
test_handle_request(&conf, &mut http, "/notfound", 404, b"not found\n");

// Uri config test
conf.config_path = Some("/uri".to_string());
Expand Down
1 change: 1 addition & 0 deletions plugins/jequi_serve_static/test/notfound.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not found
1 change: 1 addition & 0 deletions test/notfound
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not found :/

0 comments on commit 6e01248

Please sign in to comment.