Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@NoraCodes
Copy link
Contributor

In order to support custom headers for various response types, this commit adds a wrapper type, ResponseWrapper, which can service all types of response in bin.

For paste objects, the preferred Last-Modified is used, so that caches can compare their exact timings with the HEAD response when revalidating.

For static objects, an ETag is used instead, based on the Cargo version and git hash of the codebase at compilation time; a build.rs is used for this.


  • Cargo Format

    • Run cargo fmt on the project.
  • Clippy lints

    • Run cargo clippy -- -Dwarnings on the project to check for suggestions
    • Run cargo clippy --fix to let clippy apply the suggestions itself, if any.

In order to support custom headers for various response types,
  this commit adds a wrapper type, ResponseWrapper, which can service
  all types of response in `bin`.

For paste objects, the preferred `Last-Modified` is used, so that caches
  can compare their exact timings with the HEAD response when
  revalidating.

For static objects, an `ETag` is used instead, based on the Cargo version
  and git hash of the codebase at compilation time; a `build.rs` is used
  for this.
Comment on lines +26 to +27
static BINARY_ETAG: Lazy<String> =
Lazy::new(|| sha256::digest(BINARY_VERSION));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went on searching for a library which provides a constant function to generate sha256 digests.
The closest I got was https://github.com/saleemrashid/sha2-const, but it brings in more weight than we would have after replacing it with both once-cell and sha256.

Comment on lines +39 to +78
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o>
for ResponseWrapper<R>
{
fn respond_to(self, request: &'r Request<'_>) -> Result<'o> {
use ResponseWrapper::*;

// Add global headers.
let mut response = Response::build();
response.raw_header("Server", crate::SERVER_VERSION);

// Handle individual request types.
match self {
MetaInterfaceResponse(sup) => response
.join(sup.respond_to(request)?)
.raw_header("ETag", &*crate::BINARY_ETAG)
.ok(),
PasteContentResponse(sup, modified) => response
.join(sup.respond_to(request)?)
.raw_header("Last-Modified", http_strftime(modified))
.ok(),
Redirect(sup) => response.join(sup.respond_to(request)?).ok(),
NotFound(s) => {
let body = format!("Unable to find entity '{}'", s);

response
.sized_body(body.len(), Cursor::new(body))
.status(Status::NotFound)
.ok()
}

ServerError(s) => {
let body = format!("Server error: '{}'", s);
response
.sized_body(body.len(), Cursor::new(body))
.status(Status::InternalServerError)
.ok()
}
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat. It would prove useful in being versatile to future changes.

@wantguns
Copy link
Owner

wantguns commented Feb 5, 2022

Thanks for the PR !
It surely sets a strong foundation for any upcoming changes. I am gonna merge this as is, those comments were more like notes.

@wantguns wantguns merged commit 2ab7ddb into wantguns:master Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants