Skip to content

Commit

Permalink
Merge pull request #47 from sguter90/content-type-header-via-middleware
Browse files Browse the repository at this point in the history
Allows setting content type header via middleware
  • Loading branch information
zachleat committed Mar 13, 2023
2 parents a95d61a + d1e8208 commit 256c0d2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ Arguments:
module.exports = {
Logger,
Cli
}
}
37 changes: 30 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,44 @@ class EleventyDevServer {
return (content || "") + script;
}

getFileContentType(filepath, res) {
let contentType = res.getHeader("Content-Type");

// Content-Type might be already set via middleware
if (contentType) {
return contentType;
}

let mimeType = mime.getType(filepath);
if (!mimeType) {
return;
}

contentType = mimeType;

// We only want to append charset if the header is not already set
if (contentType === "text/html") {
contentType = `text/html; charset=${this.options.encoding}`;
}

return contentType;
}

renderFile(filepath, res) {
let contents = fs.readFileSync(filepath);
let mimeType = mime.getType(filepath);
let contentType = this.getFileContentType(filepath, res);

if (mimeType === "text/html") {
res.setHeader("Content-Type", `text/html; charset=${this.options.encoding}`);
if (!contentType) {
return res.end(contents);
}

res.setHeader("Content-Type", contentType);

if (contentType.startsWith("text/html")) {
// the string is important here, wrapResponse expects strings internally for HTML content (for now)
return res.end(contents.toString());
}

if (mimeType) {
res.setHeader("Content-Type", mimeType);
}

return res.end(contents);
}

Expand Down
1 change: 1 addition & 0 deletions test/stubs/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SAMPLE PHP
48 changes: 47 additions & 1 deletion test/testServerRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ async function makeRequestTo(t, server, path) {
})
}

async function fetchHeadersForRequest(t, server, path) {
let port = await server.getPort();

return new Promise(resolve => {
const options = {
hostname: 'localhost',
port,
path,
method: 'GET',
};

http.get(options, (res) => {
const { statusCode } = res;
if(statusCode !== 200) {
throw new Error("Invalid status code" + statusCode);
}

let headers = res.headers;
resolve(headers);

}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
})
}

test("Standard request", async t => {
let server = new EleventyDevServer("test-server", "./test/stubs/", getOptions());
server.serve(8080);
Expand Down Expand Up @@ -255,4 +281,24 @@ test("Fun unicode paths", async t => {
t.true(data.startsWith("This is a test"));

server.close();
});
});

test("Content-Type header via middleware", async t => {
let server = new EleventyDevServer("test-server", "./test/stubs/", getOptions({
middleware: [
function (req, res, next) {
if (/.*\.php$/.test(req.url)) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
}

next();
}
]
}));
server.serve(8080);

let data = await fetchHeadersForRequest(t, server, encodeURI(`/index.php`));
t.true(data['content-type'] === 'text/html; charset=utf-8');

server.close();
});

0 comments on commit 256c0d2

Please sign in to comment.