forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookie.cs
More file actions
152 lines (137 loc) · 4.99 KB
/
Cookie.cs
File metadata and controls
152 lines (137 loc) · 4.99 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
using System;
using System.Collections.Generic;
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 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>
/// <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 = 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 = Value;
this.domain = Domain;
this.path = Path;
this.maxAgeSeconds = MaxAgeSeconds;
this.secure = Secure;
this.httpOnly = HttpOnly;
}
/// <summary>
/// <see cref="Object.ToString()"/>
/// </summary>
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");
return Output.ToString();
}
}
}