Safe Haskell | Safe-Infered |
---|
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 _ _ bsrc <- http request manager bsrc C.$$ sinkFile "google.html"
The following headers are automatically set by this module, and should not
be added to requestHeaders
:
- Content-Length
- Host
- Accept-Encoding (not currently set, but client usage of this variable will cause breakage).
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.
- simpleHttp :: MonadIO m => String -> m ByteString
- httpLbs :: ResourceIO m => Request m -> Manager -> ResourceT m (Response ByteString)
- http :: ResourceIO m => Request m -> Manager -> ResourceT m (Response (Source m ByteString))
- data Proxy = Proxy {}
- data RequestBody m
- data Response body = Response {
- statusCode :: Status
- responseHeaders :: ResponseHeaders
- responseBody :: body
- data Request m
- def :: Default a => a
- method :: Request m -> Method
- secure :: Request m -> Bool
- host :: Request m -> Ascii
- port :: Request m -> Int
- path :: Request m -> Ascii
- queryString :: Request m -> Ascii
- requestHeaders :: Request m -> RequestHeaders
- requestBody :: Request m -> RequestBody m
- proxy :: Request m -> Maybe Proxy
- socksProxy :: Request m -> Maybe SocksConf
- rawBody :: Request m -> Bool
- decompress :: Request m -> ContentType -> Bool
- redirectCount :: Request m -> Int
- checkStatus :: Request m -> Status -> ResponseHeaders -> Maybe SomeException
- data Manager
- newManager :: ManagerSettings -> IO Manager
- closeManager :: Manager -> IO ()
- withManager :: ResourceIO m => (Manager -> ResourceT m a) -> m a
- data ManagerSettings
- managerConnCount :: ManagerSettings -> Int
- managerCheckCerts :: ManagerSettings -> Ascii -> [X509] -> IO TLSCertificateUsage
- defaultCheckCerts :: Ascii -> [X509] -> IO TLSCertificateUsage
- data Cookie = Cookie {}
- data CookieJar
- createCookieJar :: [Cookie] -> CookieJar
- destroyCookieJar :: CookieJar -> [Cookie]
- updateCookieJar :: Response a -> Request m -> UTCTime -> CookieJar -> (CookieJar, Response a)
- receiveSetCookie :: SetCookie -> Request m -> UTCTime -> Bool -> CookieJar -> CookieJar
- insertCookiesIntoRequest :: Request m -> CookieJar -> UTCTime -> (Request m, CookieJar)
- computeCookieString :: Request m -> CookieJar -> UTCTime -> Bool -> (Ascii, CookieJar)
- evictExpiredCookies :: CookieJar -> UTCTime -> CookieJar
- parseUrl :: Failure HttpException m => String -> m (Request m')
- applyBasicAuth :: ByteString -> ByteString -> Request m -> Request m
- addProxy :: ByteString -> Int -> Request m -> Request m
- lbsResponse :: Resource m => ResourceT m (Response (Source m ByteString)) -> ResourceT m (Response ByteString)
- getRedirectedRequest :: Request m -> ResponseHeaders -> Int -> Maybe (Request m)
- alwaysDecompress :: ContentType -> Bool
- browserDecompress :: ContentType -> Bool
- urlEncodedBody :: Monad m => [(ByteString, ByteString)] -> Request m' -> Request m
- data HttpException
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 httpLbsRedirect
.
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
or
httpRedirect
directly.
httpLbs :: ResourceIO m => Request m -> Manager -> ResourceT 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 :: ResourceIO m => Request m -> Manager -> ResourceT m (Response (Source 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.
Note: Unlike previous versions, this function will perform redirects, as
specified by the redirectCount
setting.
Datatypes
Define a HTTP proxy, consisting of a hostname and port number.
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.
A simple representation of the HTTP response created by lbsConsumer
.
Response | |
|
Request
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.
queryString :: Request m -> AsciiSource
requestBody :: Request m -> RequestBody mSource
socksProxy :: Request m -> Maybe SocksConfSource
Optional SOCKS proxy.
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 -> Maybe SomeExceptionSource
Check the status code. Note that this will run after all redirects are
performed. Default: return a StatusCodeException
on non-2XX responses.
Manager
Keeps track of open connections for keep-alive. May be used concurrently by multiple threads.
newManager :: ManagerSettings -> IO ManagerSource
Create a Manager
. You must manually call closeManager
to shut it down.
closeManager :: Manager -> IO ()Source
withManager :: ResourceIO 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
newManager
.
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 -> Ascii -> [X509] -> IO TLSCertificateUsageSource
Check if the server certificate is valid. Only relevant for HTTPS.
Defaults
defaultCheckCerts :: Ascii -> [X509] -> IO TLSCertificateUsageSource
Check certificates using the operating system's certificate checker.
Cookies
createCookieJar :: [Cookie] -> CookieJarSource
destroyCookieJar :: CookieJar -> [Cookie]Source
:: Response a | Response received from server |
-> Request m | Request which generated the response |
-> UTCTime | Value that should be used as "now" |
-> CookieJar | Current cookie jar |
-> (CookieJar, Response a) | (Updated cookie jar with cookies from the Response, The response stripped of any "Set-Cookie" header) |
This applies receiveSetCookie
to a given Response
:: SetCookie | The |
-> Request m | The request that originated the response that yielded the |
-> UTCTime | Value that should be used as "now" |
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that) |
-> CookieJar | Input cookie jar to modify |
-> CookieJar | Updated cookie jar |
This corresponds to the algorithm described in Section 5.3 "Storage Model"
insertCookiesIntoRequestSource
:: Request m | The request to insert into |
-> CookieJar | Current cookie jar |
-> UTCTime | Value that should be used as "now" |
-> (Request m, CookieJar) | (Ouptut request, Updated cookie jar (last-access-time is updated)) |
This applies the computeCookieString
to a given Request
:: Request m | Input request |
-> CookieJar | Current cookie jar |
-> UTCTime | Value that should be used as "now" |
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that) |
-> (Ascii, CookieJar) | (Contents of a "Cookie" header, Updated cookie jar (last-access-time is updated)) |
This corresponds to the algorithm described in Section 5.4 "The Cookie Header"
:: CookieJar | Input cookie jar |
-> UTCTime | Value that should be used as "now" |
-> CookieJar | Filtered cookie jar |
This corresponds to the eviction algorithm described in Section 5.3 "Storage Model"
Utility functions
applyBasicAuth :: ByteString -> ByteString -> Request m -> Request mSource
addProxy :: ByteString -> Int -> Request m -> Request mSource
Add a proxy to the the Request so that the Request when executed will use the provided proxy.
lbsResponse :: Resource m => ResourceT m (Response (Source m ByteString)) -> ResourceT m (Response ByteString)Source
Convert a Response
that has a Source
body to one with a lazy
ByteString
body.
getRedirectedRequest :: Request m -> ResponseHeaders -> 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 = E.catch (C.runResourceT $ http req' man >> return [req']) (\ (StatusCodeException status headers) -> do l <- myHttp (fromJust $ nextRequest status headers) man return $ req' : l) where req' = req { redirectCount = 0 } nextRequest status headers = getRedirectedRequest req' headers $ W.statusCode status
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
urlEncodedBody :: Monad m => [(ByteString, ByteString)] -> Request m' -> Request mSource
Add url-encoded paramters to the Request
.
This sets a new requestBody
, adds a content-type request header and
changes the method
to POST.