Open
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 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.
let mime_html = "text/html".parse::<Mime>()?;
let mime_xhtml = "application/xhtml+xml".parse::<Mime>()?;
// 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::<Mime>().unwrap()
as they need, or are there more better ways?