http-conduit-2.1.8/0000755000000000000000000000000012562145660012341 5ustar0000000000000000http-conduit-2.1.8/ChangeLog.md0000644000000000000000000000057712562145660014523 0ustar0000000000000000## 2.1.8 * Move HasHttpManager from http-conduit to http-client [#147](https://github.com/snoyberg/http-client/pull/147) ## 2.1.7 * Deprecate `conduitManagerSettings`, re-export `tlsManagerSettings` [#136](https://github.com/snoyberg/http-client/issues/136) [#137](https://github.com/snoyberg/http-client/issues/137) ## 2.1.6 * Deprecate `withManager` and `withManagerSettings` http-conduit-2.1.8/certificate.pem0000644000000000000000000000155312562145660015332 0ustar0000000000000000-----BEGIN CERTIFICATE----- MIICWDCCAcGgAwIBAgIJAJG1ZMlcMDW6MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX aWRnaXRzIFB0eSBMdGQwHhcNMTExMDIyMTk0MjU3WhcNMTExMTIxMTk0MjU3WjBF MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50 ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB gQCfYZx7kV6ybogMyAf9MINm7Rwin5LKh+TpD1ZkbLgmqFVotQCdthgTK66SPXkx EXGI27biNzacJhX7Ml7/4o8sp2GslYKUO46DYvgi/nnNX/bzA5cDJSSGK11eQEVs +p0GEZ/6Juhpx/oQwMDMgo0UHkiH8QtKI8ojXnFF2MsLNwIDAQABo1AwTjAdBgNV HQ4EFgQUaA6FbOj/0VJMb4egNyIDZ/ZNV/YwHwYDVR0jBBgwFoAUaA6FbOj/0VJM b4egNyIDZ/ZNV/YwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCTQyOk D86Z+yzedXjTLI6FT8QugmQne1YQ8P0w37P76z2reagSvNee2e9B1oTHoPeKZMs0 k99oS9yJ/NOQ1Ms90P+q0yBVGxAs/gF65qKgE27YGXzNtNobj/D4OoxcFG+BsORw VvYSBV4FiVy9RwJsr7AMqkUBcOEPCuJHgTx58w== -----END CERTIFICATE----- http-conduit-2.1.8/multipart-example.bin0000644000000000000000000001025312562145660016506 0ustar0000000000000000-----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="email" -----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="parent_id" 70488 -----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="captcha" -----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="homeboard" 0chan.hk -----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="text" >>72127 Мы работаем над этим. -----------------------------190723902820679116301912680260 Content-Disposition: form-data; name="upload"; filename="nyan.gif" Content-Type: image/gif GIF89a5̙33f3!  ! NETSCAPE2.0,4Ig֊`ef(#l D:07!c`2t:U5Xz,sFzن0q80@jr:Ntz=vi~z bvuz~yrN: J 1¶ JvǮ<1Icڅ1 7;" /-haG%ʐo9 Α(qŋ>\(# !  ,5p)R̺`aZi #l̘tD:0x%7!c`2tJU5Xzlm-F`^S`88Xs:N{wfv|mlJw|{ ks:1XLVv wtwѭO <ɐ(ѡŋ <\81 G !  ,59R'$chj 麌 t-3 i2pHprPJ}fl HM3@.[%+yN^0|rvuxCT|n&{ vP|'xCH&]R ){8ƸD| ʭEz ' O8@]* Z٠k{ Qa?ӏ#jpbBG?6!  ,5)R'Ŝh`钌 t-3i1aH,g8MolFBb8- maintainer: Michael Snoyman synopsis: HTTP client package with conduit interface and HTTPS support. description: This package uses conduit for parsing the actual contents of the HTTP connection. It also provides higher-level functions which allow you to avoid directly dealing with streaming data. See for more information. . The @Network.HTTP.Conduit.Browser@ module has been moved to category: Web, Conduit stability: Stable cabal-version: >= 1.8 build-type: Simple homepage: http://www.yesodweb.com/book/http-conduit extra-source-files: test/main.hs , test/CookieTest.hs , multipart-example.bin , nyan.gif , certificate.pem , key.pem , README.md , ChangeLog.md library build-depends: base >= 4 && < 5 , bytestring >= 0.9.1.4 , transformers >= 0.2 , resourcet >= 1.1 && < 1.2 , conduit >= 0.5.5 && < 1.3 , http-types >= 0.7 , lifted-base >= 0.1 , http-client >= 0.4.3 && < 0.5 , http-client-tls >= 0.2.2 , monad-control , mtl exposed-modules: Network.HTTP.Conduit Network.HTTP.Client.Conduit ghc-options: -Wall test-suite test main-is: main.hs type: exitcode-stdio-1.0 hs-source-dirs: test ghc-options: -Wall cpp-options: -DDEBUG build-depends: base >= 4 && < 5 , HUnit , hspec >= 1.3 , data-default-class , connection >= 0.2 , warp-tls , time , blaze-builder , bytestring , text , transformers , conduit >= 1.1 , utf8-string , case-insensitive , lifted-base , network , wai >= 3.0 && < 3.1 , warp >= 3.0.0.2 && < 3.2 , wai-conduit , http-types , cookie , http-client , http-conduit , conduit-extra , streaming-commons source-repository head type: git location: git://github.com/snoyberg/http-client.git http-conduit-2.1.8/LICENSE0000644000000000000000000000253012562145660013346 0ustar0000000000000000The following license covers this documentation, and the source code, except where otherwise indicated. Copyright 2010, Michael Snoyman. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. http-conduit-2.1.8/key.pem0000644000000000000000000000156712562145660013645 0ustar0000000000000000-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCfYZx7kV6ybogMyAf9MINm7Rwin5LKh+TpD1ZkbLgmqFVotQCd thgTK66SPXkxEXGI27biNzacJhX7Ml7/4o8sp2GslYKUO46DYvgi/nnNX/bzA5cD JSSGK11eQEVs+p0GEZ/6Juhpx/oQwMDMgo0UHkiH8QtKI8ojXnFF2MsLNwIDAQAB AoGAR8pgAgjo7tZ60ccIUjOX/LSxB6d5J2Eu6wvNjk6qZD9OuWtOa7up/HigmZ63 CDMjQNI2/o6AOrWtEQkPYZNbibuifzg5V517nHGSqkqjoIgesAiwEsoKpeOgGTtM MM08oHbJ9uOnDnEEnDBiE0iE3jCTDfmwjqDMpUhu9dZ1EAECQQDKVpzSSV3pzMOp ixNxMpYxzcE+4K9jgM+MlxPBJSQhVrg/cRQWb26cKBi8LdSxF23hQTsFr+8qLwid Ah2AgUOBAkEAyaaCjrNRCiHRpd6YzWZ6GKkxbUvxSuOKX3N7hDaE2OFzQTv2Li8B 5mrCsXnSZtOG+MBFdHU66UYie1OzDSDKtwJAKMsvkOID0ihbZmpIwDC/wUjHZkLs eXY14hVvgShY0XPnb7r/nspWlZsr6Xyf/hhIKfr5yFrBMFMNPIJ5qjflgQJAWsyV YTgxN4S+6BdxapvIQq58ySA3CGeo+Q4BAimibB4oTal4UpdsHZrZDB00toRs9Dlv jN70pfGkuS+ZIkIvxQJBAKSf5qpXWp4oZcThkieAiMeAhG96xqRPXhPUxq6QF+YG T4PF1sjlpZwqy7C+2oF3BqLP09mCW7YkH9Jgnl1zDF8= -----END RSA PRIVATE KEY----- http-conduit-2.1.8/README.md0000644000000000000000000000011012562145660013610 0ustar0000000000000000Make HTTP requests using the conduit library for a streaming interface. http-conduit-2.1.8/nyan.gif0000755000000000000000000000653612562145660014012 0ustar0000000000000000GIF89a5̙33f3!  ! NETSCAPE2.0,4Ig֊`ef(#l D:07!c`2t:U5Xz,sFzن0q80@jr:Ntz=vi~z bvuz~yrN: J 1¶ JvǮ<1Icڅ1 7;" /-haG%ʐo9 Α(qŋ>\(# !  ,5p)R̺`aZi #l̘tD:0x%7!c`2tJU5Xzlm-F`^S`88Xs:N{wfv|mlJw|{ ks:1XLVv wtwѭO <ɐ(ѡŋ <\81 G !  ,59R'$chj 麌 t-3 i2pHprPJ}fl HM3@.[%+yN^0|rvuxCT|n&{ vP|'xCH&]R ){8ƸD| ʭEz ' O8@]* Z٠k{ Qa?ӏ#jpbBG?6!  ,5)R'Ŝh`钌 t-3i1aH,g8MolFBb8- module Main where > import Distribution.Simple > import System.Cmd (system) > main :: IO () > main = defaultMain http-conduit-2.1.8/Network/0000755000000000000000000000000012562145660013772 5ustar0000000000000000http-conduit-2.1.8/Network/HTTP/0000755000000000000000000000000012562145660014551 5ustar0000000000000000http-conduit-2.1.8/Network/HTTP/Conduit.hs0000644000000000000000000003301512562145660016514 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} -- | This module contains everything you need to initiate HTTP connections. If -- you want a simple interface based on URLs, you can use 'simpleHttp'. If you -- want raw power, 'http' is the underlying workhorse of this package. Some -- examples: -- -- > -- Just download an HTML document and print it. -- > import Network.HTTP.Conduit -- > import qualified Data.ByteString.Lazy as L -- > -- > main = simpleHttp "http://www.haskell.org/" >>= L.putStr -- -- This example uses interleaved IO to write the response body to a file in -- constant memory space. -- -- > import Data.Conduit.Binary (sinkFile) -- Exported from the package conduit-extra -- > import Network.HTTP.Conduit -- > import qualified Data.Conduit as C -- > import Control.Monad.Trans.Resource (runResourceT) -- > -- > main :: IO () -- > main = do -- > request <- parseUrl "http://google.com/" -- > manager <- newManager tlsManagerSettings -- > runResourceT $ do -- > response <- http request manager -- > responseBody response C.$$+- sinkFile "google.html" -- -- The following headers are automatically set by this module, and should not -- be added to 'requestHeaders': -- -- * Cookie -- -- * Content-Length -- -- * Transfer-Encoding -- -- Note: In previous versions, the Host header would be set by this module in -- all cases. Starting from 1.6.1, if a Host header is present in -- @requestHeaders@, it will be used in place of the header this module would -- have generated. This can be useful for calling a server which utilizes -- virtual hosting. -- -- Use `cookieJar` If you want to supply cookies with your request: -- -- > {-# LANGUAGE OverloadedStrings #-} -- > import Network.HTTP.Conduit -- > import Network -- > import Data.Time.Clock -- > import Data.Time.Calendar -- > import qualified Control.Exception as E -- > import Network.HTTP.Types.Status (statusCode) -- > -- > past :: UTCTime -- > past = UTCTime (ModifiedJulianDay 56200) (secondsToDiffTime 0) -- > -- > future :: UTCTime -- > future = UTCTime (ModifiedJulianDay 562000) (secondsToDiffTime 0) -- > -- > cookie :: Cookie -- > cookie = Cookie { cookie_name = "password_hash" -- > , cookie_value = "abf472c35f8297fbcabf2911230001234fd2" -- > , cookie_expiry_time = future -- > , cookie_domain = "example.com" -- > , cookie_path = "/" -- > , cookie_creation_time = past -- > , cookie_last_access_time = past -- > , cookie_persistent = False -- > , cookie_host_only = False -- > , cookie_secure_only = False -- > , cookie_http_only = False -- > } -- > -- > main = withSocketsDo $ do -- > request' <- parseUrl "http://example.com/secret-page" -- > manager <- newManager tlsManagerSettings -- > let request = request' { cookieJar = Just $ createCookieJar [cookie] } -- > (fmap Just (httpLbs request manager)) `E.catch` -- > (\(StatusCodeException s _ _) -> -- > if statusCode s==403 then (putStrLn "login failed" >> return Nothing) else return Nothing) -- -- Any network code on Windows requires some initialization, and the network -- library provides withSocketsDo to perform it. Therefore, proper usage of -- this library will always involve calling that function at some point. The -- best approach is to simply call them at the beginning of your main function, -- such as: -- -- > import Network.HTTP.Conduit -- > import qualified Data.ByteString.Lazy as L -- > import Network (withSocketsDo) -- > -- > main = withSocketsDo -- > $ simpleHttp "http://www.haskell.org/" >>= L.putStr -- -- Cookies are implemented according to RFC 6265. -- -- Note that by default, the functions in this package will throw exceptions -- for non-2xx status codes. If you would like to avoid this, you should use -- 'checkStatus', e.g.: -- -- > import Data.Conduit.Binary (sinkFile) -- > import Network.HTTP.Conduit -- > import qualified Data.Conduit as C -- > import Network -- > -- > main :: IO () -- > main = withSocketsDo $ do -- > request' <- parseUrl "http://www.yesodweb.com/does-not-exist" -- > let request = request' { checkStatus = \_ _ _ -> Nothing } -- > manager <- newManager tlsManagerSettings -- > res <- httpLbs request manager -- > print res -- -- By default, when connecting to websites using HTTPS, functions in this -- package will throw an exception if the TLS certificate doesn't validate. To -- continue the HTTPS transaction even if the TLS cerficate validation fails, -- you should use 'mkManagerSetttings' as follows: -- -- > import Network.Connection (TLSSettings (..)) -- > import Network.HTTP.Conduit -- > -- > main :: IO () -- > main = do -- > request <- parseUrl "https://github.com/" -- > let settings = mkManagerSettings (TLSSettingsSimple True False False) Nothing -- > manager <- newManager settings -- > res <- httpLbs request manager -- > print res -- -- For more information, please be sure to read the documentation in the -- "Network.HTTP.Client" module. module Network.HTTP.Conduit ( -- * Perform a request simpleHttp , httpLbs , http -- * Datatypes , Proxy (..) , RequestBody (..) -- ** Request , Request , method , secure , host , port , path , queryString , requestHeaders , requestBody , proxy , hostAddress , rawBody , decompress , redirectCount , checkStatus , responseTimeout , cookieJar , requestVersion , getConnectionWrapper , HCC.setQueryString -- *** Request body , requestBodySource , requestBodySourceChunked , requestBodySourceIO , requestBodySourceChunkedIO -- * Response , Response , responseStatus , responseVersion , responseHeaders , responseBody , responseCookieJar -- * Manager , Manager , newManager , closeManager , withManager , withManagerSettings -- ** Settings , ManagerSettings , conduitManagerSettings , tlsManagerSettings , mkManagerSettings , managerConnCount , managerResponseTimeout , managerTlsConnection -- * Cookies , Cookie(..) , CookieJar , createCookieJar , destroyCookieJar -- * Utility functions , parseUrl , applyBasicAuth , addProxy , lbsResponse , getRedirectedRequest -- * Decompression predicates , alwaysDecompress , browserDecompress -- * Request bodies -- | "Network.HTTP.Client.MultipartFormData" provides an API for building -- form-data request bodies. , urlEncodedBody -- * Exceptions , HttpException (..) ) where import qualified Data.ByteString as S import qualified Data.ByteString.Lazy as L import Data.Conduit (ResumableSource, ($$+-), await, ($$++), ($$+), Source, addCleanup) import qualified Data.Conduit.Internal as CI import qualified Data.Conduit.List as CL import Data.IORef (readIORef, writeIORef, newIORef) import Data.Int (Int64) import Control.Applicative ((<$>)) import Control.Exception.Lifted (bracket) import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.Trans.Resource import qualified Network.HTTP.Client as Client (httpLbs, responseOpen, responseClose) import qualified Network.HTTP.Client.Conduit as HCC import Network.HTTP.Client.Internal (createCookieJar, destroyCookieJar) import Network.HTTP.Client.Internal (Manager, ManagerSettings, closeManager, managerConnCount, managerResponseTimeout, managerTlsConnection, newManager) import Network.HTTP.Client (parseUrl, urlEncodedBody, applyBasicAuth) import Network.HTTP.Client.Internal (addProxy, alwaysDecompress, browserDecompress) import Network.HTTP.Client.Internal (getRedirectedRequest) import Network.HTTP.Client.TLS (mkManagerSettings, tlsManagerSettings) import Network.HTTP.Client.Internal (Cookie (..), CookieJar (..), HttpException (..), Proxy (..), Request (..), RequestBody (..), Response (..)) -- | Download the specified 'Request', returning the results as a 'Response'. -- -- This is a simplified version of 'http' for the common case where you simply -- want the response data as a simple datatype. If you want more power, such as -- interleaved actions on the response body during download, you'll need to use -- 'http' directly. This function is defined as: -- -- @httpLbs = 'lbsResponse' <=< 'http'@ -- -- Even though the 'Response' contains a lazy bytestring, this -- function does /not/ utilize lazy I/O, and therefore the entire -- response body will live in memory. If you want constant memory -- usage, you'll need to use @conduit@ packages's -- 'C.Source' returned by 'http'. -- -- This function will 'throwIO' an 'HttpException' for any -- response with a non-2xx status code (besides 3xx redirects up -- to a limit of 10 redirects). This behavior can be modified by -- changing the 'checkStatus' field of your request. -- -- Note: Unlike previous versions, this function will perform redirects, as -- specified by the 'redirectCount' setting. httpLbs :: MonadIO m => Request -> Manager -> m (Response L.ByteString) httpLbs r m = liftIO $ Client.httpLbs r m -- | Download the specified URL, following any redirects, and -- return the response body. -- -- This function will 'throwIO' an 'HttpException' for any -- response with a non-2xx status code (besides 3xx redirects up -- to a limit of 10 redirects). It uses 'parseUrl' to parse the -- input. This function essentially wraps 'httpLbs'. -- -- Note: Even though this function returns a lazy bytestring, it -- does /not/ utilize lazy I/O, and therefore the entire response -- body will live in memory. If you want constant memory usage, -- you'll need to use the @conduit@ package and 'http' directly. -- -- Note: This function creates a new 'Manager'. It should be avoided -- in production code. simpleHttp :: MonadIO m => String -> m L.ByteString simpleHttp url = liftIO $ do man <- newManager tlsManagerSettings req <- liftIO $ parseUrl url responseBody <$> httpLbs (setConnectionClose req) man conduitManagerSettings :: ManagerSettings conduitManagerSettings = tlsManagerSettings {-# DEPRECATED conduitManagerSettings "Use tlsManagerSettings" #-} withManager :: (MonadIO m, MonadBaseControl IO m) => (Manager -> ResourceT m a) -> m a withManager = withManagerSettings tlsManagerSettings {-# DEPRECATED withManager "Please use newManager tlsManagerSettings" #-} withManagerSettings :: (MonadIO m, MonadBaseControl IO m) => ManagerSettings -> (Manager -> ResourceT m a) -> m a withManagerSettings set f = liftIO (newManager set) >>= runResourceT . f {-# DEPRECATED withManagerSettings "Please use newManager" #-} setConnectionClose :: Request -> Request setConnectionClose req = req{requestHeaders = ("Connection", "close") : requestHeaders req} lbsResponse :: Monad m => Response (ResumableSource m S.ByteString) -> m (Response L.ByteString) lbsResponse res = do bss <- responseBody res $$+- CL.consume return res { responseBody = L.fromChunks bss } http :: MonadResource m => Request -> Manager -> m (Response (ResumableSource m S.ByteString)) http req man = do (key, res) <- allocate (Client.responseOpen req man) Client.responseClose #if MIN_VERSION_conduit(1, 2, 0) let rsrc = CI.ResumableSource (flip CI.unConduitM CI.Done $ addCleanup (const $ release key) $ HCC.bodyReaderSource $ responseBody res) (release key) #else let rsrc = CI.ResumableSource (addCleanup (const $ release key) $ HCC.bodyReaderSource $ responseBody res) (release key) #endif return res { responseBody = rsrc } requestBodySource :: Int64 -> Source (ResourceT IO) S.ByteString -> RequestBody requestBodySource size = RequestBodyStream size . srcToPopper requestBodySourceChunked :: Source (ResourceT IO) S.ByteString -> RequestBody requestBodySourceChunked = RequestBodyStreamChunked . srcToPopper srcToPopper :: Source (ResourceT IO) S.ByteString -> HCC.GivesPopper () srcToPopper src f = runResourceT $ do (rsrc0, ()) <- src $$+ return () irsrc <- liftIO $ newIORef rsrc0 is <- getInternalState let popper :: IO S.ByteString popper = do rsrc <- readIORef irsrc (rsrc', mres) <- runInternalState (rsrc $$++ await) is writeIORef irsrc rsrc' case mres of Nothing -> return S.empty Just bs | S.null bs -> popper | otherwise -> return bs liftIO $ f popper requestBodySourceIO :: Int64 -> Source IO S.ByteString -> RequestBody requestBodySourceIO = HCC.requestBodySource requestBodySourceChunkedIO :: Source IO S.ByteString -> RequestBody requestBodySourceChunkedIO = HCC.requestBodySourceChunked http-conduit-2.1.8/Network/HTTP/Client/0000755000000000000000000000000012562145660015767 5ustar0000000000000000http-conduit-2.1.8/Network/HTTP/Client/Conduit.hs0000644000000000000000000001460412562145660017735 0ustar0000000000000000{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} -- | A new, experimental API to replace "Network.HTTP.Conduit". -- -- For more information, please be sure to read the documentation in the -- "Network.HTTP.Client" module. module Network.HTTP.Client.Conduit ( -- * Conduit-specific interface withResponse , responseOpen , responseClose , acquireResponse -- * Manager helpers , defaultManagerSettings , newManager , withManager , withManagerSettings , newManagerSettings -- * General HTTP client interface , module Network.HTTP.Client , httpLbs , httpNoBody -- * Lower-level conduit functions , requestBodySource , requestBodySourceChunked , bodyReaderSource ) where import Control.Monad (unless) import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Reader (MonadReader (..), ReaderT (..)) import Control.Monad.Trans.Control (MonadBaseControl) import Data.Acquire (Acquire, mkAcquire, with) import Data.ByteString (ByteString) import qualified Data.ByteString as S import qualified Data.ByteString.Lazy as L import Data.Conduit (ConduitM, Producer, Source, await, yield, ($$+), ($$++)) import Data.Int (Int64) import Data.IORef (newIORef, readIORef, writeIORef) import Network.HTTP.Client hiding (closeManager, defaultManagerSettings, httpLbs, newManager, responseClose, responseOpen, withManager, withResponse, BodyReader, brRead, brConsume, httpNoBody) import Network.HTTP.Client (HasHttpManager(..)) import qualified Network.HTTP.Client as H import Network.HTTP.Client.TLS (tlsManagerSettings) -- | Conduit powered version of 'H.withResponse'. Differences are: -- -- * Response body is represented as a @Producer@. -- -- * Generalized to any instance of @MonadBaseControl@, not just @IO@. -- -- * The @Manager@ is contained by a @MonadReader@ context. -- -- Since 2.1.0 withResponse :: (MonadBaseControl IO m, MonadIO n, MonadReader env m, HasHttpManager env) => Request -> (Response (ConduitM i ByteString n ()) -> m a) -> m a withResponse req f = do env <- ask with (acquireResponse req env) f -- | An @Acquire@ for getting a @Response@. -- -- Since 2.1.0 acquireResponse :: (MonadIO n, MonadReader env m, HasHttpManager env) => Request -> m (Acquire (Response (ConduitM i ByteString n ()))) acquireResponse req = do env <- ask let man = getHttpManager env return $ do res <- mkAcquire (H.responseOpen req man) H.responseClose return $ fmap bodyReaderSource res -- | TLS-powered manager settings. -- -- Since 2.1.0 defaultManagerSettings :: ManagerSettings defaultManagerSettings = tlsManagerSettings -- | Get a new manager using 'defaultManagerSettings'. -- -- Since 2.1.0 newManager :: MonadIO m => m Manager newManager = newManagerSettings defaultManagerSettings -- | Get a new manager using the given settings. -- -- Since 2.1.0 newManagerSettings :: MonadIO m => ManagerSettings -> m Manager newManagerSettings = liftIO . H.newManager -- | Get a new manager with 'defaultManagerSettings' and construct a @ReaderT@ containing it. -- -- Since 2.1.0 withManager :: MonadIO m => (ReaderT Manager m a) -> m a withManager = withManagerSettings defaultManagerSettings -- | Get a new manager with the given settings and construct a @ReaderT@ containing it. -- -- Since 2.1.0 withManagerSettings :: MonadIO m => ManagerSettings -> (ReaderT Manager m a) -> m a withManagerSettings settings (ReaderT inner) = newManagerSettings settings >>= inner -- | Conduit-powered version of 'H.responseOpen'. -- -- See 'withResponse' for the differences with 'H.responseOpen'. -- -- Since 2.1.0 responseOpen :: (MonadIO m, MonadIO n, MonadReader env m, HasHttpManager env) => Request -> m (Response (ConduitM i ByteString n ())) responseOpen req = do env <- ask liftIO $ fmap bodyReaderSource `fmap` H.responseOpen req (getHttpManager env) -- | Generalized version of 'H.responseClose'. -- -- Since 2.1.0 responseClose :: MonadIO m => Response body -> m () responseClose = liftIO . H.responseClose bodyReaderSource :: MonadIO m => H.BodyReader -> Producer m ByteString bodyReaderSource br = loop where loop = do bs <- liftIO $ H.brRead br unless (S.null bs) $ do yield bs loop requestBodySource :: Int64 -> Source IO ByteString -> RequestBody requestBodySource size = RequestBodyStream size . srcToPopperIO requestBodySourceChunked :: Source IO ByteString -> RequestBody requestBodySourceChunked = RequestBodyStreamChunked . srcToPopperIO srcToPopperIO :: Source IO ByteString -> GivesPopper () srcToPopperIO src f = do (rsrc0, ()) <- src $$+ return () irsrc <- newIORef rsrc0 let popper :: IO ByteString popper = do rsrc <- readIORef irsrc (rsrc', mres) <- rsrc $$++ await writeIORef irsrc rsrc' case mres of Nothing -> return S.empty Just bs | S.null bs -> popper | otherwise -> return bs f popper -- | Same as 'H.httpLbs', except it uses the @Manager@ in the reader environment. -- -- Since 2.1.1 httpLbs :: (MonadIO m, HasHttpManager env, MonadReader env m) => Request -> m (Response L.ByteString) httpLbs req = do env <- ask let man = getHttpManager env liftIO $ H.httpLbs req man -- | Same as 'H.httpNoBody', except it uses the @Manager@ in the reader environment. -- -- This can be more convenient that using 'withManager' as it avoids the need -- to specify the base monad for the response body. -- -- Since 2.1.2 httpNoBody :: (MonadIO m, HasHttpManager env, MonadReader env m) => Request -> m (Response ()) httpNoBody req = do env <- ask let man = getHttpManager env liftIO $ H.httpNoBody req man http-conduit-2.1.8/test/0000755000000000000000000000000012562145660013320 5ustar0000000000000000http-conduit-2.1.8/test/CookieTest.hs0000644000000000000000000007751712562145660015746 0ustar0000000000000000module CookieTest (cookieTest) where import Prelude hiding (exp) import Test.Hspec import qualified Data.ByteString as BS import Test.HUnit hiding (path) import Network.HTTP.Client import qualified Network.HTTP.Conduit as HC import Data.ByteString.UTF8 import Data.Monoid import Data.Maybe import Data.Time.Clock import Data.Time.Calendar import qualified Data.CaseInsensitive as CI import Web.Cookie default_request :: HC.Request default_request = fromJust $ HC.parseUrl "http://www.google.com/" default_cookie :: Cookie default_cookie = Cookie { cookie_name = fromString "name" , cookie_value = fromString "value" , cookie_expiry_time = default_time , cookie_domain = fromString "www.google.com" , cookie_path = fromString "/" , cookie_creation_time = default_time , cookie_last_access_time = default_time , cookie_persistent = False , cookie_host_only = False , cookie_secure_only = False , cookie_http_only = False } default_time :: UTCTime default_time = UTCTime (ModifiedJulianDay 56200) (secondsToDiffTime 0) default_diff_time :: DiffTime default_diff_time = secondsToDiffTime 1209600 default_set_cookie :: SetCookie default_set_cookie = def { setCookieName = fromString "name" , setCookieValue = fromString "value" , setCookiePath = Just $ fromString "/" , setCookieExpires = Just default_time , setCookieMaxAge = Just default_diff_time , setCookieDomain = Just $ fromString "www.google.com" , setCookieHttpOnly = False , setCookieSecure = False } testValidIp :: IO () testValidIp = assertBool "Couldn't parse valid IP address" $ isIpAddress $ fromString "1.2.3.4" testIpNumTooHigh :: IO () testIpNumTooHigh = assertBool "One of the digits in the IP address is too large" $ not $ isIpAddress $ fromString "501.2.3.4" testTooManySegmentsInIp :: IO () testTooManySegmentsInIp = assertBool "Too many segments in the ip address" $ not $ isIpAddress $ fromString "1.2.3.4.5" testCharsInIp :: IO () testCharsInIp = assertBool "Chars are not allowed in IP addresses" $ not $ isIpAddress $ fromString "1.2a3.4.5" testDomainMatchesSuccess :: IO () testDomainMatchesSuccess = assertBool "Domains should match" $ domainMatches (fromString "www.google.com") (fromString "google.com") testSameDomain :: IO () testSameDomain = assertBool "Same domain should match" $ domainMatches domain domain where domain = fromString "www.google.com" testSiblingDomain :: IO () testSiblingDomain = assertBool "Sibling domain should not match" $ not $ domainMatches (fromString "www.google.com") (fromString "secure.google.com") testParentDomain :: IO () testParentDomain = assertBool "Parent domain should fail" $ not $ domainMatches (fromString "google.com") (fromString "www.google.com") testNaiveSuffixDomain :: IO () testNaiveSuffixDomain = assertBool "Naively checking for suffix for domain matching should fail" $ not $ domainMatches (fromString "agoogle.com") (fromString "google.com") testDefaultPath :: IO () testDefaultPath = assertEqual "Getting default path from a request" (fromString "/") (defaultPath default_request) testShortDefaultPath :: IO () testShortDefaultPath = assertEqual "Getting default path from a short path" (fromString "/") (defaultPath $ default_request {HC.path = fromString "/search"}) testPopulatedDefaultPath :: IO () testPopulatedDefaultPath = assertEqual "Getting default path from a request with a path" (fromString "/search") (defaultPath $ default_request {HC.path = fromString "/search/term"}) testParamsDefaultPath :: IO () testParamsDefaultPath = assertEqual "Getting default path from a request with a path and GET params" (fromString "/search") (defaultPath $ default_request {HC.path = fromString "/search/term?var=val"}) testDefaultPathEndingInSlash :: IO () testDefaultPathEndingInSlash = assertEqual "Getting default path that ends in a slash" (fromString "/search/term") (defaultPath $ default_request {HC.path = fromString "/search/term/"}) testSamePathsMatch :: IO () testSamePathsMatch = assertBool "The same path should match" $ pathMatches path' path' where path' = fromString "/a/path" testPathSlashAtEnd :: IO () testPathSlashAtEnd = assertBool "Putting the slash at the end should still match paths" $ pathMatches (fromString "/a/path/to/here") (fromString "/a/path/") testPathNoSlashAtEnd :: IO () testPathNoSlashAtEnd = assertBool "Not putting the slash at the end should still match paths" $ pathMatches (fromString "/a/path/to/here") (fromString "/a/path") testDivergingPaths :: IO () testDivergingPaths = assertBool "Diverging paths don't match" $ not $ pathMatches (fromString "/a/path/to/here") (fromString "/a/different/path") testCookieEqualitySuccess :: IO () testCookieEqualitySuccess = assertEqual "The same cookies should be equal" cookie cookie where cookie = default_cookie testCookieEqualityResiliance :: IO () testCookieEqualityResiliance = assertEqual "Cookies should still be equal if extra options are changed" (default_cookie {cookie_persistent = True}) (default_cookie {cookie_host_only = True}) testDomainChangesEquality :: IO () testDomainChangesEquality = assertBool "Changing the domain should make cookies not equal" $ default_cookie /= (default_cookie {cookie_domain = fromString "/search"}) testRemoveCookie :: IO () testRemoveCookie = assertEqual "Removing a cookie works" (Just default_cookie, createCookieJar []) (removeExistingCookieFromCookieJar default_cookie $ createCookieJar [default_cookie]) testRemoveNonexistantCookie :: IO () testRemoveNonexistantCookie = assertEqual "Removing a nonexistant cookie doesn't work" (Nothing, createCookieJar [default_cookie]) (removeExistingCookieFromCookieJar (default_cookie {cookie_name = fromString "key2"}) $ createCookieJar [default_cookie]) testRemoveCorrectCookie :: IO () testRemoveCorrectCookie = assertEqual "Removing only the correct cookie" (Just search_for, createCookieJar [red_herring]) (removeExistingCookieFromCookieJar search_for $ createCookieJar [red_herring, search_for]) where search_for = default_cookie {cookie_name = fromString "name1"} red_herring = default_cookie {cookie_name = fromString "name2"} testEvictExpiredCookies :: IO () testEvictExpiredCookies = assertEqual "Evicting expired cookies works" (createCookieJar [a, c]) (evictExpiredCookies (createCookieJar [a, b, c, d]) middle) where a = default_cookie { cookie_name = fromString "a" , cookie_expiry_time = UTCTime (ModifiedJulianDay 3) (secondsToDiffTime 0) } b = default_cookie { cookie_name = fromString "b" , cookie_expiry_time = UTCTime (ModifiedJulianDay 1) (secondsToDiffTime 0) } c = default_cookie { cookie_name = fromString "c" , cookie_expiry_time = UTCTime (ModifiedJulianDay 3) (secondsToDiffTime 0) } d = default_cookie { cookie_name = fromString "d" , cookie_expiry_time = UTCTime (ModifiedJulianDay 1) (secondsToDiffTime 0) } middle = UTCTime (ModifiedJulianDay 2) (secondsToDiffTime 0) testEvictNoCookies :: IO () testEvictNoCookies = assertEqual "Evicting empty cookie jar" (createCookieJar []) (evictExpiredCookies (createCookieJar []) middle) where middle = UTCTime (ModifiedJulianDay 2) (secondsToDiffTime 0) testComputeCookieStringUpdateLastAccessTime :: IO () testComputeCookieStringUpdateLastAccessTime = assertEqual "Updates last access time upon using cookies" (fromString "name=value", out_cookie_jar) (computeCookieString request cookie_jar now True) where request = default_request cookie_jar = createCookieJar [default_cookie] now = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1) out_cookie_jar = createCookieJar [default_cookie {cookie_last_access_time = now}] testComputeCookieStringHostOnly :: IO () testComputeCookieStringHostOnly = assertEqual "Host only cookies should match host exactly" (fromString "name=value", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request cookie_jar = createCookieJar [default_cookie {cookie_host_only = True}] testComputeCookieStringHostOnlyFilter :: IO () testComputeCookieStringHostOnlyFilter = assertEqual "Host only cookies shouldn't match subdomain" (fromString "", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request {HC.host = fromString "sub1.sub2.google.com"} cookie_jar = createCookieJar [default_cookie { cookie_host_only = True , cookie_domain = fromString "sub2.google.com" } ] testComputeCookieStringDomainMatching :: IO () testComputeCookieStringDomainMatching = assertEqual "Domain matching works for new requests" (fromString "name=value", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request {HC.host = fromString "sub1.sub2.google.com"} cookie_jar = createCookieJar [default_cookie {cookie_domain = fromString "sub2.google.com"}] testComputeCookieStringPathMatching :: IO () testComputeCookieStringPathMatching = assertEqual "Path matching works for new requests" (fromString "name=value", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request {HC.path = fromString "/a/path/to/nowhere"} cookie_jar = createCookieJar [default_cookie {cookie_path = fromString "/a/path"}] testComputeCookieStringPathMatchingFails :: IO () testComputeCookieStringPathMatchingFails = assertEqual "Path matching fails when it should" (fromString "", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request {HC.path = fromString "/a/different/path/to/nowhere"} cookie_jar = createCookieJar [default_cookie {cookie_path = fromString "/a/path"}] testComputeCookieStringPathMatchingWithParms :: IO () testComputeCookieStringPathMatchingWithParms = assertEqual "Path matching succeeds when request has GET params" (fromString "name=value", cookie_jar) (computeCookieString request cookie_jar default_time True) where request = default_request {HC.path = fromString "/a/path/to/nowhere?var=val"} cookie_jar = createCookieJar [default_cookie {cookie_path = fromString "/a/path"}] testComputeCookieStringSecure :: IO () testComputeCookieStringSecure = assertEqual "Secure flag filters properly" (fromString "", cookie_jar) (computeCookieString default_request cookie_jar default_time True) where cookie_jar = createCookieJar [default_cookie {cookie_secure_only = True}] testComputeCookieStringHttpOnly :: IO () testComputeCookieStringHttpOnly = assertEqual "http-only flag filters properly" (fromString "", cookie_jar) (computeCookieString default_request cookie_jar default_time False) where cookie_jar = createCookieJar [default_cookie {cookie_http_only = True}] testComputeCookieStringSort :: IO () testComputeCookieStringSort = assertEqual "Sorting works correctly" (fromString "c1=v1;c3=v3;c4=v4;c2=v2", cookie_jar_out) format_output where now = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 11) cookie_jar = createCookieJar [ default_cookie { cookie_name = fromString "c1" , cookie_value = fromString "v1" , cookie_path = fromString "/all/encompassing/request" } , default_cookie { cookie_name = fromString "c2" , cookie_value = fromString "v2" , cookie_path = fromString "/all" } , default_cookie { cookie_name = fromString "c3" , cookie_value = fromString "v3" , cookie_path = fromString "/all/encompassing" , cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1) } , default_cookie { cookie_name = fromString "c4" , cookie_value = fromString "v4" , cookie_path = fromString "/all/encompassing" , cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 2) } ] cookie_jar_out = createCookieJar [ default_cookie { cookie_name = fromString "c1" , cookie_value = fromString "v1" , cookie_path = fromString "/all/encompassing/request" , cookie_last_access_time = now } , default_cookie { cookie_name = fromString "c2" , cookie_value = fromString "v2" , cookie_path = fromString "/all" , cookie_last_access_time = now } , default_cookie { cookie_name = fromString "c3" , cookie_value = fromString "v3" , cookie_path = fromString "/all/encompassing" , cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1) , cookie_last_access_time = now } , default_cookie { cookie_name = fromString "c4" , cookie_value = fromString "v4" , cookie_path = fromString "/all/encompassing" , cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 2) , cookie_last_access_time = now } ] request = default_request {HC.path = fromString "/all/encompassing/request/path"} format_output = computeCookieString request cookie_jar default_time False testInsertCookiesIntoRequestWorks :: IO () testInsertCookiesIntoRequestWorks = assertEqual "Inserting cookies works" [(CI.mk $ fromString "Cookie", fromString "key=val")] out_headers where out_headers = HC.requestHeaders req (req, _) = insertCookiesIntoRequest req' cookie_jar default_time cookie_jar = createCookieJar [ default_cookie { cookie_name = fromString "key" , cookie_value = fromString "val" } ] req' = default_request {HC.requestHeaders = [(CI.mk $ fromString "Cookie", fromString "otherkey=otherval")]} testReceiveSetCookie :: IO () testReceiveSetCookie = assertEqual "Receiving a Set-Cookie" (createCookieJar [default_cookie]) (receiveSetCookie default_set_cookie default_request default_time True $ createCookieJar []) testReceiveSetCookieTrailingDot :: IO () testReceiveSetCookieTrailingDot = assertEqual "Receiving a Set-Cookie with a trailing domain dot" (createCookieJar []) (receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookieDomain = Just $ fromString "www.google.com."} testReceiveSetCookieLeadingDot :: IO () testReceiveSetCookieLeadingDot = assertEqual "Receiving a Set-Cookie with a leading domain dot" (createCookieJar [default_cookie]) (receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookieDomain = Just $ fromString ".www.google.com"} testReceiveSetCookieNoDomain :: IO () testReceiveSetCookieNoDomain = assertEqual "Receiving cookie without domain" (createCookieJar [default_cookie]) (receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookieDomain = Nothing} testReceiveSetCookieEmptyDomain :: IO () testReceiveSetCookieEmptyDomain = assertEqual "Receiving cookie with empty domain" (createCookieJar [default_cookie]) (receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookieDomain = Just BS.empty} -- Can't test public suffixes until that module is written testReceiveSetCookieNonMatchingDomain :: IO () testReceiveSetCookieNonMatchingDomain = assertEqual "Receiving cookie with non-matching domain" (createCookieJar []) (receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookieDomain = Just $ fromString "www.wikipedia.org"} testReceiveSetCookieHostOnly :: IO () testReceiveSetCookieHostOnly = assertBool "Checking host-only flag gets set" $ cookie_host_only $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieDomain = Nothing} testReceiveSetCookieHostOnlyNotSet :: IO () testReceiveSetCookieHostOnlyNotSet = assertBool "Checking host-only flag doesn't get set" $ not $ cookie_host_only $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieDomain = Just $ fromString "google.com"} testReceiveSetCookieHttpOnly :: IO () testReceiveSetCookieHttpOnly = assertBool "Checking http-only flag gets set" $ cookie_http_only $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieHttpOnly = True} testReceiveSetCookieHttpOnlyNotSet :: IO () testReceiveSetCookieHttpOnlyNotSet = assertBool "Checking http-only flag doesn't get set" $ not $ cookie_http_only $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieHttpOnly = False} testReceiveSetCookieHttpOnlyDrop :: IO () testReceiveSetCookieHttpOnlyDrop = assertEqual "Checking non http request gets dropped" (createCookieJar []) (receiveSetCookie set_cookie default_request default_time False $ createCookieJar []) where set_cookie = default_set_cookie {setCookieHttpOnly = True} testReceiveSetCookieName :: IO () testReceiveSetCookieName = assertEqual "Name gets set correctly" (fromString "name") (cookie_name $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request default_time True $ createCookieJar []) testReceiveSetCookieValue :: IO () testReceiveSetCookieValue = assertEqual "Value gets set correctly" (fromString "value") (cookie_value $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request default_time True $ createCookieJar []) testReceiveSetCookieExpiry :: IO () testReceiveSetCookieExpiry = assertEqual "Expiry gets set correctly" now_plus_diff_time (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request default_time True $ createCookieJar []) where now_plus_diff_time = ((fromRational $ toRational default_diff_time) `addUTCTime` default_time) testReceiveSetCookieNoMaxAge :: IO () testReceiveSetCookieNoMaxAge = assertEqual "Expiry is based on the given value" default_time (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie cookie_without_max_age default_request default_time True $ createCookieJar []) where cookie_without_max_age = default_set_cookie {setCookieMaxAge = Nothing} testReceiveSetCookieNoExpiry :: IO () testReceiveSetCookieNoExpiry = assertEqual "Expiry is based on max age" now_plus_diff_time (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie cookie_without_expiry default_request default_time True $ createCookieJar []) where now_plus_diff_time = ((fromRational $ toRational default_diff_time) `addUTCTime` default_time) cookie_without_expiry = default_set_cookie {setCookieExpires = Nothing} testReceiveSetCookieNoExpiryNoMaxAge :: IO () testReceiveSetCookieNoExpiryNoMaxAge = assertBool "Expiry is set to a future date" $ default_time < (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie basic_cookie default_request default_time True $ createCookieJar []) where basic_cookie = default_set_cookie { setCookieExpires = Nothing, setCookieMaxAge = Nothing } testReceiveSetCookiePath :: IO () testReceiveSetCookiePath = assertEqual "Path gets set correctly" (fromString "/a/path") (cookie_path $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookiePath = Just $ fromString "/a/path"} testReceiveSetCookieNoPath :: IO () testReceiveSetCookieNoPath = assertEqual "Path gets set correctly when nonexistant" (fromString "/a/path/to") (cookie_path $ head $ destroyCookieJar $ receiveSetCookie set_cookie request default_time True $ createCookieJar []) where set_cookie = default_set_cookie {setCookiePath = Nothing} request = default_request {HC.path = fromString "/a/path/to/nowhere"} testReceiveSetCookieCreationTime :: IO () testReceiveSetCookieCreationTime = assertEqual "Creation time gets set correctly" now (cookie_creation_time $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request now True $ createCookieJar []) where now = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1) testReceiveSetCookieAccessTime :: IO () testReceiveSetCookieAccessTime = assertEqual "Last access time gets set correctly" now (cookie_last_access_time $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request now True $ createCookieJar []) where now = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1) testReceiveSetCookiePersistent :: IO () testReceiveSetCookiePersistent = assertBool "Persistent flag gets set correctly" $ cookie_persistent $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieExpires = Just default_time} testReceiveSetCookieSecure :: IO () testReceiveSetCookieSecure = assertBool "Secure flag gets set correctly" $ cookie_secure_only $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [] where set_cookie = default_set_cookie {setCookieSecure = True} testReceiveSetCookieMaxAge :: IO () testReceiveSetCookieMaxAge = assertEqual "Max-Age gets set correctly" total (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request now True $ createCookieJar []) where set_cookie = default_set_cookie { setCookieExpires = Nothing , setCookieMaxAge = Just $ secondsToDiffTime 10 } now = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 12) total = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 22) testReceiveSetCookiePreferMaxAge :: IO () testReceiveSetCookiePreferMaxAge = assertEqual "Max-Age is preferred over Expires" total (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request now True $ createCookieJar []) where set_cookie = default_set_cookie { setCookieExpires = Just exp , setCookieMaxAge = Just $ secondsToDiffTime 10 } exp = UTCTime (ModifiedJulianDay 11) (secondsToDiffTime 5) now = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 12) total = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 22) testReceiveSetCookieExisting :: IO () testReceiveSetCookieExisting = assertEqual "Existing cookie gets updated" t (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie set_cookie default_request default_time True $ createCookieJar [default_cookie]) where set_cookie = default_set_cookie { setCookieExpires = Just t , setCookieMaxAge = Nothing } t = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 12) testReceiveSetCookieExistingCreation :: IO () testReceiveSetCookieExistingCreation = assertEqual "Creation time gets updated in existing cookie" default_time (cookie_creation_time $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request now True $ createCookieJar [default_cookie]) where now = UTCTime (ModifiedJulianDay 10) (secondsToDiffTime 12) testReceiveSetCookieExistingHttpOnly :: IO () testReceiveSetCookieExistingHttpOnly = assertEqual "Existing http-only cookie gets dropped" default_time (cookie_expiry_time $ head $ destroyCookieJar $ receiveSetCookie default_set_cookie default_request default_time False $ createCookieJar [existing_cookie]) where existing_cookie = default_cookie {cookie_http_only = True} testMonoidPreferRecent :: IO () testMonoidPreferRecent = assertEqual "Monoid prefers more recent cookies" (cct $ createCookieJar [c2]) (cct $ createCookieJar [c1] `mappend` createCookieJar [c2]) where c1 = default_cookie {cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 1)} c2 = default_cookie {cookie_creation_time = UTCTime (ModifiedJulianDay 0) (secondsToDiffTime 2)} cct cj = cookie_creation_time $ head $ destroyCookieJar cj ipParseTests :: Spec ipParseTests = do it "Valid IP" testValidIp it "Digit Too High" testIpNumTooHigh it "Too Many Segments" testTooManySegmentsInIp it "Chars in IP" testCharsInIp domainMatchingTests :: Spec domainMatchingTests = do it "Should Match" testDomainMatchesSuccess it "Same Domain" testSameDomain it "Sibling Domain" testSiblingDomain it "Parent Domain" testParentDomain it "Checking for Naive suffix-check" testNaiveSuffixDomain defaultPathTests :: Spec defaultPathTests = do it "Basic default path test" testDefaultPath it "Basic populated default path" testPopulatedDefaultPath it "Default path from request with GET params works" testParamsDefaultPath it "Getting a default path that ends in a slash" testDefaultPathEndingInSlash it "Getting a short default path" testShortDefaultPath pathMatchingTests :: Spec pathMatchingTests = do it "Same paths match" testSamePathsMatch it "Putting slash at end" testPathSlashAtEnd it "Not putting slash at end" testPathNoSlashAtEnd it "Diverging paths don't match" testDivergingPaths equalityTests :: Spec equalityTests = do it "The same cookie should be equal to itself" testCookieEqualitySuccess it "Changing extra options shouldn't change equality" testCookieEqualityResiliance it "Changing a cookie's domain should change its equality" testDomainChangesEquality removeTests :: Spec removeTests = do it "Removing a cookie works" testRemoveCookie it "Removing a nonexistant cookie doesn't work" testRemoveNonexistantCookie it "Removing the correct cookie" testRemoveCorrectCookie evictionTests :: Spec evictionTests = do it "Testing eviction" testEvictExpiredCookies it "Evicting from empty cookie jar" testEvictNoCookies sendingTests :: Spec sendingTests = do it "Updates last access time upon using cookies" testComputeCookieStringUpdateLastAccessTime it "Host-only flag matches exact host" testComputeCookieStringHostOnly it "Host-only flag doesn't match subdomain" testComputeCookieStringHostOnlyFilter it "Domain matching works properly" testComputeCookieStringDomainMatching it "Path matching works" testComputeCookieStringPathMatching it "Path matching fails when it should" testComputeCookieStringPathMatchingFails it "Path matching succeeds when request has GET params" testComputeCookieStringPathMatchingWithParms it "Secure flag filters correctly" testComputeCookieStringSecure it "Http-only flag filters correctly" testComputeCookieStringHttpOnly it "Sorting works correctly" testComputeCookieStringSort it "Inserting cookie header works" testInsertCookiesIntoRequestWorks receivingTests :: Spec receivingTests = do it "Can receive set-cookie" testReceiveSetCookie it "Receiving a Set-Cookie with a trailing dot on the domain" testReceiveSetCookieTrailingDot it "Receiving a Set-Cookie with a leading dot on the domain" testReceiveSetCookieLeadingDot it "Set-Cookie with no domain" testReceiveSetCookieNoDomain it "Set-Cookie with empty domain" testReceiveSetCookieEmptyDomain it "Set-Cookie with non-matching domain" testReceiveSetCookieNonMatchingDomain it "Host-only flag gets set" testReceiveSetCookieHostOnly it "Host-only flag doesn't get set" testReceiveSetCookieHostOnlyNotSet it "Http-only flag gets set" testReceiveSetCookieHttpOnly it "Http-only flag doesn't get set" testReceiveSetCookieHttpOnlyNotSet it "Checking non http request gets dropped" testReceiveSetCookieHttpOnlyDrop it "Name gets set correctly" testReceiveSetCookieName it "Value gets set correctly" testReceiveSetCookieValue it "Expiry gets set correctly" testReceiveSetCookieExpiry it "Expiry gets set based on max age if no expiry is given" testReceiveSetCookieNoExpiry it "Expiry gets set based on given value if no max age is given" testReceiveSetCookieNoMaxAge it "Expiry gets set to a future date if no expiry and no max age are given" testReceiveSetCookieNoExpiryNoMaxAge it "Path gets set correctly when nonexistant" testReceiveSetCookieNoPath it "Path gets set correctly" testReceiveSetCookiePath it "Creation time gets set correctly" testReceiveSetCookieCreationTime it "Last access time gets set correctly" testReceiveSetCookieAccessTime it "Persistent flag gets set correctly" testReceiveSetCookiePersistent it "Existing cookie gets updated" testReceiveSetCookieExisting it "Creation time gets updated in existing cookie" testReceiveSetCookieExistingCreation it "Existing http-only cookie gets dropped" testReceiveSetCookieExistingHttpOnly it "Secure flag gets set correctly" testReceiveSetCookieSecure it "Max-Age flag gets set correctly" testReceiveSetCookieMaxAge it "Max-Age is preferred over Expires" testReceiveSetCookiePreferMaxAge monoidTests :: Spec monoidTests = do it "Monoid prefers more recent cookies" testMonoidPreferRecent cookieTest :: Spec cookieTest = do describe "ipParseTests" ipParseTests describe "domainMatchingTests" domainMatchingTests describe "defaultPathTests" defaultPathTests describe "pathMatchingTests" pathMatchingTests describe "equalityTests" equalityTests describe "removeTests" removeTests describe "evictionTests" evictionTests describe "sendingTests" sendingTests describe "receivingTests" receivingTests describe "monoidTest" monoidTests http-conduit-2.1.8/test/main.hs0000644000000000000000000006263012562145660014607 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} import Test.Hspec import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Lazy.Char8 as L8 import Test.HUnit import Network.Wai hiding (requestBody) import Network.Wai.Conduit (responseSource, sourceRequestBody) import qualified Network.Wai as Wai import Network.Wai.Handler.Warp (runSettings, defaultSettings, setPort, setBeforeMainLoop, Settings, setTimeout) import Network.HTTP.Conduit hiding (port) import qualified Network.HTTP.Conduit as NHC import Network.HTTP.Client.MultipartFormData import Control.Concurrent (forkIO, killThread, putMVar, takeMVar, newEmptyMVar, threadDelay) import Network.HTTP.Types import Control.Exception.Lifted (try, SomeException, bracket, onException, IOException) import qualified Data.IORef as I import qualified Control.Exception as E (catch) import Network (withSocketsDo) import Network.Socket (sClose) import qualified Network.BSD import CookieTest (cookieTest) #if MIN_VERSION_conduit(1,1,0) import Data.Conduit.Network (runTCPServer, serverSettings, HostPreference (..), appSink, appSource, ServerSettings) import Data.Streaming.Network (bindPortTCP, setAfterBind) #define bindPort bindPortTCP #else import Data.Conduit.Network (runTCPServer, serverSettings, HostPreference (..), appSink, appSource, bindPort, serverAfterBind, ServerSettings) #endif import qualified Data.Conduit.Network import System.IO.Unsafe (unsafePerformIO) import Data.Conduit (($$), ($$+-), yield, Flush (Chunk, Flush), await) import Control.Monad (void, forever) import Control.Monad.IO.Class (liftIO) import Data.ByteString.UTF8 (fromString) import Data.Conduit.List (sourceList) import Data.CaseInsensitive (mk) import Data.List (partition) import qualified Data.Conduit.List as CL import qualified Data.Text as T import qualified Data.Text.Encoding as TE import qualified Data.ByteString.Lazy as L import Blaze.ByteString.Builder (fromByteString) import System.IO import Data.Time.Clock import Data.Time.Calendar import qualified Network.Wai.Handler.WarpTLS as WT import Network.Connection (settingDisableCertificateValidation) import Data.Default.Class (def) past :: UTCTime past = UTCTime (ModifiedJulianDay 56200) (secondsToDiffTime 0) future :: UTCTime future = UTCTime (ModifiedJulianDay 562000) (secondsToDiffTime 0) cookie :: Cookie cookie = Cookie { cookie_name = "key" , cookie_value = "value" , cookie_expiry_time = future , cookie_domain = "127.0.0.1" , cookie_path = "/dump_cookies" , cookie_creation_time = past , cookie_last_access_time = past , cookie_persistent = False , cookie_host_only = False , cookie_secure_only = False , cookie_http_only = False } cookie_jar :: CookieJar cookie_jar = createCookieJar [cookie] app :: Wai.Request -> IO Wai.Response app req = case pathInfo req of [] -> if maybe False ("example.com:" `S.isPrefixOf`) $ lookup "host" $ Wai.requestHeaders req then return $ responseLBS status200 [] "homepage for example.com" else return $ responseLBS status200 [] "homepage" ["cookies"] -> return $ responseLBS status200 [tastyCookie] "cookies" ["cookie_redir1"] -> return $ responseLBS status303 [tastyCookie, (hLocation, "/checkcookie")] "" ["checkcookie"] -> return $ case lookup hCookie $ Wai.requestHeaders req of Just "flavor=chocolate-chip" -> responseLBS status200 [] "nom-nom-nom" _ -> responseLBS status412 [] "Baaaw where's my chocolate?" ["infredir", i'] -> let i = read $ T.unpack i' :: Int in return $ responseLBS status303 [(hLocation, S.append "/infredir/" $ S8.pack $ show $ i+1)] (L8.pack $ show i) ["dump_cookies"] -> return $ responseLBS status200 [] $ L.fromChunks $ return $ maybe "" id $ lookup hCookie $ Wai.requestHeaders req ["delayed"] -> return $ responseSource status200 [("foo", "bar")] $ do yield Flush liftIO $ threadDelay 30000000 yield $ Chunk $ fromByteString "Hello World!" _ -> return $ responseLBS status404 [] "not found" where tastyCookie = (mk (fromString "Set-Cookie"), fromString "flavor=chocolate-chip;") nextPort :: I.IORef Int nextPort = unsafePerformIO $ I.newIORef 15452 {-# NOINLINE nextPort #-} getPort :: IO Int getPort = do port <- I.atomicModifyIORef nextPort $ \p -> (p + 1, p + 1) esocket <- try $ bindPort port "*4" case esocket of Left (_ :: IOException) -> getPort Right socket -> do sClose socket return port withApp :: (Wai.Request -> IO Wai.Response) -> (Int -> IO ()) -> IO () withApp app' f = withApp' (const app') f withApp' :: (Int -> Wai.Request -> IO Wai.Response) -> (Int -> IO ()) -> IO () withApp' = withAppSettings id withAppSettings :: (Settings -> Settings) -> (Int -> Wai.Request -> IO Wai.Response) -> (Int -> IO ()) -> IO () withAppSettings modSettings app' f = do port <- getPort baton <- newEmptyMVar bracket (forkIO $ runSettings (modSettings $ setPort port $ setBeforeMainLoop (putMVar baton ()) defaultSettings) (app'' port) `onException` putMVar baton ()) killThread (const $ takeMVar baton >> f port) where app'' port req sendResponse = do res <- app' port req sendResponse res withAppTls :: (Wai.Request -> IO Wai.Response) -> (Int -> IO ()) -> IO () withAppTls app' f = withAppTls' (const app') f withAppTls' :: (Int -> Wai.Request -> IO Wai.Response) -> (Int -> IO ()) -> IO () withAppTls' app' f = do port <- getPort baton <- newEmptyMVar bracket (forkIO $ WT.runTLS WT.defaultTlsSettings ( setPort port $ setBeforeMainLoop (putMVar baton ()) defaultSettings) (app'' port) `onException` putMVar baton ()) killThread (const $ takeMVar baton >> f port) where app'' port req sendResponse = do res <- app' port req sendResponse res main :: IO () main = withSocketsDo $ do mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr] hspec $ do cookieTest describe "simpleHttp" $ do it "gets homepage" $ withApp app $ \port -> do lbs <- simpleHttp $ "http://127.0.0.1:" ++ show port lbs @?= "homepage" it "throws exception on 404" $ withApp app $ \port -> do elbs <- try $ simpleHttp $ concat ["http://127.0.0.1:", show port, "/404"] case elbs of Left (StatusCodeException _ _ _) -> return () _ -> error "Expected an exception" describe "httpLbs" $ do it "preserves 'set-cookie' headers" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/cookies"] withManager $ \manager -> do response <- httpLbs request manager let setCookie = mk (fromString "Set-Cookie") (setCookieHeaders, _) = partition ((== setCookie) . fst) (NHC.responseHeaders response) liftIO $ assertBool "response contains a 'set-cookie' header" $ length setCookieHeaders > 0 it "redirects set cookies" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/cookie_redir1"] withManager $ \manager -> do response <- httpLbs request manager liftIO $ (responseBody response) @?= "nom-nom-nom" it "user-defined cookie jar works" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/dump_cookies"] withManager $ \manager -> do response <- httpLbs (request {redirectCount = 1, cookieJar = Just cookie_jar}) manager liftIO $ (responseBody response) @?= "key=value" it "user-defined cookie jar is not ignored when redirection is disabled" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/dump_cookies"] withManager $ \manager -> do response <- httpLbs (request {redirectCount = 0, cookieJar = Just cookie_jar}) manager liftIO $ (responseBody response) @?= "key=value" it "cookie jar is available in response" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/cookies"] withManager $ \manager -> do response <- httpLbs (request {cookieJar = Just def}) manager liftIO $ (length $ destroyCookieJar $ responseCookieJar response) @?= 1 it "Cookie header isn't touched when no cookie jar supplied" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/dump_cookies"] withManager $ \manager -> do let request_headers = (mk "Cookie", "key2=value2") : filter ((/= mk "Cookie") . fst) (NHC.requestHeaders request) response <- httpLbs (request {NHC.requestHeaders = request_headers, cookieJar = Nothing}) manager liftIO $ (responseBody response) @?= "key2=value2" it "Response cookie jar is nothing when request cookie jar is nothing" $ withApp app $ \port -> do request <- parseUrl $ concat ["http://127.0.0.1:", show port, "/cookies"] withManager $ \manager -> do response <- httpLbs (request {cookieJar = Nothing}) manager liftIO $ (responseCookieJar response) @?= def it "TLS" $ withAppTls app $ \port -> do request <- parseUrl $ "https://127.0.0.1:" ++ show port let set = mkManagerSettings def { settingDisableCertificateValidation = True } Nothing response <- withManagerSettings set $ httpLbs request responseBody response @?= "homepage" describe "manager" $ do it "closes all connections" $ withApp app $ \port1 -> withApp app $ \port2 -> do --FIXME clearSocketsList withManager $ \manager -> do let Just req1 = parseUrl $ "http://127.0.0.1:" ++ show port1 let Just req2 = parseUrl $ "http://127.0.0.1:" ++ show port2 _res1a <- http req1 manager _res1b <- http req1 manager _res2 <- http req2 manager return () --FIXME requireAllSocketsClosed describe "http" $ do it "response body" $ withApp app $ \port -> do withManager $ \manager -> do req <- liftIO $ parseUrl $ "http://127.0.0.1:" ++ show port res1 <- http req manager bss <- responseBody res1 $$+- CL.consume res2 <- httpLbs req manager liftIO $ L.fromChunks bss `shouldBe` responseBody res2 describe "DOS protection" $ do it "overlong headers" $ overLongHeaders $ \port -> do withManager $ \manager -> do let Just req1 = parseUrl $ "http://127.0.0.1:" ++ show port res1 <- try $ http req1 manager case res1 of Left e -> liftIO $ show (e :: SomeException) @?= show OverlongHeaders _ -> error "Shouldn't have worked" it "not overlong headers" $ notOverLongHeaders $ \port -> do withManager $ \manager -> do let Just req1 = parseUrl $ "http://127.0.0.1:" ++ show port _ <- httpLbs req1 manager return () describe "redirects" $ do it "doesn't double escape" $ redir $ \port -> do withManager $ \manager -> do let go (encoded, final) = do let Just req1 = parseUrl $ concat ["http://127.0.0.1:", show port, "/redir/", encoded] res <- httpLbs req1 manager liftIO $ Network.HTTP.Conduit.responseStatus res @?= status200 liftIO $ responseBody res @?= L.fromChunks [TE.encodeUtf8 final] mapM_ go [ ("hello world%2F", "hello world/") , ("%D7%A9%D7%9C%D7%95%D7%9D", "שלום") , ("simple", "simple") , ("hello%20world", "hello world") , ("hello%20world%3f%23", "hello world?#") ] it "TooManyRedirects: redirect request body is preserved" $ withApp app $ \port -> do let Just req = parseUrl $ concat ["http://127.0.0.1:", show port, "/infredir/0"] let go (res, i) = liftIO $ responseBody res @?= (L8.pack $ show i) E.catch (withManager $ \manager -> do void $ http req{redirectCount=5} manager) $ \e -> case e of TooManyRedirects redirs -> mapM_ go (zip redirs [5,4..0 :: Int]) _ -> error $ show e describe "chunked request body" $ do it "works" $ echo $ \port -> do withManager $ \manager -> do let go bss = do let Just req1 = parseUrl $ "http://127.0.0.1:" ++ show port src = sourceList bss lbs = L.fromChunks bss res <- httpLbs req1 { method = "POST" , requestBody = requestBodySourceChunked src } manager liftIO $ Network.HTTP.Conduit.responseStatus res @?= status200 let ts = S.concat . L.toChunks liftIO $ ts (responseBody res) @?= ts lbs mapM_ go [ ["hello", "world"] , replicate 500 "foo\003\n\r" ] describe "no status message" $ do it "works" $ noStatusMessage $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do res <- httpLbs req manager liftIO $ do Network.HTTP.Conduit.responseStatus res `shouldBe` status200 responseBody res `shouldBe` "foo" describe "response body too short" $ do it "throws an exception" $ wrongLength $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show $ ResponseBodyTooShort 50 18) describe "chunked response body" $ do it "no chunk terminator" $ wrongLengthChunk1 $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show IncompleteHeaders) it "incomplete chunk" $ wrongLengthChunk2 $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show InvalidChunkHeaders) it "invalid chunk" $ invalidChunk $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show InvalidChunkHeaders) it "missing header" $ rawApp "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nabcd\r\n\r\n\r\n" $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show InvalidChunkHeaders) it "junk header" $ rawApp "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nabcd\r\njunk\r\n\r\n" $ \port -> do req <- parseUrl $ "http://127.0.0.1:" ++ show port withManager $ \manager -> do eres <- try $ httpLbs req manager liftIO $ either (Left . (show :: HttpException -> String)) (Right . id) eres `shouldBe` Left (show InvalidChunkHeaders) describe "redirect" $ do it "ignores large response bodies" $ do let app' port req = case pathInfo req of ["foo"] -> return $ responseLBS status200 [] "Hello World!" _ -> return $ responseSource status301 [("location", S8.pack $ "http://127.0.0.1:" ++ show port ++ "/foo")] $ forever $ yield $ Chunk $ fromByteString "hello\n" withApp' app' $ \port -> withManager $ \manager -> do req <- liftIO $ parseUrl $ "http://127.0.0.1:" ++ show port res <- httpLbs req manager liftIO $ do Network.HTTP.Conduit.responseStatus res `shouldBe` status200 responseBody res `shouldBe` "Hello World!" describe "multipart/form-data" $ do it "formats correctly" $ do let bd = "---------------------------190723902820679116301912680260" (RequestBodyStream _ givesPopper) <- renderParts bd [partBS "email" "" ,partBS "parent_id" "70488" ,partBS "captcha" "" ,partBS "homeboard" "0chan.hk" ,partBS "text" $ TE.encodeUtf8 ">>72127\r\nМы работаем над этим." ,partFileSource "upload" "nyan.gif" ] ires <- I.newIORef S.empty let loop front popper = do bs <- popper if S.null bs then I.writeIORef ires $ S.concat $ front [] else loop (front . (bs:)) popper givesPopper $ loop id mfd <- I.readIORef ires exam <- S.readFile "multipart-example.bin" mfd @?= exam describe "HTTP/1.0" $ do it "BaseHTTP" $ do let baseHTTP app' = do _ <- appSource app' $$ await yield "HTTP/1.0 200 OK\r\n\r\nThis is it!" $$ appSink app' withCApp baseHTTP $ \port -> withManager $ \manager -> do req <- liftIO $ parseUrl $ "http://127.0.0.1:" ++ show port res1 <- httpLbs req manager res2 <- httpLbs req manager liftIO $ res1 @?= res2 describe "hostAddress" $ do it "overrides host" $ withApp app $ \port -> do entry <- Network.BSD.getHostByName "127.0.0.1" req' <- parseUrl $ "http://example.com:" ++ show port let req = req' { hostAddress = Just $ Network.BSD.hostAddress entry } res <- withManager $ httpLbs req responseBody res @?= "homepage for example.com" describe "managerResponseTimeout" $ do it "works" $ withApp app $ \port -> do req1 <- parseUrl $ "http://localhost:" ++ show port let req2 = req1 { responseTimeout = Just 5000000 } withManagerSettings conduitManagerSettings { managerResponseTimeout = Just 1 } $ \man -> do eres1 <- try $ httpLbs req1 { NHC.path = "/delayed" } man case eres1 of Left (FailedConnectionException _ _) -> return () _ -> error "Did not time out" _ <- httpLbs req2 man return () describe "delayed body" $ do it "works" $ withApp app $ \port -> do req <- parseUrl $ "http://localhost:" ++ show port ++ "/delayed" withManager $ \man -> do _ <- http req man return () it "reuse/connection close tries again" $ do withAppSettings (setTimeout 1) (const app) $ \port -> do req <- parseUrl $ "http://localhost:" ++ show port withManager $ \man -> do res1 <- httpLbs req man liftIO $ threadDelay 3000000 res2 <- httpLbs req man let f res = res { NHC.responseHeaders = filter (not . isDate) (NHC.responseHeaders res) } isDate ("date", _) = True isDate _ = False liftIO $ f res2 `shouldBe` f res1 it "setQueryString" $ do ref <- I.newIORef undefined let app' req = do I.writeIORef ref $ Wai.queryString req return $ responseLBS status200 [] "" withApp app' $ \port -> do let qs = [ ("foo", Just "bar") , (TE.encodeUtf8 "שלום", Just "hola") , ("noval", Nothing) ] withManager $ \man -> do req <- parseUrl $ "http://localhost:" ++ show port _ <- httpLbs (setQueryString qs req) man return () res <- I.readIORef ref res `shouldBe` qs withCApp :: (Data.Conduit.Network.AppData -> IO ()) -> (Int -> IO ()) -> IO () withCApp app' f = do port <- getPort baton <- newEmptyMVar let start = putMVar baton () #if MIN_VERSION_conduit(1,1,0) settings :: ServerSettings settings = setAfterBind (const start) (serverSettings port "*") #else settings :: ServerSettings IO settings = (serverSettings port "*" :: ServerSettings IO) { serverAfterBind = const start } #endif bracket (forkIO $ runTCPServer settings app' `onException` start) killThread (const $ takeMVar baton >> f port) overLongHeaders :: (Int -> IO ()) -> IO () overLongHeaders = withCApp $ \app' -> src $$ appSink app' where src = sourceList $ "HTTP/1.0 200 OK\r\nfoo: " : repeat "bar" notOverLongHeaders :: (Int -> IO ()) -> IO () notOverLongHeaders = withCApp $ \app' -> do appSource app' $$ CL.drop 1 src $$ appSink app' where src = sourceList $ [S.concat $ "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 16384\r\n\r\n" : ( take 16384 $ repeat "x")] redir :: (Int -> IO ()) -> IO () redir = withApp' redirApp where redirApp port req = case pathInfo req of ["redir", foo] -> return $ responseLBS status301 [ ("Location", S8.pack (concat ["http://127.0.0.1:", show port, "/content/"]) `S.append` escape foo) ] "" ["content", foo] -> return $ responseLBS status200 [] $ L.fromChunks [TE.encodeUtf8 foo] _ -> return $ responseLBS status404 [] "" escape = S8.concatMap (S8.pack . encodeUrlChar) . TE.encodeUtf8 encodeUrlChar :: Char -> String encodeUrlChar c -- List of unreserved characters per RFC 3986 -- Gleaned from http://en.wikipedia.org/wiki/Percent-encoding | 'A' <= c && c <= 'Z' = [c] | 'a' <= c && c <= 'z' = [c] | '0' <= c && c <= '9' = [c] encodeUrlChar c@'-' = [c] encodeUrlChar c@'_' = [c] encodeUrlChar c@'.' = [c] encodeUrlChar c@'~' = [c] encodeUrlChar y = let (a, c) = fromEnum y `divMod` 16 b = a `mod` 16 showHex' x | x < 10 = toEnum $ x + (fromEnum '0') | x < 16 = toEnum $ x - 10 + (fromEnum 'A') | otherwise = error $ "Invalid argument to showHex: " ++ show x in ['%', showHex' b, showHex' c] echo :: (Int -> IO ()) -> IO () echo = withApp $ \req -> do bss <- sourceRequestBody req $$ CL.consume return $ responseLBS status200 [] $ L.fromChunks bss noStatusMessage :: (Int -> IO ()) -> IO () noStatusMessage = withCApp $ \app' -> src $$ appSink app' where src = yield "HTTP/1.0 200\r\nContent-Length: 3\r\n\r\nfoo: barbazbin" wrongLength :: (Int -> IO ()) -> IO () wrongLength = withCApp $ \app' -> do _ <- appSource app' $$ await src $$ appSink app' where src = do yield "HTTP/1.0 200 OK\r\nContent-Length: 50\r\n\r\n" yield "Not quite 50 bytes" wrongLengthChunk1 :: (Int -> IO ()) -> IO () wrongLengthChunk1 = withCApp $ \app' -> do _ <- appSource app' $$ await src $$ appSink app' where src = yield "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nWiki\r\n" wrongLengthChunk2 :: (Int -> IO ()) -> IO () wrongLengthChunk2 = withCApp $ \app' -> do _ <- appSource app' $$ await src $$ appSink app' where src = yield "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nWiki\r\n5\r\npedia\r\nE\r\nin\r\n\r\nch\r\n" invalidChunk :: (Int -> IO ()) -> IO () invalidChunk = withCApp $ \app' -> do _ <- appSource app' $$ await src $$ appSink app' where src = yield "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nabcd\r\ngarbage\r\nef\r\n0\r\n\r\n" rawApp :: S8.ByteString -> (Int -> IO ()) -> IO () rawApp bs = withCApp $ \app' -> do _ <- appSource app' $$ await src $$ appSink app' where src = yield bs