Lesson 10: Interpreting request headers

Contents
  • Halving the problem
  • Discerning the body encoding
  • Reading a single header field
  • Running a parser
  • Parsing a Content-Length value
  • Exercise: Parsing a comma-separated list

In the previous lesson we wrote most of an Attoparsec parser for HTTP requests. We have one more piece of the parser to write:

messageBodyParser ::
    [HeaderField] -> Parser (Maybe MessageBody)

The parsers we’ve written so far have been fairly straightforward transcriptions of the grammar given by RFC 7230. Parsing a message body is more involved, because:

  • There are multiple ways to encode the body, and we need to interpret the message headers first to figure out which encoding was used in the particular message we’re parsing.
  • We have to begin considering ways in which a request can be invalid other than by being grammatically malformed (e.g., if a request contains more than one Content-Length header).

In this lesson we use the Either monad to handle failure and the traverse function to apply a function that might fail to each element in a list. As an exercise, you will write a parser for a comma-separated list with optionally quoted elements.

Sign up for access to the full page, plus the complete archive and all the latest content.