Bug 1513152 - Load .sjs scripts in httpd.js as UTF-8, updating consumers of the in-tree .sjs scripts as necessary for the change. r=kmag

--HG--
extra : rebase_source : ff77d84433fa3fc78aea9a6c2a0e897981fd7b84
This commit is contained in:
Jeff Walden 2018-12-08 18:00:33 -05:00
Родитель bd603f3ff5
Коммит 87c8fc7bca
6 изменённых файлов: 46 добавлений и 8 удалений

Просмотреть файл

@ -1,6 +1,8 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.importGlobalProperties(["TextEncoder"]);
function gzipCompressString(string, obs) {
let scs = Cc["@mozilla.org/streamConverters;1"]
@ -71,7 +73,19 @@ function handleRequest(request, response) {
response.setStatusLine(request.httpVersion, status, "DA DA DA");
response.setHeader("Content-Type", "text/plain", false);
setCacheHeaders();
response.write("Братан, ты вообще качаешься?");
function convertToUtf8(str) {
return String.fromCharCode(...new TextEncoder().encode(str));
}
// This script must be evaluated as UTF-8 for this to write out the
// bytes of the string in UTF-8. If it's evaluated as Latin-1, the
// written bytes will be the result of UTF-8-encoding this string
// *twice*.
let data = "Братан, ты вообще качаешься?";
let stringOfUtf8Bytes = convertToUtf8(data);
response.write(stringOfUtf8Bytes);
response.finish();
break;
}

Просмотреть файл

@ -1,6 +1,8 @@
// custom *.sjs for Bug 1224225
// Punycode in CSP host sources
Cu.importGlobalProperties(["TextEncoder"]);
const HTML_PART1 =
"<!DOCTYPE HTML>" +
"<html><head><meta charset=\"utf-8\">" +
@ -9,7 +11,12 @@ const HTML_PART1 =
"<body>" +
"<script id='script' src='";
const TESTCASE1 = "http://sub2.ält.example.org/";
function convertToUtf8(str)
{
return String.fromCharCode(...new TextEncoder().encode(str));
}
const TESTCASE1 = "http://sub2." + convertToUtf8("ä") + "lt.example.org/";
const TESTCASE2 = "http://sub2.xn--lt-uia.example.org/"
const HTML_PART2 = "tests/dom/security/test/csp/file_punycode_host_src.js'></script>" +

Просмотреть файл

@ -1,6 +1,9 @@
// SJS file for getAllResponseRequests vs getResponseRequest
function handleRequest(request, response)
{
response.setHeader("X-Custom-Header-Bytes", "…", false);
// Header strings are interpreted by truncating the characters in them to
// bytes, so U+2026 HORIZONTAL ELLIPSIS here must be encoded manually: using
// "…" as the string would write a \x26 byte.
response.setHeader("X-Custom-Header-Bytes", "\xE2\x80\xA6", false);
response.write("42");
}

Просмотреть файл

@ -20,7 +20,8 @@ let xhr = new XMLHttpRequest();
xhr.open('GET', 'file_XHR_header.sjs', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
ok(xhr.getResponseHeader('X-Custom-Header-Bytes') == "\xE2\x80\xA6", 'getResponseHeader byte-inflates the output');
ok(xhr.getResponseHeader('X-Custom-Header-Bytes') == "\xE2\x80\xA6",
"getResponseHeader returns a string of the header's raw bytes");
SimpleTest.finish();
}
}

Просмотреть файл

@ -2529,7 +2529,7 @@ ServerHandler.prototype =
// separate these two lines!
var line = new Error().lineNumber;
let uri = Services.io.newFileURI(file);
Services.scriptloader.loadSubScript(uri.spec, s);
Services.scriptloader.loadSubScript(uri.spec, s, "UTF-8");
} catch (e) {
dumpn("*** syntax error in SJS at " + file.path + ": " + e);
throw HTTP_500;

Просмотреть файл

@ -4,6 +4,8 @@
Cu.import("resource://gre/modules/Timer.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.importGlobalProperties(["TextEncoder"]);
/**
* Provide search suggestions in the OpenSearch JSON format.
*/
@ -12,10 +14,21 @@ function handleRequest(request, response) {
// Get the query parameters from the query string.
let query = parseQueryString(request.queryString);
function convertToUtf8(str) {
return String.fromCharCode(...new TextEncoder().encode(str));
}
function writeSuggestions(query, completions = []) {
let result = [query, completions];
response.write(JSON.stringify(result));
return result;
let jsonString = JSON.stringify([query, completions]);
// This script must be evaluated as UTF-8 for this to write out the bytes of
// the string in UTF-8. If it's evaluated as Latin-1, the written bytes
// will be the result of UTF-8-encoding the result-string *twice*, which
// will break the "I ❤️" case further down.
let stringOfUtf8Bytes = convertToUtf8(jsonString);
response.write(stringOfUtf8Bytes);
}
response.setStatusLine(request.httpVersion, 200, "OK");
@ -73,7 +86,7 @@ function parseQueryString(queryString) {
let query = {};
queryString.split('&').forEach(function (val) {
let [name, value] = val.split('=');
query[name] = unescape(value).replace(/[+]/g, " ");
query[name] = decodeURIComponent(value).replace(/[+]/g, " ");
});
return query;
}