-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudflare_cors-proxy-2.js
90 lines (77 loc) · 3 KB
/
cloudflare_cors-proxy-2.js
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
// https://developers.cloudflare.com/workers/templates/pages/cors_header_proxy/
// We support the GET, POST, HEAD, and OPTIONS methods from any origin,
// and accept the Content-Type header on requests. These headers must be
// present on all responses to all CORS requests. In practice, this means
// all responses to OPTIONS requests.
const corsHeaders = {
// "Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Allow-Origin": "https://mindactuate.github.io",
"Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
};
async function handleRequest(event) {
let request = event.request;
const url = new URL(request.url)
const apiurl = url.searchParams.get("apiurl");
if(!apiurl || apiurl.length === 0){
return handleError("No param apiurl",400);
}
// 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(apiurl, request);
// request.headers.set("Origin", new URL(apiurl).origin);
let response = await fetch(request);
// Recreate the response so we can modify the headers
response = new Response(response.body, response);
// Set CORS headers
Object.keys(corsHeaders).map(headerName => {
response.headers.set(headerName, corsHeaders[headerName]);
})
// Append to/Add Vary header so browser will cache response correctly
response.headers.append("Vary", "Origin");
// Debug
// response.headers.set("X-Debug-1", JSON.stringify(event));
// response.headers.set("X-Debug-2", JSON.stringify([...event.request.headers]));
return response;
}
function handleOptions(event) {
// Make sure the necesssary headers are present
// for this to be a valid pre-flight request
return new Response(null, {
headers: {
...corsHeaders,
// "X-Debug-1": JSON.stringify(event),
// "X-Debug-2": JSON.stringify([...event.request.headers]),
},
});
}
function handleError(message, statusCode){
let code = 400;
if(statusCode && Number.isInteger(statusCode)){
code = Number.parseInt(statusCode);
}
return new Response(message, {status: code})
}
addEventListener("fetch", (event) => {
const request = event.request;
const origin = request.headers.get('Origin')
if (origin && origin === corsHeaders["Access-Control-Allow-Origin"]) {
if (request.method === "OPTIONS") {
// Handle CORS preflight requests
event.respondWith(handleOptions(event));
} else if (
request.method === "GET" ||
request.method === "HEAD" ||
request.method === "POST"
) {
// Handle requests to the API server
event.respondWith(handleRequest(event));
} else {
event.respondWith(handleError("Method Not Allowed",405));
}
} else {
event.respondWith(handleError("Origin Not Allowed",403));
}
});