Skip to content

Commit

Permalink
Include Accept-Encoding header value in error message (ordinals#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 8, 2023
1 parent 86eaa7c commit a9af414
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,10 @@ impl Server {
if accept_encoding.is_acceptable(&content_encoding) {
headers.insert(header::CONTENT_ENCODING, content_encoding);
} else {
return Err(ServerError::NotAcceptable(
content_encoding.to_str().unwrap_or_default().to_string(),
));
return Err(ServerError::NotAcceptable {
accept_encoding,
content_encoding,
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/server/accept_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {super::*, axum::extract::FromRef};

#[derive(Default)]
pub(crate) struct AcceptEncoding(Option<String>);
#[derive(Default, Debug)]
pub(crate) struct AcceptEncoding(pub(crate) Option<String>);

#[async_trait::async_trait]
impl<S> axum::extract::FromRequestParts<S> for AcceptEncoding
Expand Down
29 changes: 22 additions & 7 deletions src/subcommand/server/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use super::*;
use {super::*, std::fmt::Write};

#[derive(Debug)]
pub(super) enum ServerError {
BadRequest(String),
Internal(Error),
NotAcceptable(String),
NotAcceptable {
accept_encoding: AcceptEncoding,
content_encoding: HeaderValue,
},
NotFound(String),
}

Expand All @@ -24,11 +27,23 @@ impl IntoResponse for ServerError {
)
.into_response()
}
Self::NotAcceptable(content_type) => (
StatusCode::NOT_ACCEPTABLE,
format!("inscription content type `{content_type}` is not acceptable"),
)
.into_response(),
Self::NotAcceptable {
accept_encoding,
content_encoding,
} => {
let mut message = format!(
"inscription content encoding `{}` is not acceptable.",
String::from_utf8_lossy(content_encoding.as_bytes())
);

if let Some(accept_encoding) = accept_encoding.0 {
write!(message, " `Accept-Encoding` header: {accept_encoding}").unwrap();
} else {
write!(message, " `Accept-Encoding` header not present").unwrap();
};

(StatusCode::NOT_ACCEPTABLE, message).into_response()
}
Self::NotFound(message) => (
StatusCode::NOT_FOUND,
[(header::CACHE_CONTROL, HeaderValue::from_static("no-store"))],
Expand Down
2 changes: 1 addition & 1 deletion tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ fn inscribe_can_compress() {
assert_eq!(response.status(), StatusCode::NOT_ACCEPTABLE);
assert_regex_match!(
response.text().unwrap(),
"inscription content type `br` is not acceptable"
"inscription content encoding `br` is not acceptable. `Accept-Encoding` header not present"
);

let client = reqwest::blocking::Client::builder()
Expand Down

0 comments on commit a9af414

Please sign in to comment.