@@ -35,12 +35,89 @@ public AdvancedCookieProvider(IFileHandler handler, AdvancedCookieProviderOption
35
35
36
36
String ICookieProvider . GetCookie ( Url url )
37
37
{
38
- throw new NotImplementedException ( ) ;
38
+ var host = CanonicalDomain ( url . HostName ) ;
39
+ var path = String . IsNullOrEmpty ( url . Path ) ? "/" : url . Path ;
40
+ var secure = url . Scheme . IsOneOf ( ProtocolNames . Https , ProtocolNames . Wss ) ;
41
+ var now = DateTime . UtcNow ;
42
+ var cookies = FindCookies ( host , path )
43
+ . ToArray ( )
44
+ . Where ( c =>
45
+ {
46
+ if ( c . IsHostOnly ?? false )
47
+ {
48
+ if ( ! c . Domain . Is ( host ) )
49
+ {
50
+ return false ;
51
+ }
52
+ }
53
+ else if ( ! DomainMatch ( host , c . Domain , false ) )
54
+ {
55
+ return false ;
56
+ }
57
+
58
+ if ( ! CheckPaths ( path , c . Path ) )
59
+ {
60
+ return false ;
61
+ }
62
+
63
+ // "If the cookie's secure-only-flag is true, then the request-uri's
64
+ // scheme must denote a "secure" protocol"
65
+ if ( c . IsSecure && ! secure )
66
+ {
67
+ return false ;
68
+ }
69
+
70
+ // deferred from Section 5.3
71
+ // non-RFC: allow retention of expired cookies by choice
72
+ if ( c . ComputeExpiration ( now ) <= now )
73
+ {
74
+ RemoveCookie ( c . Domain , c . Path , c . Key ) ;
75
+ return false ;
76
+ }
77
+
78
+ return true ;
79
+ } )
80
+ . Select ( c => c . ToGetCookie ( ) )
81
+ . ToArray ( ) ;
82
+ return String . Join ( "; " , cookies ) ;
39
83
}
40
84
41
85
void ICookieProvider . SetCookie ( Url url , String value )
42
86
{
43
- throw new NotImplementedException ( ) ;
87
+ var host = CanonicalDomain ( url . HostName ) ;
88
+ var cookie = WebCookie . FromString ( value ) ;
89
+
90
+ if ( cookie != null )
91
+ {
92
+ if ( ! String . IsNullOrEmpty ( cookie . Domain ) )
93
+ {
94
+ var cdom = cookie . CanonicalDomain ;
95
+ var suffix = GetPublicSuffix ( cdom ) ;
96
+
97
+ if ( suffix == null || ! DomainMatch ( host , cdom , false ) )
98
+ {
99
+ return ;
100
+ }
101
+
102
+ // don't reset if already set
103
+ if ( ! cookie . IsHostOnly . HasValue )
104
+ {
105
+ cookie . IsHostOnly = false ;
106
+ }
107
+ }
108
+ else
109
+ {
110
+ cookie . Domain = host ;
111
+ cookie . IsHostOnly = true ;
112
+ }
113
+
114
+ if ( String . IsNullOrEmpty ( cookie . Path ) || cookie . Path [ 0 ] != '/' )
115
+ {
116
+ cookie . Path = GetDefaultPath ( url . Path ) ;
117
+ }
118
+
119
+ AddCookie ( cookie ) ;
120
+ }
44
121
}
45
122
46
123
/// <summary>
@@ -87,9 +164,11 @@ public IEnumerable<WebCookie> FindCookies(String domain, String path = null)
87
164
/// <param name="cookie">The cookie to add.</param>
88
165
public void AddCookie ( WebCookie cookie )
89
166
{
90
- _cookies . Remove ( FindCookie ( cookie . Domain , cookie . Path , cookie . Key ) ) ;
91
- _cookies . Add ( cookie ) ;
92
- WriteCookies ( ) ;
167
+ if ( cookie != null )
168
+ {
169
+ _cookies . Remove ( FindCookie ( cookie . Domain , cookie . Path , cookie . Key ) ) ;
170
+ InsertCookie ( cookie ) ;
171
+ }
93
172
}
94
173
95
174
/// <summary>
@@ -99,7 +178,11 @@ public void AddCookie(WebCookie cookie)
99
178
/// <param name="newCookie">The updated cookie content.</param>
100
179
public void UpdateCookie ( WebCookie oldCookie , WebCookie newCookie )
101
180
{
102
- _cookies . Remove ( FindCookie ( oldCookie . Domain , oldCookie . Path , oldCookie . Key ) ) ;
181
+ if ( oldCookie != null )
182
+ {
183
+ _cookies . Remove ( FindCookie ( oldCookie . Domain , oldCookie . Path , oldCookie . Key ) ) ;
184
+ }
185
+
103
186
AddCookie ( newCookie ) ;
104
187
}
105
188
@@ -155,6 +238,21 @@ public IEnumerable<WebCookie> RemoveAllCookies()
155
238
156
239
private List < WebCookie > ReadCookies ( ) => Deserialize ( _handler . ReadFile ( ) , _forceParse , _httpOnlyExtension ) ;
157
240
241
+ private void InsertCookie ( WebCookie cookie )
242
+ {
243
+ for ( var i = 0 ; i < _cookies . Count ; i ++ )
244
+ {
245
+ if ( cookie . CompareTo ( _cookies [ i ] ) > 0 )
246
+ {
247
+ _cookies . Insert ( i , cookie ) ;
248
+ return ;
249
+ }
250
+ }
251
+
252
+ _cookies . Add ( cookie ) ;
253
+ WriteCookies ( ) ;
254
+ }
255
+
158
256
private void WriteCookies ( )
159
257
{
160
258
var selection = _cookies . Where ( m => m . IsPersistent ) ;
0 commit comments