5
5
using System . Diagnostics ;
6
6
using System . Net . Http ;
7
7
using System . Runtime . InteropServices ;
8
+ using System . Text ;
8
9
using System . Threading ;
9
10
using System . Threading . Tasks ;
10
11
@@ -21,6 +22,8 @@ internal class CurlHandler : HttpMessageHandler
21
22
#region Constants
22
23
23
24
private const string UriSchemeHttps = "https" ;
25
+ private const string EncodingNameGzip = "gzip" ;
26
+ private const string EncodingNameDeflate = "deflate" ;
24
27
private readonly static string [ ] AuthenticationSchemes = { "Negotiate" , "Digest" , "Basic" } ; // the order in which libcurl goes over authentication schemes
25
28
#endregion
26
29
@@ -32,6 +35,7 @@ internal class CurlHandler : HttpMessageHandler
32
35
private IWebProxy _proxy = null ;
33
36
private ICredentials _serverCredentials = null ;
34
37
private ProxyUsePolicy _proxyPolicy = ProxyUsePolicy . UseDefaultProxy ;
38
+ private DecompressionMethods _automaticDecompression = DecompressionMethods . GZip | DecompressionMethods . Deflate ;
35
39
36
40
#endregion
37
41
@@ -123,6 +127,27 @@ internal ClientCertificateOption ClientCertificateOptions
123
127
}
124
128
}
125
129
130
+ internal bool SupportsAutomaticDecompression
131
+ {
132
+ get
133
+ {
134
+ return true ;
135
+ }
136
+ }
137
+
138
+ internal DecompressionMethods AutomaticDecompression
139
+ {
140
+ get
141
+ {
142
+ return _automaticDecompression ;
143
+ }
144
+ set
145
+ {
146
+ CheckDisposedOrStarted ( ) ;
147
+ _automaticDecompression = value ;
148
+ }
149
+ }
150
+
126
151
#endregion
127
152
128
153
protected override void Dispose ( bool disposing )
@@ -245,11 +270,26 @@ private SafeCurlHandle CreateRequestHandle(RequestCompletionSource state)
245
270
}
246
271
247
272
SetProxyOptions ( state , requestHandle ) ;
273
+ SetRequestHandleDecompressionOptions ( requestHandle ) ;
248
274
// TODO: Handle headers and other options
249
275
250
276
return requestHandle ;
251
277
}
252
278
279
+ private void SetRequestHandleDecompressionOptions ( SafeCurlHandle requestHandle )
280
+ {
281
+ bool gzip = ( AutomaticDecompression & DecompressionMethods . GZip ) != 0 ;
282
+ bool deflate = ( AutomaticDecompression & DecompressionMethods . Deflate ) != 0 ;
283
+ if ( gzip || deflate )
284
+ {
285
+ string encoding = ( gzip && deflate ) ?
286
+ EncodingNameGzip + "," + EncodingNameDeflate :
287
+ gzip ? EncodingNameGzip :
288
+ EncodingNameDeflate ;
289
+ Interop . libcurl . curl_easy_setopt ( requestHandle , CURLoption . CURLOPT_ACCEPTENCODING , encoding ) ;
290
+ }
291
+ }
292
+
253
293
private static void SetProxyOptions ( RequestCompletionSource state , SafeCurlHandle requestHandle )
254
294
{
255
295
var requestUri = state . RequestMessage . RequestUri ;
0 commit comments