streaming-utils-0.1.4.7: http, attoparsec, pipes and other utilities for the streaming libraries

Safe HaskellNone
LanguageHaskell2010

Data.ByteString.Streaming.HTTP

Contents

Description

This module replicates `pipes-http` as closely as will type-check, adding a conduit-like http in ResourceT and a primitive simpleHTTP that emits a streaming bytestring rather than a lazy one.

Here is an example GET request that streams the response body to standard output:

import qualified Data.ByteString.Streaming as Q
import Data.ByteString.Streaming.HTTP

main = do
  req <- parseRequest "https://www.example.com"
  m <- newManager tlsManagerSettings 
  withHTTP req m $ \resp -> Q.stdout (responseBody resp) 

Here is an example POST request that also streams the request body from standard input:

{-#LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Streaming as Q
import Data.ByteString.Streaming.HTTP

main = do
   req <- parseRequest "https://httpbin.org/post"
   let req' = req
           { method = "POST"
           , requestBody = stream Q.stdin
           }
   m <- newManager tlsManagerSettings
   withHTTP req' m $ \resp -> Q.stdout (responseBody resp)

Here is the GET request modified to use http and write to a file. runResourceT manages the file handle and the interaction.

import qualified Data.ByteString.Streaming as Q
import Data.ByteString.Streaming.HTTP

main = do
  req <- parseUrl "https://www.example.com"
  m <- newManager tlsManagerSettings 
  runResourceT $ do
     resp <- http request manager 
     Q.writeFile "example.html" (responseBody resp) 

simpleHTTP can be used in ghci like so:

ghci> runResourceT $ Q.stdout $ Q.take 137 $ simpleHTTP "http://lpaste.net/raw/13"
-- Adaptation and extension of a parser for data definitions given in
-- appendix of G. Huttons's paper - Monadic Parser Combinators.
--

Synopsis

Streaming Interface

withHTTP Source #

Arguments

:: Request 
-> Manager 
-> (Response (ByteString IO ()) -> IO a)

Handler for response

-> IO a 

Send an HTTP Request and wait for an HTTP Response

streamN :: Int64 -> ByteString IO () -> RequestBody Source #

Create a RequestBody from a content length and an effectful ByteString

stream :: ByteString IO () -> RequestBody Source #

Create a RequestBody from an effectful ByteString

stream is more flexible than streamN, but requires the server to support chunked transfer encoding.

ghci testing

simpleHTTP :: MonadResource m => String -> ByteString m () Source #

This is a quick method - oleg would call it 'unprofessional' - to bring a web page in view. It sparks its own internal manager and closes itself. Thus something like this makes sense

>>> runResourceT $ Q.putStrLn $ simpleHttp "http://lpaste.net/raw/12"
chunk _ [] = []
chunk n xs = let h = take n xs in h : (chunk n (drop n xs))

but if you try something like

>>> rest <- runResourceT $ Q.putStrLn $ Q.splitAt 40 $ simpleHTTP "http://lpaste.net/raw/146532"
import Data.ByteString.Streaming.HTTP 

it will just be good luck if with

>>> runResourceT $ Q.putStrLn rest

you get the rest of the file:

import qualified Data.ByteString.Streaming.Char8 as Q
main = runResourceT $ Q.putStrLn $ simpleHTTP "http://lpaste.net/raw/146532"

rather than

*** Exception: <socket: 13>: hGetBuf: illegal operation (handle is closed)

Since, of course, the handle was already closed by the first use of runResourceT. The same applies of course to the more hygienic withHTTP above, which permits one to extract an IO (ByteString IO r), by using splitAt or the like.

The reaction of some streaming-io libraries was simply to forbid operations like splitAt. That this paternalism was not viewed as simply outrageous is a consequence of the opacity of the older iteratee-io libraries. It is obvious that I can no more run an effectful bytestring after I have made its effects impossible by using runResourceT (which basically means closeEverythingDown). I might as well try to run it after tossing my machine into the flames. Similarly, it is obvious that I cannot read from a handle after I have applied hClose; there is simply no difference between the two cases.

re-exports

data ResourceT m a :: (* -> *) -> * -> * #

The Resource transformer. This transformer keeps track of all registered actions, and calls them upon exit (via runResourceT). Actions may be registered via register, or resources may be allocated atomically via allocate. allocate corresponds closely to bracket.

Releasing may be performed before exit via the release function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling release will deregister the action, so that a release action will only ever be called once.

Since 0.3.0

Instances

MFunctor ResourceT

Since 0.4.7

Methods

hoist :: Monad m => (forall a. m a -> n a) -> ResourceT m b -> ResourceT n b #

MMonad ResourceT

Since 0.4.7

Methods

embed :: Monad n => (forall a. m a -> ResourceT n a) -> ResourceT m b -> ResourceT n b #

MonadTrans ResourceT 

Methods

lift :: Monad m => m a -> ResourceT m a #

MonadTransControl ResourceT 

Associated Types

type StT (ResourceT :: (* -> *) -> * -> *) a :: * #

Methods

liftWith :: Monad m => (Run ResourceT -> m a) -> ResourceT m a #

restoreT :: Monad m => m (StT ResourceT a) -> ResourceT m a #

MonadRWS r w s m => MonadRWS r w s (ResourceT m) 
MonadBase b m => MonadBase b (ResourceT m) 

Methods

liftBase :: b α -> ResourceT m α #

MonadBaseControl b m => MonadBaseControl b (ResourceT m) 

Associated Types

type StM (ResourceT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ResourceT m) b -> b a) -> ResourceT m a #

restoreM :: StM (ResourceT m) a -> ResourceT m a #

MonadError e m => MonadError e (ResourceT m) 

Methods

throwError :: e -> ResourceT m a #

catchError :: ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #

MonadReader r m => MonadReader r (ResourceT m) 

Methods

ask :: ResourceT m r #

local :: (r -> r) -> ResourceT m a -> ResourceT m a #

reader :: (r -> a) -> ResourceT m a #

MonadState s m => MonadState s (ResourceT m) 

Methods

get :: ResourceT m s #

put :: s -> ResourceT m () #

state :: (s -> (a, s)) -> ResourceT m a #

MonadWriter w m => MonadWriter w (ResourceT m) 

Methods

writer :: (a, w) -> ResourceT m a #

tell :: w -> ResourceT m () #

listen :: ResourceT m a -> ResourceT m (a, w) #

pass :: ResourceT m (a, w -> w) -> ResourceT m a #

Monad m => Monad (ResourceT m) 

Methods

(>>=) :: ResourceT m a -> (a -> ResourceT m b) -> ResourceT m b #

(>>) :: ResourceT m a -> ResourceT m b -> ResourceT m b #

return :: a -> ResourceT m a #

fail :: String -> ResourceT m a #

Functor m => Functor (ResourceT m) 

Methods

fmap :: (a -> b) -> ResourceT m a -> ResourceT m b #

(<$) :: a -> ResourceT m b -> ResourceT m a #

MonadFix m => MonadFix (ResourceT m)

Since: 1.1.8

Methods

mfix :: (a -> ResourceT m a) -> ResourceT m a #

Applicative m => Applicative (ResourceT m) 

Methods

pure :: a -> ResourceT m a #

(<*>) :: ResourceT m (a -> b) -> ResourceT m a -> ResourceT m b #

(*>) :: ResourceT m a -> ResourceT m b -> ResourceT m b #

(<*) :: ResourceT m a -> ResourceT m b -> ResourceT m a #

Alternative m => Alternative (ResourceT m)

Since 1.1.5

Methods

empty :: ResourceT m a #

(<|>) :: ResourceT m a -> ResourceT m a -> ResourceT m a #

some :: ResourceT m a -> ResourceT m [a] #

many :: ResourceT m a -> ResourceT m [a] #

MonadPlus m => MonadPlus (ResourceT m)

Since 1.1.5

Methods

mzero :: ResourceT m a #

mplus :: ResourceT m a -> ResourceT m a -> ResourceT m a #

MonadIO m => MonadIO (ResourceT m) 

Methods

liftIO :: IO a -> ResourceT m a #

MonadThrow m => MonadThrow (ResourceT m) 

Methods

throwM :: Exception e => e -> ResourceT m a #

MonadCatch m => MonadCatch (ResourceT m) 

Methods

catch :: Exception e => ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #

MonadMask m => MonadMask (ResourceT m) 

Methods

mask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b #

uninterruptibleMask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b #

MonadCont m => MonadCont (ResourceT m) 

Methods

callCC :: ((a -> ResourceT m b) -> ResourceT m a) -> ResourceT m a #

(MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) 

Methods

liftResourceT :: ResourceT IO a -> ResourceT m a #

type StT ResourceT a 
type StT ResourceT a = a
type StM (ResourceT m) a 
type StM (ResourceT m) a = StM m a

class (MonadThrow m, MonadIO m, Applicative m, MonadBase IO m) => MonadResource m where #

A Monad which allows for safe resource allocation. In theory, any monad transformer stack which includes a ResourceT can be an instance of MonadResource.

Note: runResourceT has a requirement for a MonadBaseControl IO m monad, which allows control operations to be lifted. A MonadResource does not have this requirement. This means that transformers such as ContT can be an instance of MonadResource. However, the ContT wrapper will need to be unwrapped before calling runResourceT.

Since 0.3.0

Minimal complete definition

liftResourceT

Methods

liftResourceT :: ResourceT IO a -> m a #

Lift a ResourceT IO action into the current Monad.

Since 0.4.0

Instances

MonadResource m => MonadResource (ListT m) 

Methods

liftResourceT :: ResourceT IO a -> ListT m a #

MonadResource m => MonadResource (MaybeT m) 

Methods

liftResourceT :: ResourceT IO a -> MaybeT m a #

(MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) 

Methods

liftResourceT :: ResourceT IO a -> ResourceT m a #

MonadResource m => MonadResource (ByteString m) 
(Error e, MonadResource m) => MonadResource (ErrorT e m) 

Methods

liftResourceT :: ResourceT IO a -> ErrorT e m a #

MonadResource m => MonadResource (ExceptT e m) 

Methods

liftResourceT :: ResourceT IO a -> ExceptT e m a #

MonadResource m => MonadResource (StateT s m) 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a #

MonadResource m => MonadResource (StateT s m) 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a #

(MonadResource m, Functor f) => MonadResource (Stream f m) 

Methods

liftResourceT :: ResourceT IO a -> Stream f m a #

MonadResource m => MonadResource (IdentityT * m) 

Methods

liftResourceT :: ResourceT IO a -> IdentityT * m a #

MonadResource m => MonadResource (ContT * r m) 

Methods

liftResourceT :: ResourceT IO a -> ContT * r m a #

MonadResource m => MonadResource (ReaderT * r m) 

Methods

liftResourceT :: ResourceT IO a -> ReaderT * r m a #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a #

runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a #

Unwrap a ResourceT transformer, and call all registered release actions.

Note that there is some reference counting involved due to resourceForkIO. If multiple threads are sharing the same collection of resources, only the last call to runResourceT will deallocate the resources.

Since 0.3.0