]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/Multipart.hs
Lucu 0.7
[Lucu.git] / examples / Multipart.hs
1 import qualified Data.ByteString.Lazy.Char8 as L8
2 import Data.Maybe
3 import Network.HTTP.Lucu
4
5 main :: IO ()
6 main = let config    = defaultConfig { cnfServerPort = "9999" }
7            resources = mkResTree [ ([], resMain) ]
8        in
9          do putStrLn "Access http://localhost:9999/ with your browser."
10             runHttpd config resources []
11
12
13 resMain :: ResourceDef
14 resMain 
15     = ResourceDef {
16         resUsesNativeThread = False
17       , resIsGreedy         = False
18       , resGet
19           = Just $ do setContentType $ read "text/html"
20                       output ("<title>Multipart Form Test</title>" ++
21                               "<form action=\"/\" method=\"post\" enctype=\"multipart/form-data\">" ++
22                               "  Upload some file:" ++
23                               "  <input type=\"text\" name=\"text\">" ++
24                               "  <input type=\"file\" name=\"file\">" ++
25                               "  <input type=\"submit\" value=\"Submit\">" ++
26                               "</form>")
27       , resHead   = Nothing
28       , resPost
29           = Just $ do form <- inputForm defaultLimit
30                       let text     = fromMaybe L8.empty $ fmap fdContent $ lookup "text" form
31                           file     = fromMaybe L8.empty $ fmap fdContent $ lookup "file" form
32                           fileName = fdFileName =<< lookup "file" form
33                       setContentType $ read "text/plain"
34                       outputChunk ("You entered \"" ++ L8.unpack text ++ "\".\n")
35                       outputChunk ("You uploaded a " ++ show (L8.length file) ++ " bytes long file.\n")
36                       output ("The file name is " ++ show fileName ++ ".\n")
37       , resPut    = Nothing
38       , resDelete = Nothing
39       }