# http

### MODULE

http

### MODULE SUMMARY

An HTTP/1.1 client

### DESCRIPTION

This module provides the API to a HTTP/1.1 client according to RFC 2616, however this early version is somewhat limited for instance caching is not supported.

| The functions request/4, and set_options/1, will start the inets application if it was not already started. When starting the inets application the client manager process that spawns request handlers, keeps track of proxy options etc will be started. Normaly the application using this API should have started inets application. This is also true for the ssl application when using https. Also note that an application that does not set the pipeline-timeout value will benefit very little from pipelining as the default timeout is 0.

There are some usage examples in the Inets User's Guide.

### COMMON DATA TYPES

Type definitions that are used more than once in this module:

    boolean() = true | false

    string() = list of ASCII characters

    request_id() = ref()

### HTTP DATA TYPES

Type definitions that are related to HTTP:

For more information about HTTP see rfc 2616

     method() = head | get | put | post | trace | options | delete

     request() - {url(), headers()} |
          {url(), headers(), content_type(), body()}

     url() = string() - Syntax according to the URI definition in rfc 2396, ex: "http://www.erlang.org"

     status_line() =
          {http_version(), status_code(), reason_phrase()}

     http_version() = string() ex: "HTTP/1.1"

     status_code() = integer()

     reason_phrase() = string()

     content_type() = string()

     headers() = [{field(), value()}]

     field() = string()

     value() = string()

     body() = string() | binary()

     filename = string()

### SSL DATA TYPES

Some type definitions relevant when using https, for details ssl(3):

     ssl_options() =
          {verify, code()} | {depth, depth()} | {certfile, path()}
          | {keyfile, path()} | {password, string()} | {cacertfile, path()}
          | {ciphers, string()}

### EXPORTS

`cancel_request(RequestId) -> ok`

Types:

`RequestId = request_id() - A unique identifier as returned by request/4`

Cancels an asynchronous HTTP-request.

`request(Url) -> {ok, Result} | {error, Reason}`

Types:

`Url = url()`
`Result = {status_line(), headers(), body()} | {status_code(), body()} | request_id()`
`Reason = term()`

Equivalent to http:request(get, {Url, []}, [], []).

`request(Method, Request, HTTPOptions, Options) -> {ok, Result} | {ok, saved_to_file} | {error, Reason}`

Types:

`Method = method()`
`Request - request()`
`HTTPOptions - [HttpOption]`
`HTTPOption - {timeout, integer()} | {ssl, ssl_options()} | {autoredirect, boolean()} | {proxy_auth, {userstring(), passwordstring()}}`
`autoredirect`

This option is true by default i.e. the client will automatically retrive the information from the new URI and return that as the result instead of a 30X-result code. Note that for some 30X-result codes automatic redirect is not allowed in these cases the 30X-result will always be returned.
`proxy_auth`
A proxy-authorization header using the provided user name and password will be added to the request.
`Options - [option()]`
`Option - {sync, boolean()} | {stream, StreamTo} | {body_format, body_format()} | {full_result, boolean()} | {headers_as_is, boolean()}`
The request function will be synchronous and return a full http response by default.
`StreamTo = self | filename()`
Streams the body of a 200 response to the calling process or to a file. When streaming to the calling process the the following stream messages will be sent to that process: {http, {RequestId, stream_start, Headers}, {http, {RequestId, stream, BinBodyPart}, {http, {RequestId, stream_end, Headers}. Note that it is possible that chunked encoding will add headers so that there are more headers in the stream_end message than in the stream_start. When streaming to a file and the request is asynchronous the message {http, {RequestId, saved_to_file}} will be sent.
`body_format() = string() | binary()`
The body_format option is only valid for the synchronous request and the default is string. When making an asynchronous request the body will always be received as a binary.
`headers_as_is`
The headers_as_is option is by default false, if set to true the headers provided by the user will be regarded as case sensative. Note that the http standard requires them to be case insensative. This feature should only be used if there is no other way to communicate with the server or for testing purpose. Also note that when this option is used no headers will be automaticly added, all necessary headers has to be provided by the user.
`Result = {status_line(), headers(), body()} | {status_code(), body()} | request_id()`
`Reason = term()`

Sends a HTTP-request. The function can be both synchronous and asynchronous in the later case the function will return {ok, RequestId} and later on message/messaes will be sent to the calling process on the format {http, {RequestId, Result}} {http, {RequestId, {error, Reason}}}, {http, {RequestId, stream_start, Headers}, {http, {RequestId, stream, BinBodyPart}, {http, {RequestId, stream_end, Headers} or {http, {RequestId, saved_to_file}}.

`set_options(Options) -> ok`

Types:

`Options = [Option]`
`Option = {proxy, {Proxy, NoProxy}} | {max_sessions, MaxSessions} | {max_pipeline_length, MaxPipeline} | {pipeline_timeout, PipelineTimeout} | {cookies | CookieMode} | {ipv6, Ipv6Mode} | {verbose, VerboseMode}`
`Proxy = {Hostname, Port}`
`Hostname = string()`

ex: "localhost" or "foo.bar.se"
`Port = integer()`
ex: 8080
`NoProxy = [NoProxyDesc]`
`NoProxyDesc = DomainDesc | HostName | IPDesc`
`DomainDesc = "*.Domain"`
ex: "*.ericsson.se"
`IpDesc = string()`
ex: "134.138" or "[FEDC:BA98" (all IP-adresses starting with 134.138 or FEDC:BA98), "66.35.250.150" or "[2010:836B:4179::836B:4179]" (a complete IP-address).
`MaxSessions = integer()`
Maximum number of persistent connections to a host.Default is 2.
`MaxPipeline = integer()`
Maximum number of outstanding requests on the same connection to a host. Default is 2.
`PipelineTimeout = integer()`
If a persistent connection is idle longer than the pipeline_timeout the client will close the connection. Default is 0. The server may also have a such a time out but you should not count on it!
`CookieMode = enabled | disabled | verify`
Default is disabled. If Cookies are enabled all valid cookies will automatically be saved in the client manager's cookie database. If the option verify is used the function http:verify_cookie/2 has to be called for the cookie to be saved.
`ipv6Mode = enabled | disabled`
By default enabled. This should normally be what you want. When it is enabled you can use both ipv4 and ipv6. The option is here to provide a workaround for buggy ipv6 stacks to ensure that ipv4 will always work.
`VerboseMode = false | verbose |debug | trace`
By default false. This option unless it is set to false switches on different levels of erlang trace on the client. It is a debug feature.

Sets options to be used for subsequent requests. Later implementations may support user profiles, but currently these are global settings for all clients running on the same erlang node.

| If possible the client will keep its connections alive and use them to pipeline requests whenever the circumstances allow. The HTTP/1.1 specification does not provide a guideline for how many requests that would be ideal to pipeline, this very much depends on the application. Note that a very long pipeline may cause a user perceived delays as earlier request may take a long time to complete. The HTTP/1.1 specification does suggest a limit of 2 persistent connections per server, which is the default value of the max_sessions option.

`verify_cookie(SetCookieHeaders, Url) -> ok`

Types:

`SetCookieHeaders = headers() - where field = "set-cookie"`
`Url = url()`

Saves the cookies defined in SetCookieHeaders in the client manager's cookie database. You need to call this function if you set the option cookies to verify.

`cookie_header(Url) -> header()`

Types:

`Url = url()`

Returns the cookie header that would be sent when making a request to Url.

### SEE ALSO

RFC 2616, ssl(3)

### AUTHORS

Ingela Anderton Andin - support@erlang.ericsson.se

inets 4.7.8
Copyright © 1991-2007 Ericsson AB