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;
}
Manage multiple HTTP requests.
Handles the launching and tracking of multiple Requests
.
Return
true
if this has any requests pending.
Launch new requests, and mark requests that are complete.
Doesn't block if requests are still pending -- check isEmpty
to see if all pending Request
s are complete.
Complete all requests.
Blocks until all requests are completed. Periodically calls
cb
if it is non-null.
An HTTP request.
The address of the server to connect to.
This doesn't include the protocol.
"www.example.com"
, not "http://www.example.com"
.
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"
.
The port to connect to the remote server with.
Should this request use HTTPS?
Construct a new Request
that is managed by http
.
Get the result of the request.
Get the result of the request as a string.
Has this request completed?
Was an error generated for this request?
Get an error string.
How many bytes have been downloaded so far?
How big is the content? If unknown, this is zero.