pistache: Accept header parsing is locale-dependent, fails for valid values
My default Firefox Accept
header looks like this: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
.
I suspect it’s similar for a lot of people.
Pistache fails to parse the header, because of the q=0.9
(and q=0.8
for the matter) quality factor.
Stepping through the debugger, I traced the problem here: https://github.com/pistacheio/pistache/blob/master/src/common/mime.cc#L243
match_double
internally calls strtod
, which is locale dependent. My locale has ,
as a decimal separator, which means strtod
only consumes the “0” of “0.9”, leaving the “.” as the next token, and thus breaking the parser, resulting in the “Unfinished Media Type parameter”.
The solution to this would be parsing the quality factor in a locale-independent way, as seems correct (see https://httpwg.org/specs/rfc7231.html#rfc.section.5.3.1).
To do so, I propose getting inspiration from g++ stdlib here: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B17/floating_from_chars.cc#L331, the underlying implementation of C++17’s from_chars
(i.e. temporarily changing the locale before parsing, and restoring it after).
I’d be happy to provide the fix if the solution seems ok.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (9 by maintainers)
Commits related to this issue
- fix(mime): ignore locales when parsing qvalues Qvalues are now parsed manually in str_to_qvalue() instead of using the locale-dependant std::strtof. Using a custom function allowed me to add some mo... — committed to pistacheio/pistache by Tachi107 2 years ago
- fix(mime): ignore locales when parsing qvalues Qvalues are now parsed manually in str_to_qvalue() instead of using the locale-dependant std::strtof. Using a custom function allowed me to add some mo... — committed to pistacheio/pistache by Tachi107 2 years ago
Why not using
std::from_chars
directly? We target C++17