-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCurlHttpWebRequest.cs
More file actions
257 lines (167 loc) · 9.13 KB
/
CurlHttpWebRequest.cs
File metadata and controls
257 lines (167 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Gsemac.IO;
using Gsemac.IO.Extensions;
using Gsemac.Net.Curl.Native;
using Gsemac.Net.Extensions;
using Gsemac.Net.Http;
using Gsemac.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using DecompressionMethods = Gsemac.Polyfills.System.Net.DecompressionMethods;
namespace Gsemac.Net.Curl {
public class CurlHttpWebRequest :
HttpWebRequestBase {
// Public members
public CurlHttpWebRequest(Uri requestUri, ICurlWebRequestOptions options = null) :
base(requestUri) {
this.options = options ?? CurlWebRequestOptions.Default;
}
public CurlHttpWebRequest(string requestUri, ICurlWebRequestOptions options = null) :
this(new Uri(requestUri), options) {
}
// Methods overidden from WebRequest
public override WebResponse GetResponse() {
Stream responseStream = new ProducerConsumerStream(ResponseStreamBufferSize) {
Blocking = true,
ReadTimeout = Timeout,
WriteTimeout = Timeout,
};
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;
Task curlTask = Task.Factory.StartNew(() => {
try {
GlobalInit();
using (CurlEasyHandle easyHandle = LibCurl.EasyInit())
using (SList headers = new SList())
using (MemoryStream requestStream = new MemoryStream(GetRequestStream(validateMethod: false).ToArray())) {
LibCurl.EasySetOpt(easyHandle, CurlOption.Url, base.RequestUri.AbsoluteUri);
LibCurl.EasySetOpt(easyHandle, CurlOption.FollowLocation, AllowAutoRedirect ? 1 : 0);
LibCurl.EasySetOpt(easyHandle, CurlOption.MaxRedirs, MaximumAutomaticRedirections);
LibCurl.EasySetOpt(easyHandle, CurlOption.Timeout, base.Timeout);
if ((DecompressionMethods)AutomaticDecompression != DecompressionMethods.None)
LibCurl.EasySetOpt(easyHandle, CurlOption.AcceptEncoding, GetAcceptEncoding());
if (File.Exists(options.CABundlePath))
LibCurl.EasySetOpt(easyHandle, CurlOption.CAInfo, options.CABundlePath);
SetCertificateValidationEnabled(easyHandle);
SetCookies(easyHandle);
SetCredentials(easyHandle);
SetHeaders(easyHandle, headers);
SetHttpVersion(easyHandle);
SetKeepAlive(easyHandle);
SetMethod(easyHandle);
SetProxy(easyHandle);
// Execute the request.
using (ICurlDataCopier dataCopier = new CurlDataCopier(requestStream, responseStream, cancellationToken)) {
LibCurl.EasySetOpt(easyHandle, CurlOption.HeaderFunction, dataCopier.Header);
LibCurl.EasySetOpt(easyHandle, CurlOption.ReadFunction, dataCopier.Read);
LibCurl.EasySetOpt(easyHandle, CurlOption.WriteFunction, dataCopier.Write);
LibCurl.EasySetOpt(easyHandle, CurlOption.ProgessFunction, dataCopier.Progress);
CurlCode resultCode = LibCurl.EasyPerform(easyHandle);
if (resultCode != CurlCode.OK)
throw new CurlException(resultCode);
}
}
}
finally {
// Close the stream to indicate that we're done writing to it, unblocking readers.
responseStream.Close();
GlobalCleanup();
}
}, cancellationToken);
HaveResponse = true;
return new CurlHttpWebResponse(this, responseStream, curlTask, cancellationTokenSource);
}
// Private members
private const int ResponseStreamBufferSize = 8192;
private readonly ICurlWebRequestOptions options;
private void GlobalInit() {
CurlUtilities.InitializeCurl();
}
private void GlobalCleanup() {
//CurlUtilities.DeinitializeCurl();
}
private string GetAcceptEncoding() {
List<string> decompressionMethodStrs = new List<string>();
DecompressionMethods decompressionMethods = (DecompressionMethods)AutomaticDecompression;
if (decompressionMethods.HasFlag(DecompressionMethods.GZip))
decompressionMethodStrs.Add("gzip");
if (decompressionMethods.HasFlag(DecompressionMethods.Deflate))
decompressionMethodStrs.Add("deflate");
if (decompressionMethods.HasFlag(DecompressionMethods.Brotli))
decompressionMethodStrs.Add("br");
return string.Join(", ", decompressionMethodStrs);
}
private CurlHttpVersion GetHttpVersion() {
if (ProtocolVersion is object) {
if (ProtocolVersion.Major == 1 && ProtocolVersion.Minor == 0)
return CurlHttpVersion.Http10;
if (ProtocolVersion.Major == 1 && ProtocolVersion.Minor == 1)
return CurlHttpVersion.Http11;
if (ProtocolVersion.Major == 2 && ProtocolVersion.Minor == 0)
return CurlHttpVersion.Http2;
if (ProtocolVersion.Major == 3 && ProtocolVersion.Minor == 0)
return CurlHttpVersion.Http3;
}
return CurlHttpVersion.None;
}
private void SetCertificateValidationEnabled(CurlEasyHandle easyHandle) {
if (!ServicePointManagerUtilities.IsCertificateValidationEnabled()) {
LibCurl.EasySetOpt(easyHandle, CurlOption.SslVerifyHost, 0);
LibCurl.EasySetOpt(easyHandle, CurlOption.SslVerifyPeer, 0);
}
}
private void SetCookies(CurlEasyHandle easyHandle) {
string cookieHeader = CookieContainer?.GetCookieHeader(RequestUri);
if (!string.IsNullOrEmpty(cookieHeader))
LibCurl.EasySetOpt(easyHandle, CurlOption.Cookie, cookieHeader);
}
private void SetCredentials(CurlEasyHandle easyHandle) {
if (Credentials is object) {
string credentialString = Credentials.ToCredentialsString(RequestUri);
if (!string.IsNullOrEmpty(credentialString))
LibCurl.EasySetOpt(easyHandle, CurlOption.UserPwd, credentialString);
}
}
private void SetHeaders(CurlEasyHandle easyHandle, SList headersList) {
IEnumerable<IHttpHeader> headers = Headers.GetAll();
foreach (IHttpHeader header in headers)
headersList.Append($"{header.Name}: {header.Value}");
if (!Headers.TryGet(HttpRequestHeader.AcceptEncoding, out _) && AutomaticDecompression != System.Net.DecompressionMethods.None) {
// Adding the Accept-Encoding header manually ensures that it's below the Accept header.
// See See https://sansec.io/research/http-header-order-is-important
string acceptEncoding = GetAcceptEncoding();
if (!string.IsNullOrWhiteSpace(acceptEncoding))
headersList.Append($"Accept-Encoding: {acceptEncoding}");
}
// Remove default headers unless they were added manually by the user.
if (!Headers.TryGet(HttpRequestHeader.ContentType, out _))
headersList.Append($"Content-Type:");
LibCurl.EasySetOpt(easyHandle, CurlOption.HttpHeader, headersList.Handle);
}
private void SetHttpVersion(CurlEasyHandle easyHandle) {
LibCurl.EasySetOpt(easyHandle, CurlOption.HttpVersion, (int)GetHttpVersion());
}
private void SetKeepAlive(CurlEasyHandle easyHandle) {
LibCurl.EasySetOpt(easyHandle, CurlOption.TcpKeepAlive, KeepAlive ? 1 : 0);
}
private void SetMethod(CurlEasyHandle easyHandle) {
if (string.IsNullOrWhiteSpace(Method))
Method = "GET";
LibCurl.EasySetOpt(easyHandle, CurlOption.CustomRequest, Method);
if (Method.Equals("post", StringComparison.OrdinalIgnoreCase))
LibCurl.EasySetOpt(easyHandle, CurlOption.Post, 1);
else if (Method.Equals("put", StringComparison.OrdinalIgnoreCase))
LibCurl.EasySetOpt(easyHandle, CurlOption.Put, 1);
else if (Method.Equals("head", StringComparison.OrdinalIgnoreCase))
LibCurl.EasySetOpt(easyHandle, CurlOption.NoBody, 1);
}
private void SetProxy(CurlEasyHandle easyHandle) {
if (Proxy is object && !Proxy.IsBypassed(RequestUri))
LibCurl.EasySetOpt(easyHandle, CurlOption.Proxy, Proxy.ToProxyString(RequestUri));
}
}
}