/
cookie.cs
1645 lines (1456 loc) · 59 KB
/
cookie.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="cookie.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Net {
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
internal enum CookieVariant {
Unknown,
Plain,
Rfc2109,
Rfc2965,
Default = Rfc2109
}
//
// Cookie class
//
// Adheres to RFC 2965
//
// Currently, only client-side cookies. The cookie classes know how to
// parse a set-cookie format string, but not a cookie format string
// (e.g. "Cookie: $Version=1; name=value; $Path=/foo; $Secure")
//
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[Serializable]
public sealed class Cookie {
internal const int MaxSupportedVersion = 1;
internal const string CommentAttributeName = "Comment";
internal const string CommentUrlAttributeName = "CommentURL";
internal const string DiscardAttributeName = "Discard";
internal const string DomainAttributeName = "Domain";
internal const string ExpiresAttributeName = "Expires";
internal const string MaxAgeAttributeName = "Max-Age";
internal const string PathAttributeName = "Path";
internal const string PortAttributeName = "Port";
internal const string SecureAttributeName = "Secure";
internal const string VersionAttributeName = "Version";
internal const string HttpOnlyAttributeName = "HttpOnly";
internal const string SeparatorLiteral = "; ";
internal const string EqualsLiteral = "=";
internal const string QuotesLiteral = "\"";
internal const string SpecialAttributeLiteral = "$";
internal static readonly char[] PortSplitDelimiters = new char[] {' ', ',', '\"'};
internal static readonly char[] Reserved2Name = new char[] {' ', '\t', '\r', '\n', '=', ';', ',' };
internal static readonly char[] Reserved2Value = new char[] {';', ',' };
private static Comparer staticComparer = new Comparer();
// fields
string m_comment = string.Empty;
Uri m_commentUri = null;
CookieVariant m_cookieVariant = CookieVariant.Plain;
bool m_discard = false;
string m_domain = string.Empty;
bool m_domain_implicit = true;
DateTime m_expires = DateTime.MinValue;
string m_name = string.Empty;
string m_path = string.Empty;
bool m_path_implicit = true;
string m_port = string.Empty;
bool m_port_implicit = true;
int[] m_port_list = null;
bool m_secure = false;
[System.Runtime.Serialization.OptionalField]
bool m_httpOnly=false;
DateTime m_timeStamp = DateTime.Now;
string m_value = string.Empty;
int m_version = 0;
string m_domainKey = string.Empty;
internal bool IsQuotedVersion = false;
internal bool IsQuotedDomain = false;
// constructors
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Cookie() {
}
//public Cookie(string cookie) {
// if ((cookie == null) || (cookie == String.Empty)) {
// throw new ArgumentException("cookie");
// }
// Parse(cookie.Trim());
// Validate();
//}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Cookie(string name, string value) {
Name = name;
m_value = value;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Cookie(string name, string value, string path)
: this(name, value) {
Path = path;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Cookie(string name, string value, string path, string domain)
: this(name, value, path) {
Domain = domain;
}
// properties
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Comment {
get {
return m_comment;
}
set {
if (value == null) {
value = string.Empty;
}
m_comment = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Uri CommentUri {
get {
return m_commentUri;
}
set {
m_commentUri = value;
}
}
public bool HttpOnly{
get{
return m_httpOnly;
}
set{
m_httpOnly = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool Discard {
get {
return m_discard;
}
set {
m_discard = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Domain {
get {
return m_domain;
}
set {
m_domain = (value == null? String.Empty : value);
m_domain_implicit = false;
m_domainKey = string.Empty; //this will get it value when adding into the Container.
}
}
private string _Domain {
get {
return (Plain || m_domain_implicit || (m_domain.Length == 0))
? string.Empty
: (SpecialAttributeLiteral
+ DomainAttributeName
+ EqualsLiteral + (IsQuotedDomain? "\"": string.Empty)
+ m_domain + (IsQuotedDomain? "\"": string.Empty)
);
}
}
internal bool DomainImplicit {
get {
return m_domain_implicit;
}
set {
m_domain_implicit = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool Expired {
get {
return (m_expires != DateTime.MinValue) && (m_expires.ToLocalTime() <= DateTime.Now);
}
set {
if (value == true) {
m_expires = DateTime.Now;
}
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public DateTime Expires {
get {
return m_expires;
}
set {
m_expires = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Name {
get {
return m_name;
}
set {
if (ValidationHelper.IsBlankString(value) || !InternalSetName(value)) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, "Name", value == null? "<null>": value));
}
}
}
internal bool InternalSetName(string value) {
if (ValidationHelper.IsBlankString(value) || value[0] == '$' || value.IndexOfAny(Reserved2Name) != -1) {
m_name = string.Empty;
return false;
}
m_name = value;
return true;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Path {
get {
return m_path;
}
set {
m_path = (value == null? String.Empty : value);
m_path_implicit = false;
}
}
private string _Path {
get {
return (Plain || m_path_implicit || (m_path.Length == 0))
? string.Empty
: (SpecialAttributeLiteral
+ PathAttributeName
+ EqualsLiteral
+ m_path
);
}
}
internal bool Plain {
get {
return Variant == CookieVariant.Plain;
}
}
internal Cookie Clone() {
Cookie clonedCookie = new Cookie(m_name, m_value);
//
// Copy over all the properties from the original cookie
//
if (!m_port_implicit) {
clonedCookie.Port = m_port;
}
if (!m_path_implicit) {
clonedCookie.Path = m_path;
}
clonedCookie.Domain = m_domain;
//
// If the domain in the original cookie was implicit, we should preserve that property
clonedCookie.DomainImplicit = m_domain_implicit;
clonedCookie.m_timeStamp = m_timeStamp;
clonedCookie.Comment = m_comment;
clonedCookie.CommentUri = m_commentUri;
clonedCookie.HttpOnly = m_httpOnly;
clonedCookie.Discard = m_discard;
clonedCookie.Expires = m_expires;
clonedCookie.Version = m_version;
clonedCookie.Secure = m_secure;
//
// The variant is set when we set properties like port/version. So,
// we should copy over the variant from the original cookie after
// we set all other properties
clonedCookie.m_cookieVariant = m_cookieVariant;
return clonedCookie;
}
private static bool IsDomainEqualToHost(string domain, string host) {
//
// +1 in the host length is to account for the leading dot in domain
if ((host.Length + 1 == domain.Length) &&
(string.Compare(host, 0, domain, 1, host.Length, StringComparison.OrdinalIgnoreCase) == 0)) {
return true;
}
else {
return false;
}
}
//
// According to spec we must assume default values for attributes but still
// keep in mind that we must not include them into the requests.
// We also check the validiy of all attributes based on the version and variant (read RFC)
//
// To work properly this function must be called after cookie construction with
// default (response) URI AND set_default == true
//
// Afterwards, the function can be called many times with other URIs and
// set_default == false to check whether this cookie matches given uri
//
internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDomain, string localDomain, bool set_default, bool isThrow) {
string host = uri.Host;
int port = uri.Port;
string path = uri.AbsolutePath;
bool valid= true;
if (set_default) {
// Set Variant. If version is zero => reset cookie to Version0 style
if (Version == 0) {
variant = CookieVariant.Plain;
}
else if (Version == 1 && variant == CookieVariant.Unknown) {
//since we don't expose Variant to an app, set it to Default
variant = CookieVariant.Default;
}
m_cookieVariant = variant;
}
//Check the name
if (m_name == null || m_name.Length == 0 || m_name[0] == '$' || m_name.IndexOfAny(Reserved2Name) != -1) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, "Name", m_name == null? "<null>": m_name));
}
return false;
}
//Check the value
if (m_value == null ||
(!(m_value.Length > 2 && m_value[0] == '\"' && m_value[m_value.Length-1] == '\"') && m_value.IndexOfAny(Reserved2Value) != -1)) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, "Value", m_value == null? "<null>": m_value));
}
return false;
}
//Check Comment syntax
if (Comment != null && !(Comment.Length > 2 && Comment[0] == '\"' && Comment[Comment.Length-1] == '\"')
&& (Comment.IndexOfAny(Reserved2Value) != -1)) {
if (isThrow)
throw new CookieException(SR.GetString(SR.net_cookie_attribute, CommentAttributeName, Comment));
return false;
}
//Check Path syntax
if (Path != null && !(Path.Length > 2 && Path[0] == '\"' && Path[Path.Length-1] == '\"')
&& (Path.IndexOfAny(Reserved2Value) != -1)) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PathAttributeName, Path));
}
return false;
}
//Check/set domain
// if domain is implicit => assume a) uri is valid, b) just set domain to uri hostname
if (set_default && m_domain_implicit == true) {
m_domain = host;
}
else {
if (!m_domain_implicit) {
// Forwarding note: If Uri.Host is of IP address form then the only supported case
// is for IMPLICIT domain property of a cookie.
// The below code (explicit cookie.Domain value) will try to parse Uri.Host IP string
// as a fqdn and reject the cookie
//aliasing since we might need the KeyValue (but not the original one)
string domain = m_domain;
//Syntax check for Domain charset plus empty string
if (!DomainCharsTest(domain)) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, DomainAttributeName, domain == null? "<null>": domain));
}
return false;
}
//domain must start with '.' if set explicitly
if(domain[0] != '.' ) {
if (!(variant == CookieVariant.Rfc2965 || variant == CookieVariant.Plain)) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, DomainAttributeName, m_domain));
}
return false;
}
domain = '.' + domain;
}
int host_dot = host.IndexOf('.');
// First quick check is for pushing a cookie into the local domain
if (isLocalDomain && (string.Compare(localDomain, domain, StringComparison.OrdinalIgnoreCase ) == 0)) {
valid = true;
}
else if (domain.IndexOf('.', 1, domain.Length-2) == -1) {
// A single label domain is valid only if the domain is exactly the same as the host specified in the URI
if (!IsDomainEqualToHost(domain, host)) {
valid = false;
}
}
else if (variant == CookieVariant.Plain) {
// We distiguish between Version0 cookie and other versions on domain issue
// According to Version0 spec a domain must be just a substring of the hostname
if (!IsDomainEqualToHost(domain, host)) {
if (host.Length <= domain.Length ||
string.Compare(host, host.Length-domain.Length, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0) {
valid = false;
}
}
}
else if (host_dot == -1 ||
domain.Length != host.Length-host_dot ||
string.Compare(host, host_dot, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0) {
//starting the first dot, the host must match the domain
// for null hosts, the host must match the domain exactly
if (!IsDomainEqualToHost(domain, host)) {
valid = false;
}
}
if (valid) {
m_domainKey = domain.ToLower(CultureInfo.InvariantCulture);
}
}
else {
// for implicitly set domain AND at the set_default==false time
// we simply got to match uri.Host against m_domain
if (string.Compare(host, m_domain, StringComparison.OrdinalIgnoreCase) != 0) {
valid = false;
}
}
if(!valid) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, DomainAttributeName, m_domain));
}
return false;
}
}
//Check/Set Path
if (set_default && m_path_implicit == true) {
//assuming that uri path is always valid and contains at least one '/'
switch (m_cookieVariant) {
case CookieVariant.Plain:
m_path = path;
break;
case CookieVariant.Rfc2109:
m_path = path.Substring(0, path.LastIndexOf('/')); //may be empty
break;
case CookieVariant.Rfc2965:
default: //hope future versions will have same 'Path' semantic?
m_path = path.Substring(0, path.LastIndexOf('/')+1);
break;
}
}
else {
//check current path (implicit/explicit) against given uri
if (!path.StartsWith(CookieParser.CheckQuoted(m_path))) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PathAttributeName, m_path));
}
return false;
}
}
// set the default port if Port attribute was present but had no value
if (set_default && (m_port_implicit == false && m_port.Length == 0)) {
m_port_list = new int[1] {port};
}
if(m_port_implicit == false) {
// Port must match agaist the one from the uri
valid = false;
foreach (int p in m_port_list) {
if (p == port) {
valid = true;
break;
}
}
if (!valid) {
if (isThrow) {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PortAttributeName, m_port));
}
return false;
}
}
return true;
}
//Very primitive test to make sure that the name does not have illegal characters
// As per RFC 952 (relaxed on first char could be a digit and string can have '_')
private static bool DomainCharsTest(string name) {
if (name == null || name.Length == 0) {
return false;
}
for(int i=0; i < name.Length; ++i) {
char ch = name[i];
if (!(
(ch >= '0' && ch <= '9') ||
(ch == '.' || ch == '-') ||
(ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch == '_')
))
return false;
}
return true;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Port {
get {
return m_port;
}
set {
m_port_implicit = false;
if ((value == null || value.Length == 0)) {
//"Port" is present but has no value.
m_port = string.Empty;
}
else {
// Parse port list
if (value[0] != '\"' || value[value.Length-1] != '\"') {
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PortAttributeName, value));
}
string[] ports = value.Split(PortSplitDelimiters);
List<int> portList = new List<int>();
int port;
for (int i = 0; i < ports.Length; ++i) {
// Skip spaces
if (ports[i] != string.Empty) {
if (!Int32.TryParse(ports[i], out port))
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PortAttributeName, value));
// valid values for port 0 - 0xFFFF
if ((port < 0) || (port > 0xFFFF))
throw new CookieException(SR.GetString(SR.net_cookie_attribute, PortAttributeName, value));
portList.Add(port);
}
}
m_port_list = portList.ToArray();
m_port = value;
m_version = MaxSupportedVersion;
m_cookieVariant = CookieVariant.Rfc2965;
}
}
}
internal int[] PortList {
get {
//It must be null if Port Attribute was ommitted in the response
return m_port_list;
}
}
private string _Port {
get {
return m_port_implicit ? string.Empty :
(SpecialAttributeLiteral
+ PortAttributeName
+ ((m_port.Length == 0) ? string.Empty : (EqualsLiteral + m_port))
);
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool Secure {
get {
return m_secure;
}
set {
m_secure = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public DateTime TimeStamp {
get {
return m_timeStamp;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Value {
get {
return m_value;
}
set {
m_value = (value == null? String.Empty : value);
}
}
internal CookieVariant Variant {
get {
return m_cookieVariant;
}
set {
// only set by HttpListenerRequest::Cookies_get()
GlobalLog.Assert(value == CookieVariant.Rfc2965, "Cookie#{0}::set_Variant()|value:{1}" , ValidationHelper.HashString(this), value);
m_cookieVariant = value;
}
}
// m_domainKey member is set internally in VerifySetDefaults()
// If it is not set then verification function was not called
// and this should never happen
internal string DomainKey {
get {
return m_domain_implicit ? Domain : m_domainKey;
}
}
//public Version Version {
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int Version {
get {
return m_version;
}
set {
if (value<0) {
throw new ArgumentOutOfRangeException("value");
}
m_version = value;
if (value>0 && m_cookieVariant<CookieVariant.Rfc2109) {
m_cookieVariant = CookieVariant.Rfc2109;
}
}
}
private string _Version {
get {
return (Version == 0) ? string.Empty :
( SpecialAttributeLiteral
+ VersionAttributeName
+ EqualsLiteral + (IsQuotedVersion? "\"": string.Empty)
+ m_version.ToString(NumberFormatInfo.InvariantInfo) + (IsQuotedVersion? "\"": string.Empty));
}
}
// methods
internal static IComparer GetComparer() {
//the class don't have any members, it's safe reuse the instance
return staticComparer;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override bool Equals(object comparand) {
if (!(comparand is Cookie)) {
return false;
}
Cookie other = (Cookie)comparand;
return (string.Compare(Name, other.Name, StringComparison.OrdinalIgnoreCase) == 0)
&& (string.Compare(Value, other.Value, StringComparison.Ordinal) == 0)
&& (string.Compare(Path, other.Path, StringComparison.Ordinal) == 0)
&& (string.Compare(Domain, other.Domain, StringComparison.OrdinalIgnoreCase) == 0)
&& (Version == other.Version)
;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override int GetHashCode() {
//
//string hashString = Name + "=" + Value + ";" + Path + "; " + Domain + "; " + Version;
//int hash = 0;
//
//foreach (char ch in hashString) {
// hash = unchecked(hash << 1 ^ (int)ch);
//}
//return hash;
return (Name + "=" + Value + ";" + Path + "; " + Domain + "; " + Version).GetHashCode();
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override string ToString() {
string domain = _Domain;
string path = _Path;
string port = _Port;
string version = _Version;
string result =
((version.Length == 0)? string.Empty : (version + SeparatorLiteral))
+ Name + EqualsLiteral + Value
+ ((path.Length == 0) ? string.Empty : (SeparatorLiteral + path))
+ ((domain.Length == 0) ? string.Empty : (SeparatorLiteral + domain))
+ ((port.Length == 0) ? string.Empty : (SeparatorLiteral + port))
;
if (result == "=") {
return string.Empty;
}
return result;
}
internal string ToServerString() {
string result = Name + EqualsLiteral + Value;
if (m_comment!=null && m_comment.Length>0) {
result += SeparatorLiteral + CommentAttributeName + EqualsLiteral + m_comment;
}
if (m_commentUri!=null) {
result += SeparatorLiteral + CommentUrlAttributeName + EqualsLiteral + QuotesLiteral + m_commentUri.ToString() + QuotesLiteral;
}
if (m_discard) {
result += SeparatorLiteral + DiscardAttributeName;
}
if (!m_domain_implicit && m_domain!=null && m_domain.Length>0) {
result += SeparatorLiteral + DomainAttributeName + EqualsLiteral + m_domain;
}
if (Expires != DateTime.MinValue) {
int seconds = (int) (Expires.ToLocalTime() - DateTime.Now).TotalSeconds;
if (seconds < 0) {
// This means that the cookie has already expired. Set Max-Age to 0
// so that the client will discard the cookie immediately
seconds = 0;
}
result += SeparatorLiteral + MaxAgeAttributeName + EqualsLiteral + seconds.ToString(NumberFormatInfo.InvariantInfo);
}
if (!m_path_implicit && m_path!=null && m_path.Length>0) {
result += SeparatorLiteral + PathAttributeName + EqualsLiteral + m_path;
}
if (!Plain && !m_port_implicit && m_port!=null && m_port.Length>0) {
// QuotesLiteral are included in m_port
result += SeparatorLiteral + PortAttributeName + EqualsLiteral + m_port;
}
if (m_version>0) {
result += SeparatorLiteral + VersionAttributeName + EqualsLiteral + m_version.ToString(NumberFormatInfo.InvariantInfo);
}
return result==EqualsLiteral ? null : result;
}
//private void Validate() {
// if ((m_name == String.Empty) && (m_value == String.Empty)) {
// throw new CookieException();
// }
// if ((m_name != String.Empty) && (m_name[0] == '$')) {
// throw new CookieException();
// }
//}
#if DEBUG
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
internal void Dump() {
GlobalLog.Print("Cookie: " + ToString() + "->\n"
+ "\tComment = " + Comment + "\n"
+ "\tCommentUri = " + CommentUri + "\n"
+ "\tDiscard = " + Discard + "\n"
+ "\tDomain = " + Domain + "\n"
+ "\tExpired = " + Expired + "\n"
+ "\tExpires = " + Expires + "\n"
+ "\tName = " + Name + "\n"
+ "\tPath = " + Path + "\n"
+ "\tPort = " + Port + "\n"
+ "\tSecure = " + Secure + "\n"
+ "\tTimeStamp = " + TimeStamp + "\n"
+ "\tValue = " + Value + "\n"
+ "\tVariant = " + Variant + "\n"
+ "\tVersion = " + Version + "\n"
+ "\tHttpOnly = " + HttpOnly + "\n"
);
}
#endif
}
internal enum CookieToken {
// state types
Nothing,
NameValuePair, // X=Y
Attribute, // X
EndToken, // ';'
EndCookie, // ','
End, // EOLN
Equals,
// value types
Comment,
CommentUrl,
CookieName,
Discard,
Domain,
Expires,
MaxAge,
Path,
Port,
Secure,
HttpOnly,
Unknown,
Version
}
//
// CookieTokenizer
//
// Used to split a single or multi-cookie (header) string into individual
// tokens
//
internal class CookieTokenizer {
// fields
bool m_eofCookie;
int m_index;
int m_length;
string m_name;
bool m_quoted;
int m_start;
CookieToken m_token;
int m_tokenLength;
string m_tokenStream;
string m_value;
// constructors
internal CookieTokenizer(string tokenStream) {
m_length = tokenStream.Length;
m_tokenStream = tokenStream;
}
// properties
internal bool EndOfCookie {
get {
return m_eofCookie;
}
set {
m_eofCookie = value;
}
}
internal bool Eof {
get {
return m_index >= m_length;
}
}
internal string Name {
get {
return m_name;
}
set {
m_name = value;
}
}
internal bool Quoted {
get {
return m_quoted;
}
set {
m_quoted = value;
}
}
internal CookieToken Token {
get {
return m_token;
}
set {
m_token = value;
}
}
internal string Value {
get {
return m_value;
}
set {
m_value = value;
}
}
// methods
//
// Extract
//
// extract the current token
//
internal string Extract() {
string tokenString = string.Empty;
if (m_tokenLength != 0) {
tokenString = m_tokenStream.Substring(m_start, m_tokenLength);
if (!Quoted) {
tokenString = tokenString.Trim();
}
}
return tokenString;
}
//
// FindNext
//
// Find the start and length of the next token. The token is terminated
// by one of:
//
// - end-of-line
// - end-of-cookie: unquoted comma separates multiple cookies
// - end-of-token: unquoted semi-colon
// - end-of-name: unquoted equals
//
// Inputs:
// <argument> ignoreComma
// true if parsing doesn't stop at a comma. This is only true when
// we know we're parsing an original cookie that has an expires=
// attribute, because the format of the time/date used in expires
// is:
// Wdy, dd-mmm-yyyy HH:MM:SS GMT
//
// <argument> ignoreEquals
// true if parsing doesn't stop at an equals sign. The LHS of the