fix: ensure includes are pulled in relative to their parent

This commit is contained in:
Andy Burke 2025-06-30 15:33:13 -07:00
parent 0f65e57539
commit 02c10aaa72
8 changed files with 9 additions and 8 deletions

View file

@ -1,7 +1,7 @@
{
"name": "@andyburke/serverus",
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
"version": "0.7.0",
"version": "0.7.1",
"license": "MIT",
"exports": {
".": "./serverus.ts",

View file

@ -25,7 +25,8 @@ async function load_html_with_ssi(html_file: string): Promise<string> {
async (_match, type, location, index): Promise<string | undefined> => {
switch (type) {
case 'file': {
const resolved = path.resolve(location);
const directory = path.dirname(html_file);
const resolved = path.resolve(path.join(directory, location));
if (!resolved.startsWith(Deno.cwd())) {
console.error(
`Cannot include files above the working directory (${Deno.cwd()}): ${location} ${html_file}:${index}`

View file

@ -17,7 +17,7 @@ Deno.test({
Deno.chdir('./tests/www');
test_server_info = await get_ephemeral_listen_server();
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/`, {
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/includes/index.html`, {
method: 'GET'
});
@ -36,7 +36,7 @@ Deno.test({
});
Deno.test({
name: 'get html file (text/plain)',
name: 'get html file (text/plain) (also test automatic index.html)',
permissions: {
env: true,
read: true,
@ -51,7 +51,7 @@ Deno.test({
Deno.chdir('./tests/www');
test_server_info = await get_ephemeral_listen_server();
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/index.html`, {
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/includes`, {
method: 'GET',
headers: {
'Accept': 'text/plain'

View file

@ -0,0 +1,3 @@
<div>Include #1</div>
<!-- #include file="./subdir/another_include.html" -->

View file

@ -1,3 +0,0 @@
<div>Include #1</div>
<!-- #include file="./another_include.html" -->