围请求复制

时间:2015-12-01 13:28:04

标签: haskell middleware haskell-wai

我希望能够将请求正文的内容用作缓存密钥的一部分。

我目前的代码如下:

caching app req respond =
    -- Request Body is consumed here
    cacheKey <- strictRequestBody req
    -- the req object is no more usable as body was consumed
    maybe (app req (addToCacheAndRespond cacheKey))
        (sendResponse . responseFromCachedValue)
        (lookup cacheKey cacheContainer)

我在这里看不到任何解决方案。我怎么能复制请求或从cacheKey和req对象生成另一个请求?

或者事件更好还有另一个更好的解决方案吗?

作为奖励点,有人可以指出将Wai申请类型从Request -> IO Response更改为Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived的理由。

1 个答案:

答案 0 :(得分:2)

我终于找到了如何使用requestLogger作为示例:

http://haddock.stackage.org/lts-3.15/wai-extra-3.0.13/src/Network.Wai.Middleware.RequestLogger.html#logStdout

主要是你需要复制请求正文......

getRequestBody :: Request -> IO (Request, [S8.ByteString])
getRequestBody req = do
  let loop front = do
         bs <- requestBody req
         if S8.null bs
             then return $ front []
             else loop $ front . (bs:)
  body <- loop id
  -- logging the body here consumes it, so fill it back up
  -- obviously not efficient, but this is the development logger
  --
  -- Note: previously, we simply used CL.sourceList. However,
  -- that meant that you could read the request body in twice.
  -- While that in itself is not a problem, the issue is that,
  -- in production, you wouldn't be able to do this, and
  -- therefore some bugs wouldn't show up during testing. This
  -- implementation ensures that each chunk is only returned
  -- once.
  ichunks <- newIORef body
  let rbody = atomicModifyIORef ichunks $ \chunks ->
         case chunks of
             [] -> ([], S8.empty)
             x:y -> (y, x)
  let req' = req { requestBody = rbody }
  return (req', body)