http-enumerator 0.6.5 → 0.6.5.1
raw patch · 2 files changed
+49/−13 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Network.HTTP.Enumerator: Proxy :: Ascii -> Int -> Proxy
+ Network.HTTP.Enumerator: addProxy :: ByteString -> Int -> Request m -> Request m
+ Network.HTTP.Enumerator: data Proxy
+ Network.HTTP.Enumerator: proxy :: Request m -> Maybe Proxy
+ Network.HTTP.Enumerator: proxyHost :: Proxy -> Ascii
+ Network.HTTP.Enumerator: proxyPort :: Proxy -> Int
+ Network.HTTP.Enumerator: rawBody :: Request m -> Bool
- Network.HTTP.Enumerator: Request :: Method -> Bool -> ([X509] -> IO Bool) -> Ascii -> Int -> Ascii -> Query -> RequestHeaders -> RequestBody m -> Request m
+ Network.HTTP.Enumerator: Request :: Method -> Bool -> ([X509] -> IO Bool) -> Ascii -> Int -> Ascii -> Query -> RequestHeaders -> RequestBody m -> Maybe Proxy -> Bool -> Request m
Files
- Network/HTTP/Enumerator.hs +48/−12
- http-enumerator.cabal +1/−1
Network/HTTP/Enumerator.hs view
@@ -22,7 +22,7 @@ -- > import Data.Enumerator.Binary -- > import Network.HTTP.Enumerator -- > import System.IO--- > +-- > -- > main :: IO () -- > main = withFile "google.html" WriteMode $ \handle -> do -- > request <- parseUrl "http://google.com/"@@ -59,6 +59,7 @@ , httpRedirect , redirectIter -- * Datatypes+ , Proxy (..) , Request (..) , RequestBody (..) , Response (..)@@ -70,6 +71,7 @@ -- * Utility functions , parseUrl , applyBasicAuth+ , addProxy , semiParseUrl , lbsIter -- * Request bodies@@ -92,11 +94,11 @@ ) import qualified Data.Enumerator.List as EL import Network.HTTP.Enumerator.HttpParser-import Control.Exception (Exception, bracket)+import Control.Exception (Exception, bracket, throwIO, SomeException, try) import Control.Arrow (first) import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.Trans.Class (lift)-import Control.Failure+import Control.Failure (Failure (failure)) import Data.Typeable (Typeable) import Codec.Binary.UTF8.String (encodeString) import qualified Blaze.ByteString.Builder as Blaze@@ -124,8 +126,13 @@ (addr:_) <- NS.getAddrInfo (Just hints) (Just host') (Just $ show port') sock <- NS.socket (NS.addrFamily addr) (NS.addrSocketType addr) (NS.addrProtocol addr)- NS.connect sock (NS.addrAddress addr)- return sock+ ee <- try' $ NS.connect sock (NS.addrAddress addr)+ case ee of+ Left e -> NS.sClose sock >> throwIO e+ Right () -> return sock+ where+ try' :: IO a -> IO (Either SomeException a)+ try' = try withSocketConn :: MonadIO m => Manager@@ -169,6 +176,14 @@ -- FIXME liftIO $ hClose handle return a ++-- | Define a HTTP proxy, consisting of a hostname and port number.++data Proxy = Proxy+ { proxyHost :: W.Ascii -- ^ The host name of the HTTP proxy.+ , proxyPort :: Int -- ^ The port numner of the HTTP proxy.+ }+ -- | All information on how to connect to a host and what should be sent in the -- HTTP request. --@@ -183,6 +198,8 @@ , queryString :: W.Query -- ^ Automatically escaped for your convenience. , requestHeaders :: W.RequestHeaders , requestBody :: RequestBody m+ , proxy :: Maybe Proxy -- ^ Optional HTTP proxy.+ , rawBody :: Bool -- ^ If True, a chunked and/or gzipped body will not be decoded. Use with caution. } -- | When using the 'RequestBodyEnum' constructor and any function which calls@@ -198,9 +215,9 @@ -- | Add a Basic Auth header (with the specified user name and password) to the -- given Request. Ignore error handling: ----- applyBasicAuth "user" "pass" $ fromJust $ HE.parseUrl url+-- applyBasicAuth "user" "pass" $ fromJust $ parseUrl url -applyBasicAuth :: S8.ByteString -> S8.ByteString -> Request m -> Request m+applyBasicAuth :: S.ByteString -> S.ByteString -> Request m -> Request m applyBasicAuth user passwd req = req { requestHeaders = authHeader : requestHeaders req } where@@ -208,7 +225,13 @@ basic = S8.append "Basic " (B64.encode $ S8.concat [ user, ":", passwd ]) +-- | Add a proxy to the the Request so that the Request when executed will use+-- the provided proxy.+addProxy :: S.ByteString -> Int -> Request m -> Request m+addProxy hst prt req =+ req { proxy = Just $ Proxy hst prt } + -- | A simple representation of the HTTP response created by 'lbsIter'. data Response = Response { statusCode :: Int@@ -242,10 +265,16 @@ -> Manager -> Iteratee S.ByteString m a http Request {..} bodyStep m = do- let h' = S8.unpack host- let withConn = if secure then withSslConn checkCerts else withSocketConn- withConn m h' port requestEnum $$ go+ withConn m connhost connport requestEnum $$ go where+ (useProxy, connhost, connport) =+ case proxy of+ Just p -> (True, S8.unpack (proxyHost p), proxyPort p)+ Nothing -> (False, S8.unpack host, port)+ withConn =+ if secure && not useProxy+ then withSslConn checkCerts+ else withSocketConn (contentLength, bodyEnum) = case requestBody of RequestBodyLBS lbs -> (L.length lbs, enumSingle $ Blaze.fromLazyByteString lbs)@@ -265,6 +294,11 @@ Blaze.fromByteString method `mappend` Blaze.fromByteString " " `mappend`+ (if useProxy+ then Blaze.fromByteString (if secure then "https://" else "http://")+ `mappend` Blaze.fromByteString hh+ else mempty)+ `mappend` (case S8.uncons path of Just ('/', _) -> Blaze.fromByteString path _ -> Blaze.fromByteString "/"@@ -286,13 +320,13 @@ let hs' = map (first CI.mk) hs let mcl = lookup "content-length" hs' let body' x =- if ("transfer-encoding", "chunked") `elem` hs'+ if not rawBody && ("transfer-encoding", "chunked") `elem` hs' then joinI $ chunkedEnumeratee $$ x else case mcl >>= readMay . S8.unpack of Just len -> joinI $ takeLBS len $$ x Nothing -> x let decompress x =- if ("content-encoding", "gzip") `elem` hs'+ if not rawBody && ("content-encoding", "gzip") `elem` hs' then joinI $ Z.ungzip x else returnI x if method == "HEAD"@@ -406,6 +440,8 @@ else [] , requestBody = RequestBodyLBS L.empty , method = "GET"+ , proxy = Nothing+ , rawBody = False } where (beforeSlash, afterSlash) = break (== '/') s
http-enumerator.cabal view
@@ -1,5 +1,5 @@ name: http-enumerator-version: 0.6.5+version: 0.6.5.1 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>