Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/browser/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export function addIndex(pack: Pack, text: string) {

const BLOCK = 64 << 10;

function encodePath(path: string): string {
return path.split("/").map(encodeURIComponent).join("/");
}

function compressed(xhr: XMLHttpRequest): boolean {
const enc = xhr.getResponseHeader("Content-Encoding");
return !!enc && enc !== "identity";
Expand Down Expand Up @@ -96,7 +100,7 @@ export class HttpBackend implements Backend {

private request(method: string, path: string): XMLHttpRequest | null {
const xhr = new XMLHttpRequest();
xhr.open(method, this.base + encodeURI(path), false);
xhr.open(method, this.base + encodePath(path), false);
if (method === "GET") xhr.responseType = "arraybuffer";
try {
xhr.send();
Expand Down Expand Up @@ -170,7 +174,7 @@ export class HttpBackend implements Backend {
let end = b;
while (end < last && !blocks.has(end + 1)) end++;
const xhr = new XMLHttpRequest();
xhr.open("GET", this.base + encodeURI(path), false);
xhr.open("GET", this.base + encodePath(path), false);
xhr.responseType = "arraybuffer";
xhr.setRequestHeader("Range", `bytes=${b * BLOCK}-${(end + 1) * BLOCK - 1}`);
try {
Expand Down
45 changes: 45 additions & 0 deletions test/http-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { HttpBackend } from "../src/browser/http.ts";

test("HTTP file reads preserve reserved characters in filenames", (t) => {
const files = new Map([
["/app/a#b.txt", new Uint8Array([1, 2, 3])],
["/app/a?b.txt", new Uint8Array([4, 5, 6])],
["/app/a%20b.txt", new Uint8Array([7, 8, 9])],
["/app/a\\b-é.txt", new Uint8Array([10, 11, 12])],
]);
class XHR {
status = 404;
response = new ArrayBuffer(0);
private url!: URL;
private range = false;
open(_method: string, url: string) { this.url = new URL(url); }
setRequestHeader() { this.range = true; }
getResponseHeader(name: string) {
if (name === "Content-Length") return "3";
if (name === "Content-Range" && this.range) return "bytes 0-2/3";
return null;
}
send() {
const data = files.get(decodeURIComponent(this.url.pathname));
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
if (!data || this.url.search || this.url.hash) return;
this.status = this.range ? 206 : 200;
this.response = data.slice().buffer;
}
}
const original = Object.getOwnPropertyDescriptor(globalThis, "XMLHttpRequest");
Object.defineProperty(globalThis, "XMLHttpRequest", { configurable: true, value: XHR });
t.after(() => {
if (original) Object.defineProperty(globalThis, "XMLHttpRequest", original);
else Reflect.deleteProperty(globalThis, "XMLHttpRequest");
});

for (const [path, data] of files) {
const name = path.slice("/app".length);
const backend = new HttpBackend("https://example.test/app");
assert.equal(backend.stat(name)?.size, data.length, path);
assert.deepEqual(backend.read(name), data, path);
assert.deepEqual(new HttpBackend("https://example.test/app").readRange(name, 1, 1), data.subarray(1, 2), path);
}
});
Loading