Bug 1448553 - Part 2: Decodeds Punycode-encoded international domain names in the Netmonitor so that they are displayed as human-readable Unicode text. r?Honza draft
authorZhang Junzhi <zjz@zjz.name>
Sun, 01 Apr 2018 15:52:13 +0800
changeset 775477 231f533d6ce7f5c312aae53505ba7841cd775b7d
parent 775476 a3e9cc05b99b6fd6593ed93b012b79b45086ca91
child 775899 9bd78666e71848506c8e132bf6c79b8d365a3778
child 775900 dca154a760773d20a1d3fbf94c965a58c293de71
child 775901 ffa7ad62d2612bf68627a3d65e6fe0b4cfd8526c
push id104738
push userbmo:zjz@zjz.name
push dateSun, 01 Apr 2018 07:52:58 +0000
reviewersHonza
bugs1448553
milestone61.0a1
Bug 1448553 - Part 2: Decodeds Punycode-encoded international domain names in the Netmonitor so that they are displayed as human-readable Unicode text. r?Honza The Punycode-encoded international domain names are human-unreadable, so they should be displayed as human-readable Unicode text. This commit decodes this kind of names in the Netmonitor. MozReview-Commit-ID: B82x0ql6z6c
devtools/client/netmonitor/src/utils/request-utils.js
--- a/devtools/client/netmonitor/src/utils/request-utils.js
+++ b/devtools/client/netmonitor/src/utils/request-utils.js
@@ -1,16 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* eslint-disable mozilla/reject-some-requires */
 
 "use strict";
 
+const { Cc, Ci } = require("chrome");
 const {
   UPDATE_PROPS,
 } = require("devtools/client/netmonitor/src/constants");
 
 const CONTENT_MIME_TYPE_ABBREVIATIONS = {
   "ecmascript": "js",
   "javascript": "js",
   "x-javascript": "js"
@@ -116,18 +117,23 @@ function formDataURI(mimeType, encoding,
  * @param {array} headers - array of headers info { name, value }
  * @return {string} list of headers in text format
  */
 function writeHeaderText(headers) {
   return headers.map(({name, value}) => name + ": " + value).join("\n");
 }
 
 /**
- * Convert a string into unicode if string is valid.
- * If there is a malformed URI sequence, it returns input string.
+ * If the `string` is a valid URI component, this function converts the
+ * URI-encoded characters into Unicode. If there is a malformed URI sequence, it
+ * returns the original string.
+ *
+ * Note that this function ONLY decodes URI-encoded characters, this means that
+ * the domain name part, which is encoded in Punycode other than URI encoding,
+ * won't be decoded into Unicode in this function.
  *
  * @param {string} url - a string
  * @return {string} unicode string
  */
 function decodeUnicodeUrl(string) {
   try {
     return decodeURIComponent(string);
   } catch (err) {
@@ -235,17 +241,27 @@ function getUrlScheme(url) {
  */
 function getUrlDetails(url) {
   let baseNameWithQuery = getUrlBaseNameWithQuery(url);
   let host = getUrlHost(url);
   let hostname = getUrlHostName(url);
   let unicodeUrl = decodeUnicodeUrl(url);
   let scheme = getUrlScheme(url);
 
-  // Mark local hosts specially, where "local" is  as defined in the W3C
+  // If the hostname is an domain name in Punycode, decode it into a Unicode
+  // domain name.
+  const idnService =
+          Cc["@mozilla.org/network/idn-service;1"].getService(Ci.nsIIDNService);
+  const unicodeHostname = idnService.convertToDisplayIDN(hostname, {});
+  if (unicodeHostname !== hostname) {
+    host = host.replace(hostname, unicodeHostname);
+    unicodeUrl = unicodeUrl.replace(hostname, unicodeHostname);
+  }
+
+  // Mark local hosts specially, where "local" is as defined in the W3C
   // spec for secure contexts.
   // http://www.w3.org/TR/powerful-features/
   //
   //  * If the name falls under 'localhost'
   //  * If the name is an IPv4 address within 127.0.0.0/8
   //  * If the name is an IPv6 address within ::1/128
   //
   // IPv6 parsing is a little sloppy; it assumes that the address has