Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] serveResources: Correctly encode non UTF-8 resources #201

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const etag = require("etag");
const fresh = require("fresh");
const parseurl = require("parseurl");

const rProperties = /\.properties$/;
const rProperties = /\.properties$/i;
const rReplaceVersion = /\.(library|js|json)$/i;

function isFresh(req, res) {
return fresh(req.headers, {
Expand Down Expand Up @@ -35,12 +36,13 @@ function createMiddleware({resourceCollections}) {

let type;
let charset;
if (rProperties.test(resource.getPath())) {
const resourcePath = resource.getPath();
if (rProperties.test(resourcePath)) {
// Special handling for *.properties files which are encoded with charset ISO-8859-1.
type = "text/plain";
charset = "ISO-8859-1";
} else {
type = mime.lookup(resource.getPath()) || "application/octet-stream";
type = mime.lookup(resourcePath) || "application/octet-stream";
}

if (!res.getHeader("Content-Type")) {
Expand All @@ -63,7 +65,11 @@ function createMiddleware({resourceCollections}) {

let stream = resource.getStream();

if ((type.startsWith("text/") || type === "application/javascript")) {
// Only execute version replacement for UTF-8 encoded resources because replaceStream will always output
// UTF-8 anyways.
// Also, only process .library, *.js and *.json files. Just like it's done in Application-
// and LibraryBuilder
if (charset === "UTF-8" && rReplaceVersion.test(resourcePath)) {
if (resource._project) {
stream = stream.pipe(replaceStream("${version}", resource._project.version));
} else {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/application.a/webapp/i18n/i18n_de.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
showHelloButtonText=Say �!
2 changes: 1 addition & 1 deletion test/fixtures/application.a/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<body>

</body>
</html>
</html>
9 changes: 9 additions & 0 deletions test/fixtures/application.a/webapp/versionTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Not replaced: ${version}</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/application.a/webapp/versionTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`${version}`);
49 changes: 46 additions & 3 deletions test/lib/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test.before((t) => {
});
});

test.after(() => {
test.after.always(() => {
return new Promise((resolve, reject) => {
serve.close((error) => {
if (error) {
Expand All @@ -44,17 +44,57 @@ test("Get resource from application.a (/index.html)", (t) => {
});
});


test("Get resource from application.a with not replaced version placeholder(/versionTest.html)", (t) => {
return request.get("/versionTest.html").then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Not replaced: \${version}<\/title>/, "Correct response");
});
});

test("Get resource from application.a with replaced version placeholder (/versionTest.js)", (t) => {
return request.get("/versionTest.js").then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /application\/javascript/, "Correct content type");
t.deepEqual(res.text, "console.log(`1.0.0`);\n", "Correct response");
});
});

test("Get resource from application.a (/i18n/i18n.properties) with correct charset 'ISO-8859-1'", (t) => {
return request.get("/i18n/i18n.properties").then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.deepEqual(res.headers["content-type"], "text/plain; charset=ISO-8859-1", "Correct content type and charset");
t.deepEqual(res.text, "showHelloButtonText=Say Hello!", "Correct response");
t.deepEqual(Buffer.from(res.text, "latin1").toString(), "showHelloButtonText=Say Hello!", "Correct response");
});
});

test("Get resource from application.a (/i18n/i18n_de.properties) with correct encoding 'ISO-8859-1'", (t) => {
return request.get("/i18n/i18n_de.properties")
.responseType("arraybuffer")
.then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.deepEqual(res.headers["content-type"], "text/plain; charset=ISO-8859-1",
"Correct content type and charset");

t.deepEqual(res.body.toString("latin1"), "showHelloButtonText=Say ä!", "Correct response");
// Because it took so long to figure this out I keep the below line. It is equivalent to the deepEqual above
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment makes me curious: what did you want to achieve with that line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, now it seems pretty clear. But I first struggled converting res.text (without calling .responseType("arraybuffer") form latin1 to UTF-8. I did not get that working.

I still find it a bit confusing that I have a raw Buffer, containing some latin1 encoded characters, that I can only convert to a UTF-8 string by calling .toString("latin1").

// t.deepEqual(res.body.toString("latin1"), Buffer.from("showHelloButtonText=Say \u00e4!", "latin1").toString("latin1"),
// "Correct response");
});
});

test("Get resource from library.a (/resources/library/a/.library)", (t) => {
return request.get("/resources/library/a/.library").then((res) => {
Expand Down Expand Up @@ -102,6 +142,9 @@ test("Get app_pages from discovery middleware (/discovery/app_pages)", (t) => {
"app_pages": [
{
"entry": "index.html"
},
{
"entry": "versionTest.html"
}
]
}, "Correct response");
Expand Down Expand Up @@ -463,7 +506,7 @@ test("Get index of resources", (t) => {
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.is(res.headers["content-type"], "text/html", "Correct content type");
t.is(/<title>(.*)<\/title>/i.exec(res.text)[1], "Index of /", "Found correct title");
t.deepEqual(res.text.match(/<td/g).length, 30, "Found correct amount of <td> elements");
t.deepEqual(res.text.match(/<td/g).length, 42, "Found correct amount of <td> elements");
}),
request.get("/resources").then((res) => {
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
Expand Down