Bug 1042859 - Ignore host port when fetching cookies with the gcli cookie command. r=pbrosset
authorGeoffroy Planquart <geoffroy@planquart.fr>
Fri, 13 Mar 2015 09:05:00 -0400
changeset 233760 051cb67813e3fba2b9a62e27b3bad7b6d0362d62
parent 233759 3871b59eaa49e734bf49ef002fac580678098794
child 233761 cb6a2374cfa1c73c81d1097c630d2eb37a8b02a9
push id11785
push userryanvm@gmail.com
push dateMon, 16 Mar 2015 13:57:27 +0000
treeherderfx-team@97f74a12b427 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspbrosset
bugs1042859
milestone39.0a1
Bug 1042859 - Ignore host port when fetching cookies with the gcli cookie command. r=pbrosset
toolkit/devtools/gcli/commands/cookie.js
--- a/toolkit/devtools/gcli/commands/cookie.js
+++ b/toolkit/devtools/gcli/commands/cookie.js
@@ -4,16 +4,29 @@
 
 "use strict";
 
 const { Cc, Ci, Cu } = require("chrome");
 const gcli = require("gcli/index");
 const cookieMgr = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
 
 /**
+ * Check host value and remove port part as it is not used
+ * for storing cookies.
+ *
+ * Parameter will usually be context.environment.document.location.host
+ */
+function sanitizeHost(host) {
+  if (host == null || host == "") {
+    throw new Error(gcli.lookup("cookieListOutNonePage"));
+  }
+  return host.split(':')[0];
+}
+
+/**
  * The cookie 'expires' value needs converting into something more readable
  */
 function translateExpires(expires) {
   if (expires == 0) {
     return gcli.lookup("cookieListOutSession");
   }
   return new Date(expires).toLocaleString();
 }
@@ -40,20 +53,17 @@ exports.items = [
     manual: gcli.lookup("cookieManual")
   },
   {
     name: "cookie list",
     description: gcli.lookup("cookieListDesc"),
     manual: gcli.lookup("cookieListManual"),
     returnType: "cookies",
     exec: function(args, context) {
-      let host = context.environment.document.location.host;
-      if (host == null || host == "") {
-        throw new Error(gcli.lookup("cookieListOutNonePage"));
-      }
+      let host = sanitizeHost(context.environment.document.location.host);
 
       let enm = cookieMgr.getCookiesFromHost(host);
 
       let cookies = [];
       while (enm.hasMoreElements()) {
         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
         if (isCookieAtHost(cookie, host)) {
           cookies.push({
@@ -79,17 +89,17 @@ exports.items = [
     params: [
       {
         name: "name",
         type: "string",
         description: gcli.lookup("cookieRemoveKeyDesc"),
       }
     ],
     exec: function(args, context) {
-      let host = context.environment.document.location.host;
+      let host = sanitizeHost(context.environment.document.location.host);
       let enm = cookieMgr.getCookiesFromHost(host);
 
       let cookies = [];
       while (enm.hasMoreElements()) {
         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
         if (isCookieAtHost(cookie, host)) {
           if (cookie.name == args.name) {
             cookieMgr.remove(cookie.host, cookie.name, cookie.path, false);
@@ -99,17 +109,17 @@ exports.items = [
     }
   },
   {
     item: "converter",
     from: "cookies",
     to: "view",
     exec: function(cookies, context) {
       if (cookies.length == 0) {
-        let host = context.environment.document.location.host;
+        let host = sanitizeHost(context.environment.document.location.host);
         let msg = gcli.lookupFormat("cookieListOutNoneHost", [ host ]);
         return context.createView({ html: "<span>" + msg + "</span>" });
       }
 
       for (let cookie of cookies) {
         cookie.expires = translateExpires(cookie.expires);
 
         let noAttrs = !cookie.secure && !cookie.httpOnly && !cookie.sameDomain;
@@ -212,17 +222,17 @@ exports.items = [
             type: "string",
             defaultValue: "Jan 17, 2038",
             description: gcli.lookup("cookieSetExpiresDesc")
           },
         ]
       }
     ],
     exec: function(args, context) {
-      let host = context.environment.document.location.host;
+      let host = sanitizeHost(context.environment.document.location.host);
       let time = Date.parse(args.expires) / 1000;
 
       cookieMgr.add(args.domain ? "." + args.domain : host,
                     args.path ? args.path : "/",
                     args.name,
                     args.value,
                     args.secure,
                     args.httpOnly,