drill trailingSlash

This commit is contained in:
Rishi Raj Jain 2023-09-18 17:37:03 +05:30
parent d0e513f214
commit daa5bab85a
4 changed files with 27 additions and 7 deletions

View file

@ -23,13 +23,14 @@ function parsePathname(pathname: string, host: string | undefined, port: number)
export function createServer( export function createServer(
{ client, port, host, removeBase }: CreateServerOptions, { client, port, host, removeBase }: CreateServerOptions,
handler: http.RequestListener handler: http.RequestListener,
trailingSlash: string
) { ) {
const listener: http.RequestListener = (req, res) => { const listener: http.RequestListener = (req, res) => {
if (req.url) { if (req.url) {
let pathname: string | undefined = removeBase(req.url); let pathname: string | undefined = removeBase(req.url);
pathname = pathname[0] === '/' ? pathname : '/' + pathname; pathname = pathname[0] === '/' ? pathname : '/' + pathname;
const encodedURI = parsePathname(pathname, host, port); let encodedURI = parsePathname(pathname, host, port);
if (!encodedURI) { if (!encodedURI) {
res.writeHead(400); res.writeHead(400);
@ -37,7 +38,21 @@ export function createServer(
return res; return res;
} }
const stream = send(req, encodedURI, { let pathForSend = encodedURI;
if (trailingSlash === 'never') {
if (pathname.endsWith('/')) {
encodedURI = parsePathname(pathname.substring(0, pathname.length - 1), host, port);
if (!encodedURI) {
res.writeHead(400);
res.end('Bad request.');
return res;
}
}
pathForSend = encodedURI + '/';
}
const stream = send(req, pathForSend, {
root: fileURLToPath(client), root: fileURLToPath(client),
dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny', dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny',
}); });
@ -64,9 +79,11 @@ export function createServer(
location = req.url + '/'; location = req.url + '/';
} }
res.statusCode = 301; if (!pathForSend.endsWith('/')) {
res.setHeader('Location', location); res.statusCode = 301;
res.end(location); res.setHeader('Location', location);
res.end(location);
}
}); });
stream.on('file', () => { stream.on('file', () => {
forwardError = true; forwardError = true;

View file

@ -46,6 +46,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
server: config.build.server?.toString(), server: config.build.server?.toString(),
host: config.server.host, host: config.server.host,
port: config.server.port, port: config.server.port,
trailingSlash: config.trailingSlash,
}; };
setAdapter(getAdapter(_options)); setAdapter(getAdapter(_options));

View file

@ -53,7 +53,8 @@ export default function startServer(app: NodeApp, options: Options) {
host, host,
removeBase: app.removeBase.bind(app), removeBase: app.removeBase.bind(app),
}, },
handler handler,
options.trailingSlash
); );
const protocol = server.server instanceof https.Server ? 'https' : 'http'; const protocol = server.server instanceof https.Server ? 'https' : 'http';

View file

@ -15,6 +15,7 @@ export interface Options extends UserOptions {
port: number; port: number;
server: string; server: string;
client: string; client: string;
trailingSlash: string;
} }
export type RequestHandlerParams = [ export type RequestHandlerParams = [