@cloudflare/pages-shared 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -192,38 +192,47 @@ export async function generateHandler<
192
192
  staticRedirectsMatcher() || generateRedirectsMatcher()({ request })[0];
193
193
 
194
194
  if (match) {
195
- const { status, to } = match;
196
- const destination = new URL(to, request.url);
197
- const location =
198
- destination.origin === new URL(request.url).origin
199
- ? `${destination.pathname}${destination.search || search}${
200
- destination.hash
201
- }`
202
- : `${destination.href}${destination.search ? "" : search}${
203
- destination.hash
204
- }`;
205
- switch (status) {
206
- case 301:
207
- return new MovedPermanentlyResponse(location, undefined, {
208
- preventLeadingDoubleSlash: false,
209
- });
210
- case 303:
211
- return new SeeOtherResponse(location, undefined, {
212
- preventLeadingDoubleSlash: false,
213
- });
214
- case 307:
215
- return new TemporaryRedirectResponse(location, undefined, {
216
- preventLeadingDoubleSlash: false,
217
- });
218
- case 308:
219
- return new PermanentRedirectResponse(location, undefined, {
220
- preventLeadingDoubleSlash: false,
221
- });
222
- case 302:
223
- default:
224
- return new FoundResponse(location, undefined, {
225
- preventLeadingDoubleSlash: false,
226
- });
195
+ if (match.status === 200) {
196
+ // A 200 redirect means that we are proxying to a different asset, for example,
197
+ // a request with url /users/12345 could be pointed to /users/id.html. In order to
198
+ // do this, we overwrite the pathname, and instead match for assets with that url,
199
+ // and importantly, do not use the regular redirect handler - as the url visible to
200
+ // the user does not change
201
+ pathname = new URL(match.to, request.url).pathname;
202
+ } else {
203
+ const { status, to } = match;
204
+ const destination = new URL(to, request.url);
205
+ const location =
206
+ destination.origin === new URL(request.url).origin
207
+ ? `${destination.pathname}${destination.search || search}${
208
+ destination.hash
209
+ }`
210
+ : `${destination.href}${destination.search ? "" : search}${
211
+ destination.hash
212
+ }`;
213
+ switch (status) {
214
+ case 301:
215
+ return new MovedPermanentlyResponse(location, undefined, {
216
+ preventLeadingDoubleSlash: false,
217
+ });
218
+ case 303:
219
+ return new SeeOtherResponse(location, undefined, {
220
+ preventLeadingDoubleSlash: false,
221
+ });
222
+ case 307:
223
+ return new TemporaryRedirectResponse(location, undefined, {
224
+ preventLeadingDoubleSlash: false,
225
+ });
226
+ case 308:
227
+ return new PermanentRedirectResponse(location, undefined, {
228
+ preventLeadingDoubleSlash: false,
229
+ });
230
+ case 302:
231
+ default:
232
+ return new FoundResponse(location, undefined, {
233
+ preventLeadingDoubleSlash: false,
234
+ });
235
+ }
227
236
  }
228
237
  }
229
238
 
@@ -11,7 +11,7 @@ function mergeHeaders(base: HeadersInit, extra: HeadersInit) {
11
11
  }
12
12
 
13
13
  export function stripLeadingDoubleSlashes(location: string) {
14
- return location.replace(/^(\/|%2F|%2f)+(.*)/, "/$2");
14
+ return location.replace(/^(\/|%2F|%2f|%5C|%5c|\\)+(.*)/, "/$2");
15
15
  }
16
16
 
17
17
  export class OkResponse extends Response {
@@ -3,7 +3,7 @@ export const HEADERS_VERSION = 2;
3
3
  export const ANALYTICS_VERSION = 1;
4
4
  export const ROUTES_JSON_VERSION = 1;
5
5
 
6
- export const PERMITTED_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
6
+ export const PERMITTED_STATUS_CODES = new Set([200, 301, 302, 303, 307, 308]);
7
7
  export const HEADER_SEPARATOR = ":";
8
8
  export const MAX_LINE_LENGTH = 2000;
9
9
  export const MAX_HEADER_RULES = 100;
@@ -104,7 +104,7 @@ export function parseRedirects(input: string): ParsedRedirects {
104
104
  invalid.push({
105
105
  line,
106
106
  lineNumber: i + 1,
107
- message: `Valid status codes are 301, 302 (default), 303, 307, or 308. Got ${str_status}.`,
107
+ message: `Valid status codes are 200, 301, 302 (default), 303, 307, or 308. Got ${str_status}.`,
108
108
  });
109
109
  continue;
110
110
  }
@@ -119,6 +119,22 @@ export function parseRedirects(input: string): ParsedRedirects {
119
119
  }
120
120
  seen_paths.add(from);
121
121
 
122
+ if (status === 200) {
123
+ // Error can only be that it's not relative - given validateUrl is called above without onlyRelative:true,
124
+ // so if it's present, we can error to the user that proxying only expects relative urls
125
+ const [proxyTo, error] = validateUrl(to, true, true, true);
126
+ if (error) {
127
+ invalid.push({
128
+ line,
129
+ lineNumber: i + 1,
130
+ message: `Proxy (200) redirects can only point to relative paths. Got ${to}`,
131
+ });
132
+ continue;
133
+ }
134
+ rules.push({ from, to: proxyTo as string, status, lineNumber: i + 1 });
135
+ continue;
136
+ }
137
+
122
138
  rules.push({ from, to, status, lineNumber: i + 1 });
123
139
  }
124
140
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/pages-shared",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/workers-sdk.git",