Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 1x 1x 1x 12x 1x 11x 11x 2x 9x 1x 8x 9x 1x 8x 8x 1x 7x 7x 14x 14x 7x 7x 7x 7x 9x 9x 9x 14x 14x 7x 2x 2x 2x 7x 9x | // `/__url-load-proxy?url=<https URL>`: fetches an archive from another origin for the player's `?music=` and the embed's
// `?track=`, and a difficulty table's page and header for the course import, whose hosts often send no CORS headers.
// The browser may keep the response but revalidates it on every use (`no-cache`): the upstream's ETag / Last-Modified
// pass through, and so do the browser's If-None-Match / If-Modified-Since, so an unchanged archive comes back as a 304
// instead of downloading again.
/** The validators the proxy carries from the browser to the upstream. */
const CONDITIONAL_REQUEST_HEADERS = ['if-none-match', 'if-modified-since'] as const;
/** The validators the proxy carries from the upstream back to the browser. */
const VALIDATOR_RESPONSE_HEADERS = ['etag', 'last-modified'] as const;
/**
* The User-Agent the proxy names itself with upstream. A Worker's fetch sends none, and some hosts (忍者ホームページ, which
* serves the NEW GENERATION difficulty tables) answer a request without one with 403. It names the service only, never
* the player.
*/
export const URL_LOAD_PROXY_USER_AGENT = 'Be-Music URL proxy (+https://play.be-music.app)';
export function resolveURLLoadProxyTarget(rawURL: string | null): URL {
if (!rawURL) {
throw new Error('missing url query parameter');
}
const target = new URL(rawURL);
if (target.protocol !== 'https:') {
throw new Error('only https proxy targets are allowed');
}
if (target.username || target.password) {
throw new Error('proxy target credentials are not allowed');
}
return target;
}
export async function serveURLLoadProxy(
request: Request,
fetcher: (input: URL, init: RequestInit) => Promise<Response> = fetch,
): Promise<Response> {
if (request.method !== 'GET' && request.method !== 'HEAD') {
return new Response('Method Not Allowed', { status: 405, headers: { Allow: 'GET, HEAD' } });
}
let target: URL;
try {
target = resolveURLLoadProxyTarget(new URL(request.url).searchParams.get('url'));
} catch (error) {
return new Response(error instanceof Error ? error.message : 'invalid proxy target', { status: 400 });
}
const upstreamHeaders = new Headers({ 'user-agent': URL_LOAD_PROXY_USER_AGENT });
for (const name of CONDITIONAL_REQUEST_HEADERS) {
const value = request.headers.get(name);
if (value !== null) upstreamHeaders.set(name, value);
}
let upstream: Response;
try {
upstream = await fetcher(target, {
method: request.method === 'HEAD' ? 'HEAD' : 'GET',
headers: upstreamHeaders,
redirect: 'follow',
});
} catch (error) {
return new Response(`failed to fetch ${target.href}: ${error instanceof Error ? error.message : String(error)}`, {
status: 502,
});
}
const headers = new Headers();
headers.set('content-type', upstream.headers.get('content-type') ?? 'application/octet-stream');
// Keep it, but ask again each time: the same URL may serve a new archive.
headers.set('cache-control', 'no-cache');
headers.set('cross-origin-resource-policy', 'same-origin');
for (const name of VALIDATOR_RESPONSE_HEADERS) {
const value = upstream.headers.get(name);
if (value !== null) headers.set(name, value);
}
// A HEAD request asks for the size before the download (the embed's Load button on a phone). Only there: a GET body
// may come decoded from a compressed transfer, which the upstream's length would not match.
if (request.method === 'HEAD') {
const length = upstream.headers.get('content-length');
const encoding = upstream.headers.get('content-encoding') ?? 'identity';
if (length !== null && encoding === 'identity') headers.set('content-length', length);
}
const bodyless = request.method === 'HEAD' || upstream.status === 304;
return new Response(bodyless ? null : upstream.body, {
status: upstream.status,
statusText: upstream.statusText,
headers,
});
}
|