diff --git a/.changes/http-abort-listener-cleanup.md b/.changes/http-abort-listener-cleanup.md new file mode 100644 index 000000000..20821b2e2 --- /dev/null +++ b/.changes/http-abort-listener-cleanup.md @@ -0,0 +1,6 @@ +--- +http: patch +http-js: patch +--- + +Fix request cancellation, including signals supplied through `Request` objects, abort-listener cleanup, and concurrent response-body reads. `fetch_cancel` and `fetch_cancel_body` now only close the plugin's own requests and responses, instead of any resource id they are given. diff --git a/.changes/http-resource-cleanup.md b/.changes/http-resource-cleanup.md new file mode 100644 index 000000000..9adcc28e8 --- /dev/null +++ b/.changes/http-resource-cleanup.md @@ -0,0 +1,6 @@ +--- +http: patch +http-js: patch +--- + +Fix HTTP resource leaks on cancellation, errors, navigation, window close, and bodyless responses, including WebKit memory leaks. Clean up abandoned responses after garbage collection on supported webviews and reject response statuses outside 200–599. diff --git a/.changes/http-response-clone-cleanup.md b/.changes/http-response-clone-cleanup.md new file mode 100644 index 000000000..f6cf8ee74 --- /dev/null +++ b/.changes/http-response-clone-cleanup.md @@ -0,0 +1,6 @@ +--- +http: patch +http-js: patch +--- + +Fix loss of response URLs and headers when cloning a previously cloned response. diff --git a/examples/api/src-tauri/capabilities/http-e2e.json b/examples/api/src-tauri/capabilities/http-e2e.json new file mode 100644 index 000000000..211e8ce1b --- /dev/null +++ b/examples/api/src-tauri/capabilities/http-e2e.json @@ -0,0 +1,13 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "http-e2e", + "description": "Lets the e2e suite's extra http windows reach the fixture server. Only http is granted, so the example's close confirmation (which needs the dialog plugin) is not registered and closing these windows is not prevented.", + "windows": ["http-e2e-*"], + "platforms": ["linux", "macOS", "windows"], + "permissions": [ + { + "identifier": "http:default", + "allow": [{ "url": "http://127.0.0.1:3004" }] + } + ] +} diff --git a/packages/api-e2e/test/helpers/server.ts b/packages/api-e2e/test/helpers/server.ts index b67c78c43..cce0faf32 100644 --- a/packages/api-e2e/test/helpers/server.ts +++ b/packages/api-e2e/test/helpers/server.ts @@ -37,6 +37,9 @@ export const WEBSOCKET_CLOSE_REQUEST = 'close-me' export const WEBSOCKET_CLOSE_CODE = 4000 export const WEBSOCKET_CLOSE_REASON = 'closed by the fixture server' +/** First (and only) body chunk `GET /stall-body` sends. */ +export const STALL_BODY_FIRST_CHUNK = 'first chunk' + export interface FixtureServer { close(): void } @@ -56,6 +59,11 @@ export interface FixtureServer { * - `GET /raw-header` — an empty response whose `content-disposition` names * {@link RAW_HEADER_FILENAME} in raw UTF-8 bytes. * - `GET /empty/{204,205,304}` — a response with no body and the requested status. + * - `GET /stall[/]` — never answers, so a request can be aborted while it waits for headers. + * - `GET /stall-body[/]` — answers with headers and {@link STALL_BODY_FIRST_CHUNK}, then + * never sends the rest of the body, so a body read can be aborted mid-stream. + * - `GET /stall-state/` — `{ "state": "open" | "closed" | null }`: whether the + * client still holds the connection of the stalled request named ``. * - `ws /ws` — a WebSocket echo endpoint: text and binary messages are sent * back as-is, and {@link WEBSOCKET_CLOSE_REQUEST} makes the server close the * connection with {@link WEBSOCKET_CLOSE_CODE}. On `/ws/headers` the server @@ -64,6 +72,14 @@ export interface FixtureServer { * the client attached a listener). */ export function startFixtureServer(): Promise { + // Connection state of the named `/stall` and `/stall-body` requests. + const stalls = new Map() + const trackStall = (id: string | undefined, res: http.ServerResponse) => { + if (!id) return + stalls.set(id, 'open') + res.on('close', () => stalls.set(id, 'closed')) + } + const server = http.createServer((req, res) => { const chunks: Buffer[] = [] req.on('data', (chunk: Buffer) => chunks.push(chunk)) @@ -80,6 +96,24 @@ export function startFixtureServer(): Promise { } } + if (route === 'stall' && req.method === 'GET') { + // The connection is dropped by the client or by `close()`. + trackStall(rest[0], res) + return + } + + if (route === 'stall-body' && req.method === 'GET') { + trackStall(rest[0], res) + res.writeHead(200, { 'content-type': 'text/plain' }) + res.write(STALL_BODY_FIRST_CHUNK) + return + } + + if (route === 'stall-state' && req.method === 'GET') { + json(res, { state: stalls.get(rest[0]) ?? null }) + return + } + if (route === 'updater' && req.method === 'GET') { const [target] = rest if (target === UPDATER_TARGET_NO_UPDATE) { diff --git a/packages/api-e2e/test/specs/http.spec.ts b/packages/api-e2e/test/specs/http.spec.ts index b0d4476f2..babf109c3 100644 --- a/packages/api-e2e/test/specs/http.spec.ts +++ b/packages/api-e2e/test/specs/http.spec.ts @@ -3,8 +3,29 @@ // SPDX-License-Identifier: MIT import { expect } from '@wdio/globals' -import { tauri, tauriError, describePlugin } from '../helpers/index.js' -import { FIXTURE_SERVER_URL, RAW_HEADER_FILENAME } from '../helpers/server.js' +import { + tauri, + tauriError, + describePlugin, + eventually, + itDesktop +} from '../helpers/index.js' +import { + FIXTURE_SERVER_URL, + RAW_HEADER_FILENAME, + STALL_BODY_FIRST_CHUNK +} from '../helpers/server.js' + +// Requests and responses of the http plugin still in the webview's resource table. +async function httpResourceCount(): Promise { + const names = await tauri((api) => + api.core.invoke('webview_resource_names') + ) + return names.filter( + (name) => + name.endsWith('::FetchRequest') || name.endsWith('::ReqwestResponse') + ).length +} // The example spawns an echo server on this port: it replies with the request // body and the request headers, and sets a `session-token` cookie on requests @@ -204,6 +225,153 @@ describePlugin('http', () => { expect(message).toMatch(/abort|cancel/i) }) + it('aborting a request that waits for headers releases it', async () => { + const before = await httpResourceCount() + const message = await tauriError(async (api, url) => { + const controller = new AbortController() + setTimeout(() => controller.abort(), 300) + await api.http.fetch(`${url}/stall`, { signal: controller.signal }) + }, FIXTURE_SERVER_URL) + expect(message).toMatch(/abort|cancel/i) + await eventually(async () => expect(await httpResourceCount()).toBe(before)) + }) + + it('aborting a pending body read releases the response', async () => { + const before = await httpResourceCount() + const result = await tauri(async (api, url) => { + const controller = new AbortController() + const response = await api.http.fetch(`${url}/stall-body`, { + signal: controller.signal + }) + const reader = response.body!.getReader() + const first = new TextDecoder().decode((await reader.read()).value) + // The server never sends the next chunk, so this read stays pending. + const pending = reader.read() + setTimeout(() => controller.abort(), 300) + try { + await pending + return { first, second: 'resolved' } + } catch (error) { + return { first, second: String(error) } + } + }, FIXTURE_SERVER_URL) + expect(result.first).toBe(STALL_BODY_FIRST_CHUNK) + expect(result.second).toMatch(/abort|cancel/i) + await eventually(async () => expect(await httpResourceCount()).toBe(before)) + }) + + it('canceling the body stream releases the response', async () => { + const before = await httpResourceCount() + const first = await tauri(async (api, url) => { + const response = await api.http.fetch(`${url}/stall-body`) + const reader = response.body!.getReader() + const chunk = new TextDecoder().decode((await reader.read()).value) + await reader.cancel() + return chunk + }, FIXTURE_SERVER_URL) + expect(first).toBe(STALL_BODY_FIRST_CHUNK) + await eventually(async () => expect(await httpResourceCount()).toBe(before)) + }) + + it('the cancel commands do not close resources of other APIs', async () => { + const result = await tauri(async (api) => { + const image = await api.image.Image.new(new Uint8Array(4), 1, 1) + const errors: string[] = [] + for (const command of ['fetch_cancel', 'fetch_cancel_body']) { + try { + await api.core.invoke(`plugin:http|${command}`, { rid: image.rid }) + errors.push('resolved') + } catch (error) { + errors.push(String(error)) + } + } + const size = await image.size() + await image.close() + return { errors, size: `${size.width}x${size.height}` } + }) + expect(result.errors).toEqual([ + expect.stringMatching(/resource id .* is invalid/), + expect.stringMatching(/resource id .* is invalid/) + ]) + expect(result.size).toBe('1x1') + }) + + itDesktop( + 'closing a window cancels its requests and response bodies', + async () => { + // `capabilities/http-e2e.json` grants http to `http-e2e-*` windows. + const label = 'http-e2e-close' + const stallState = async (id: string) => { + const response = await fetch(`${FIXTURE_SERVER_URL}/stall-state/${id}`) + return ((await response.json()) as { state: string | null }).state + } + + const main = await browser.getWindowHandle() + const handles = await browser.getWindowHandles() + await tauri( + (api, label) => + new Promise((resolve, reject) => { + const window = new api.webviewWindow.WebviewWindow(label) + window.once('tauri://created', () => resolve()).catch(reject) + window + .once('tauri://error', (event) => + reject( + new Error(`window creation failed: ${String(event.payload)}`) + ) + ) + .catch(reject) + }), + label + ) + const handle = await eventually(async () => { + const handle = (await browser.getWindowHandles()).find( + (handle) => !handles.includes(handle) + ) + if (!handle) + throw new Error('the new window has no WebDriver handle yet') + return handle + }) + + await browser.switchToWindow(handle) + try { + await eventually(async () => { + const ready = await browser.execute( + () => + typeof (window as { __TAURI__?: { http?: { fetch?: unknown } } }) + .__TAURI__?.http?.fetch === 'function' + ) + if (!ready) throw new Error('the new window has not loaded yet') + }) + await tauri(async (api, url) => { + // One request waiting for headers, one response with an unread body. + void api.http.fetch(`${url}/stall/close-window-send`).catch(() => {}) + // keep the response alive so only the window close can release it + ;(window as { __unreadResponse?: Response }).__unreadResponse = + await api.http.fetch(`${url}/stall-body/close-window-body`) + return null + }, FIXTURE_SERVER_URL) + } finally { + await browser.switchToWindow(main) + } + + await eventually(async () => + expect(await stallState('close-window-send')).toBe('open') + ) + expect(await stallState('close-window-body')).toBe('open') + + await tauri(async (api, label) => { + const window = await api.webviewWindow.WebviewWindow.getByLabel(label) + await window?.close() + return null + }, label) + + await eventually(async () => { + expect(await stallState('close-window-send')).toBe('closed') + expect(await stallState('close-window-body')).toBe('closed') + }) + } + ) + it('rejects URLs outside the configured scope', async () => { const message = await tauriError((api) => api.http.fetch('http://localhost:3999/not-in-scope') diff --git a/plugins/fs/src/file_path.rs b/plugins/fs/src/file_path.rs index dd0c08998..13747efcc 100644 --- a/plugins/fs/src/file_path.rs +++ b/plugins/fs/src/file_path.rs @@ -143,12 +143,8 @@ impl<'de> serde::Deserialize<'de> for FilePath { where E: serde::de::Error, { - FilePath::from_str(s).map_err(|e| { - serde::de::Error::invalid_value( - serde::de::Unexpected::Str(s), - &e.to_string().as_str(), - ) - }) + let Ok(path) = FilePath::from_str(s); + Ok(path) } } diff --git a/plugins/http/api-iife.js b/plugins/http/api-iife.js index aced8b3c8..4510e8b6e 100644 --- a/plugins/http/api-iife.js +++ b/plugins/http/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e,t){"use strict";const r="Request cancelled";return e.fetch=async function(e,n){const a=n?.signal;if(a?.aborted)throw new Error(r);const o=n?.maxRedirections,i=n?.connectTimeout,s=n?.proxy,c=n?.danger;n&&(delete n.maxRedirections,delete n.connectTimeout,delete n.proxy,delete n.danger);const d=n?.headers?n.headers instanceof Headers?n.headers:new Headers(n.headers):new Headers,l=new Request(e,n),u=await l.arrayBuffer();for(const[e,t]of l.headers)d.get(e)||d.set(e,t);const h=(d instanceof Headers?Array.from(d.entries()):Array.isArray(d)?d:Object.entries(d)).map(([e,t])=>[e,"string"==typeof t?t:t.toString()]);if(a?.aborted)throw new Error(r);const w={method:l.method,url:l.url,headers:h,maxRedirections:o,connectTimeout:i,proxy:s,danger:c},_=await t.invoke("plugin:http|fetch",new Uint8Array(u),{headers:{"client-config":encodeURIComponent(JSON.stringify(w))}}),f=()=>t.invoke("plugin:http|fetch_cancel",{rid:_}).catch(()=>{});if(a?.aborted)throw f(),new Error(r);a?.addEventListener("abort",()=>{f()});const{status:p,statusText:y,url:b,headers:g,rid:v}=await t.invoke("plugin:http|fetch_send",{rid:_});let T=!1;const m=()=>T?Promise.resolve():(T=!0,t.invoke("plugin:http|fetch_cancel_body",{rid:v}).catch(()=>{})),R=[101,103,204,205,304].includes(p)?null:new ReadableStream({start:e=>{a?.addEventListener("abort",()=>{e.error(r),m()})},pull:e=>(async e=>{let r;try{r=await t.invoke("plugin:http|fetch_read_body",{rid:v})}catch(t){return e.error(t),void m()}const n=new Uint8Array(r),a=n[n.byteLength-1],o=n.slice(0,n.byteLength-1);1!==a?e.enqueue(o):e.close()})(e),cancel:()=>{m()}});null===R&&await m();const P=new Response(R,{status:p,statusText:y});Object.defineProperty(P,"url",{value:b,writable:!1}),Object.defineProperty(P,"headers",{value:new Headers(g),writable:!1});const A=P.clone.bind(P);return Object.defineProperty(P,"clone",{value:()=>{const e=A();return Object.defineProperty(e,"url",{value:b,writable:!1}),Object.defineProperty(e,"headers",{value:new Headers(g),writable:!1}),e}}),P},e}({},window.__TAURI__.core);Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e,t){"use strict";const r="Request cancelled",n="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef?new FinalizationRegistry(e=>{e()}):void 0;function o(e,t,r){return Object.defineProperty(e,"url",{value:t,writable:!1}),Object.defineProperty(e,"headers",{value:new Headers(r),writable:!1}),Object.defineProperty(e,"clone",{value:function(){return o(Response.prototype.clone.call(this),t,r)}}),e}return e.fetch=async function(e,a){const i=a?.maxRedirections,c=a?.connectTimeout,s=a?.proxy,d=a?.danger;a&&(delete a.maxRedirections,delete a.connectTimeout,delete a.proxy,delete a.danger);const l=a?.headers?a.headers instanceof Headers?a.headers:new Headers(a.headers):new Headers,u=new Request(e,a),f=u.signal,h=await async function(e){const t=e.signal;if(t.aborted)throw new Error(r);if(null===e.body)return new ArrayBuffer(0);const n=e.body.getReader(),o=()=>{n.cancel().catch(()=>{})};t.addEventListener("abort",o,{once:!0});try{const e=new ReadableStream({async pull(e){const{done:t,value:r}=await n.read();t?e.close():e.enqueue(r)}});return await new Response(e).arrayBuffer()}catch(e){throw o(),e}finally{t.removeEventListener("abort",o),n.releaseLock()}}(u);for(const[e,t]of u.headers)l.get(e)||l.set(e,t);const w=(l instanceof Headers?Array.from(l.entries()):Array.isArray(l)?l:Object.entries(l)).map(([e,t])=>[e,"string"==typeof t?t:t.toString()]);if(f.aborted)throw new Error(r);const y={method:u.method,url:u.url,headers:w,maxRedirections:i,connectTimeout:c,proxy:s,danger:d},p=await t.invoke("plugin:http|fetch",new Uint8Array(h),{headers:{"client-config":encodeURIComponent(JSON.stringify(y))}});let _=!1;const b=()=>{_||(_=!0,t.invoke("plugin:http|fetch_cancel",{rid:p}).catch(()=>{}))};if(f.aborted)throw b(),new Error(r);let v;try{f.addEventListener("abort",b,{once:!0}),v=await t.invoke("plugin:http|fetch_send",{rid:p})}catch(e){if(f.aborted)throw Object.assign(new Error(r),{cause:e});throw e}finally{f.removeEventListener("abort",b)}if(f.aborted)throw b(),new Error(r);const{status:g,statusText:R,url:m,headers:T,rid:A}=v;let E;const L={};let U=!1;const P=()=>{U=!0,n?.unregister(L),f.removeEventListener("abort",x),E=void 0},k=()=>U?Promise.resolve():(P(),t.invoke("plugin:http|fetch_cancel_body",{rid:A}).catch(()=>{})),x=()=>{E?.deref()?.error(r),k()},H={start:e=>{E=n?new WeakRef(e):{deref:()=>e},f.addEventListener("abort",x,{once:!0})},pull:e=>(async e=>{try{const r=await t.invoke("plugin:http|fetch_read_body",{rid:A});if(U)return;const n=new Uint8Array(r),o=n[n.byteLength-1],a=n.slice(0,n.byteLength-1);if(1===o)return P(),void e.close();e.enqueue(a)}catch(t){if(U)return;e.error(t),k()}})(e),cancel:()=>k()};try{const e="HEAD"===u.method||[101,103,204,205,304].includes(g)?null:new ReadableStream(H);return null!==e&&n?.register(H,k,L),o(new Response(e,{status:g,statusText:R}),m,T)}catch(e){throw k(),e}},e}({},window.__TAURI__.core);Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})} diff --git a/plugins/http/guest-js/index.ts b/plugins/http/guest-js/index.ts index c6e560731..ad5d2cfa4 100644 --- a/plugins/http/guest-js/index.ts +++ b/plugins/http/guest-js/index.ts @@ -121,8 +121,97 @@ export interface DangerousSettings { acceptInvalidHostnames?: boolean } +interface FetchSendResponse { + status: number + statusText: string + headers: [[string, string]] + url: string + rid: number +} + const ERROR_REQUEST_CANCELLED = 'Request cancelled' +const bodyFinalizer = + typeof FinalizationRegistry !== 'undefined' && typeof WeakRef !== 'undefined' + ? new FinalizationRegistry<() => Promise>((dropBody) => { + void dropBody() + }) + : undefined + +/** + * Read the request body while allowing an abort to interrupt a stalled upload. + * `request.arrayBuffer()` locks the body internally without observing its abort + * signal, so keep access to the reader and cancel it when the signal fires. + * The caller checks the signal after buffering to reject canceled uploads. + */ +async function readRequestBody(request: Request): Promise { + const signal = request.signal + if (signal.aborted) { + throw new Error(ERROR_REQUEST_CANCELLED) + } + + if (request.body === null) { + return new ArrayBuffer(0) + } + + const reader = request.body.getReader() + + const abort = () => { + void reader.cancel().catch(() => {}) + } + signal.addEventListener('abort', abort, { once: true }) + + try { + // Keep a reader we can cancel while the browser collects and validates bytes. + const body = new ReadableStream({ + async pull(controller) { + const { done, value } = await reader.read() + if (done) controller.close() + else controller.enqueue(value) + } + }) + return await new Response(body).arrayBuffer() + } catch (error) { + abort() + throw error + } finally { + signal.removeEventListener('abort', abort) + reader.releaseLock() + } +} + +/** + * Apply the response URL and headers and preserve them when cloning. + * Keep response references out of the body stream's closure scope so WebKit can + * collect the response once it is no longer used. + */ +function patchResponse( + response: Response, + url: string, + headers: [string, string][] +): Response { + // `Response.url` cannot be set via the constructor, so we define it manually. + Object.defineProperty(response, 'url', { value: url, writable: false }) + + // Expose `set-cookie` via `response.headers` (and `getSetCookie()` where + // supported). This is not Fetch-spec compliant for network responses in + // browsers, where `set-cookie` is treated as a forbidden response + // header and is generally not readable from JavaScript. + Object.defineProperty(response, 'headers', { + value: new Headers(headers), + writable: false + }) + + // Use the receiver instead of retaining a response in a bound clone method. + Object.defineProperty(response, 'clone', { + value: function (this: Response): Response { + return patchResponse(Response.prototype.clone.call(this), url, headers) + } + }) + + return response +} + /** * Fetch a resource from the network. It returns a `Promise` that resolves to the * `Response` to that `Request`, whether it is successful or not. @@ -151,12 +240,6 @@ export async function fetch( input: URL | Request | string, init?: RequestInit & ClientOptions ): Promise { - // Optimistically check for abort signal and avoid doing any work - const signal = init?.signal - if (signal?.aborted) { - throw new Error(ERROR_REQUEST_CANCELLED) - } - const maxRedirections = init?.maxRedirections const connectTimeout = init?.connectTimeout const proxy = init?.proxy @@ -177,7 +260,10 @@ export async function fetch( : new Headers() const req = new Request(input, init) - const buffer = await req.arrayBuffer() + const signal = req.signal + + // read the upload with abort support. + const buffer = await readRequestBody(req) // append new headers created by the browser `Request` implementation, // if not already declared by the caller of this function @@ -204,8 +290,8 @@ export async function fetch( ] ) - // Optimistically check for abort signal and avoid doing any work on the Rust side - if (signal?.aborted) { + // Cancellation may have ended the body read with only a partial upload. + if (signal.aborted) { throw new Error(ERROR_REQUEST_CANCELLED) } @@ -229,25 +315,37 @@ export async function fetch( } ) - const abort = () => - invoke('plugin:http|fetch_cancel', { rid }).catch(() => {}) + let requestCancelled = false + const abortRequest = () => { + if (requestCancelled) return + requestCancelled = true + void invoke('plugin:http|fetch_cancel', { rid }).catch(() => {}) + } - // Optimistically check for abort signal - // and avoid doing any work after doing intial work on the Rust side - if (signal?.aborted) { - // we don't care about the result of this promise - void abort() + if (signal.aborted) { + abortRequest() throw new Error(ERROR_REQUEST_CANCELLED) } - signal?.addEventListener('abort', () => void abort()) + let response: FetchSendResponse + try { + signal.addEventListener('abort', abortRequest, { once: true }) + response = await invoke('plugin:http|fetch_send', { + rid + }) + } catch (error) { + if (signal.aborted) { + throw Object.assign(new Error(ERROR_REQUEST_CANCELLED), { cause: error }) + } + throw error + } finally { + signal.removeEventListener('abort', abortRequest) + } - interface FetchSendResponse { - status: number - statusText: string - headers: [[string, string]] - url: string - rid: number + // An abort may have happened before the response's stream listener is attached. + if (signal.aborted) { + abortRequest() + throw new Error(ERROR_REQUEST_CANCELLED) } const { @@ -256,102 +354,93 @@ export async function fetch( url, headers: responseHeaders, rid: responseRid - } = await invoke('plugin:http|fetch_send', { - rid - }) + } = response - let bodyDropped = false + // The finalizer and abort listener must not keep the stream alive through its + // controller. Older webviews without finalization retain explicit cleanup. + let bodyController: + | { deref(): ReadableStreamDefaultController | undefined } + | undefined + const finalizerToken = {} + + let bodyClosed = false + const finishBody = () => { + bodyClosed = true + bodyFinalizer?.unregister(finalizerToken) + signal.removeEventListener('abort', abortBody) + bodyController = undefined + } const dropBody = () => { - if (bodyDropped) return Promise.resolve() - bodyDropped = true - return invoke('plugin:http|fetch_cancel_body', { rid: responseRid }).catch( - () => {} - ) + if (bodyClosed) return Promise.resolve() + finishBody() + return invoke('plugin:http|fetch_cancel_body', { + rid: responseRid + }).catch(() => {}) + } + const abortBody = () => { + bodyController?.deref()?.error(ERROR_REQUEST_CANCELLED) + void dropBody() } const readChunk = async ( controller: ReadableStreamDefaultController ) => { - let data: ArrayBuffer try { - data = await invoke('plugin:http|fetch_read_body', { + const data = await invoke('plugin:http|fetch_read_body', { rid: responseRid }) - } catch (e) { - // close the stream if an error occurs - // and drop the body on Rust side - controller.error(e) + if (bodyClosed) return + + const dataUint8 = new Uint8Array(data) + const lastByte = dataUint8[dataUint8.byteLength - 1] + const actualData = dataUint8.slice(0, dataUint8.byteLength - 1) + + // close when the signal to close (last byte is 1) is sent from the IPC. + if (lastByte === 1) { + finishBody() + controller.close() + return + } + + controller.enqueue(actualData) + } catch (error) { + if (bodyClosed) return + controller.error(error) void dropBody() - return } - - const dataUint8 = new Uint8Array(data) - const lastByte = dataUint8[dataUint8.byteLength - 1] - const actualData = dataUint8.slice(0, dataUint8.byteLength - 1) - - // close when the signal to close (last byte is 1) is sent from the IPC. - if (lastByte === 1) { - controller.close() - return - } - - controller.enqueue(actualData) } - // no body for 101, 103, 204, 205 and 304 - // see https://fetch.spec.whatwg.org/#null-body-status - const body = [101, 103, 204, 205, 304].includes(status) - ? null - : new ReadableStream({ - start: (controller) => { - // listen for abort events to cancel reading - signal?.addEventListener('abort', () => { - controller.error(ERROR_REQUEST_CANCELLED) - void dropBody() - }) - }, - pull: (controller) => readChunk(controller), - cancel: () => { - // Ensure body resources are released on stream cancellation - void dropBody() - } - }) - - if (body === null) { - // No stream will read or cancel this response's Rust resource. - await dropBody() + const source: UnderlyingDefaultSource = { + start: (controller) => { + bodyController = bodyFinalizer + ? new WeakRef(controller) + : { deref: () => controller } + signal.addEventListener('abort', abortBody, { once: true }) + }, + pull: (controller) => readChunk(controller), + cancel: () => dropBody() } - const res = new Response(body, { - status, - statusText - }) + try { + // No body for HEAD requests or null-body statuses. + // see https://fetch.spec.whatwg.org/#null-body-status + const body = + req.method === 'HEAD' || [101, 103, 204, 205, 304].includes(status) + ? null + : new ReadableStream(source) - // `Response.url` cannot be set via the constructor, so we define it manually - Object.defineProperty(res, 'url', { value: url, writable: false }) - - // Expose `set-cookie` via `response.headers` (and `getSetCookie()` where - // supported). This is not Fetch-spec compliant for network responses in - // browsers, where `set-cookie` is treated as a forbidden response - // header and is generally not readable from JavaScript. - Object.defineProperty(res, 'headers', { - value: new Headers(responseHeaders), - writable: false - }) - - // Patch clone() per-instance so cloning preserves the overridden properties - const originalClone = res.clone.bind(res) - Object.defineProperty(res, 'clone', { - value: () => { - const cloned = originalClone() - Object.defineProperty(cloned, 'url', { value: url, writable: false }) - Object.defineProperty(cloned, 'headers', { - value: new Headers(responseHeaders), - writable: false - }) - return cloned + if (body !== null) { + // Clones share this source, so cleanup waits until none can read it. + bodyFinalizer?.register(source, dropBody, finalizerToken) } - }) - return res + return patchResponse( + new Response(body, { status, statusText }), + url, + responseHeaders + ) + } catch (error) { + void dropBody() + throw error + } } diff --git a/plugins/http/src/commands.rs b/plugins/http/src/commands.rs index b22afcf1f..ccf2b64ed 100644 --- a/plugins/http/src/commands.rs +++ b/plugins/http/src/commands.rs @@ -2,18 +2,29 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -use std::{borrow::Cow, future::Future, pin::Pin, str::FromStr, sync::Arc, time::Duration}; +use std::{ + any::type_name, + borrow::Cow, + future::Future, + pin::Pin, + str::FromStr, + sync::{Arc, Mutex}, + time::Duration, +}; use http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, header}; use reqwest::{NoProxy, redirect::Policy}; use serde::{Deserialize, Serialize, de::Error as _}; use tauri::{ Manager, ResourceId, ResourceTable, Runtime, State, Webview, - async_runtime::Mutex, + async_runtime::Mutex as AsyncMutex, command, ipc::{CommandScope, GlobalScope, InvokeBody}, }; -use tokio::sync::oneshot::{Receiver, Sender, channel}; +use tokio::sync::{ + oneshot::{Receiver, Sender, channel}, + watch, +}; use crate::{ Error, Http, Result, @@ -26,46 +37,164 @@ const HTTP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_P /// configure `maxRedirections`. const DEFAULT_MAX_REDIRECTIONS: usize = 10; -struct ReqwestResponse(reqwest::Response); -impl tauri::Resource for ReqwestResponse {} +fn is_bodyless_response(method: &Method, status: StatusCode) -> bool { + method == Method::HEAD + || matches!( + status, + StatusCode::NO_CONTENT | StatusCode::RESET_CONTENT | StatusCode::NOT_MODIFIED + ) +} + +// Removing this per-page token invalidates requests still being prepared. +struct HttpPage; +impl tauri::Resource for HttpPage {} + +impl HttpPage { + /// Returns the webview's current page token, creating one after a navigation + /// closed the previous token. + fn current(state: &Http, webview: &Webview) -> (ResourceId, Arc) { + let mut pages = state.pages.lock().unwrap(); + let mut resources = webview.resources_table(); + + // A stale rid (closed by navigation, or left by a closed webview whose label + // was reused) is not an `HttpPage` in this table, so it is replaced below. + if let Some(&rid) = pages.get(webview.label()) + && let Ok(page) = resources.get::(rid) + { + return (rid, page); + } + + let page = Arc::new(Self); + let rid = resources.add_arc(page.clone()); + pages.insert(webview.label().to_string(), rid); + (rid, page) + } +} + +pub(crate) fn cancel_all_requests(webview: &Webview) { + let mut resources = webview.resources_table(); + let rids: Vec<_> = resources + .names() + .filter_map(|(rid, name)| { + (name == type_name::() + || name == type_name::() + || name == type_name::()) + .then_some(rid) + }) + .collect(); + for rid in rids { + let _ = resources.close(rid); + } +} + +struct ReqwestResponse { + response: AsyncMutex>, + cancel_tx: watch::Sender, +} + +impl ReqwestResponse { + fn new(response: reqwest::Response) -> Self { + let (cancel_tx, _) = watch::channel(false); + Self { + response: AsyncMutex::new(Some(response)), + cancel_tx, + } + } + + fn cancel(&self) { + self.cancel_tx.send_replace(true); + // A pending read holds this lock and will release the response when it + // observes cancellation. Otherwise, release it immediately. + if let Ok(mut response) = self.response.try_lock() { + response.take(); + } + } + + async fn read_chunk(&self) -> Result>> { + let mut cancel_rx = self.cancel_tx.subscribe(); + let mut response = self.response.lock().await; + + // Cancellation may have happened before subscribing or while waiting for the lock. + if *cancel_rx.borrow() { + response.take(); + return Err(Error::RequestCanceled); + } + + let body = response.as_mut().ok_or(Error::RequestCanceled)?; + let result = tokio::select! { + biased; + _ = cancel_rx.changed() => Err(Error::RequestCanceled), + chunk = body.chunk() => chunk.map_err(Error::from), + }; + + match result { + Ok(Some(chunk)) => Ok(Some(chunk.into())), + result => { + response.take(); + result.map(|_| None) + } + } + } +} + +impl tauri::Resource for ReqwestResponse { + fn close(self: Arc) { + self.cancel(); + } +} type CancelableResponseResult = Result; type CancelableResponseFuture = Pin + Send + Sync>>; struct FetchRequest { - fut: Mutex, - abort_tx_rid: ResourceId, - abort_rx_rid: ResourceId, + inner: Mutex, + method: Method, } -impl tauri::Resource for FetchRequest {} -struct AbortSender(Sender<()>); -impl tauri::Resource for AbortRecveiver {} +struct FetchRequestInner { + pending: Option<(CancelableResponseFuture, Receiver<()>)>, + abort_tx: Option>, +} -impl AbortSender { - fn abort(self) { - let _ = self.0.send(()); +impl FetchRequest { + fn new(fut: CancelableResponseFuture, method: Method) -> Self { + let (abort_tx, abort_rx) = channel(); + Self { + inner: Mutex::new(FetchRequestInner { + pending: Some((fut, abort_rx)), + abort_tx: Some(abort_tx), + }), + method, + } + } + + fn take_pending(&self) -> Result<(CancelableResponseFuture, Receiver<()>)> { + self.inner + .lock() + .unwrap() + .pending + .take() + .ok_or(Error::RequestCanceled) + } + + fn cancel(&self) { + let (pending, abort_tx) = { + let mut inner = self.inner.lock().unwrap(); + (inner.pending.take(), inner.abort_tx.take()) + }; + if let Some(abort_tx) = abort_tx { + let _ = abort_tx.send(()); + } + // If sending has not started, release the future here rather than waiting + // for a fetch_send call that may never arrive. + drop(pending); } } -struct AbortRecveiver(Receiver<()>); -impl tauri::Resource for AbortSender {} - -trait AddRequest { - fn add_request(&mut self, fut: CancelableResponseFuture) -> ResourceId; -} - -impl AddRequest for ResourceTable { - fn add_request(&mut self, fut: CancelableResponseFuture) -> ResourceId { - let (tx, rx) = channel::<()>(); - let (tx, rx) = (AbortSender(tx), AbortRecveiver(rx)); - let req = FetchRequest { - fut: Mutex::new(fut), - abort_tx_rid: self.add(tx), - abort_rx_rid: self.add(rx), - }; - self.add(req) +impl tauri::Resource for FetchRequest { + fn close(self: Arc) { + self.cancel(); } } @@ -247,6 +376,9 @@ pub async fn fetch( command_scope: CommandScope, global_scope: GlobalScope, ) -> crate::Result { + // Navigation invalidates this token, including while a request is being prepared. + let (page_rid, page) = HttpPage::current(&state, &webview); + let ClientConfig { method, url, @@ -288,7 +420,7 @@ pub async fn fetch( headers.append(name, byte_string_to_header_value(&v)?); } - match scheme { + let fut: CancelableResponseFuture = match scheme { "http" | "https" => { let scope = Scope::new( command_scope @@ -397,10 +529,7 @@ pub async fn fetch( let fut = async move { request.send().await.map_err(map_request_error) }; - let mut resources_table = webview.resources_table(); - let rid = resources_table.add_request(Box::pin(fut)); - - Ok(rid) + Box::pin(fut) } "data" => { let data_url = @@ -418,23 +547,42 @@ pub async fn fetch( tracing::trace!("{:?}", response); let fut = async move { Ok(reqwest::Response::from(response)) }; - let mut resources_table = webview.resources_table(); - let rid = resources_table.add_request(Box::pin(fut)); - Ok(rid) + Box::pin(fut) } - _ => Err(Error::SchemeNotSupport(scheme.to_string())), + _ => return Err(Error::SchemeNotSupport(scheme.to_string())), + }; + + // Reject requests prepared for a page that navigated away before registration. + let mut resources = webview.resources_table(); + let current = resources + .get::(page_rid) + .map_err(|_| Error::RequestCanceled)?; + if !Arc::ptr_eq(¤t, &page) { + return Err(Error::RequestCanceled); } + + Ok(resources.add(FetchRequest::new(fut, method))) +} + +/// Closes `rid` only if it is one of this plugin's requests or responses, so the +/// cancel commands cannot close resources that belong to other APIs. +fn close_http_resource( + resources: &mut ResourceTable, + rid: ResourceId, + allow_request: bool, +) -> Result<()> { + let is_request = allow_request && resources.get::(rid).is_ok(); + if !is_request { + resources.get::(rid)?; + } + resources.close(rid)?; + Ok(()) } #[command] pub fn fetch_cancel(webview: Webview, rid: ResourceId) -> crate::Result<()> { - let mut resources_table = webview.resources_table(); - let req = resources_table.get::(rid)?; - let abort_tx = resources_table.take::(req.abort_tx_rid)?; - if let Some(abort_tx) = Arc::into_inner(abort_tx) { - abort_tx.abort(); - } - Ok(()) + // The response reuses the request's rid, so this cancels either one. + close_http_resource(&mut webview.resources_table(), rid, true) } // Header values are byte strings. Decode as ISO-8859-1 so the raw bytes survive @@ -462,53 +610,69 @@ pub async fn fetch_send( webview: Webview, rid: ResourceId, ) -> crate::Result { - let (req, abort_rx) = { - let mut resources_table = webview.resources_table(); + let (req, (mut fut, abort_rx)) = { + let resources_table = webview.resources_table(); let req = resources_table.get::(rid)?; - let abort_rx = resources_table.take::(req.abort_rx_rid)?; - (req, abort_rx) + let pending = req.take_pending()?; + (req, pending) }; - let Some(abort_rx) = Arc::into_inner(abort_rx) else { - return Err(Error::RequestCanceled); - }; - - let mut fut = req.fut.lock().await; - let res = tokio::select! { + biased; + _ = abort_rx => Err(Error::RequestCanceled), res = fut.as_mut() => res, - _ = abort_rx.0 => Err(Error::RequestCanceled), }; - // Release resources - { - let mut resources_table = webview.resources_table(); - let _ = resources_table.take::(req.abort_tx_rid); - let _ = resources_table.close(rid); - } + let response = res.and_then(|res| { + #[cfg(feature = "tracing")] + tracing::trace!("{:?}", res); - let res = res?; + let status = res.status(); + // The JavaScript Response constructor only accepts statuses from 200 to 599. + if !(200..=599).contains(&status.as_u16()) { + return Err(Error::InvalidResponseStatus(status.as_u16())); + } - #[cfg(feature = "tracing")] - tracing::trace!("{:?}", res); + let url = res.url().to_string(); + let mut headers = Vec::new(); + for (key, val) in res.headers().iter() { + headers.push((key.as_str().into(), header_value_to_byte_string(val))); + } - let status = res.status(); - let url = res.url().to_string(); - let mut headers = Vec::new(); - for (key, val) in res.headers().iter() { - headers.push((key.as_str().into(), header_value_to_byte_string(val))); - } + let metadata = FetchResponse { + status: status.as_u16(), + status_text: status.canonical_reason().unwrap_or_default().to_string(), + headers, + url, + rid, + }; + Ok((res, metadata)) + }); let mut resources_table = webview.resources_table(); - let rid = resources_table.add(ReqwestResponse(res)); + let current = resources_table + .get::(rid) + .map_err(|_| Error::RequestCanceled)?; + if !Arc::ptr_eq(¤t, &req) { + return Err(Error::RequestCanceled); + } - Ok(FetchResponse { - status: status.as_u16(), - status_text: status.canonical_reason().unwrap_or_default().to_string(), - headers, - url, - rid, - }) + match response { + Ok((res, metadata)) => { + if is_bodyless_response(&req.method, res.status()) { + // No body reader will consume or cancel this response. + let _ = resources_table.close(rid); + } else { + // Keep cancellation valid across the request-to-response transition. + resources_table.replace(rid, ReqwestResponse::new(res)); + } + Ok(metadata) + } + Err(error) => { + let _ = resources_table.close(rid); + Err(error) + } + } } #[command] @@ -521,23 +685,21 @@ pub async fn fetch_read_body( resources_table.get::(rid)? }; - // SAFETY: we can access the inner value mutably - // because we are the only ones with a reference to it - // and we don't want to use `Arc::into_inner` because we want to keep the value in the table - // for potential future calls to `fetch_cancel_body` - let res_ptr = Arc::as_ptr(&res) as *mut ReqwestResponse; - let res = unsafe { &mut *res_ptr }; - let res = &mut res.0; - - let Some(chunk) = res.chunk().await? else { + let result = res.read_chunk().await; + if !matches!(&result, Ok(Some(_))) { let mut resources_table = webview.resources_table(); - resources_table.close(rid)?; + if let Ok(current) = resources_table.get::(rid) + && Arc::ptr_eq(¤t, &res) + { + let _ = resources_table.close(rid); + } + } + let Some(mut chunk) = result? else { // return a response with a single byte to indicate that the body is empty return Ok(tauri::ipc::Response::new(vec![1])); }; - let mut chunk = chunk.to_vec(); // append a 0 byte to indicate that the body is not empty chunk.push(0); @@ -549,9 +711,7 @@ pub async fn fetch_cancel_body( webview: Webview, rid: ResourceId, ) -> crate::Result<()> { - let mut resources_table = webview.resources_table(); - resources_table.close(rid)?; - Ok(()) + close_http_resource(&mut webview.resources_table(), rid, false) } // forbidden headers per fetch spec https://fetch.spec.whatwg.org/#terminology-headers diff --git a/plugins/http/src/error.rs b/plugins/http/src/error.rs index 3f68f1cdb..83d770fc1 100644 --- a/plugins/http/src/error.rs +++ b/plugins/http/src/error.rs @@ -24,6 +24,9 @@ pub enum Error { /// Error from the [`http`] crate, raised while building the response of a `data:` URL. #[error(transparent)] Http(#[from] http::Error), + /// The response status cannot be represented by the JavaScript Fetch API. + #[error("response status {0} is not supported by the Fetch API")] + InvalidResponseStatus(u16), /// A header name given by the frontend is not a valid HTTP header name. #[error(transparent)] HttpInvalidHeaderName(#[from] http::header::InvalidHeaderName), @@ -46,7 +49,7 @@ pub enum Error { /// The requested URL uses an unsupported scheme. Only `http`, `https` and `data` are handled. #[error("scheme {0} not supported")] SchemeNotSupport(String), - /// The request was aborted by the frontend before the response was received. + /// The request or response body was canceled. #[error("Request canceled")] RequestCanceled, /// Error from the file system plugin. diff --git a/plugins/http/src/lib.rs b/plugins/http/src/lib.rs index e6db850f0..eec52f95d 100644 --- a/plugins/http/src/lib.rs +++ b/plugins/http/src/lib.rs @@ -80,6 +80,8 @@ const COOKIES_FILENAME: &str = ".cookies"; pub(crate) struct Http { pub(crate) config: Config, + /// Each webview's current page token rid, by webview label. + pub(crate) pages: std::sync::Mutex>, #[cfg(feature = "cookies")] cookies_jar: std::sync::Arc, } @@ -125,6 +127,7 @@ pub fn init() -> TauriPlugin> { let state = Http { config: api.config().clone().unwrap_or_default(), + pages: Default::default(), #[cfg(feature = "cookies")] cookies_jar: std::sync::Arc::new(cookies_jar), }; @@ -149,6 +152,31 @@ pub fn init() -> TauriPlugin> { } } }) + // Cancel a webview's requests and response bodies when its window is destroyed. + // This listens on each window instead of using the plugin's `on_event`: + // by the time `on_event` sees `WindowEvent::Destroyed`, Tauri has already + // removed the window and its webviews from the app, so their resource tables + // can no longer be looked up. The listener keeps the webview it was registered + // for, so it also cancels on the right table if the label is reused later. + // + // TODO: Also cancel on webview close (a webview closed on its own in a + // multiwebview window) once Tauri provides a plugin lifecycle hook for it. + .on_webview_ready(|webview| { + let webview_ = webview.clone(); + webview.window().on_window_event(move |event| { + if matches!(event, tauri::WindowEvent::Destroyed) { + commands::cancel_all_requests(&webview_); + if let Some(state) = webview_.try_state::() { + state.pages.lock().unwrap().remove(webview_.label()); + } + } + }); + }) + .on_page_load(|webview, payload| { + if matches!(payload.event(), tauri::webview::PageLoadEvent::Started) { + commands::cancel_all_requests(webview); + } + }) .invoke_handler(tauri::generate_handler![ commands::fetch, commands::fetch_cancel,