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