Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Module abhttp

A HTTP protocol implementation (currently client only). This allows to send a HTTP request and process its response, accessing contents, cookies and other header fields. Chunked reads are handled properly.

Sample usage:

// get the airbit logo into a file
s:abhttp.Socket=abhttp.Socket("www.airbit.ch", 80);
s.request(abhttp.GET, "/media/14/logo.jpg");
if s.handleResponse()=200 then
  print "Date:",s.fields["Date"];
  f=io.create("airbit.jpg");
  b=s.readContent(1024); // just any buffer size
  while b#null do
    io.write(f, b);
    b=s.readContent(1024)
  end;
  io.close(f)
end;
s.close()
The module also supports simple URL parsing and connection caching via abhttp.parseUrl and abhttp.connect, abhttp.request. The example above can thus be simplified to:
url="http://www.airbit.ch/media/14/logo.jpg";
s:abhttp.Socket=abhttp.request(url, abhttp.GET);
if s.handleResponse()=200 then
  ...

Classes

Socket A HTTP client socket.

Constants

const GET The GET method string.
const HEAD The HEAD method string.
const POST The POST method string.

Variables

sockets The cache of existing sockets, indexed by "host:port".

Functions

closeExpired() Close and and remove any expired socket.
connect(url, ttl=240):abhttp.Socket Get a connected socket by its URL, going through the connection cache.
encodeparams(params) Encode an array of parameters.
parseUrl(url) Parse a URL into host, port and path.
request(url, method, params=..., fields=...):abhttp.Socket Send a request to an URL, and return its socket, going through the connection cache.
function encodeparams(params)
Encode an array of parameters.
Parameters:
params – the parameter array (with keys). May be null.
Returns:the encoded and concatenated parameters.
function parseUrl(url)
Parse a URL into host, port and path. Recognized prefixes are "http:" and "https:".
Parameters:
url – the URL.
Returns:["host":host,"port":port,"path":path].
function closeExpired()
Close and and remove any expired socket.
function connect(url, ttl=240):abhttp.Socket
Get a connected socket by its URL, going through the connection cache.
Parameters:
url – the URL (only host and port are used).
ttl – the time to live for the socket (in seconds).
Returns:the socket.
function request(url, method, params=null, fields=null):abhttp.Socket
Send a request to an URL, and return its socket, going through the connection cache.
Parameters:
url – the URL defining the host and path.
method – the request method (GET, HEAD, POST).
params – the request parameters.
fields – the extra header fields, as an array indexed by name.
Returns:the socket to get the response from, and to call Socket.handleResponse on. See also Socket.request.
mShell Home  > Documentation  > mdoc