Skip to content

Commit

Permalink
refactor: adjust redirect for admin prefix path
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 27, 2024
1 parent 34028d0 commit 761f043
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/http_extra/http_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ impl HttpResponse {
..Default::default()
}
}
/// Create a redirect response with `302` status.
pub fn redirect(location: &str) -> pingora::Result<Self> {
let value = http::HeaderValue::from_str(location).map_err(|e| {
error!(error = e.to_string(), "to header value fail");
util::new_internal_error(500, e.to_string())
})?;
Ok(Self {
status: StatusCode::TEMPORARY_REDIRECT,
headers: Some(vec![
(http::header::LOCATION.clone(), value),
HTTP_HEADER_NO_CACHE.clone(),
]),
..Default::default()
})
}

#[cfg(feature = "full")]
/// Create a text response with `no-cache` cache-control.
Expand Down
14 changes: 13 additions & 1 deletion src/plugin/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,16 @@ impl TryFrom<&PluginConf> for AdminServe {
}
})?;
}
let mut path = get_str_conf(value, "path");
if path.len() > 1 && path.ends_with("/") {
path = path.substring(0, path.len() - 1).to_string();
}

let params = AdminServe {
hash_value,
max_age,
plugin_step: get_step_conf(value),
path: get_str_conf(value, "path"),
path,
ip_fail_limit: TtlLruLimit::new(
512,
Duration::from_secs(5 * 60),
Expand Down Expand Up @@ -508,6 +512,14 @@ impl Plugin for AdminServe {
let path = header.uri.path();
let mut new_path =
path.substring(self.path.len(), path.len()).to_string();
if new_path.is_empty() {
new_path = format!("{path}/");
if let Some(query) = header.uri.query() {
new_path = format!("{new_path}?{query}");
}
let resp = HttpResponse::redirect(&new_path)?;
return Ok(Some(resp));
}
if let Some(query) = header.uri.query() {
new_path = format!("{new_path}?{query}");
}
Expand Down

0 comments on commit 761f043

Please sign in to comment.