Skip to content

getHeader doesn't trim trailing OWS #292

@hvr

Description

@hvr

RFC7230 says (emphasis added):

A field value might be preceded and/or followed by optional whitespace (OWS); a single SP preceding the field-value is preferred for consistent readability by humans. The field value does not include any leading or trailing whitespace: OWS occurring before the first non-whitespace octet of the field value or after the last non-whitespace octet of the field value ought to be excluded by parsers when extracting the field value from a header field.

And yet, getHeader currently preserves any trailing optional whitespace (and doesn't even contain a stern warning about this in its Haddock docstring), which can easily result in subtle bugs when not taken into account when processing the field-value as returned by getHeader

I currently have to workaround this via helpers such as

-- | Helper to workaround 'getHeader' currently not stripping trailing WS
getHeaderTrimmed :: HasHeaders a => CI ByteString -> a -> Maybe ByteString
getHeaderTrimmed n rq = rtrim <$> getHeader n rq
  where
    rtrim = fst . BS.spanEnd isWS

    isWS 0x09 = True
    isWS 0x20 = True
    isWS _    = False

but it would be helpful in making getHeader less error-prone if it stripped trailing OWS by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions