forked from andyburke/UnityHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieJar.cs
407 lines (347 loc) · 10.5 KB
/
CookieJar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
// Based on node-cookiejar (https://github.com/bmeck/node-cookiejar)
namespace HTTP
{
public class CookieAccessInfo
{
public string domain = null;
public string path = null;
public bool secure = false;
public bool scriptAccessible = true;
public CookieAccessInfo( string domain, string path )
{
this.domain = domain;
this.path = path;
}
public CookieAccessInfo( string domain, string path, bool secure )
{
this.domain = domain;
this.path = path;
this.secure = secure;
}
public CookieAccessInfo( string domain, string path, bool secure, bool scriptAccessible )
{
this.domain = domain;
this.path = path;
this.secure = secure;
this.scriptAccessible = scriptAccessible;
}
public CookieAccessInfo( Cookie cookie )
{
this.domain = cookie.domain;
this.path = cookie.path;
this.secure = cookie.secure;
this.scriptAccessible = cookie.scriptAccessible;
}
}
public class Cookie
{
public string name = null;
public string value = null;
public DateTime expirationDate = DateTime.MaxValue;
public string path = null;
public string domain = null;
public bool secure = false;
public bool scriptAccessible = true;
private static string cookiePattern = "\\s*([^=]+)(?:=((?:.|\\n)*))?";
public Cookie( string cookieString )
{
string[] parts = cookieString.Split( ';' );
foreach ( string part in parts )
{
Match match = Regex.Match( part, cookiePattern );
if ( !match.Success )
{
throw new Exception( "Could not parse cookie string: " + cookieString );
}
if ( this.name == null )
{
this.name = match.Groups[ 1 ].Value;
this.value = match.Groups[ 2 ].Value;
continue;
}
switch( match.Groups[ 1 ].Value.ToLower() )
{
case "httponly":
this.scriptAccessible = false;
break;
case "expires":
this.expirationDate = DateTime.Parse( match.Groups[ 2 ].Value );
break;
case "path":
this.path = match.Groups[ 2 ].Value;
break;
case "domain":
this.domain = match.Groups[ 2 ].Value;
break;
case "secure":
this.secure = true;
break;
default:
// TODO: warn of unknown cookie setting?
break;
}
}
}
public bool Matches( CookieAccessInfo accessInfo )
{
if ( this.secure != accessInfo.secure
|| !this.CollidesWith( accessInfo ) )
{
return false;
}
return true;
}
public bool CollidesWith( CookieAccessInfo accessInfo )
{
if ( ( this.path != null && accessInfo.path == null ) || ( this.domain != null && accessInfo.domain == null ) )
{
return false;
}
if ( this.path != null && accessInfo.path != null && accessInfo.path.IndexOf( this.path ) != 0 )
{
return false;
}
if ( this.domain == accessInfo.domain )
{
return true;
}
else if ( this.domain != null && this.domain.Length >= 1 && this.domain[ 0 ] == '.' )
{
int wildcard = accessInfo.domain.IndexOf( this.domain.Substring( 1 ) );
if( wildcard == -1 || wildcard != accessInfo.domain.Length - this.domain.Length + 1 )
{
return false;
}
}
else if ( this.domain != null )
{
return false;
}
return true;
}
public string ToValueString()
{
return this.name + "=" + this.value;
}
public override string ToString()
{
List< string > elements = new List< string >();
elements.Add( this.name + "=" + this.value );
if( this.expirationDate != DateTime.MaxValue )
{
elements.Add( "expires=" + this.expirationDate.ToString() );
}
if( this.domain != null )
{
elements.Add( "domain=" + this.domain );
}
if( this.path != null )
{
elements.Add( "path=" + this.path );
}
if( this.secure )
{
elements.Add( "secure" );
}
if( this.scriptAccessible == false )
{
elements.Add( "httponly" );
}
return String.Join( "; ", elements.ToArray() );
}
}
public delegate void ContentsChangedDelegate();
public class CookieJar
{
private static string version = "v2";
private object cookieJarLock = new object();
private static CookieJar instance;
public Dictionary< string, List< Cookie > > cookies;
public ContentsChangedDelegate ContentsChanged;
public static CookieJar Instance
{
get
{
if ( instance == null )
{
instance = new CookieJar();
}
return instance;
}
}
public CookieJar ()
{
this.Clear();
}
public void Clear()
{
lock( cookieJarLock )
{
cookies = new Dictionary< string, List< Cookie > >();
if ( ContentsChanged != null )
{
ContentsChanged();
}
}
}
public bool SetCookie( Cookie cookie )
{
lock( cookieJarLock )
{
bool expired = cookie.expirationDate < DateTime.Now;
if ( cookies.ContainsKey( cookie.name ) )
{
for( int index = 0; index < cookies[ cookie.name ].Count; ++index )
{
Cookie collidableCookie = cookies[ cookie.name ][ index ];
if ( collidableCookie.CollidesWith( new CookieAccessInfo( cookie ) ) )
{
if( expired )
{
cookies[ cookie.name ].RemoveAt( index );
if ( cookies[ cookie.name ].Count == 0 )
{
cookies.Remove( cookie.name );
if ( ContentsChanged != null )
{
ContentsChanged();
}
}
return false;
}
else
{
cookies[ cookie.name ][ index ] = cookie;
if ( ContentsChanged != null )
{
ContentsChanged();
}
return true;
}
}
}
if ( expired )
{
return false;
}
cookies[ cookie.name ].Add( cookie );
if ( ContentsChanged != null )
{
ContentsChanged();
}
return true;
}
if ( expired )
{
return false;
}
cookies[ cookie.name ] = new List< Cookie >();
cookies[ cookie.name ].Add( cookie );
if ( ContentsChanged != null )
{
ContentsChanged();
}
return true;
}
}
// TODO: figure out a way to respect the scriptAccessible flag and supress cookies being
// returned that should not be. The issue is that at some point, within this
// library, we need to send all the correct cookies back in the request. Right now
// there's no way to add all cookies (regardless of script accessibility) to the
// request without exposing cookies that should not be script accessible.
public Cookie GetCookie( string name, CookieAccessInfo accessInfo )
{
if ( !cookies.ContainsKey( name ) )
{
return null;
}
for ( int index = 0; index < cookies[ name ].Count; ++index )
{
Cookie cookie = cookies[ name ][ index ];
if ( cookie.expirationDate > DateTime.Now && cookie.Matches( accessInfo ) )
{
return cookie;
}
}
return null;
}
public List< Cookie > GetCookies( CookieAccessInfo accessInfo )
{
List< Cookie > result = new List< Cookie >();
foreach ( string cookieName in cookies.Keys )
{
Cookie cookie = this.GetCookie( cookieName, accessInfo );
if ( cookie != null )
{
result.Add( cookie );
}
}
return result;
}
public void SetCookies( Cookie[] cookieObjects )
{
for ( var index = 0; index < cookieObjects.Length; ++index )
{
this.SetCookie( cookieObjects[ index ] );
}
}
private static string cookiesStringPattern = "[:](?=\\s*[a-zA-Z0-9_\\-]+\\s*[=])";
public void SetCookies( string cookiesString )
{
Match match = Regex.Match( cookiesString, cookiesStringPattern );
if ( !match.Success )
{
throw new Exception( "Could not parse cookies string: " + cookiesString );
}
for ( int index = 0; index < match.Groups.Count; ++index )
{
this.SetCookie( new Cookie( match.Groups[ index ].Value ) );
}
}
private static string boundary = "\n!!::!!\n";
public string Serialize()
{
string result = version + boundary;
lock( cookieJarLock )
{
foreach ( string key in cookies.Keys )
{
for ( int index = 0; index < cookies[ key ].Count; ++index )
{
result += cookies[ key ][ index ].ToString() + boundary;
}
}
}
return result;
}
public void Deserialize( string cookieJarString, bool clear )
{
if ( clear )
{
this.Clear();
}
Regex regex = new Regex( boundary );
string[] cookieStrings = regex.Split( cookieJarString );
bool readVersion = false;
foreach ( string cookieString in cookieStrings )
{
if ( !readVersion )
{
if ( cookieString.IndexOf( version ) != 0 )
{
return;
}
readVersion = true;
continue;
}
if ( cookieString.Length > 0 )
{
this.SetCookie( new Cookie( cookieString ) );
}
}
}
}
}