Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f79c1cd

Browse files
committed
Enabling cookie container in HTTP xplat
modified: ../../Common/src/Interop/Unix/libcurl/Interop.libcurl_types.cs modified: System/Net/Http/Unix/CurlHandler.cs modified: System/Net/Http/Unix/HttpClientHandler.Unix.cs
1 parent 304b884 commit f79c1cd

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

src/Common/src/Interop/Unix/libcurl/Interop.libcurl_types.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal static partial class CURLoption
3838
internal const int CURLOPT_PROXY = CurlOptionObjectPointBase + 4;
3939
internal const int CURLOPT_PROXYUSERPWD = CurlOptionObjectPointBase + 6;
4040
internal const int CURLOPT_READDATA = CurlOptionObjectPointBase + 9;
41+
internal const int CURLOPT_COOKIE = CurlOptionObjectPointBase + 22;
4142
internal const int CURLOPT_HTTPHEADER = CurlOptionObjectPointBase + 23;
4243
internal const int CURLOPT_HEADERDATA = CurlOptionObjectPointBase + 29;
4344
internal const int CURLOPT_ACCEPTENCODING = CurlOptionObjectPointBase + 102;

src/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.cs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424

2525
namespace System.Net.Http
2626
{
27+
#region Enums
28+
internal enum CookieUsePolicy
29+
{
30+
IgnoreCookies = 0,
31+
UseSpecifiedCookieContainer = 1
32+
}
33+
34+
#endregion
35+
2736
internal partial class CurlHandler : HttpMessageHandler
2837
{
2938
#region Constants
@@ -40,7 +49,7 @@ internal partial class CurlHandler : HttpMessageHandler
4049
#endregion
4150

4251
#region Fields
43-
52+
4453
private volatile bool _anyOperationStarted;
4554
private volatile bool _disposed;
4655
private bool _automaticRedirection = true;
@@ -50,8 +59,10 @@ internal partial class CurlHandler : HttpMessageHandler
5059
private DecompressionMethods _automaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
5160
private SafeCurlMultiHandle _multiHandle;
5261
private GCHandle _multiHandlePtr = new GCHandle();
62+
private CookieContainer _cookieContainer = null;
63+
private CookieUsePolicy _cookieUsePolicy = CookieUsePolicy.IgnoreCookies;
5364

54-
#endregion
65+
#endregion
5566

5667
static CurlHandler()
5768
{
@@ -178,6 +189,40 @@ internal DecompressionMethods AutomaticDecompression
178189
}
179190
}
180191

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+
181226
#endregion
182227

183228
protected override void Dispose(bool disposing)
@@ -370,6 +415,8 @@ private SafeCurlHandle CreateRequestHandle(RequestCompletionSource state, GCHand
370415

371416
SetProxyOptions(requestHandle, state.RequestMessage.RequestUri);
372417

418+
SetCookieOption(requestHandle, state.RequestMessage.RequestUri);
419+
373420
state.RequestHeaderHandle = SetRequestHeaders(requestHandle, state.RequestMessage);
374421

375422
// TODO: Handle other options
@@ -441,6 +488,25 @@ private void SetProxyOptions(SafeCurlHandle requestHandle, Uri requestUri)
441488
}
442489
}
443490

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+
444510
private NetworkCredential GetCredentials(ICredentials proxyCredentials, Uri requestUri)
445511
{
446512
if (proxyCredentials == null)

src/System.Net.Http/src/System/Net/Http/Unix/HttpClientHandler.Unix.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ public virtual bool SupportsRedirectConfiguration
2727

2828
public bool UseCookies
2929
{
30-
get { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
31-
set { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
30+
get { return (_curlHandler.CookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer); }
31+
set { _curlHandler.CookieUsePolicy = value ? CookieUsePolicy.UseSpecifiedCookieContainer : CookieUsePolicy.IgnoreCookies; }
3232
}
3333

3434
public CookieContainer CookieContainer
3535
{
36-
get { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
37-
set { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
36+
get
37+
{
38+
return _curlHandler.CookieContainer;
39+
}
40+
41+
set
42+
{
43+
_curlHandler.CookieContainer = value;
44+
}
3845
}
3946

4047
public ClientCertificateOption ClientCertificateOptions

0 commit comments

Comments
 (0)