24
24
25
25
namespace System . Net . Http
26
26
{
27
+ #region Enums
28
+ internal enum CookieUsePolicy
29
+ {
30
+ IgnoreCookies = 0 ,
31
+ UseSpecifiedCookieContainer = 1
32
+ }
33
+
34
+ #endregion
35
+
27
36
internal partial class CurlHandler : HttpMessageHandler
28
37
{
29
38
#region Constants
@@ -40,7 +49,7 @@ internal partial class CurlHandler : HttpMessageHandler
40
49
#endregion
41
50
42
51
#region Fields
43
-
52
+
44
53
private volatile bool _anyOperationStarted ;
45
54
private volatile bool _disposed ;
46
55
private bool _automaticRedirection = true ;
@@ -50,8 +59,10 @@ internal partial class CurlHandler : HttpMessageHandler
50
59
private DecompressionMethods _automaticDecompression = DecompressionMethods . GZip | DecompressionMethods . Deflate ;
51
60
private SafeCurlMultiHandle _multiHandle ;
52
61
private GCHandle _multiHandlePtr = new GCHandle ( ) ;
62
+ private CookieContainer _cookieContainer = null ;
63
+ private CookieUsePolicy _cookieUsePolicy = CookieUsePolicy . IgnoreCookies ;
53
64
54
- #endregion
65
+ #endregion
55
66
56
67
static CurlHandler ( )
57
68
{
@@ -178,6 +189,40 @@ internal DecompressionMethods AutomaticDecompression
178
189
}
179
190
}
180
191
192
+ internal CookieUsePolicy CookieUsePolicy
193
+ {
194
+ get
195
+ {
196
+ return _cookieUsePolicy ;
197
+ }
198
+
199
+ set
200
+ {
201
+ if ( value != CookieUsePolicy . IgnoreCookies
202
+ && value != CookieUsePolicy . UseSpecifiedCookieContainer )
203
+ {
204
+ throw new ArgumentOutOfRangeException ( "value" ) ;
205
+ }
206
+
207
+ CheckDisposedOrStarted ( ) ;
208
+ _cookieUsePolicy = value ;
209
+ }
210
+ }
211
+
212
+ internal CookieContainer CookieContainer
213
+ {
214
+ get
215
+ {
216
+ return _cookieContainer ;
217
+ }
218
+
219
+ set
220
+ {
221
+ CheckDisposedOrStarted ( ) ;
222
+ _cookieContainer = value ;
223
+ }
224
+ }
225
+
181
226
#endregion
182
227
183
228
protected override void Dispose ( bool disposing )
@@ -370,6 +415,8 @@ private SafeCurlHandle CreateRequestHandle(RequestCompletionSource state, GCHand
370
415
371
416
SetProxyOptions ( requestHandle , state . RequestMessage . RequestUri ) ;
372
417
418
+ SetCookieOption ( requestHandle , state . RequestMessage . RequestUri ) ;
419
+
373
420
state . RequestHeaderHandle = SetRequestHeaders ( requestHandle , state . RequestMessage ) ;
374
421
375
422
// TODO: Handle other options
@@ -441,6 +488,25 @@ private void SetProxyOptions(SafeCurlHandle requestHandle, Uri requestUri)
441
488
}
442
489
}
443
490
491
+ private void SetCookieOption ( SafeCurlHandle requestHandle , Uri requestUri )
492
+ {
493
+ if ( _cookieUsePolicy != CookieUsePolicy . UseSpecifiedCookieContainer )
494
+ {
495
+ return ;
496
+ }
497
+ else if ( _cookieContainer == null )
498
+ {
499
+ throw new InvalidOperationException ( SR . net_http_invalid_cookiecontainer ) ;
500
+ }
501
+
502
+ string cookieValues = _cookieContainer . GetCookieHeader ( requestUri ) ;
503
+
504
+ if ( cookieValues != null )
505
+ {
506
+ SetCurlOption ( requestHandle , CURLoption . CURLOPT_COOKIE , cookieValues ) ;
507
+ }
508
+ }
509
+
444
510
private NetworkCredential GetCredentials ( ICredentials proxyCredentials , Uri requestUri )
445
511
{
446
512
if ( proxyCredentials == null )
0 commit comments