Skip to content

Instantly share code, notes, and snippets.

@AaronC81
Created September 16, 2016 06:54
Show Gist options
  • Save AaronC81/f5d22b5e3955104381129e875146c22f to your computer and use it in GitHub Desktop.
Save AaronC81/f5d22b5e3955104381129e875146c22f to your computer and use it in GitHub Desktop.
HTTP server using Network.Socket
{-# LANGUAGE RecordWildCards #-}
import Control.Monad
import qualified Data.ByteString as B
import Network.HTTP
import Network.Socket
import Network.URI
main = do
lsock <- socket AF_INET Stream defaultProtocol
bind lsock (SockAddrInet 8080 iNADDR_ANY)
listen lsock 1
forever $ do
(csock, _) <- accept lsock
hs <- socketConnection "" 8080 csock
req <- receiveHTTP hs
case req of
Left _ -> error "Receiving request failed"
Right (Request {..}) -> if uriPath rqURI == "/"
then do
respondHTTP hs $
Response (2,0,0) "OK" [] "Hello HTTP"
Network.HTTP.close hs
else do
respondHTTP hs $
Response (4,0,4) "Not found" [] "Nothing here"
Network.HTTP.close hs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment