1 /* See license.txt for terms of usage */
  2 
  3 define([
  4     "firebug/cookies/cookie",
  5     "firebug/lib/string"
  6 ],
  7 function(Cookie, Str) {
  8 
  9 // ********************************************************************************************* //
 10 // Menu Utils
 11 
 12 var CookieUtils = 
 13 {
 14     getCookieId: function(cookie)
 15     {
 16         return cookie.host + cookie.path + cookie.name;
 17     },
 18 
 19     makeStrippedHost: function(aHost)
 20     {
 21         if (!aHost)
 22             return aHost;
 23 
 24         var formattedHost = aHost.charAt(0) == "." ? aHost.substring(1, aHost.length) : aHost;
 25         return formattedHost.substring(0, 4) == "www." ? formattedHost.substring(4, formattedHost.length) : formattedHost;
 26     },
 27 
 28     makeCookieObject: function(cookie)
 29     {
 30         // Remember the raw value.
 31         var rawValue = cookie.value;
 32 
 33         // Unescape '+' characters that are used to encode a space.
 34         // This isn't done by unescape method.
 35         var value = cookie.value;
 36         if (value)
 37             value = value.replace(/\+/g, " ");
 38 
 39         value = unescape(value);
 40 
 41         try
 42         {
 43             value = Str.convertToUnicode(value);
 44         }
 45         catch (exc) { }
 46 
 47         var c = {
 48             name        : cookie.name,
 49             value       : value,
 50             isDomain    : cookie.isDomain,
 51             host        : cookie.host,
 52             path        : cookie.path,
 53             isSecure    : cookie.isSecure,
 54             expires     : cookie.expires,
 55             isHttpOnly  : cookie.isHttpOnly,
 56             rawValue    : rawValue
 57         };
 58 
 59         return c;
 60     },
 61 
 62     parseFromString: function(string)
 63     {
 64         var cookie = new Object();
 65         var pairs = string.split("; ");
 66 
 67         for (var i=0; i<pairs.length; i++)
 68         {
 69             var option = pairs[i].split("=");
 70             if (i == 0)
 71             {
 72                 cookie.name = option[0];
 73                 cookie.value = option[1];
 74             } 
 75             else
 76             {
 77                 var name = option[0].toLowerCase();
 78                 name = (name == "domain") ? "host" : name;
 79                 if (name == "httponly")
 80                 {
 81                     cookie.isHttpOnly = true;
 82                 }
 83                 else if (name == "expires")
 84                 {
 85                     var value = option[1];
 86                     value = value.replace(/-/g, " ");
 87                     cookie[name] = Date.parse(value) / 1000;
 88 
 89                     // Log error if the date isn't correctly parsed.
 90                     if (FBTrace.DBG_COOKIES)
 91                     {
 92                         var tempDate = new Date(cookie[name] * 1000);
 93                         if (value != tempDate.toGMTString())
 94                         {
 95                             FBTrace.sysout("cookies.parseFromString: ERROR, " + 
 96                                 "from: " + value + 
 97                                 ", to: " + tempDate.toGMTString() + 
 98                                 ", cookie: " + string);
 99                         }
100                     }
101                 }
102                 else
103                 {
104                     cookie[name] = option[1];
105                 }
106             }
107         }
108 
109         return cookie;
110     },
111 
112     parseSentCookiesFromString: function(header)
113     {
114         var cookies = [];
115 
116         if (!header)
117             return cookies;
118 
119         var pairs = header.split("; ");
120         for (var i=0; i<pairs.length; i++)
121         {
122             var pair = pairs[i];
123             var index = pair.indexOf("=");
124             if (index > 0) {
125                 var name = pair.substring(0, index);
126                 var value = pair.substr(index+1);
127                 if (name.length && value.length)
128                     cookies.push(new Cookie(this.makeCookieObject({name: name, value: value})));
129             }
130         }
131 
132         return cookies;
133     }
134 };
135 
136 // ********************************************************************************************* //
137 
138 return CookieUtils;
139 
140 // ********************************************************************************************* //
141 });
142 
143