Lesson 11: Body parsing

Contents
  • Parsers that don’t parse anything
  • Parsing given a content length
  • Parsing a chunked body
  • Function server
  • UTF-8 responses
  • Echo example
  • Exercise 1: Introducing I/O
  • Exercise 2: Keeping state

In this lesson we fill in the final pieces to complete the Request parser, and then we begin putting it to work. We’re going to touch the sockets again; this time in addition to writing to sockets we’ll be reading from them.

The function we left unwritten in the last lesson was:

parserForBodyEncoding
    :: Either String (Maybe BodyEncoding)
    -> Parser (Maybe MessageBody)

There are four cases to consider when we write a function whose argument is the result of the body encoding analysis function we wrote in the last lesson:

parserForBodyEncoding x =
    case x of
        Left err                       -> _   -- 1
        Right Nothing                  -> _   -- 2
        Right (Just (ContentLength n)) -> _   -- 3
        Right (Just Chunked)           -> _   -- 4

    <?> "message-body"
  1. The analysis failed and produced an error message.
  2. There is no body to parse.
  3. A Content-Length header indicated some number of bytes to read.
  4. A Transfer-Encoding header indicated that the body is chunked.

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