http-enumerator-0.1.0: HTTP client package with enumerator interface and HTTPS support.

Network.HTTP.Enumerator

Contents

Description

This module contains everything you need to initiate HTTP connections. Make sure to wrap your code with withHttpEnumerator. 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.Enumerator
 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. By using httpRedirect, it will automatically follow 3xx redirects.

 import Network.HTTP.Enumerator
 import Data.Enumerator.IO
 import System.IO

 main = withFile "google.html" WriteMode $ \handle -> do
     request <- parseUrl "http://google.com/"
     httpRedirect (\_ _ -> iterHandle handle) request

Synopsis

Perform a request

simpleHttp :: (Failure InvalidUrlException m, Failure HttpException m, MonadIO m) => String -> m ByteStringSource

Download the specified URL, following any redirects, and return the response body.

This function will failure an HttpException for any response with a non-2xx status code. It uses parseUrl to parse the input. This function essentially wraps httpLbsRedirect.

httpLbs :: MonadIO m => Request -> m ResponseSource

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 = http lbsIter

Please see lbsIter for more information on how the Response value is created.

httpLbsRedirect :: (Failure InvalidUrlException m, MonadIO m) => Request -> m ResponseSource

Download the specified Request, returning the results as a Response and automatically handling redirects.

This is a simplified version of httpRedirect 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 httpRedirect directly. This function is defined as:

httpLbsRedirect = httpRedirect lbsIter

Please see lbsIter for more information on how the Response value is created.

http :: MonadIO m => (Int -> Headers -> Iteratee ByteString m a) -> Request -> m aSource

The most low-level function for initiating an HTTP request.

The second argument to this function gives a full specification on the request: the host to connect to, whether to use SSL, headers, etc. Please see Request for full details.

The first argument specifies how the response should be handled. It's a function that takes two arguments: the first is the HTTP status code of the response, and the second is a list of all response headers. This module exports lbsIter, which generates a Response value.

Note that this allows you to have fully interleaved IO actions during your HTTP download, making it possible to download very large responses in constant memory.

httpRedirect :: (MonadIO m, Failure InvalidUrlException m) => (Int -> Headers -> Iteratee ByteString m a) -> Request -> m aSource

Same as http, but follows all 3xx redirect status codes that contain a location header.

Datatypes

data Request Source

All information on how to connect to a host and what should be sent in the HTTP request.

If you simply wish to download from a URL, see parseUrl.

Constructors

Request 

Fields

method :: ByteString

HTTP request method, eg GET, POST.

secure :: Bool

Whether to use HTTPS (ie, SSL).

host :: ByteString
 
port :: Int
 
path :: ByteString

Everything from the host to the query string.

queryString :: Headers

Automatically escaped for your convenience.

requestHeaders :: Headers
 
requestBody :: ByteString
 

data Response Source

A simple representation of the HTTP response created by lbsIter.

Utility functions

parseUrl :: Failure InvalidUrlException m => String -> m RequestSource

Convert a URL into a Request.

This defaults some of the values in Request, such as setting method to GET and requestHeaders to [].

Since this function uses Failure, the return monad can be anything that is an instance of Failure, such as IO or Maybe.

withHttpEnumerator :: IO a -> IO aSource

The OpenSSL library requires some initialization of variables to be used, and therefore you must call withOpenSSL before using any of its functions. As this library uses OpenSSL, you must use withOpenSSL as well. (As a side note, you'll also want to use the withSocketsDo function for network activity.)

To future-proof this package against switching to different SSL libraries, we re-export withOpenSSL under this name. You can call this function as early as you like; in fact, simply wrapping the do block of your main function is probably best.

lbsIter :: Monad m => Int -> Headers -> Iteratee ByteString m ResponseSource

Convert the HTTP response into a Response value.

Even though a 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 write your own iteratee and use http or httpRedirect directly.

Exceptions

data InvalidUrlException Source

Thrown by parseUrl when a URL could not be parsed correctly.

httpRedirect also uses the parseUrl function to read the location header from a server, so it can also throw this exception.

data HttpException Source

Throw by simpleHttp when a response does not have a 2xx status code.