module watt.http

A simple interface for making HTTP requests.

HttpInterface and RequestInterface are implemented with classes with the names Http and Request. Import watt.http, and it will import the appropriate implementation for the current platform.

Example

import watt.http;
...
http := new Http();
req  := new Request(http);
req.server = "www.example.com";
req.port = 80;
while (!http.isEmpty()) http.perform();
result := req.toString();

Code Map

//! A simple interface for making HTTP requests.
module watt.http;

public import watt.http.curl;


enum Status
{
	Continue,
	Abort,
}

//! Manage multiple HTTP requests.
interface HttpInterface
{
public:
	fn isEmpty() bool;
	//! Launch new requests, and mark requests that are complete.
	fn perform();
	//! Complete all requests.
	fn loop(cb: Status delegate());
}

//! An HTTP request.
class RequestInterface
{
public:
	//! The address of the server to connect to.
	server: string;
	//! The url to connect to on the server.
	url: string;
	//! The port to connect to the remote server with.
	port: u16;
	//! Should this request use HTTPS?
	secure: bool;


public:
	//! Construct a new Request that is managed by http.
	this(http: HttpInterface) { }
	//! Get the result of the request.
	fn getData() void[];
	//! Get the result of the request as a string.
	fn getString() string;
	//! Has this request completed?
	fn completed() bool;
	//! Was an error generated for this request?
	fn errorGenerated() bool;
	//! Get an error string.
	fn errorString() string;
	//! How many bytes have been downloaded so far?
	fn bytesDownloaded() size_t;
	//! How big is the content? If unknown, this is zero.
	fn contentLength() size_t;
}
interface HttpInterface

Manage multiple HTTP requests.

Handles the launching and tracking of multiple Requests.

fn isEmpty() bool

Return

true if this has any requests pending.

fn perform()

Launch new requests, and mark requests that are complete.

Doesn't block if requests are still pending -- check isEmpty to see if all pending Requests are complete.

fn loop(cb: Status delegate())

Complete all requests.

Blocks until all requests are completed. Periodically calls cb if it is non-null.

class RequestInterface

An HTTP request.

server: string

The address of the server to connect to.

This doesn't include the protocol.
"www.example.com", not "http://www.example.com".

url: string

The url to connect to on the server.

So if server is "www.example.com", and this is set to "/index.html", then this request would look up "www.example.com/index.html".

port: u16

The port to connect to the remote server with.

secure: bool

Should this request use HTTPS?

this(http: HttpInterface)

Construct a new Request that is managed by http.

fn getData() void[]

Get the result of the request.

fn getString() string

Get the result of the request as a string.

fn completed() bool

Has this request completed?

fn errorGenerated() bool

Was an error generated for this request?

fn errorString() string

Get an error string.

fn bytesDownloaded() size_t

How many bytes have been downloaded so far?

fn contentLength() size_t

How big is the content? If unknown, this is zero.