on master help sign in
Fixed rpath and chunked enum
commit1354eb4d01ec318ee29ee64b89f2967d8849ea00 authorMichael Snoyman <michael@snoyman.com> dateFri 14 Jan 2011, 12:56 (16 years ago) parent4030c17
2 files changed+63−9
Network/Wai/Handler/Warp.hs+4−6 pong.hs+59−3
Network/Wai/Handler/Warp.hs+4−6
@@ -59,7 +59,7 @@ import Blaze.ByteString.Builder.HTTP
5959import Blaze.ByteString.Builder
6060 (copyByteString, Builder, toLazyByteString, toByteStringIO)
6161import Blaze.ByteString.Builder.Char8 (fromChar, fromShow)
62−import Data.Monoid (mappend)
62+import Data.Monoid (mappend, mconcat)
6363import Network.Socket.SendFile (sendFile)
6464import Network.Socket.Enumerator (iterSocket)
6565
@@ -133,9 +133,7 @@ parseRequest' port (firstLine:otherLines) remoteHost' = do
133133 let rpath =
134134 if S.null rpath'
135135 then "/"
136− else if '/' == B.head rpath'
137− then "/"
138− else B.cons '/' rpath'
136+ else rpath'
139137 let heads = map parseHeaderNoAttr otherLines
140138 let host = fromMaybe "" $ lookup "host" heads
141139 let len =
@@ -276,8 +274,8 @@ sendResponse req hv socket (ResponseEnumerator res) =
276274 chunk = E.checkDone $ E.continue . step
277275 step k E.EOF = k (E.Chunks [chunkedTransferTerminator]) >>== return
278276 step k (E.Chunks []) = E.continue $ step k
279− -- FIXME ensure that the mconcat on the concents here is unnecessary
280− step k x = k x >>== chunk
277+ step k (E.Chunks [x]) = k (E.Chunks [chunkedTransferEncoding x]) >>== chunk
278+ step k (E.Chunks xs) = k (E.Chunks [chunkedTransferEncoding $ mconcat xs]) >>== chunk
281279
282280parseHeaderNoAttr :: ByteString -> (CIByteString, ByteString)
283281parseHeaderNoAttr s =
pong.hs+59−3
@@ -1,11 +1,67 @@
11{-# LANGUAGE OverloadedStrings #-}
22import Network.Wai
33import Network.Wai.Handler.Warp
4−import Blaze.ByteString.Builder (fromByteString)
4+import Blaze.ByteString.Builder (copyByteString)
5+import Data.Monoid
6+import Data.Enumerator (run_, enumList, ($$))
57
6−main = run 3000 $ const $ return $ ResponseBuilder
8+main = run 3000 app
9+
10+app req = return $
11+ case pathInfo req of
12+ "/builder/withlen" -> builderWithLen
13+ "/builder/nolen" -> builderNoLen
14+ "/enum/withlen" -> enumWithLen
15+ "/enum/nolen" -> enumNoLen
16+ "/file/withlen" -> fileWithLen
17+ "/file/nolen" -> fileNoLen
18+ _ -> index $ pathInfo req
19+
20+builderWithLen = ResponseBuilder
721 status200
822 [ ("Content-Type", "text/plain")
923 , ("Content-Length", "4")
1024 ]
11− $ fromByteString "PONG"
25+ $ copyByteString "PONG"
26+
27+builderNoLen = ResponseBuilder
28+ status200
29+ [ ("Content-Type", "text/plain")
30+ ]
31+ $ copyByteString "PONG"
32+
33+fileWithLen = ResponseFile
34+ status200
35+ [ ("Content-Type", "text/plain")
36+ , ("Content-Length", "4")
37+ ]
38+ "pong.txt"
39+
40+fileNoLen = ResponseFile
41+ status200
42+ [ ("Content-Type", "text/plain")
43+ ]
44+ "pong.txt"
45+
46+enumWithLen = ResponseEnumerator $ \f ->
47+ run_ $ (enumList 1 $ map copyByteString ["P", "O", "NG"]) $$ f
48+ status200
49+ [ ("Content-Type", "text/plain")
50+ , ("Content-Length", "4")
51+ ]
52+
53+enumNoLen = ResponseEnumerator $ \f ->
54+ run_ $ (enumList 1 $ map copyByteString ["P", "O", "NG"]) $$ f
55+ status200
56+ [ ("Content-Type", "text/plain")
57+ ]
58+
59+index p = ResponseBuilder status200 [("Content-Type", "text/html")] $ mconcat $ map copyByteString
60+ [ "<p><a href='/builder/withlen'>builder withlen</a></p>\n"
61+ , "<p><a href='/builder/nolen'>builder nolen</a></p>\n"
62+ , "<p><a href='/enum/withlen'>enum withlen</a></p>\n"
63+ , "<p><a href='/enum/nolen'>enum nolen</a></p>\n"
64+ , "<p><a href='/file/withlen'>file withlen</a></p>\n"
65+ , "<p><a href='/file/nolen'>file nolen</a></p>\n"
66+ , p
67+ ]