Skip to content

Instantly share code, notes, and snippets.

@aaronlevin
Last active June 27, 2017 16:37

Revisions

  1. aaronlevin revised this gist Nov 30, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion minimal-http.hs
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,6 @@ application req continuation =
    _ ->
    continuation (responseLBS status401 [] "ERROR")


    main :: IO ()
    main = run 8080 application

  2. aaronlevin created this gist Nov 30, 2016.
    48 changes: 48 additions & 0 deletions minimal-http.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    {-# LANGUAGE OverloadedStrings #-}

    module Main where

    import Network.Wai (pathInfo, Request, requestMethod, Response, responseLBS, ResponseReceived)
    import Network.Wai.Handler.Warp (run)
    import Network.HTTP.Types (status200, status401)

    -- note: type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
    application :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
    application req continuation =
    let method = requestMethod req
    path = pathInfo req
    in case (method, path) of
    ("GET", ["hello" , "world"]) ->
    continuation (responseLBS status200 [] "!")
    _ ->
    continuation (responseLBS status401 [] "ERROR")


    main :: IO ()
    main = run 8080 application

    {-
    $ curl -iXGET 'localhost:8080/hello/world'
    HTTP/1.1 200 OK
    Transfer-Encoding: chunked
    Date: Wed, 30 Nov 2016 10:33:23 GMT
    Server: Warp/3.2.8
    !
    $ curl -iXGET 'localhost:8080/hello/wd'
    HTTP/1.1 401 Unauthorized
    Transfer-Encoding: chunked
    Date: Wed, 30 Nov 2016 10:33:29 GMT
    Server: Warp/3.2.8
    ERROR
    $ curl -iXGET 'localhost:8080/hel'
    HTTP/1.1 401 Unauthorized
    Transfer-Encoding: chunked
    Date: Wed, 30 Nov 2016 10:33:34 GMT
    Server: Warp/3.2.8
    ERROR
    -}