http-rs / http-rs/http-types

UTF-8 mime-type constants don't work well with browsers' `Accept` header

Open
#371 2 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
Rust
Stars
209
Forks
91
PR merge metrics
No merged PRs in 30d

Description

Some of predefined text mime-types (such as `mime::HTML` and `mime::XML`) have `;charset=utf-8` parameter, but browsers' default `Accept` header does not.
(See [List of default Accept values - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values) for browsers' defaults.)

When servers (written in Rust) uses `mime::{HTML, XML, ...}` to represent available content types, `content::Accept::negotiate()` does not work well with browsers' default `Accept`, and it fails with "No suitable Content-Type found" error.

```rust
let mime_html = "text/html".parse::()?;
let mime_xhtml = "application/xhtml+xml".parse::()?;

// This is simple version of browser's `Accept` value.
let mut browser_accept = Accept::new();
browser_accept.push(MediaTypeProposal::new(mime_html, None)?);
browser_accept.push(MediaTypeProposal::new(mime_xhtml, None)?);

// This is server's default.
let acceptable = &[mime::HTML];

let res = Response::new(200);
let content_type = browser_accept.negotiate(acceptable);

// I expected this to success, but fails!
assert!(
content_type.is_ok(),
"server is expected to return HTML content"
);
```

So, the questions are:

* Should difference of parameters' existence cause negotiation to fail?
* Is UTF-8 MIME constants (`mime::{JAVASCRIPT, CSS, HTML, PLAIN, XML}` as of http-types 2.11.1) intended to be used by servers?
+ If so, how they make negotiation succeed when requests are from browsers?
+ If not, are servers recommended to execute `"text/html".parse::().unwrap()` as they need, or are there more better ways?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.