Sockets and Pipes: Connect with Haskell

Sockets and Pipes: Connect with Haskell

English | 2022 | 288 Pages | PDF | 10 MB

Sockets and Pipes is not an introduction to Haskell; it is an introduction to writing software in Haskell. Using a handful of everyday Haskell libraries, this book walks through reading the HTTP specification and implementing it to create a web server.

The content that eventually grew into this book began with the question: What exactly is a web server? A satisfactory answer that does not assume substantial background knowledge requires spanning quite a few areas of computing. Fortunately, they all serve as fruitful motivations for simultaneously learning about how to use Haskell, which is the larger objective of the Joy of Haskell collection.

The language a web server speaks is the Hypertext Transfer Protocol (HTTP), which this book explores in great detail while walking through the creation of a server from “scratch”. We encourage readers to follow along in reading the official definition of HTTP (RFC 7230 published by the Internet Engineering Task Force) as we implement the specification in Haskell. While high-level libraries make it possible to create web applications without detailed knowledge of HTTP, we believe that a full understanding of the underlying layers we build upon helps us use a platform more effectively. By studying HTTP we also gain an appreciation for what it is and is not good for, and for what applications we might stand to benefit from choosing a different network protocol instead.

This book is for Haskell learners who have some basic faculty with the language and are now ready to work up to a substantial project. We expect that you understand the basic syntax and can do things like:

  • write a case expression to pattern match over a sum type
  • sequence IO actions in a do block
  • use qualified imports
  • define datatypes
  • use GHCi
  • install Haskell libraries

From the base package, we assume some familiarity with:

  • types Maybe, Either, and []
  • classes Eq, Show, Monoid, Foldable, Functor, and Monad
  • We do not assume prior knowledge of any additional libraries or GHC language extensions.

What’s inside
Bytes and characters – The first several chapters introduce the bytestring and text libraries and are largely dedicated to tearing apart a traditional hello world program, looking underneath the abstract notion of “printing text” to start greeting the world in terms of writing bytes to a file handle. After discussing bytes, we need only a short hop to sockets, our means of writing bytes across great distances using the network library.

Encoding and parsers – First we encoding HTTP messages as byte strings. That’s the easy part; next, we go in the opposite direction and learn how to interpret byte strings using the attoparsec library. This will acquaint us even more closely with the HTTP message format.

Monad transformers – We introduce three Monad transformers that are especially applicable to our subject matter: ResourceT, ListT, and ExceptT. No prior experience with transformers is required. We do not linger on the general concept, preferring instead to focus on each of the three examples and to create familiarity with transformers and lifting chiefly by demonstration.

Resource safety – Use of ResourceT begins in chapter 1, and we use it throughout the book. This makes it a breeze to deal with files and sockets without resource leaks.

Streaming – To move past toy examples that fit easily into memory, we have to start writing streaming processes that can deal with large amounts of data by handling it in smaller pieces. All of the code within this book is written with memory usage in mind. ListT, the subject of chapter 12, provides an especially convenient facility for working with streams.

Error handling – As the amount of functionality of our server builds up, the number of possible error conditions starts to rise. Chapter 14 introduces ExceptT to work with errors in a clean and well-typed manner.