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

Commit f6e99e3

Browse files
committed
Merge pull request #2758 from rajansingh10/myfeature
Enabling cookie container in HTTP xplat
2 parents 2ab98bf + b2de381 commit f6e99e3

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
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: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using size_t = System.IntPtr;
2424

2525
namespace System.Net.Http
26-
{
26+
{
2727
internal partial class CurlHandler : HttpMessageHandler
2828
{
2929
#region Constants
@@ -40,7 +40,7 @@ internal partial class CurlHandler : HttpMessageHandler
4040
#endregion
4141

4242
#region Fields
43-
43+
4444
private volatile bool _anyOperationStarted;
4545
private volatile bool _disposed;
4646
private bool _automaticRedirection = true;
@@ -50,8 +50,10 @@ internal partial class CurlHandler : HttpMessageHandler
5050
private DecompressionMethods _automaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
5151
private SafeCurlMultiHandle _multiHandle;
5252
private GCHandle _multiHandlePtr = new GCHandle();
53+
private CookieContainer _cookieContainer = null;
54+
private bool _useCookie = false;
5355

54-
#endregion
56+
#endregion
5557

5658
static CurlHandler()
5759
{
@@ -178,6 +180,34 @@ internal DecompressionMethods AutomaticDecompression
178180
}
179181
}
180182

183+
internal bool UseCookie
184+
{
185+
get
186+
{
187+
return _useCookie;
188+
}
189+
190+
set
191+
{
192+
CheckDisposedOrStarted();
193+
_useCookie = value;
194+
}
195+
}
196+
197+
internal CookieContainer CookieContainer
198+
{
199+
get
200+
{
201+
return _cookieContainer;
202+
}
203+
204+
set
205+
{
206+
CheckDisposedOrStarted();
207+
_cookieContainer = value;
208+
}
209+
}
210+
181211
#endregion
182212

183213
protected override void Dispose(bool disposing)
@@ -370,6 +400,8 @@ private SafeCurlHandle CreateRequestHandle(RequestCompletionSource state, GCHand
370400

371401
SetProxyOptions(requestHandle, state.RequestMessage.RequestUri);
372402

403+
SetCookieOption(requestHandle, state.RequestMessage.RequestUri);
404+
373405
state.RequestHeaderHandle = SetRequestHeaders(requestHandle, state.RequestMessage);
374406

375407
// TODO: Handle other options
@@ -441,6 +473,25 @@ private void SetProxyOptions(SafeCurlHandle requestHandle, Uri requestUri)
441473
}
442474
}
443475

476+
private void SetCookieOption(SafeCurlHandle requestHandle, Uri requestUri)
477+
{
478+
if (!_useCookie)
479+
{
480+
return;
481+
}
482+
else if (_cookieContainer == null)
483+
{
484+
throw new InvalidOperationException(SR.net_http_invalid_cookiecontainer);
485+
}
486+
487+
string cookieValues = _cookieContainer.GetCookieHeader(requestUri);
488+
489+
if (cookieValues != null)
490+
{
491+
SetCurlOption(requestHandle, CURLoption.CURLOPT_COOKIE, cookieValues);
492+
}
493+
}
494+
444495
private NetworkCredential GetCredentials(ICredentials proxyCredentials, Uri requestUri)
445496
{
446497
if (proxyCredentials == null)

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,28 @@ 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
31+
{
32+
return _curlHandler.UseCookie;
33+
}
34+
35+
set
36+
{
37+
_curlHandler.UseCookie = value;
38+
}
3239
}
3340

3441
public CookieContainer CookieContainer
3542
{
36-
get { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
37-
set { throw NotImplemented.ByDesignWithMessage("HTTP stack not implemented"); }
43+
get
44+
{
45+
return _curlHandler.CookieContainer;
46+
}
47+
48+
set
49+
{
50+
_curlHandler.CookieContainer = value;
51+
}
3852
}
3953

4054
public ClientCertificateOption ClientCertificateOptions

0 commit comments

Comments
 (0)