Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 96e3383

Browse files
committed
fixed response headers
1 parent a958b6c commit 96e3383

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

src/cors.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
async function handleCorsRequest(targetUrl, headers) {
22
try {
3-
// Fetch the target URL with the specified headers
43
const response = await fetch(targetUrl, {
54
redirect: 'follow',
6-
headers,
5+
headers: headers,
76
});
87

9-
// Stream the response body back to the client
8+
const responseHeaders = new Headers(response.headers);
9+
responseHeaders.set('Access-Control-Allow-Origin', '*');
10+
responseHeaders.set('Content-Type', response.headers.get('Content-Type') || 'application/octet-stream');
11+
1012
return new Response(response.body, {
1113
status: response.status,
12-
headers: {
13-
'Access-Control-Allow-Origin': '*', // Adjust based on your CORS policy
14-
'Content-Type': response.headers.get('Content-Type') || 'application/octet-stream',
15-
},
14+
headers: responseHeaders,
1615
});
1716
} catch (error) {
1817
console.error('Error fetching the webpage:', error.message);
1918
return new Response('An error occurred while fetching the webpage.', {
2019
status: 500,
20+
headers: { 'Access-Control-Allow-Origin': '*' },
2121
});
2222
}
2323
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222
if (url.pathname === '/proxy') {
2323
return proxy(request);
2424
} else if (url.pathname === '/cors') {
25-
const [url, headers] = handleRequest(request);
25+
const [url, headers, origin] = handleRequest(request);
2626
return handleCorsRequest(url, headers);
2727
} else if (url.pathname === '/image') {
2828
const [url, headers, origin] = handleRequest(request);

src/utils/handler.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
export const decodeHeaders = (base64Headers) => {
2-
const headers = new Headers();
3-
if (!base64Headers) return headers;
2+
const headers = {}; // Use a plain object instead of `Headers`
3+
if (!base64Headers) {
4+
return headers;
5+
}
6+
47
try {
58
const decodedString = atob(base64Headers);
6-
const headersObj = JSON.parse(decodedString);
9+
10+
let headersObj;
11+
try {
12+
headersObj = JSON.parse(decodedString);
13+
} catch (error) {
14+
console.error('Error parsing JSON:', error, 'Decoded string:', decodedString);
15+
return headers;
16+
}
717

818
Object.entries(headersObj).forEach(([key, value]) => {
9-
headers.append(key, value);
19+
headers[key] = value;
1020
});
11-
headers.append(
12-
'User-Agent',
13-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/237.84.2.178 Safari/537.36'
14-
);
21+
headers['User-Agent'] =
22+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/237.84.2.178 Safari/537.36';
1523
return headers;
1624
} catch (error) {
25+
console.error('Error decoding base64:', error);
1726
return headers;
1827
}
1928
};
@@ -36,5 +45,6 @@ export const handleRequest = (request) => {
3645
}
3746

3847
const headers = decodeHeaders(headersBase64);
48+
3949
return [targetUrl, headers, url.origin];
4050
};

0 commit comments

Comments
 (0)