From dbeb079262c8d32b6e0b368f84ba6754f69ab8d1 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 7 Oct 2023 12:23:11 +0200 Subject: [PATCH] Use errors::unsupported if scheme != http(s) --- net/http/client.ha | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/net/http/client.ha b/net/http/client.ha index 38f1227..98fa8bb 100644 --- a/net/http/client.ha +++ b/net/http/client.ha @@ -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" => + 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); };