http-rs / http-types

Common types for HTTP operations
https://docs.rs/http-types
Apache License 2.0
200 stars 83 forks source link

Add `content::{Accept, ContentType}` headers #270

Closed yoshuawuyts closed 3 years ago

yoshuawuyts commented 3 years ago

Adds the content::{Accept, ContentType} headers, implementing content negotation for media types. Supersedes https://github.com/http-rs/http-types/pull/269 which only implemented content::ContentType. Ref #99. Thanks!

yoshuawuyts commented 3 years ago

@Fishrock123 To me it sounds like the features you're suggesting may be out of scope for this PR, even if they seem useful. This PR introduces two typed headers only:

Both headers implement the full spec, and in addition to that we also include the content negotiation algorithm to actually make Accept useful.

Your suggestions around mime-db likely best be implemented as extensions to our existing http_types::mime::Mime implementation. Mime already has some capabilities to infer the type either by reading magic bytes (Mime::sniff) or by attempting to guess the mime from the file extension (Mime::from_extension).

We also already have a direct "create a mime from a file" conversion available through (Body::from_file), which applies both the from_extension and sniff methods to determine the file type. Finally Mime also has a Display impl that can be used to convert it back out to a string. Though a Mime -> file extension conversion may indeed be useful.

In short: I think your suggestions are good, but they don't need to block this PR. I've filed separate issues to track what's not been implemented so we can remember to implement them later:

Fishrock123 commented 3 years ago

I don't think we should merge this until the API for content-type is flushed out more.

Ah, well, this and the issues from #274 are what I meant by this - i.e. that we have at least made an effort to think about it.