Skip to content

Conversation

c960657
Copy link
Contributor

@c960657 c960657 commented Oct 4, 2024

Some servers ignore the Accept header. It usually works fine for static files, but not dynamically generated responses.

This PR adds a new feature which compares the response Content-Type header to the request Accept header and generates a synthetic 406 Not Acceptable response if they don't match.

@ixti
Copy link
Member

ixti commented Nov 22, 2024

IMO in this case it's better raise UnacceptableError. Or even make that logic configurable:

HTTP.use(acceptable: { mime_type: "...", on_mismatch: :raise })
  • mime_type - if request does not have Accept header - it will inject one
  • on_mismatch
    • :raise - fails with UnacceptableError
      • UnacceptableError#response - will contain original response
    • :rewrite_status - rewrites response with new status

@c960657
Copy link
Contributor Author

c960657 commented Feb 6, 2025

Sorry about my late reply. I somehow missed your comments here.

I created this PR (about the Acceptable feature) and PR #792 (about the RaiseError feature) together. My idea was that two could be used together or separately, i.e. you could achieve your suggested mix of exceptions vs. rewrite by combining these two features rather than creating one feature with a lot of options.

This doesn't support a dedicated UnacceptableError exception as you suggest, though RaiseError could easily be extended to raise specified exceptions based on the HTTP status code (e.g. NotFoundError, ForbiddenError, InternalServerErrorError etc).

What do you think of that?

@ixti ixti added this to the v6.0.0 milestone Jun 30, 2025
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