Sockets and Pipes
$40.00
Minimum price
$60.00
Suggested price

Sockets and Pipes

Connect with Haskell

About the Book

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 9110 and 9112 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.

Prerequisites

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 install GHC, use GHCi, write a case expression, sequence IO actions in a do block, use qualified imports, and define datatypes. From the base package, we assume some familiarity with Maybe, Either, [], 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 encode 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 some monad transformers that are especially applicable to our subject matter: ResourceT, Producer, ExceptT, and ReaderT. 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. Later on, we use newtype wrappers and DerivingVia to manage the complexity of larger monad stacks.

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. Producer, Pipe, and Consumer constitute an especially convenient facility for working with streams.

Error handling - As features accumulate, the number of possible error conditions starts to rise. We introduce ExceptT to work with errors in a clean and well-typed manner.

About the Author

Type Classes
Type Classes

Type Classes is Chris Martin and Julie Moronuki. We've been writing about Haskell together since 2018.

Table of Contents

  • 1 - Handles
    • The necessity of indirection
    • Writing to a file
    • Exceptions
    • Diligent cleanup
    • MonadIO
    • Exercises
  • 2 - Chunks
    • Packed characters
    • Reading from a file
    • Exercises
  • 3 - Bytes
    • Packed octets
    • Copying a file
    • Character encodings
    • Dangerous classes
    • Avoiding system defaults
    • Exercises
  • 4 - Sockets
    • Open up and connect
    • Extra details
    • Names and addresses
    • Address information
    • Exercises
  • 5 - HTTP
    • The specification
    • HTTP requests
    • ASCII strings
    • HTTP responses
    • Serving others
    • Exercises
  • 6 - HTTP types
    • The ASCII type
    • Request line
    • Status line
    • Fields
    • Body
    • Exercises
  • 7 - Encoding
    • String builders
    • Measuring time
    • Request and response
    • Higher-order encodings
    • The start line
    • Exercises
  • 8 - Responding
    • A measure of success
    • Response-building utilities
    • Integers
    • Response transmission
    • Exercises
  • 9 - Content types
    • Some common types
    • UTF-8
    • HTML
    • JSON
    • Exercises
  • 10 - Change
    • STM
    • Increment
    • Atomically
    • The counting server
    • Other STM topics
    • Exercises
  • 11 - Streaming
    • Chunked hello
    • Chunk types
    • Encoding a chunk
    • Transfer-Encoding
    • Serving the file
    • Exercises
  • 12 - Pipes
    • The new response type
    • What is a Producer
    • Constructing a response
    • Encoding a response
    • Sending a response
    • Exercises
  • 13 - Parsing
    • Encoding vs decoding
    • Attoparsec
    • Request line
    • Explaining what's wrong
    • Incremental parsing
    • Exercises
  • 14 - Errors
    • Status codes
    • Constructing error responses
    • Visibility in two places
    • Thread-safe logging
    • Either
    • ExceptT
    • Exercises
  • 15 - Reading fields
    • Alternatives and repetition
    • Accept-Language
    • Field parser
    • Field lookup
    • Content-Language
    • Exercises
  • 16 - Context
    • When it pays to be vague
    • ReaderT and deriving
    • Ask and you shall receive
    • A place for your files
    • Context initialization
    • Exercises
  • 17 - Reading the body
    • Getting
    • Putting
    • A new type of stream
    • Which kind of body?
    • Grabbing some input
    • Chunky
    • Finishing up
    • Exercises

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub