Sockets and Pipes

Sockets and Pipes
出版时间:2022.9
官网链接:Leanpub
下载地址:百度网盘(PDF)

内容简介:

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.

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.

给TA买糖
共{{data.count}}人
人已赞赏
电子书

Streaming Data Mesh

2023-5-22 19:34:13

电子书

Strategic Monoliths and Microservices

2023-5-23 0:55:45

搜索