From 8ac0f40896136339154e85653437b71fa9d88d5d Mon Sep 17 00:00:00 2001 From: ading2210 Date: Tue, 30 Apr 2024 13:22:47 -0400 Subject: [PATCH] add support for relative urls --- CHANGELOG.md | 10 +++++++--- README.md | 11 +++++++++++ client/javascript/http.js | 4 ++++ client/package.json | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 279802b..85b4665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Libcurl.js Changelog: +## v0.6.8 (4/30/24): +- Add support for relative URLs in HTTP sessions +- Better error handling for invalid URLs + ## v0.6.7 (3/26/24): - Fix handling of `Request` objects when passed into `libcurl.fetch` @@ -91,14 +95,14 @@ - Add an option to redirect the verbose curl output. - Use separate callbacks for stdout and stderr. -## v0.1.2 (2/1/23): +## v0.1.2 (2/1/24): - Fix bundling the WASM into a single file - Add unit tests -## v0.1.1 (1/29/23): +## v0.1.1 (1/29/24): - Don't set a default websocket proxy URL -## v0.1.0 (1/28/23): +## v0.1.0 (1/28/24): - Initial release on NPM - Add Github Actions for automatic builds - Add WebSocket support diff --git a/README.md b/README.md index d9aa4d6..dcbdd68 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,17 @@ Each HTTP session has the following methods available: - `export_cookies` - Export any cookies which were recorded in the session. This will return an empty string if cookies are disabled or no cookies have been set yet. - `close` - Close all connections and clean up the session. You must call this after you are done using the session, otherwise it will leak memory. +The following attributes are supported: +- `base_url` - Set the base URL used to resolve relative URLs. If this is not defined then an error will be thrown when attempting to fetch a relative URL. + +```js +let session = new libcurl.HTTPSession(); +session.base_url = "https://ading.dev"; +let r = await session.fetch("/projects/"); +console.log(await r.text()); +session.close(); +``` + ### Creating WebSocket Connections: To use WebSockets, create a `libcurl.CurlWebSocket` object, which takes the following arguments: - `url` - The Websocket URL. diff --git a/client/javascript/http.js b/client/javascript/http.js index 380075c..ee2ed6f 100644 --- a/client/javascript/http.js +++ b/client/javascript/http.js @@ -2,6 +2,7 @@ class HTTPSession extends CurlSession { constructor(options={}) { super(); this.options = options; + this.base_url = undefined; this.set_connections(50, 40); this.import_cookies(); @@ -90,6 +91,9 @@ class HTTPSession extends CurlSession { params.headers = params.headers || Object.fromEntries(resource.headers); params.method = params.method || resource.method; } + else { + url = (new URL(url, this.base_url)).href; + } let body = await this.constructor.create_options(params); return await this.request_async(url, params, body); } diff --git a/client/package.json b/client/package.json index a71516e..0a9269c 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "libcurl.js", - "version": "0.6.7", + "version": "0.6.8", "description": "An experimental port of libcurl to WebAssembly for use in the browser.", "main": "libcurl.mjs", "exports": {