net::http::get et al: perform request
This commit is contained in:
parent
ca93a48b12
commit
b297d9d3a0
|
@ -17,14 +17,13 @@ export fn main() void = {
|
||||||
};
|
};
|
||||||
defer uri::finish(&target);
|
defer uri::finish(&target);
|
||||||
|
|
||||||
const req = http::get(&client, &target);
|
const resp = match (http::get(&client, &target)) {
|
||||||
const resp = match (http::do(&client, &req)) {
|
|
||||||
case let err: http::error =>
|
case let err: http::error =>
|
||||||
log::fatal("HTTP error:", http::strerror(err));
|
log::fatal("HTTP error:", http::strerror(err));
|
||||||
case let resp: http::response =>
|
case let resp: http::response =>
|
||||||
yield resp;
|
yield resp;
|
||||||
};
|
};
|
||||||
defer http::request_finish(&req);
|
defer http::response_finish(&resp);
|
||||||
|
|
||||||
log::printfln("HTTP/{}.{}: {} {}",
|
log::printfln("HTTP/{}.{}: {} {}",
|
||||||
resp.version.0, resp.version.1,
|
resp.version.0, resp.version.1,
|
||||||
|
|
|
@ -33,7 +33,7 @@ fn new_request(client: *client, method: str, target: *uri::uri) request = {
|
||||||
let req = request {
|
let req = request {
|
||||||
method = method,
|
method = method,
|
||||||
target = alloc(uri::dup(target)),
|
target = alloc(uri::dup(target)),
|
||||||
header = alloc(client.default_header...),
|
header = header_dup(&client.default_header),
|
||||||
body = void,
|
body = void,
|
||||||
};
|
};
|
||||||
if (req.target.port == 0) {
|
if (req.target.port == 0) {
|
||||||
|
@ -62,8 +62,8 @@ fn new_request(client: *client, method: str, target: *uri::uri) request = {
|
||||||
} else {
|
} else {
|
||||||
host = strings::dup(host);
|
host = strings::dup(host);
|
||||||
};
|
};
|
||||||
|
defer free(host);
|
||||||
append(req.header, ("Host", host));
|
add_header(&req.header, "Host", host);
|
||||||
|
|
||||||
return req;
|
return req;
|
||||||
};
|
};
|
||||||
|
@ -105,52 +105,44 @@ fn uri_origin_form(target: *uri::uri) uri::uri = {
|
||||||
return target;
|
return target;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Prepares a new HTTP GET request for the given client and fills in the default
|
// Performs a synchronous HTTP GET request with the given client.
|
||||||
// headers, such as User-Agent and Host. Provide the return value to [[do]] to
|
export fn get(client: *client, target: *uri::uri) (response | error) = {
|
||||||
// execute the request and free associated resources, or use [[request_finish]]
|
const req = new_request(client, GET, target);
|
||||||
// to free resources without executing the request.
|
defer request_finish(&req);
|
||||||
//
|
return do(client, &req);
|
||||||
// The URI parameter is borrowed from the caller for the lifetime of the request
|
|
||||||
// object.
|
|
||||||
export fn get(client: *client, target: *uri::uri) request = {
|
|
||||||
return new_request(client, GET, target);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Prepares a new HTTP HEAD request for the given client and fills in the
|
// Performs a synchronous HTTP HEAD request with the given client.
|
||||||
// default headers, such as User-Agent and Host. Provide the return value to
|
export fn head(client: *client, target: *uri::uri) (response | error) = {
|
||||||
// [[do]] to execute the request and free associated resources, or use
|
const req = new_request(client, HEAD, target);
|
||||||
// [[request_finish]] to free resources without executing the request.
|
defer request_finish(&req);
|
||||||
//
|
return do(client, &req);
|
||||||
// The URI parameter is borrowed from the caller for the lifetime of the request
|
|
||||||
// object.
|
|
||||||
export fn head(client: *client, target: *uri::uri) request = {
|
|
||||||
return new_request(client, HEAD, target);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Prepares a new HTTP POST request for the given client and fills in the
|
// Performs a synchronous HTTP POST request with the given client.
|
||||||
// default headers, such as User-Agent and Host. Provide the return value to
|
|
||||||
// [[do]] to execute the request and free associated resources, or use
|
|
||||||
// [[request_finish]] to free resources without executing the request.
|
|
||||||
//
|
//
|
||||||
// If the provided I/O handle is seekable, the Content-Length header is added
|
// If the provided I/O handle is seekable, the Content-Length header is added
|
||||||
// automatically. Otherwise, Transfer-Encoding: chunked will be used.
|
// automatically. Otherwise, Transfer-Encoding: chunked will be used.
|
||||||
//
|
export fn post(
|
||||||
// The URI parameter is borrowed from the caller for the lifetime of the request
|
client: *client,
|
||||||
// object.
|
target: *uri::uri,
|
||||||
export fn post(client: *client, target: *uri::uri, body: io::handle) request = {
|
body: io::handle,
|
||||||
return new_request_body(client, POST, target, body);
|
) (response | error) = {
|
||||||
|
const req = new_request_body(client, POST, target, body);
|
||||||
|
defer request_finish(&req);
|
||||||
|
return do(client, &req);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Prepares a new HTTP PUT request for the given client and fills in the
|
// Performs a synchronous HTTP PUT request with the given client.
|
||||||
// default headers, such as User-Agent and Host. Provide the return value to
|
|
||||||
// [[do]] to execute the request and free associated resources, or use
|
|
||||||
// [[request_finish]] to free resources without executing the request.
|
|
||||||
//
|
//
|
||||||
// If the provided I/O handle is seekable, the Content-Length header is added
|
// If the provided I/O handle is seekable, the Content-Length header is added
|
||||||
// automatically. Otherwise, Transfer-Encoding: chunked will be used.
|
// automatically. Otherwise, Transfer-Encoding: chunked will be used.
|
||||||
//
|
export fn put(
|
||||||
// The URI parameter is borrowed from the caller for the lifetime of the request
|
client: *client,
|
||||||
// object.
|
target: *uri::uri,
|
||||||
export fn put(client: *client, target: *uri::uri, body: io::handle) request = {
|
body: io::handle,
|
||||||
return new_request_body(client, POST, target, body);
|
) (response | error) = {
|
||||||
|
const req = new_request_body(client, PUT, target, body);
|
||||||
|
defer request_finish(&req);
|
||||||
|
return do(client, &req);
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,6 +6,7 @@ use strings;
|
||||||
|
|
||||||
// List of HTTP headers.
|
// List of HTTP headers.
|
||||||
// TODO: [](str, []str)
|
// TODO: [](str, []str)
|
||||||
|
// s/add_header/header_add/ et al
|
||||||
export type header = [](str, str);
|
export type header = [](str, str);
|
||||||
|
|
||||||
// Adds a given HTTP header, which may be added more than once. The name should
|
// Adds a given HTTP header, which may be added more than once. The name should
|
||||||
|
@ -43,6 +44,16 @@ export fn header_free(head: *header) void = {
|
||||||
free(*head);
|
free(*head);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Duplicates a set of HTTP headers.
|
||||||
|
export fn header_dup(head: *header) header = {
|
||||||
|
let new: header = [];
|
||||||
|
for (let i = 0z; i < len(head); i += 1) {
|
||||||
|
const (key, val) = head[i];
|
||||||
|
add_header(&new, key, val);
|
||||||
|
};
|
||||||
|
return new;
|
||||||
|
};
|
||||||
|
|
||||||
// Writes a list of HTTP headers to the provided I/O handle in the HTTP wire
|
// Writes a list of HTTP headers to the provided I/O handle in the HTTP wire
|
||||||
// format.
|
// format.
|
||||||
export fn write_header(sink: io::handle, head: *header) (size | io::error) = {
|
export fn write_header(sink: io::handle, head: *header) (size | io::error) = {
|
||||||
|
|
Loading…
Reference in a new issue