second-transfer-0.5.3.2: Second Transfer HTTP/2 web server

Safe HaskellNone
LanguageHaskell2010

SecondTransfer.Utils.HTTPHeaders

Contents

Description

Utilities for working with headers.

Synopsis

Simple manipulation

These transformations are simple enough that don't require going away from the list representation (see type Headers)

lowercaseHeaders :: Headers -> Headers Source

HTTP headers are case-insensitive, so we can use lowercase versions everywhere

headersAreLowercase :: Headers -> Bool Source

Checks that headers are lowercase

fetchHeader :: Headers -> ByteString -> Maybe ByteString Source

Looks for a given header

Transformations based on maps

Many operations benefit from transforming the list to a map with custom sorting and doing a set of operations on that representation.

data HeaderEditor Source

Abstract data-type. Use fromList to get one of these from Headers. The underlying representation admits better asymptotics.

Introducing and removing the HeaderEditor

fromList :: Headers -> HeaderEditor Source

O(n*log n) Builds the editor from a list.

toList :: HeaderEditor -> Headers Source

O(n) Takes the HeaderEditor back to Headers

Access to a particular header

headerLens :: ByteString -> Lens' HeaderEditor (Maybe ByteString) Source

headerLens header_name represents a lens into the headers, and you can use it then to add, alter and remove headers. It uses the same semantics than replaceHeaderValue

replaceHeaderValue :: HeaderEditor -> ByteString -> Maybe ByteString -> HeaderEditor Source

replaceHeaderValue headers header_name maybe_header_value looks for header_name. If header_name is found and maybe_header_value is nothing, it returns a new headers list with the header deleted. If header_name is found and header_value is Just new_value, it returns a new list with the header containing the new value. If header_name is not in headers and maybe_header_value is Nothing, it returns the original headers list. If header_name is not in headers and maybe_header_value is Just new_value, it returns a new list where the last element is (header_name, new_value)

HTTP utilities

replaceHostByAuthority :: HeaderEditor -> HeaderEditor Source

Replaces a "host" HTTP/1.1 header by an ":authority" HTTP/2 header. The list is expected to be already in lowercase, so nothing will happen if there the header name portion is "Host" instead of "host".

Notice that having a Host header in an HTTP/2 message is perfectly valid in certain circumstances, check Section 8.1.2.3 of the spec for details.

introduceDateHeader :: HeaderEditor -> IO HeaderEditor Source

Given a header editor, introduces a Date header. This function has a side-effect: to get the current time