Redirect response
Respond to the worker with the response of another site.
async function handleRequest(request) {
const url = new URL(request.url)
let image_url = url.searchParams.get("sub_url")
if (!image_url) return new Response("Error. No sub_url URL param detected", { status: 500 })
// Rewrite request to point to API url. This also makes the request mutable
// so we can add the correct Origin header to make the API server think
// that this request isn't cross-site.
request = new Request(sub_url, request);
request.headers.set("Origin", new URL(sub_url).origin);
request.headers.set("user-agent", "1337 User Agent");
request.headers.set("Referer", new URL(sub_url).origin);
let response = await fetch(request)
// Recreate the response so we can modify the headers
response = new Response(response.body, response)
// Set CORS headers
response.headers.set("Cross-Origin-Resource-Policy", "cross-origin")
// Append to/Add Vary header so browser will cache response correctly
response.headers.append("Vary", "Origin")
return response
}
async function onRequest({ req }) {
const request = req.request;
if (request.method !== "GET") return new Response("Not Allowed", {
status: 405,
header: {
Allow: "GET"
}
});
const response = await handleRequest(request);
req.respondWith(response);
}
export { onRequest }