A web server

data Socket recv :: Socket -> Int -> IO ByteString -- returns empty when the socket is closed sendAll :: Socket -> ByteString -> IO ()

data Request -- request headers, HTTP version, etc data Response -- status code, response headers, resposne body type Application = Request -> IO Response handleConn :: Application -> Socket -> IO ()

Parse the request line Parse the request headers Construct the Request value Pass Request to the Application and get back a Response Send the Response over the Socket

data RequestLine = RequestLine ByteString ByteString ByteString parseRequestLine :: Socket -> IO RequestLine parseRequestLine socket = do bs <- recv socket 4096 let (method:path:version:ignored) = S8. words bs return $ RequestLine method path version

import Data.ByteString ( ByteString ) import qualified Data.ByteString as S import Data.Conduit import qualified Data.Conduit.Binary as CB import qualified Data.Conduit.List as CL data RequestLine = RequestLine ByteString ByteString ByteString parseRequestLine :: Sink ByteString IO RequestLine parseRequestLine = do let space = toEnum $ fromEnum ' ' let getWord = do CB. dropWhile (== space) bss <- CB. takeWhile (/= space) =$ CL. consume return $ S. concat bss method <- getWord path <- getWord version <- getWord return $ RequestLine method path version