http-client-brread-timeout (empty) → 0.1.0.0
raw patch · 5 files changed
+140/−0 lines, 5 filesdep +basedep +bytestringdep +http-clientsetup-changed
Dependencies added: base, bytestring, http-client, template-haskell
Files
- Changelog.md +4/−0
- LICENSE +20/−0
- Network/HTTP/Client/BrReadWithTimeout.hs +78/−0
- Setup.hs +3/−0
- http-client-brread-timeout.cabal +35/−0
+ Changelog.md view
@@ -0,0 +1,4 @@+### 0.1.0.0++- Initial version.+
+ LICENSE view
@@ -0,0 +1,20 @@+The MIT License (MIT)++Copyright (c) 2022 Alexey Radkov++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+the Software, and to permit persons to whom the Software is furnished to do so,+subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Network/HTTP/Client/BrReadWithTimeout.hs view
@@ -0,0 +1,78 @@+{-# LANGUAGE NumDecimals #-}++-----------------------------------------------------------------------------+-- |+-- Module : Network.HTTP.Client.BrReadWithTimeout+-- Copyright : (c) Alexey Radkov 2022+-- License : BSD-style+--+-- Maintainer : alexey.radkov@gmail.com+-- Stability : experimental+-- Portability : portable+--+-- Http client with timeouts applied in between body read events.+--+-- Note that the response timeout in /http-client/ is applied only when+-- receiving the response headers which is not always satisfactory given+-- that a slow server may send the rest of the response very slowly.+-----------------------------------------------------------------------------+++module Network.HTTP.Client.BrReadWithTimeout (+ fromResponseTimeout+ ,brReadWithTimeout+ ,httpLbsBrReadWithTimeout+ ) where++import Network.HTTP.Client hiding (HttpExceptionContent (..))+import qualified Network.HTTP.Client as E (HttpExceptionContent (..))+import qualified Network.HTTP.Client.Internal as I (ResponseTimeout (..)+ ,mResponseTimeout+ )+import Data.ByteString (ByteString)+import qualified Data.ByteString.Lazy as L+import Control.Exception+import System.Timeout++-- | Converts 'ResponseTimeout' of the request into the number of microseconds.+fromResponseTimeout :: Request -> Manager -> Int+fromResponseTimeout req man =+ case responseTimeout req of+ I.ResponseTimeoutDefault ->+ case I.mResponseTimeout man of+ I.ResponseTimeoutDefault -> 30e6+ I.ResponseTimeoutNone -> -1+ I.ResponseTimeoutMicro u -> u+ I.ResponseTimeoutNone -> -1+ I.ResponseTimeoutMicro u -> u++-- | Reads the next chunk of the response body with the specified timeout.+--+-- Note that 'brRead' and 'httpLbs' do not apply any timeouts after reading+-- response headers. This may hang the client if the server implementation is+-- buggy, for instance, when it sends a body of a lesser size than the value of+-- the /Content-Length/ response header. This function solves this problem by+-- applying a timeout passed in the first parameter as a number of microseconds+-- between body read events.+--+-- Throws 'E.ResponseTimeout' if reading of the next chunk of the response body+-- timed out.+brReadWithTimeout :: Int -> Request -> BodyReader -> IO ByteString+brReadWithTimeout tmo req br = do+ x <- timeout tmo br+ case x of+ Nothing -> throwIO $ HttpExceptionRequest+ req { responseTimeout = I.ResponseTimeoutMicro tmo }+ E.ResponseTimeout+ Just bs -> return bs++-- | This is like 'httpLbs' but with a timeout between body read events.+--+-- The value of the timeout is retrieved from the 'ResponseTimeout' of the+-- request.+httpLbsBrReadWithTimeout :: Request -> Manager -> IO (Response L.ByteString)+httpLbsBrReadWithTimeout req man = withResponse req man $ \res -> do+ let tmo = fromResponseTimeout req man+ bss <- brConsume $ brReadWithTimeout tmo req $ responseBody res+ return res { responseBody = L.fromChunks bss }+
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple+main = defaultMain+
+ http-client-brread-timeout.cabal view
@@ -0,0 +1,35 @@+name: http-client-brread-timeout+version: 0.1.0.0+synopsis: Http client with time-limited brRead+description: Http client with timeouts applied in between body read events.+ .+ Note that the response timeout in /http-client/ is applied only when+ receiving the response headers which is not always satisfactory given+ that a slow server may send the rest of the response very slowly.+homepage: https://github.com/lyokha/http-client-brread-timeout+license: MIT+license-file: LICENSE+extra-source-files: Changelog.md+author: Alexey Radkov <alexey.radkov@gmail.com>+maintainer: Alexey Radkov <alexey.radkov@gmail.com>+stability: experimental+copyright: 2022 Alexey Radkov+category: Network+build-type: Simple+cabal-version: 1.20++source-repository head+ type: git+ location: https://github.com/lyokha/http-client-brread-timeout++library+ default-language: Haskell2010+ build-depends: base >= 4.8 && < 5+ , http-client >= 0.5.0+ , template-haskell >= 2.11.0.0+ , bytestring++ exposed-modules: Network.HTTP.Client.BrReadWithTimeout++ ghc-options: -Wall+