0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00

Let middleware handle the original request URL (#11211)

* Let middleware handle the original request URL

* Add a changeset
This commit is contained in:
Matthew Phillips 2024-06-10 05:58:44 -04:00 committed by GitHub
parent 2da877bfe9
commit 97724da93e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 19 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Let middleware handle the original request URL

View file

@ -14,7 +14,6 @@ import { AstroIntegrationLogger, Logger } from '../logger/core.js';
import { sequence } from '../middleware/index.js';
import {
appendForwardSlash,
collapseDuplicateSlashes,
joinPaths,
prependForwardSlash,
removeTrailingForwardSlash,
@ -298,10 +297,6 @@ export class App {
if (clientAddress) {
Reflect.set(request, clientAddressSymbol, clientAddress);
}
// Handle requests with duplicate slashes gracefully by cloning with a cleaned-up request URL
if (request.url !== collapseDuplicateSlashes(request.url)) {
request = new Request(collapseDuplicateSlashes(request.url), request);
}
if (!routeData) {
routeData = this.match(request);
this.#logger.debug('router', 'Astro matched the following route for ' + request.url);

View file

@ -1,6 +1,6 @@
import type http from 'node:http';
import type { ManifestData } from '../@types/astro.js';
import { collapseDuplicateSlashes, removeTrailingForwardSlash } from '../core/path.js';
import { removeTrailingForwardSlash } from '../core/path.js';
import type { DevServerController } from './controller.js';
import { runWithErrorHandling } from './controller.js';
import { recordServerError } from './error.js';
@ -27,7 +27,7 @@ export async function handleRequest({
const { config, loader } = pipeline;
const origin = `${loader.isHttps() ? 'https' : 'http'}://${incomingRequest.headers.host}`;
const url = new URL(collapseDuplicateSlashes(origin + incomingRequest.url));
const url = new URL(origin + incomingRequest.url);
let pathname: string;
if (config.trailingSlash === 'never' && !incomingRequest.url) {
pathname = '';

View file

@ -0,0 +1,14 @@
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(({ url }, next) => {
// Redirect when there are extra slashes
if(url.pathname === '/this//is/my/////directory') {
return new Response(null, {
status: 301,
headers: {
Location: '/'
}
});
}
return next();
});

View file

@ -43,18 +43,6 @@ describe('Using Astro.request in SSR', () => {
assert.equal($('#origin').text(), 'http://example.com');
});
it('Duplicate slashes are collapsed', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/subpath////request/////');
const response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('#origin').text(), 'http://example.com');
assert.equal($('#pathname').text(), '/subpath/request/');
assert.equal($('#request-pathname').text(), '/subpath/request/');
});
it('public file is copied over', async () => {
const json = await fixture.readFile('/client/cars.json');
assert.notEqual(json, undefined);
@ -107,4 +95,11 @@ describe('Using Astro.request in SSR', () => {
const data = await response.json();
assert.equal(data instanceof Array, true);
});
it('middleware gets the actual path sent in the request', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/this//is/my/////directory');
const response = await app.render(request);
assert.equal(response.status, 301);
});
});