bugger.
All challenges/Pythoneasythe-charset-without-a-value-v1
THE INVESTIGATION

The charset without a value

A malformed metadata flag becomes a boolean that crashes string processing.

Requests & BoundariesParsingMissing values

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

01 Bare charset is not a usable value
["text/html; charset"]
[{"media":"text/html","params":{},"encoding":"ISO-8859-1"}]
02 Explicit charset control
["text/plain; charset=utf-8"]
[{"media":"text/plain","params":{"charset":"utf-8"},"encoding":"utf-8"}]
03 JSON fallback
["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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.