readRequestBodyAndPushback :: Request -> IO (B.ByteString, IO B.ByteString)
readRequestBodyAndPushback req = do
    -- This loop body and then the ichunks rbody stuff comes from http://hackage.haskell.org/package/wai-extra-3.0.3.2/docs/src/Network-Wai-Middleware-RequestLogger.html
    -- ** BEGIN READ BODY
    let loop front = do
            bs <- requestBody req
            if BC.null bs
                then return $ front []
                else loop $ front . (bs:)
    body <- loop id
    -- ** END READ BODY
    
    -- ** BEGIN BODY PUSHBACK
    ichunks <- newIORef body
    let rbody = atomicModifyIORef ichunks $ \chunks -> case chunks of
            [] -> ([], BC.empty)
            x:y -> (y, x)
    -- ** END BODY PUSHBACK

    return (B.concat body, rbody)



-- | Middleware to validate Twilio request.  Parameterized by API auth token.
--   Returns 401 Unauthorized if the request does not contain a valid signature based on the given auth token.
readRequestBodyAndPushback :: Request -> IO (B.ByteString, IO B.ByteString)
readRequestBodyAndPushback req = do
    -- This loop body and then the ichunks rbody stuff comes from http://hackage.haskell.org/package/wai-extra-3.0.3.2/docs/src/Network-Wai-Middleware-RequestLogger.html
    -- ** BEGIN READ BODY
    let loop front = do
            bs <- requestBody req
            if BC.null bs
                then return $ front []
                else loop $ front . (bs:)
    body <- loop id
    -- ** END READ BODY
    
    -- ** BEGIN BODY PUSHBACK
    ichunks <- newIORef body
    let rbody = atomicModifyIORef ichunks $ \chunks -> case chunks of
            [] -> ([], BC.empty)
            x:y -> (y, x)
    -- ** END BODY PUSHBACK

    return (B.concat body, rbody)



-- | Middleware to validate Twilio request.  Parameterized by API auth token.
--   Returns 401 Unauthorized if the request does not contain a valid signature based on the given auth token.
readRequestBodyAndPushback req = do
    -- This loop body and then the ichunks rbody stuff comes from http://hackage.haskell.org/package/wai-extra-3.0.3.2/docs/src/Network-Wai-Middleware-RequestLogger.html
    -- ** BEGIN READ BODY
    let loop front = do
            bs <- requestBody req
            if BC.null bs
                then return $ front []
                else loop $ front . (bs:)
    body <- loop id
    -- ** END READ BODY
    
    -- ** BEGIN BODY PUSHBACK
    ichunks <- newIORef body
    let rbody = atomicModifyIORef ichunks $ \chunks -> case chunks of
            [] -> ([], BC.empty)
            x:y -> (y, x)
    -- ** END BODY PUSHBACK

    return (B.concat body, rbody)



-- | Middleware to validate Twilio request.  Parameterized by API auth token.
--   Returns 401 Unauthorized if the request does not contain a valid signature based on the given auth token.