Skip to content

Commit

Permalink
Set Cache-Control: no-store header on 404 responses (ordinals#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifofifoX committed Nov 7, 2023
1 parent 8e8449b commit 37ba32b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,19 @@ mod tests {
);
}

#[test]
fn error_content_responses_have_max_age_zero_cache_control_headers() {
let server = TestServer::new_with_regtest();
let response =
server.get("/content/6ac5cacb768794f4fd7a78bf00f2074891fce68bd65c4ff36e77177237aacacai0");

assert_eq!(response.status(), 404);
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"no-store"
);
}

#[test]
fn inscriptions_page_with_no_prev_or_next() {
TestServer::new_with_regtest_with_index_sats().assert_response_regex(
Expand Down
9 changes: 7 additions & 2 deletions src/subcommand/server/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(super) type ServerResult<T> = Result<T, ServerError>;
impl IntoResponse for ServerError {
fn into_response(self) -> Response {
match self {
Self::BadRequest(message) => (StatusCode::BAD_REQUEST, message).into_response(),
Self::Internal(error) => {
eprintln!("error serving request: {error}");
(
Expand All @@ -21,8 +22,12 @@ impl IntoResponse for ServerError {
)
.into_response()
}
Self::NotFound(message) => (StatusCode::NOT_FOUND, message).into_response(),
Self::BadRequest(message) => (StatusCode::BAD_REQUEST, message).into_response(),
Self::NotFound(message) => (
StatusCode::NOT_FOUND,
[(header::CACHE_CONTROL, HeaderValue::from_static("no-store"))],
message,
)
.into_response(),
}
}
}
Expand Down

0 comments on commit 37ba32b

Please sign in to comment.