1+ function Cookie ( s , host ) {
2+ this . parse ( s , host ) ;
3+ }
4+ Cookie . computeId = function ( c ) {
5+ return c . name + ";" + c . host + "/" + c . path ;
6+ } ;
7+ Cookie . find = function ( f ) {
8+ var cc = Cookie . prototype . cookieManager . enumerator ;
9+ var c ;
10+ while ( cc . hasMoreElements ( ) ) {
11+ if ( f ( c = cc . getNext ( ) ) ) return c ;
12+ }
13+ return null ;
14+ } ;
15+
16+ Cookie . attributes = { host : 'domain' , path : 'path' , expires : 'expires' , isHttpOnly : 'HttpOnly' , isSecure : 'Secure' } ;
17+ Cookie . prototype = {
18+
19+ name : '' ,
20+ value : '' ,
21+ source : '' ,
22+ domain : '' ,
23+ host : '' ,
24+ rawHost : '' ,
25+ path : '' ,
26+ secure : false ,
27+ httponly : false ,
28+ session : true ,
29+ expires : 0 ,
30+
31+ id : '' ,
32+
33+
34+ toString : function ( ) {
35+ var c = [ this [ 'name' ] + "=" + this . value ] ;
36+ var v ;
37+ const aa = Cookie . attributes ;
38+ for ( var k in aa ) {
39+ var p = aa [ k ] ;
40+ v = this [ k ] ;
41+ switch ( typeof ( v ) ) {
42+ case "string" :
43+ if ( v ) c . push ( p + "=" + v ) ;
44+ break ;
45+ case "boolean" :
46+ if ( v ) c . push ( p ) ;
47+ break ;
48+ case "number" :
49+ if ( ! this . isSession ) c . push ( p + "=" + new Date ( v * 1000 ) . toUTCString ( ) ) ;
50+ break ;
51+ }
52+ }
53+ return c . join ( "; " ) ;
54+ } ,
55+ parse : function ( s , host ) {
56+ var p ;
57+ if ( this . source ) {
58+ // cleanup for recycle
59+ for ( p in this ) {
60+ if ( typeof ( p ) != "function" ) delete this [ p ] ;
61+ }
62+ }
63+ this . source = s ;
64+ this . host = host ;
65+
66+ var parts = s . split ( / ; \s * / ) ;
67+ var nv = parts . shift ( ) . split ( "=" ) ;
68+
69+ this . name = nv . shift ( ) || '' ;
70+ this . value = nv . join ( '=' ) || '' ;
71+
72+ var n , v ;
73+ for each ( p in parts ) {
74+ nv = p . split ( "=" ) ;
75+ switch ( n = nv [ 0 ] . toLowerCase ( ) ) {
76+ case 'expires' :
77+ v = Math . round ( Date . parse ( ( nv [ 1 ] || '' ) . replace ( / \- / g, ' ' ) ) / 1000 ) ;
78+ break ;
79+ case 'domain' :
80+ case 'path' :
81+ v = nv [ 1 ] || '' ;
82+ break ;
83+ case 'secure' :
84+ case 'httponly' :
85+ v = true ;
86+ break ;
87+ default :
88+ n = 'unknown'
89+ }
90+ this [ n ] = v ;
91+ }
92+ if ( ! this . expires ) {
93+ this . session = true ;
94+ this . expires = Math . round ( new Date ( ) / 1000 ) + 31536000 ;
95+ }
96+ if ( this . domain ) {
97+ if ( ! this . isDomain ) this . domain = "." + this . domain ;
98+ this . host = this . domain ;
99+ }
100+ this . rawHost = this . host . replace ( / ^ \. / , '' ) ;
101+
102+ this . id = Cookie . computeId ( this ) ;
103+ } ,
104+
105+
106+ get cookieManager ( ) {
107+ delete Cookie . prototype . cookieManager ;
108+ var cman = CC [ "@mozilla.org/cookiemanager;1" ]
109+ . getService ( CI . nsICookieManager2 ) . QueryInterface ( CI . nsICookieManager ) ;
110+ return Cookie . prototype . cookieManager = cman ;
111+ } ,
112+ belongsTo : function ( host , path ) {
113+ if ( path && this . path && path . indexOf ( this . path ) != 0 ) return false ;
114+ if ( host == this . rawHost ) return true ;
115+ var d = this . domain ;
116+ return d && ( host == d || this . isDomain && host . slice ( - d . length ) == d ) ;
117+ } ,
118+ save : function ( ) {
119+ this . save = ( "cookieExists" in this . cookieManager )
120+ ? function ( ) { this . cookieManager . add ( this . host , this . path , this . name , this . value , this . secure , this . httponly , this . session , this . expires ) ; }
121+ : function ( ) { this . cookieManager . add ( this . host , this . path , this . name , this . value , this . secure , this . session , this . expires ) ; }
122+ ;
123+ return this . save ( ) ;
124+ } ,
125+ exists : function ( ) {
126+ var cc = this . cookieManager . enumerator ;
127+ while ( cc . hasMoreElements ( ) ) {
128+ if ( this . sameAs ( cc . getNext ( ) ) ) return true ;
129+ }
130+ return false ;
131+ } ,
132+
133+ sameAs : function ( c ) {
134+ ( c instanceof CI . nsICookie ) && ( c instanceof CI . nsICookie2 ) ;
135+ return Cookie . computeId ( c ) == this . id ;
136+ } ,
137+
138+ // nsICookie2 interface extras
139+ get isSecure ( ) { return this . secure ; } ,
140+ get expiry ( ) { return this . expires ; } ,
141+ get isSession ( ) { return this . session ; } ,
142+ get isHttpOnly ( ) { return this . httponly ; } ,
143+ get isDomain ( ) { return this . domain && this . domain [ 0 ] == '.' ; } ,
144+ policy : 0 ,
145+ status : 0 ,
146+ QueryInterface : xpcom_generateQI ( [ CI . nsICookie , CI . nsICookie2 , CI . nsISupports ] )
147+
148+ }
0 commit comments