Sockets and Pipes
Sockets and Pipes
$25.00
Minimum price
$35.00
Suggested price
Sockets and Pipes

This book is 30% complete

Last updated on 2020-05-14

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.

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, read code that uses qualified imports, use GHCi, and install Haskell libraries. We do not assume prior knowledge of any particular libraries or GHC language extensions.

The first several chapters introduce the 'bytestring', 'text', and 'network' 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. From there, we need only a short hop to sockets, our means of writing bytes across great distances.

The language a web server speaks is the Hypertext Transfer Protocol (HTTP), which this book explores in great detail. 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. Most parts of the code we present will fall into one of two categories:

  1. the "pure" functions for encoding and decoding HTTP messages, which will closely acquaint us with the HTTP message format and the 'bytestring' and 'attoparsec' libraries;
  2. the "operational" aspects of the server: accepting connections with the 'network' library, forking threads, handling exceptions, and streaming data using the 'pipes' library.

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.

About the Author

Type Classes
Type Classes

Julie Moronuki wrote an extensive book on Haskell while leading a thriving Haskell meetup in Austin. She has a master’s degree in linguistics and taught English, English as a second language, philosophy, linguistics, and medical transcription for over ten years. When she is not writing Haskell or writing about Haskell, she homeschools her two children, pickles everything in sight, and adopts too many pets.

Chris Martin has been writing software for a decade, most recently as a blockchain engineer for Hijro and previously as cofounder and CTO of Fold. He has a master’s degree in computer science from Georgia Tech, and has worked for Georgia Tech Research Institute. Chris tries to increase public awareness of Haskell, NixOS, and the power of good documentation. He enjoys barefoot running, sour beers, and arguing with Julie about math.

Together they run Typeclass Consulting.

Table of Contents

  • 1 - Handles
    • The necessity of indirection
    • Writing to a file
    • Diligent cleanup
    • Exercises
  • 2 - Chunks
    • Packed characters
    • Reading from a file, one chunk at a time
    • Exercises
  • 3 - Bytes
    • Packed octets
    • Copying a file
    • Character encodings
    • The Show and IsString classes
    • Avoiding system defaults
    • Exercises
  • 4 - Sockets
    • Open up and connect
    • Names and addresses
    • Exercises
  • 5 - HTTP
    • The specification
    • HTTP requests
    • ASCII strings
    • HTTP responses
    • Serving others
    • Exercises
  • Planned chapters:
    • 6 - HTTP types
    • 7 - Response encoding
    • 8 - Constructing and using Responses
    • 9 - Content length
    • 10 - Chunking the message body
    • 11 - The never-ending response
    • 12 - Parsing requests
    • 13 - Interpreting request headers
    • 14 - Body parsing
    • 15 - Connection reuse
    • 16 - Pipes

Authors have earned$8,970,574writing, publishing and selling on Leanpub,
earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

Learn more about writing on Leanpub

The Leanpub 45-day 100% Happiness Guarantee

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

See full terms

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), EPUB (for phones and tablets) and MOBI (for 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. It really is that easy.

Learn more about writing on Leanpub