-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCookie.cs
More file actions
358 lines (311 loc) · 10.2 KB
/
Cookie.cs
File metadata and controls
358 lines (311 loc) · 10.2 KB
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Waher.Content;
namespace Waher.Networking.HTTP
{
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
public class Cookie
{
private Dictionary<string, string> otherProperties = null;
private DateTimeOffset? expires = null;
private readonly string name;
private readonly string value;
private readonly string domain = null;
private readonly string path = null;
private readonly int? maxAgeSeconds = null;
private readonly bool secure = false;
private readonly bool httpOnly = false;
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
private Cookie()
{
}
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
/// <param name="Name">Cookie name.</param>
/// <param name="Value">Value of cookie</param>
/// <param name="Domain">To which domain the cookie belongs.</param>
/// <param name="Path">Path limitation.</param>
public Cookie(string Name, string Value, string Domain, string Path)
: this(Name, Value, Domain, Path, null, false, false)
{
}
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
/// <param name="Name">Cookie name.</param>
/// <param name="Value">Value of cookie</param>
/// <param name="Domain">To which domain the cookie belongs.</param>
/// <param name="Path">Path limitation.</param>
/// <param name="Expires">When cookie expires.</param>
public Cookie(string Name, string Value, string Domain, string Path, DateTimeOffset? Expires)
: this(Name, Value, Domain, Path, Expires, false, false)
{
}
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
/// <param name="Name">Cookie name.</param>
/// <param name="Value">Value of cookie</param>
/// <param name="Domain">To which domain the cookie belongs.</param>
/// <param name="Path">Path limitation.</param>
/// <param name="Expires">When cookie expires.</param>
/// <param name="Secure">If the cookie should only be used over secure channels.</param>
/// <param name="HttpOnly">If the cookie should only be made available over HTTP requests.</param>
public Cookie(string Name, string Value, string Domain, string Path, DateTimeOffset? Expires, bool Secure, bool HttpOnly)
{
this.name = Name;
this.value = WebUtility.UrlEncode(Value);
this.domain = Domain;
this.path = Path;
this.expires = Expires;
this.secure = Secure;
this.httpOnly = HttpOnly;
}
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
/// <param name="Name">Cookie name.</param>
/// <param name="Value">Value of cookie</param>
/// <param name="Domain">To which domain the cookie belongs.</param>
/// <param name="Path">Path limitation.</param>
/// <param name="MaxAgeSeconds">Maximum age of cookie in seconds. A relative alternative to the absolut Expires parameter.</param>
public Cookie(string Name, string Value, string Domain, string Path, int MaxAgeSeconds)
: this(Name, Value, Domain, Path, MaxAgeSeconds, false, false)
{
}
/// <summary>
/// Contains information about a cookie, as defined in RFC 6265.
/// https://tools.ietf.org/html/rfc6265
/// </summary>
/// <param name="Name">Cookie name.</param>
/// <param name="Value">Value of cookie</param>
/// <param name="Domain">To which domain the cookie belongs.</param>
/// <param name="Path">Path limitation.</param>
/// <param name="MaxAgeSeconds">Maximum age of cookie in seconds. A relative alternative to the absolut Expires parameter.</param>
/// <param name="Secure">If the cookie should only be used over secure channels.</param>
/// <param name="HttpOnly">If the cookie should only be made available over HTTP requests.</param>
public Cookie(string Name, string Value, string Domain, string Path, int MaxAgeSeconds, bool Secure, bool HttpOnly)
{
this.name = Name;
this.value = WebUtility.UrlEncode(Value);
this.domain = Domain;
this.path = Path;
this.maxAgeSeconds = MaxAgeSeconds;
this.secure = Secure;
this.httpOnly = HttpOnly;
}
/// <summary>
/// Any other properties registered on the cookie.
/// </summary>
public IEnumerable<KeyValuePair<string, string>> OtherProperties => (IEnumerable<KeyValuePair<string, string>>)this.otherProperties ?? Array.Empty<KeyValuePair<string, string>>();
/// <summary>
/// Name of cookie
/// </summary>
public string Name => this.name;
/// <summary>
/// Value of cookie.
/// </summary>
public string Value => this.value;
/// <summary>
/// Domain of cookie.
/// </summary>
public string Domain => this.domain;
/// <summary>
/// Path of cookie.
/// </summary>
public string Path => this.path;
/// <summary>
/// When cookie expires.
/// </summary>
public DateTimeOffset? Expires => this.expires;
/// <summary>
/// Maximum age of cookie, in seconds.
/// </summary>
public int? MaxAgeSeconds => this.maxAgeSeconds;
/// <summary>
/// If cookie is secure.
/// </summary>
public bool Secure => this.secure;
/// <summary>
/// If cookie is for HTTP use only.
/// </summary>
public bool HttpOnly => this.httpOnly;
/// <summary>
/// Creates a Cookie object from a Set-Cookie header field value.
/// </summary>
/// <param name="SetCookieFieldValue">Set-Cookie header field value.</param>
/// <returns>Cookie object representation. If no cookie information available, null is returned.</returns>
public static Cookie FromSetCookie(string SetCookieFieldValue)
{
DateTimeOffset? Expires = null;
string Name = string.Empty;
string Value = string.Empty;
string Domain = null;
string Path = null;
string SameSite = null;
int? MaxAgeSeconds = null;
bool Secure = false;
bool HttpOnly = false;
bool Partitioned = false;
bool First = true;
int i;
foreach (KeyValuePair<string, string> P in CommonTypes.ParseFieldValues(SetCookieFieldValue))
{
if (First)
{
Name = P.Key;
Value = WebUtility.UrlDecode(P.Value);
First = false;
}
else
{
switch (P.Key.ToLower())
{
case "domain":
Domain = P.Value;
break;
case "expires":
if (CommonTypes.TryParseRfc822(P.Value, out DateTimeOffset DTO))
Expires = DTO;
else if (int.TryParse(P.Value, out i))
Expires = DateTimeOffset.UtcNow.AddSeconds(i);
break;
case "secure":
Secure = true;
break;
case "httponly":
HttpOnly = true;
break;
case "max-age":
if (int.TryParse(P.Value, out i))
MaxAgeSeconds = i;
break;
case "partitioned":
Partitioned = true;
break;
case "path":
Path = P.Value;
break;
case "samesite":
SameSite = P.Value;
break;
}
}
}
if (First)
return null;
Cookie Result;
if (MaxAgeSeconds.HasValue)
Result = new Cookie(Name, Value, Domain, Path, MaxAgeSeconds.Value, Secure, HttpOnly);
else
Result = new Cookie(Name, Value, Domain, Path, Expires, Secure, HttpOnly);
if (Partitioned)
Result.AddProperty("Partitioned", string.Empty);
if (!string.IsNullOrEmpty(SameSite))
Result.AddProperty("SameSite", SameSite);
return Result;
}
/// <summary>
/// Adds another property on the cookie.
/// </summary>
/// <param name="Name">Name of property.</param>
/// <param name="Value">Value of property.</param>
public void AddProperty(string Name, string Value)
{
if (this.otherProperties is null)
this.otherProperties = new Dictionary<string, string>();
this.otherProperties[Name] = Value;
}
/// <summary>
/// Tries to get another property value previously added using <see cref="AddProperty(string, string)"/>.
/// </summary>
/// <param name="Name">Name of property.</param>
/// <param name="Value">Value of property, if found, null otherwise.</param>
/// <returns>If the property was found.</returns>
public bool TryGetProperty(string Name, out string Value)
{
if (this.otherProperties is null)
{
Value = null;
return false;
}
else
return this.otherProperties.TryGetValue(Name, out Value);
}
/// <inheritdoc/>
public override string ToString()
{
StringBuilder Output = new StringBuilder();
Output.Append(this.name);
Output.Append('=');
Output.Append(this.value);
if (this.expires.HasValue)
{
Output.Append("; Expires=");
Output.Append(CommonTypes.EncodeRfc822(this.expires.Value));
}
if (this.maxAgeSeconds.HasValue)
{
Output.Append("; Max-Age=");
Output.Append(this.maxAgeSeconds.Value.ToString());
}
if (!(this.domain is null))
{
Output.Append("; Domain=");
Output.Append(this.domain);
}
if (!(this.path is null))
{
Output.Append("; Path=");
Output.Append(this.path);
}
if (this.secure)
Output.Append("; Secure");
if (this.httpOnly)
Output.Append("; HttpOnly");
if (!(this.otherProperties is null))
{
foreach (KeyValuePair<string, string> P in this.otherProperties)
{
Output.Append("; ");
Output.Append(P.Key);
if (!string.IsNullOrEmpty(P.Value))
{
Output.Append('=');
Output.Append(P.Value);
}
}
}
return Output.ToString();
}
/// <summary>
/// Converts a <see cref="Cookie"/> to a <see cref="System.Net.Cookie"/>
/// </summary>
/// <param name="Cookie">Cookie</param>
public static explicit operator System.Net.Cookie(Cookie Cookie)
{
return new System.Net.Cookie(Cookie.name, Cookie.value, Cookie.path, Cookie.domain);
}
/// <summary>
/// Converts a <see cref="System.Net.Cookie"/> to a <see cref="Cookie"/>
/// </summary>
/// <param name="Cookie">Cookie</param>
public static explicit operator Cookie(System.Net.Cookie Cookie)
{
return new Cookie(Cookie.Name, Cookie.Value, Cookie.Domain, Cookie.Path, Cookie.Expires, Cookie.Secure, Cookie.HttpOnly);
}
}
}