1
Fork 0

Use errors::unsupported if scheme != http(s)

This commit is contained in:
Drew DeVault 2023-10-07 12:23:11 +02:00
parent 78a5d982eb
commit dbeb079262

View file

@ -1,3 +1,4 @@
use errors;
use fmt;
use io;
use net::ip;
@ -43,7 +44,11 @@ export fn client_default_transport(client: *client) *transport = {
return &client.default_transport;
};
fn new_request(client: *client, method: str, target: *uri::uri) request = {
fn new_request(
client: *client,
method: str,
target: *uri::uri,
) (request | errors::unsupported) = {
let req = request {
method = method,
target = alloc(uri::dup(target)),
@ -51,14 +56,17 @@ fn new_request(client: *client, method: str, target: *uri::uri) request = {
transport = null,
body = void,
};
if (req.target.port == 0) {
switch (req.target.scheme) {
case "http" =>
if (req.target.port == 0) {
req.target.port = 80;
case "https" =>
req.target.port = 443;
case => abort("net::http: unsupported URL scheme");
};
case "https" =>
if (req.target.port == 0) {
req.target.port = 443;
};
case =>
return errors::unsupported;
};
let host = match (req.target.host) {
@ -89,8 +97,8 @@ fn new_request_body(
method: str,
target: *uri::uri,
body: io::handle,
) request = {
let req = new_request(client, method, target);
) (request | errors::unsupported) = {
let req = new_request(client, method, target)?;
req.body = body;
const offs = match (io::seek(body, 0, io::whence::CUR)) {
@ -121,14 +129,14 @@ fn uri_origin_form(target: *uri::uri) uri::uri = {
// Performs a synchronous HTTP GET request with the given client.
export fn get(client: *client, target: *uri::uri) (response | error) = {
const req = new_request(client, GET, target);
const req = new_request(client, GET, target)?;
defer request_finish(&req);
return do(client, &req);
};
// Performs a synchronous HTTP HEAD request with the given client.
export fn head(client: *client, target: *uri::uri) (response | error) = {
const req = new_request(client, HEAD, target);
const req = new_request(client, HEAD, target)?;
defer request_finish(&req);
return do(client, &req);
};
@ -142,7 +150,7 @@ export fn post(
target: *uri::uri,
body: io::handle,
) (response | error) = {
const req = new_request_body(client, POST, target, body);
const req = new_request_body(client, POST, target, body)?;
defer request_finish(&req);
return do(client, &req);
};
@ -156,7 +164,7 @@ export fn put(
target: *uri::uri,
body: io::handle,
) (response | error) = {
const req = new_request_body(client, PUT, target, body);
const req = new_request_body(client, PUT, target, body)?;
defer request_finish(&req);
return do(client, &req);
};