The charset without a value
A malformed metadata flag becomes a boolean that crashes string processing.
What’s going wrong
A response decoder crashes on a Content-Type parameter with no equals sign. Valid headers work. Fix the parser at the boundary rather than hiding the crash in the decoder.
Stdin is an array of simplified Content-Type strings. The first semicolon-delimited token is a nonempty media type. Normalize that token to lowercase and trim surrounding whitespace. Remaining tokens are parameters: trim whitespace, split at the FIRST equals sign, lowercase the trimmed name, and trim whitespace then surrounding single/double quote characters from the value. Ignore empty tokens and parameters without an equals sign. Values contain no semicolons; quote escape syntax is excluded. Later valid occurrences overwrite earlier values. Empty explicit values are valid and distinct from missing values.
Return an array of objects containing media, params, and encoding. A present charset supplies encoding after whitespace trimming, even if empty. Otherwise use ISO-8859-1 for text/*, UTF-8 for application/json, and null for other media types. No response bytes are decoded.
Expected behavior
Preserve type invariants by distinguishing malformed, missing and explicitly empty metadata.
Reproduction
["text/html; charset"]
[{"media":"text/html","params":{},"encoding":"ISO-8859-1"}]["text/plain; charset=utf-8"]
[{"media":"text/plain","params":{"charset":"utf-8"},"encoding":"utf-8"}]["application/json"]
[{"media":"application/json","params":{},"encoding":"UTF-8"}]Before you begin
- Build on byte lengths: validate representation before a downstream consumer assumes its type.
- Python strings, lists and dictionaries
- Read the report contract before changing code
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.