Skip to content
Browse files

use strict version of hack2

  • Loading branch information...
1 parent 8ce138c commit 30d5e537471a9a010ba5b0dbd9d54ded8bf8a3ff @nfjinjing committed
Showing with 22 additions and 18 deletions.
  1. +2 −2 hack2-handler-snap-server.cabal
  2. +20 −16 src/Hack2/Handler/SnapServer.hs
View
4 hack2-handler-snap-server.cabal
@@ -1,5 +1,5 @@
Name: hack2-handler-snap-server
-Version: 2011.10.30
+Version: 2012.1.19
Build-type: Simple
Synopsis: Hack2 Snap server handler
Description: Hack2 Snap server handler
@@ -20,7 +20,7 @@ library
, network
, bytestring
, data-default >= 0.2
- , hack2 >= 2011.6.20
+ , hack2 >= 2012.1.19
, containers
, mtl
, enumerator
View
36 src/Hack2/Handler/SnapServer.hs
@@ -39,28 +39,32 @@ import qualified Snap.Http.Server as SnapServer
import System.Directory (createDirectory, doesDirectoryExist)
import Control.Monad (when)
-{-
-{ requestMethod :: RequestMethod
-, scriptName :: ByteString
-, pathInfo :: ByteString
-, queryString :: ByteString
-, serverName :: ByteString
-, serverPort :: Int
-, httpHeaders :: [(ByteString, ByteString)]
-, hackVersion :: (Int, Int, Int)
-, hackUrlScheme :: HackUrlScheme
-, hackInput :: HackEnumerator
-, hackErrors :: HackErrors
-, hackHeaders :: [(ByteString, ByteString)]
+-- backports from earlier hack2 utils
--}
+import qualified Data.ByteString.Lazy.Char8 as Lazy
+import qualified Data.Map as M
+import Hack2.Contrib.AirBackports
+import qualified Data.ByteString.Char8 as Strict
+
+import qualified Data.Enumerator.Binary as EB
+import qualified Data.Enumerator.List as EL
+
+import Data.Enumerator (run_, enumList, Enumerator, ($$))
+
+fromEnumerator :: Monad m => Enumerator Strict.ByteString m Lazy.ByteString -> m Lazy.ByteString
+fromEnumerator m = run_ - m $$ EB.consume
+
+toEnumerator :: Monad m => Lazy.ByteString -> Enumerator Strict.ByteString m a
+toEnumerator = enumList 1 < Lazy.toChunks
requestToEnv :: Snap.Request -> IO Env
requestToEnv request = do
(Snap.SomeEnumerator some_enumerator) <- readIORef - request.SnapInternal.rqBody
+ _requestBody <- fromEnumerator some_enumerator
+
return - def
{
@@ -72,7 +76,7 @@ requestToEnv request = do
, serverPort = request.Snap.rqServerPort
, httpHeaders = request.SnapInternal.rqHeaders.Snap.Headers.toList.map caseInsensitiveHeaderToHeader
, hackUrlScheme = if request.Snap.rqIsSecure then HTTPS else HTTP
- , hackInput = HackEnumerator some_enumerator
+ , hackInput = _requestBody.l2s
, hackHeaders =
[
("RemoteHost", request.Snap.rqRemoteAddr)
@@ -103,7 +107,7 @@ hackResponseToSnapResponse response =
Snap.emptyResponse
. Snap.setResponseCode (response.status)
. (\r -> r { SnapInternal.rspHeaders = response.headers.map headerToCaseInsensitiveHeader.Snap.Headers.fromList })
- . Snap.setResponseBody (response.body.unHackEnumerator $= EL.map fromByteString)
+ . Snap.setResponseBody (response.body.s2l.toEnumerator $= EL.map fromByteString)

0 comments on commit 30d5e53

Please sign in to comment.
Something went wrong with that request. Please try again.