http-conduit-1.9.5.1: HTTP client package with conduit interface and HTTPS support.

Safe HaskellNone

Network.HTTP.Conduit

Contents

Description

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)
 import Network.HTTP.Conduit
 import qualified Data.Conduit as C

 main :: IO ()
 main = do
      request <- parseUrl "http://google.com/"
      withManager $ \manager -> 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

 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"
      let request = request' { cookieJar = Just $ createCookieJar [cookie] }
      E.catch (withManager $ httpLbs request)
              (\(StatusCodeException s _ _) ->
                if statusCode==403 then putStrLn "login failed" else return ())

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 }
      res <- withManager $ httpLbs request
      print res

Synopsis

Perform a request

simpleHttp :: MonadIO m => String -> m ByteStringSource

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.

httpLbs :: (MonadBaseControl IO m, MonadResource m) => Request m -> Manager -> m (Response ByteString)Source

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 Source returned by http.

Note: Unlike previous versions, this function will perform redirects, as specified by the redirectCount setting.

http :: (MonadResource m, MonadBaseControl IO m) => Request m -> Manager -> m (Response (ResumableSource m ByteString))Source

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

The first 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 second argument specifies which Manager should be used.

This function then returns a Response with a Source. The Response contains the status code and headers that were sent back to us, and the Source contains the body of the request. Note that this Source allows you to have fully interleaved IO actions during your HTTP download, making it possible to download very large responses in constant memory. You may also directly connect the returned Source into a Sink, perhaps a file or another socket.

An important note: the response body returned by this function represents a live HTTP connection. As such, if you do not use the response body, an open socket will be retained until the containing ResourceT block exits. If you do not need the response body, it is recommended that you explicitly shut down the connection immediately, using the pattern:

 responseBody res $$+- return ()

As a more thorough example, consider the following program. Without the explicit response body closing, the program will run out of file descriptors around the 1000th request (depending on the operating system limits).

 import Control.Monad          (replicateM_)
 import Control.Monad.IO.Class (liftIO)
 import Data.Conduit           (($$+-))
 import Network                (withSocketsDo)
 import Network.HTTP.Conduit

 main = withSocketsDo $ withManager $ \manager -> do
     req <- parseUrl "http://localhost/"
     mapM_ (worker manager req) [1..5000]

 worker manager req i = do
     res <- http req manager
     responseBody res $$+- return () -- The important line
     liftIO $ print (i, responseStatus res)

Note: Unlike previous versions, this function will perform redirects, as specified by the redirectCount setting.

Datatypes

data Proxy Source

Define a HTTP proxy, consisting of a hostname and port number.

Constructors

Proxy 

Fields

proxyHost :: ByteString

The host name of the HTTP proxy.

proxyPort :: Int

The port number of the HTTP proxy.

data RequestBody m Source

When using one of the RequestBodySource / RequestBodySourceChunked constructors, you must ensure that the Source can be called multiple times. Usually this is not a problem.

The RequestBodySourceChunked will send a chunked request body, note that not all servers support this. Only use RequestBodySourceChunked if you know the server you're sending to supports chunked request bodies.

Instances

Show (RequestBody m)

Since 1.8.7

Monad m => Monoid (RequestBody m)

Since 1.8.7

Request

data Request m 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.

The constructor for this data type is not exposed. Instead, you should use either the def method to retrieve a default instance, or parseUrl to construct from a URL, and then use the records below to make modifications. This approach allows http-conduit to add configuration options without breaking backwards compatibility.

For example, to construct a POST request, you could do something like:

 initReq <- parseUrl "http://www.example.com/path"
 let req = initReq
             { method = "POST"
             }

For more information, please see http://www.yesodweb.com/book/settings-types.

Instances

def :: Default a => a

The default value for this type.

method :: Request m -> MethodSource

HTTP request method, eg GET, POST.

secure :: Request m -> BoolSource

Whether to use HTTPS (ie, SSL).

clientCertificates :: Request m -> [(X509, Maybe PrivateKey)]Source

SSL client certificates

path :: Request m -> ByteStringSource

Everything from the host to the query string.

requestHeaders :: Request m -> RequestHeadersSource

Custom HTTP request headers

As already stated in the introduction, the Content-Length and Host headers are set automatically by this module, and shall not be added to requestHeaders.

Moreover, the Accept-Encoding header is set implicitly to gzip for convenience by default. This behaviour can be overridden if needed, by setting the header explicitly to a different value. In order to omit the Accept-Header altogether, set it to the empty string "". If you need an empty Accept-Header (i.e. requesting the identity encoding), set it to a non-empty white-space string, e.g. " ". See RFC 2616 section 14.3 for details about the semantics of the Accept-Header field. If you request a content-encoding not supported by this module, you will have to decode it yourself (see also the decompress field).

Note: Multiple header fields with the same field-name will result in multiple header fields being sent and therefore it's the responsibility of the client code to ensure that the rules from RFC 2616 section 4.2 are honoured.

proxy :: Request m -> Maybe ProxySource

Optional HTTP proxy.

socksProxy :: Request m -> Maybe SocksConfSource

Optional SOCKS proxy.

hostAddress :: Request m -> Maybe HostAddressSource

Optional resolved host address.

Since 1.8.9

rawBody :: Request m -> BoolSource

If True, a chunked and/or gzipped body will not be decoded. Use with caution.

decompress :: Request m -> ContentType -> BoolSource

Predicate to specify whether gzipped data should be decompressed on the fly (see alwaysDecompress and browserDecompress). Default: browserDecompress.

redirectCount :: Request m -> IntSource

How many redirects to follow when getting a resource. 0 means follow no redirects. Default value: 10.

checkStatus :: Request m -> Status -> ResponseHeaders -> CookieJar -> Maybe SomeExceptionSource

Check the status code. Note that this will run after all redirects are performed. Default: return a StatusCodeException on non-2XX responses.

responseTimeout :: Request m -> Maybe IntSource

Number of microseconds to wait for a response. If Nothing, will wait indefinitely. Default: 5 seconds.

cookieJar :: Request m -> Maybe CookieJarSource

A user-defined cookie jar. If Nothing, no cookie handling will take place, "Cookie" headers in requestHeaders will be sent raw, and responseCookieJar will be empty.

Since 1.9.0

getConnectionWrapper :: Request m -> forall n. (MonadResource n, MonadBaseControl IO n) => Maybe Int -> HttpException -> n (ConnRelease n, ConnInfo, ManagedConn) -> n (Maybe Int, (ConnRelease n, ConnInfo, ManagedConn))Source

Wraps the calls for getting new connections. This can be useful for instituting some kind of timeouts. The first argument is the value of responseTimeout. Second argument is the exception to be thrown on failure.

Default: If responseTimeout is Nothing, does nothing. Otherwise, institutes timeout, and returns remaining time for responseTimeout.

Since 1.8.8

Response

data Response body Source

A simple representation of the HTTP response created by lbsConsumer.

Instances

Functor Response

Since 1.1.2.

Typeable1 Response 
Eq body => Eq (Response body) 
Show body => Show (Response body) 

responseStatus :: Response body -> StatusSource

Status code of the response.

responseVersion :: Response body -> HttpVersionSource

HTTP version used by the server.

responseHeaders :: Response body -> ResponseHeadersSource

Response headers sent by the server.

responseBody :: Response body -> bodySource

Response body sent by the server.

responseCookieJar :: Response body -> CookieJarSource

Cookies set on the client after interacting with the server. If cookies have been disabled by setting cookieJar to Nothing, then this will always be empty.

Manager

data Manager Source

Keeps track of open connections for keep-alive. If possible, you should share a single Manager between multiple threads and requests.

newManager :: ManagerSettings -> IO ManagerSource

Create a Manager. You must manually call closeManager to shut it down.

Creating a new Manager is an expensive operation, you are advised to share a single Manager between requests instead.

closeManager :: Manager -> IO ()Source

Close all connections in a Manager. Afterwards, the Manager can be reused if desired.

Note that this doesn't affect currently in-flight connections, meaning you can safely use it without hurting any queries you may have concurrently running.

withManager :: (MonadIO m, MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m) => (Manager -> ResourceT m a) -> m aSource

Create a new manager, use it in the provided function, and then release it.

This function uses the default manager settings. For more control, use withManagerSettings.

withManagerSettings :: (MonadIO m, MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m) => ManagerSettings -> (Manager -> ResourceT m a) -> m aSource

Create a new manager with provided settings, use it in the provided function, and then release it.

Settings

data ManagerSettings Source

Settings for a Manager. Please use the def function and then modify individual settings.

managerConnCount :: ManagerSettings -> IntSource

Number of connections to a single host to keep alive. Default: 10.

managerCheckCerts :: ManagerSettings -> CertificateStore -> ByteString -> [X509] -> IO CertificateUsageSource

Check if the server certificate is valid. Only relevant for HTTPS.

managerCertStore :: ManagerSettings -> IO CertificateStoreSource

Load up the certificate store. By default uses the system store.

managerResponseTimeout :: ManagerSettings -> Maybe IntSource

Default timeout (in microseconds) to be applied to requests which do not provide a timeout value.

Default is 5 seconds

Since 1.9.3

Defaults

defaultCheckCerts :: CertificateStore -> ByteString -> [X509] -> IO CertificateUsageSource

Check certificates using the operating system's certificate checker.

Cookies

Utility functions

parseUrl :: Failure HttpException m => String -> m (Request m')Source

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.

applyBasicAuth :: ByteString -> ByteString -> Request m -> Request mSource

Add a Basic Auth header (with the specified user name and password) to the given Request. Ignore error handling:

applyBasicAuth user pass $ fromJust $ parseUrl url

addProxy :: ByteString -> Int -> Request m -> Request mSource

Add a proxy to the Request so that the Request when executed will use the provided proxy.

lbsResponse :: Monad m => Response (ResumableSource m ByteString) -> m (Response ByteString)Source

Convert a Response that has a Source body to one with a lazy ByteString body.

getRedirectedRequest :: Request m -> ResponseHeaders -> CookieJar -> Int -> Maybe (Request m)Source

If a request is a redirection (status code 3xx) this function will create a new request from the old request, the server headers returned with the redirection, and the redirection code itself. This function returns Nothing if the code is not a 3xx, there is no location header included, or if the redirected response couldn't be parsed with parseUrl.

If a user of this library wants to know the url chain that results from a specific request, that user has to re-implement the redirect-following logic themselves. An example of that might look like this:

 myHttp req man = do
    (res, redirectRequests) <- (`runStateT` []) $
         'httpRedirect'
             9000
             (\req' -> do
                res <- http req'{redirectCount=0} man
                modify (\rqs -> req' : rqs)
                return (res, getRedirectedRequest req' (responseHeaders res) (responseCookieJar res) (W.statusCode (responseStatus res))
                )
             'lift'
             req
    applyCheckStatus (checkStatus req) res
    return redirectRequests

Decompression predicates

alwaysDecompress :: ContentType -> BoolSource

Always decompress a compressed stream.

browserDecompress :: ContentType -> BoolSource

Decompress a compressed stream unless the content-type is 'application/x-tar'.

Request bodies

Network.HTTP.Conduit.MultipartFormData provides an API for building form-data request bodies.

urlEncodedBody :: Monad m => [(ByteString, ByteString)] -> Request m' -> Request mSource

Add url-encoded parameters to the Request.

This sets a new requestBody, adds a content-type request header and changes the method to POST.

Exceptions