Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Sync from GitHub via hub-sync
Browse files
src/app/api/proxy/[...path]/route.ts
CHANGED
|
@@ -49,6 +49,21 @@ function upstreamSignal(req: NextRequest): AbortSignal {
|
|
| 49 |
]);
|
| 50 |
}
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
// Build the upstream URL and validate it. Returns the URL or null if the
|
| 53 |
// request should be rejected.
|
| 54 |
//
|
|
@@ -92,13 +107,7 @@ export async function GET(
|
|
| 92 |
);
|
| 93 |
if (!upstreamUrl) return new Response("Forbidden", { status: 403 });
|
| 94 |
|
| 95 |
-
const headers =
|
| 96 |
-
const token = req.cookies.get(COOKIE_NAME)?.value;
|
| 97 |
-
if (token) headers.set("authorization", `Bearer ${token}`);
|
| 98 |
-
for (const h of FORWARD_REQUEST_HEADERS) {
|
| 99 |
-
const v = req.headers.get(h);
|
| 100 |
-
if (v) headers.set(h, v);
|
| 101 |
-
}
|
| 102 |
|
| 103 |
let upstream: Response;
|
| 104 |
try {
|
|
@@ -148,9 +157,7 @@ export async function HEAD(
|
|
| 148 |
);
|
| 149 |
if (!upstreamUrl) return new Response(null, { status: 403 });
|
| 150 |
|
| 151 |
-
const headers =
|
| 152 |
-
const token = req.cookies.get(COOKIE_NAME)?.value;
|
| 153 |
-
if (token) headers.set("authorization", `Bearer ${token}`);
|
| 154 |
|
| 155 |
let upstream: Response;
|
| 156 |
try {
|
|
|
|
| 49 |
]);
|
| 50 |
}
|
| 51 |
|
| 52 |
+
// Shared by GET and HEAD so they always forward the same set of headers.
|
| 53 |
+
// Previously HEAD only attached Authorization, so a client sending a
|
| 54 |
+
// conditional HEAD (If-None-Match etag check) would always get a fresh
|
| 55 |
+
// 200 instead of a 304 — defeating the cache validation it was asking for.
|
| 56 |
+
function buildUpstreamHeaders(req: NextRequest): Headers {
|
| 57 |
+
const headers = new Headers();
|
| 58 |
+
const token = req.cookies.get(COOKIE_NAME)?.value;
|
| 59 |
+
if (token) headers.set("authorization", `Bearer ${token}`);
|
| 60 |
+
for (const h of FORWARD_REQUEST_HEADERS) {
|
| 61 |
+
const v = req.headers.get(h);
|
| 62 |
+
if (v) headers.set(h, v);
|
| 63 |
+
}
|
| 64 |
+
return headers;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
// Build the upstream URL and validate it. Returns the URL or null if the
|
| 68 |
// request should be rejected.
|
| 69 |
//
|
|
|
|
| 107 |
);
|
| 108 |
if (!upstreamUrl) return new Response("Forbidden", { status: 403 });
|
| 109 |
|
| 110 |
+
const headers = buildUpstreamHeaders(req);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
let upstream: Response;
|
| 113 |
try {
|
|
|
|
| 157 |
);
|
| 158 |
if (!upstreamUrl) return new Response(null, { status: 403 });
|
| 159 |
|
| 160 |
+
const headers = buildUpstreamHeaders(req);
|
|
|
|
|
|
|
| 161 |
|
| 162 |
let upstream: Response;
|
| 163 |
try {
|