Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CookieTokenizer.cs
Go to the documentation of this file.
1namespace System.Net;
2
3internal struct CookieTokenizer
4{
5 private struct RecognizedAttribute
6 {
7 private readonly string _name;
8
9 private readonly CookieToken _token;
10
11 internal CookieToken Token => _token;
12
13 internal RecognizedAttribute(string name, CookieToken token)
14 {
15 _name = name;
16 _token = token;
17 }
18
19 internal bool IsEqualTo(string value)
20 {
21 return string.Equals(_name, value, StringComparison.OrdinalIgnoreCase);
22 }
23 }
24
25 private bool _eofCookie;
26
27 private int _index;
28
29 private readonly int _length;
30
31 private string _name;
32
33 private bool _quoted;
34
35 private int _start;
36
38
39 private int _tokenLength;
40
41 private readonly string _tokenStream;
42
43 private string _value;
44
45 private int _cookieStartIndex;
46
47 private int _cookieLength;
48
49 private static readonly RecognizedAttribute[] s_recognizedAttributes = new RecognizedAttribute[11]
50 {
51 new RecognizedAttribute("Path", CookieToken.Path),
52 new RecognizedAttribute("Max-Age", CookieToken.MaxAge),
53 new RecognizedAttribute("Expires", CookieToken.Expires),
54 new RecognizedAttribute("Version", CookieToken.Version),
55 new RecognizedAttribute("Domain", CookieToken.Domain),
56 new RecognizedAttribute("Secure", CookieToken.Secure),
57 new RecognizedAttribute("Discard", CookieToken.Discard),
58 new RecognizedAttribute("Port", CookieToken.Port),
59 new RecognizedAttribute("Comment", CookieToken.Comment),
60 new RecognizedAttribute("CommentURL", CookieToken.CommentUrl),
61 new RecognizedAttribute("HttpOnly", CookieToken.HttpOnly)
62 };
63
64 private static readonly RecognizedAttribute[] s_recognizedServerAttributes = new RecognizedAttribute[5]
65 {
66 new RecognizedAttribute("$Path", CookieToken.Path),
67 new RecognizedAttribute("$Version", CookieToken.Version),
68 new RecognizedAttribute("$Domain", CookieToken.Domain),
69 new RecognizedAttribute("$Port", CookieToken.Port),
70 new RecognizedAttribute("$HttpOnly", CookieToken.HttpOnly)
71 };
72
73 internal bool EndOfCookie
74 {
75 get
76 {
77 return _eofCookie;
78 }
79 set
80 {
82 }
83 }
84
85 internal bool Eof => _index >= _length;
86
87 internal string Name
88 {
89 get
90 {
91 return _name;
92 }
93 set
94 {
95 _name = value;
96 }
97 }
98
99 internal bool Quoted
100 {
101 get
102 {
103 return _quoted;
104 }
105 set
106 {
107 _quoted = value;
108 }
109 }
110
111 internal CookieToken Token
112 {
113 get
114 {
115 return _token;
116 }
117 set
118 {
119 _token = value;
120 }
121 }
122
123 internal string Value
124 {
125 get
126 {
127 return _value;
128 }
129 set
130 {
131 _value = value;
132 }
133 }
134
135 internal CookieTokenizer(string tokenStream)
136 {
137 this = default(CookieTokenizer);
138 _length = tokenStream.Length;
139 _tokenStream = tokenStream;
140 _value = string.Empty;
141 }
142
143 internal string Extract()
144 {
145 string result = string.Empty;
146 if (_tokenLength != 0)
147 {
148 result = (Quoted ? _tokenStream.Substring(_start, _tokenLength) : _tokenStream.SubstringTrim(_start, _tokenLength));
149 }
150 return result;
151 }
152
153 internal CookieToken FindNext(bool ignoreComma, bool ignoreEquals)
154 {
155 _tokenLength = 0;
156 _start = _index;
157 while (_index < _length && char.IsWhiteSpace(_tokenStream[_index]))
158 {
159 _index++;
160 _start++;
161 }
162 CookieToken result = CookieToken.End;
163 int num = 1;
164 if (!Eof)
165 {
166 if (_tokenStream[_index] == '"')
167 {
168 Quoted = true;
169 _index++;
170 bool flag = false;
171 while (_index < _length)
172 {
173 char c = _tokenStream[_index];
174 if (!flag && c == '"')
175 {
176 break;
177 }
178 if (flag)
179 {
180 flag = false;
181 }
182 else if (c == '\\')
183 {
184 flag = true;
185 }
186 _index++;
187 }
188 if (_index < _length)
189 {
190 _index++;
191 }
193 num = 0;
194 ignoreComma = false;
195 }
196 while (_index < _length && _tokenStream[_index] != ';' && (ignoreEquals || _tokenStream[_index] != '=') && (ignoreComma || _tokenStream[_index] != ','))
197 {
198 if (_tokenStream[_index] == ',')
199 {
200 _start = _index + 1;
201 _tokenLength = -1;
202 ignoreComma = false;
203 }
204 _index++;
205 _tokenLength += num;
206 }
207 if (!Eof)
208 {
209 switch (_tokenStream[_index])
210 {
211 case ';':
212 result = CookieToken.EndToken;
213 break;
214 case '=':
215 result = CookieToken.Equals;
216 break;
217 default:
219 result = CookieToken.EndCookie;
220 break;
221 }
222 _index++;
223 }
224 if (Eof)
225 {
227 }
228 }
229 return result;
230 }
231
232 internal CookieToken Next(bool first, bool parseResponseCookies)
233 {
234 Reset();
235 if (first)
236 {
238 _cookieLength = 0;
239 }
240 CookieToken cookieToken = FindNext(ignoreComma: false, ignoreEquals: false);
241 if (cookieToken == CookieToken.EndCookie)
242 {
243 EndOfCookie = true;
244 }
245 if (cookieToken == CookieToken.End || cookieToken == CookieToken.EndCookie)
246 {
247 string text2 = (Name = Extract());
248 if (text2.Length != 0)
249 {
250 Token = TokenFromName(parseResponseCookies);
251 return CookieToken.Attribute;
252 }
253 return cookieToken;
254 }
255 Name = Extract();
256 if (first)
257 {
258 Token = CookieToken.CookieName;
259 }
260 else
261 {
262 Token = TokenFromName(parseResponseCookies);
263 }
264 if (cookieToken == CookieToken.Equals)
265 {
266 cookieToken = FindNext(!first && Token == CookieToken.Expires, ignoreEquals: true);
267 if (cookieToken == CookieToken.EndCookie)
268 {
269 EndOfCookie = true;
270 }
271 Value = Extract();
272 return CookieToken.NameValuePair;
273 }
274 return CookieToken.Attribute;
275 }
276
277 internal void Reset()
278 {
279 _eofCookie = false;
280 _name = string.Empty;
281 _quoted = false;
282 _start = _index;
283 _token = CookieToken.Nothing;
284 _tokenLength = 0;
285 _value = string.Empty;
286 }
287
288 internal CookieToken TokenFromName(bool parseResponseCookies)
289 {
290 if (!parseResponseCookies)
291 {
292 for (int i = 0; i < s_recognizedServerAttributes.Length; i++)
293 {
294 if (s_recognizedServerAttributes[i].IsEqualTo(Name))
295 {
296 return s_recognizedServerAttributes[i].Token;
297 }
298 }
299 }
300 else
301 {
302 for (int j = 0; j < s_recognizedAttributes.Length; j++)
303 {
304 if (s_recognizedAttributes[j].IsEqualTo(Name))
305 {
306 return s_recognizedAttributes[j].Token;
307 }
308 }
309 }
310 return CookieToken.Unknown;
311 }
312}
readonly System.Net.CookieToken _token
RecognizedAttribute(string name, CookieToken token)
CookieToken TokenFromName(bool parseResponseCookies)
CookieToken FindNext(bool ignoreComma, bool ignoreEquals)
static readonly RecognizedAttribute[] s_recognizedAttributes
CookieToken Next(bool first, bool parseResponseCookies)
System.Net.CookieToken FindNext(bool ignoreComma, bool ignoreEquals)
System.Net.CookieToken _token
CookieTokenizer(string tokenStream)
System.Net.CookieToken TokenFromName(bool parseResponseCookies)
static readonly RecognizedAttribute[] s_recognizedServerAttributes