Hi Michael,
Seeing as you thought my puzzle would be useful to the community, I thought that by the same logic you might want to update that sample you posted with the next thing I had to bust my brain over. parseRequestBody gave me a bit of a headache cos the only sample I could find was out of date and used something called lbsSink. I'm sure you'd want to tidy it up first...
I'm also a bit worried about those (toHtml.show)s near the bottom.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wai
import Network.HTTP.Types
import Network.HTTP.Types.Header (hContentType, hContentLength, hConnection)
import Network.Wai.Handler.Warp (run)
import Blaze.ByteString.Builder (fromByteString, fromLazyByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Text
import qualified Text.Blaze.Html5 as H (form)
--import qualified Text.Blaze.Html5.Attributes as A
import Text.Blaze.Html5
import Text.Blaze.Html5.Attributes
import Text.Blaze.Html.Renderer.Utf8
import qualified Data.ByteString.Lazy as LB
import System.Environment (getEnv)
import Network.Wai.Parse (parseRequestBody, lbsBackEnd)
application:: Application
application req =
parseRequestBody lbsBackEnd req >>= \(b,_) ->
let (code, html) = app ((requestMethod req),(pathInfo req),(queryString req),b) in
return $ ResponseBuilder
code
[ (hContentType, "text/html"),
(hConnection, "keep-alive")]
$ renderHtmlBuilder html
app x = case x of
("GET", url, [], _) -> (status200, aform url)
("GET", url, qs, _) -> (status200, gform qs)
("POST", url, _, hsm) -> (status200, pform hsm)
aform :: [Text] -> Html
aform url = docTypeHtml $ do
body $ do
h3 "GETting form"
H.form ! name "fooform" ! method "get" ! action "/in" $
( mapM_ (\n -> (toHtml n) >> input ! name (toValue n) ! type_ "text" >> br ) url )
>> input ! type_ "submit" ! value "Submit"
h3 "POSTting form"
H.form ! name "fooform" ! method "post" ! action "/in" $
( mapM_ (\n -> (toHtml n) >> input ! name (toValue n) ! type_ "text" >> br ) url )
>> input ! type_ "submit" ! value "Submit"
gform :: [QueryItem] -> Html
gform qs = docTypeHtml $
body $ do
h3 "GETted form"
mapM_ (\(n,mv) -> "The " >> ((toHtml.show) n) >> " is " >> maybe "" (toHtml.show) mv >> br) qs
pform :: [(BS.ByteString, BS.ByteString)] -> Html
pform hs = docTypeHtml $
body $ do
h3 "POSTed form"
mapM_ (\(n,v) -> "The " >> ((toHtml.show) n) >> " is " >> (toHtml.show) v >> br) hs
main:: IO ()
main = getEnv "PORT" >>= flip run application . read
Adrian.