-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Http.cs
371 lines (293 loc) · 13.3 KB
/
Http.cs
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using RestSharp.Extensions;
#pragma warning disable 618
namespace RestSharp
{
/// <summary>
/// HttpWebRequest wrapper
/// </summary>
public partial class Http : IHttp
{
const string LineBreak = "\r\n";
public string FormBoundary { get; } = "---------" + Guid.NewGuid().ToString().ToUpperInvariant();
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
static readonly Regex AddRangeRegex = new Regex("(\\w+)=(\\d+)-(\\d+)$");
readonly IDictionary<string, Action<HttpWebRequest, string>> _restrictedHeaderActions;
public Http()
{
_restrictedHeaderActions =
new Dictionary<string, Action<HttpWebRequest, string>>(StringComparer.OrdinalIgnoreCase);
AddSharedHeaderActions();
AddSyncHeaderActions();
void AddSyncHeaderActions()
{
_restrictedHeaderActions.Add("Connection", (r, v) => { r.KeepAlive = v.ToLowerInvariant().Contains("keep-alive"); });
_restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v));
_restrictedHeaderActions.Add("Expect", (r, v) => r.Expect = v);
_restrictedHeaderActions.Add(
"If-Modified-Since",
(r, v) => r.IfModifiedSince = Convert.ToDateTime(v, CultureInfo.InvariantCulture)
);
_restrictedHeaderActions.Add("Referer", (r, v) => r.Referer = v);
_restrictedHeaderActions.Add(
"Transfer-Encoding",
(r, v) =>
{
r.TransferEncoding = v;
r.SendChunked = true;
}
);
_restrictedHeaderActions.Add("User-Agent", (r, v) => r.UserAgent = v);
}
void AddSharedHeaderActions()
{
_restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v);
_restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v);
_restrictedHeaderActions.Add(
"Date",
(r, v) =>
{
if (DateTime.TryParse(v, out var parsed)) r.Date = parsed;
}
);
_restrictedHeaderActions.Add("Host", (r, v) => r.Host = v);
_restrictedHeaderActions.Add("Range", AddRange);
static void AddRange(HttpWebRequest r, string range)
{
var m = AddRangeRegex.Match(range);
if (!m.Success) return;
var rangeSpecifier = m.Groups[1].Value;
var from = Convert.ToInt64(m.Groups[2].Value);
var to = Convert.ToInt64(m.Groups[3].Value);
r.AddRange(rangeSpecifier, from, to);
}
}
}
/// <summary>
/// True if this HTTP request has any HTTP parameters
/// </summary>
protected bool HasParameters => Parameters.Any();
/// <summary>
/// True if this HTTP request has any HTTP cookies
/// </summary>
protected bool HasCookies => Cookies.Any();
/// <summary>
/// True if a request body has been specified
/// </summary>
protected bool HasBody => RequestBodyBytes != null || !string.IsNullOrEmpty(RequestBody);
/// <summary>
/// True if files have been set to be uploaded
/// </summary>
protected bool HasFiles => Files.Any();
internal Func<string, string> Encode { get; set; } = s => s.UrlEncode();
/// <inheritdoc />
public bool AutomaticDecompression { get; set; }
/// <summary>
/// Always send a multipart/form-data request - even when no Files are present.
/// </summary>
public bool AlwaysMultipartFormData { get; set; }
/// <inheritdoc />
public string UserAgent { get; set; }
/// <inheritdoc />
public int Timeout { get; set; }
/// <inheritdoc />
public int ReadWriteTimeout { get; set; }
/// <inheritdoc />
public ICredentials? Credentials { get; set; }
/// <inheritdoc />
public CookieContainer? CookieContainer { get; set; }
/// <inheritdoc />
public Action<Stream, IHttpResponse> AdvancedResponseWriter { get; set; }
/// <inheritdoc />
public Action<Stream> ResponseWriter { get; set; }
/// <inheritdoc />
public IList<HttpFile> Files { get; internal set; }
/// <inheritdoc />
public bool FollowRedirects { get; set; }
/// <inheritdoc />
public bool Pipelined { get; set; }
/// <inheritdoc />
public X509CertificateCollection? ClientCertificates { get; set; }
/// <inheritdoc />
public int? MaxRedirects { get; set; }
/// <inheritdoc />
public bool UseDefaultCredentials { get; set; }
/// <inheritdoc />
public string ConnectionGroupName { get; set; }
/// <inheritdoc />
public Encoding Encoding { get; set; } = Encoding.UTF8;
/// <inheritdoc />
public IList<HttpHeader> Headers { get; internal set; }
/// <inheritdoc />
public IList<HttpParameter> Parameters { get; internal set; }
/// <inheritdoc />
public IList<HttpCookie> Cookies { get; internal set; }
/// <inheritdoc />
public string RequestBody { get; set; }
/// <inheritdoc />
public string RequestContentType { get; set; }
/// <inheritdoc />
public byte[] RequestBodyBytes { get; set; }
/// <inheritdoc />
public Uri Url { get; set; }
/// <inheritdoc />
public string? Host { get; set; }
/// <inheritdoc />
public IList<DecompressionMethods> AllowedDecompressionMethods { get; set; }
/// <inheritdoc />
public bool PreAuthenticate { get; set; }
/// <inheritdoc />
public bool UnsafeAuthenticatedConnectionSharing { get; set; }
/// <inheritdoc />
public IWebProxy? Proxy { get; set; }
/// <inheritdoc />
public RequestCachePolicy? CachePolicy { get; set; }
/// <inheritdoc />
/// <summary>
/// Callback function for handling the validation of remote certificates.
/// </summary>
public RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get; set; }
/// <inheritdoc />
public Action<HttpWebRequest>? WebRequestConfigurator { get; set; }
public bool ThrowOnAnyError { get; set; }
[Obsolete]
public static IHttp Create() => new Http();
[Obsolete("Overriding this method won't be possible in future version")]
protected virtual HttpWebRequest? CreateWebRequest(Uri url) => null;
static HttpWebRequest CreateRequest(Uri uri) => (HttpWebRequest) WebRequest.Create(uri);
string GetMultipartFileHeader(HttpFile file)
=> $"--{FormBoundary}{LineBreak}Content-Disposition: form-data; name=\"{file.Name}\";" +
$" filename=\"{file.FileName}\"{LineBreak}" +
$"Content-Type: {file.ContentType ?? "application/octet-stream"}{LineBreak}{LineBreak}";
string GetMultipartFormData(HttpParameter param)
{
var format = param.Name == RequestContentType
? "--{0}{3}Content-Type: {4}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}"
: "--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}";
return string.Format(format, FormBoundary, param.Name, param.Value, LineBreak, param.ContentType!);
}
string GetMultipartFooter() => $"--{FormBoundary}--{LineBreak}";
void PreparePostBody(WebRequest webRequest)
{
var needsContentType = string.IsNullOrEmpty(webRequest.ContentType);
if (HasFiles || AlwaysMultipartFormData)
{
if (needsContentType)
{
webRequest.ContentType = GetMultipartFormContentType();
}
else if (!webRequest.ContentType.Contains("boundary"))
{
webRequest.ContentType = webRequest.ContentType + "; boundary=" + FormBoundary;
}
}
else if (HasBody)
{
if (needsContentType) webRequest.ContentType = RequestContentType;
}
else if (HasParameters)
{
if (needsContentType) webRequest.ContentType = "application/x-www-form-urlencoded";
RequestBody = EncodeParameters();
}
string EncodeParameters() => string.Join("&", Parameters.Select(p => $"{Encode(p.Name)}={Encode(p.Value)}"));
string GetMultipartFormContentType() => $"multipart/form-data; boundary={FormBoundary}";
}
void WriteMultipartFormData(Stream requestStream)
{
foreach (var param in Parameters) requestStream.WriteString(GetMultipartFormData(param), Encoding);
foreach (var file in Files)
{
// Add just the first part of this param, since we will write the file data directly to the Stream
requestStream.WriteString(GetMultipartFileHeader(file), Encoding);
// Write the file data directly to the Stream, rather than serializing it to a string.
file.Writer(requestStream);
requestStream.WriteString(LineBreak, Encoding);
}
requestStream.WriteString(GetMultipartFooter(), Encoding);
}
HttpResponse ExtractResponseData(HttpWebResponse webResponse)
{
var response = new HttpResponse
{
ContentEncoding = webResponse.ContentEncoding,
Server = webResponse.Server,
ProtocolVersion = webResponse.ProtocolVersion,
ContentType = webResponse.ContentType,
ContentLength = webResponse.ContentLength,
StatusCode = webResponse.StatusCode,
StatusDescription = webResponse.StatusDescription,
ResponseUri = webResponse.ResponseUri,
ResponseStatus = ResponseStatus.Completed
};
if (webResponse.Cookies != null)
foreach (Cookie cookie in webResponse.Cookies)
{
response.Cookies.Add(
new HttpCookie
{
Comment = cookie.Comment,
CommentUri = cookie.CommentUri,
Discard = cookie.Discard,
Domain = cookie.Domain,
Expired = cookie.Expired,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Port = cookie.Port,
Secure = cookie.Secure,
TimeStamp = cookie.TimeStamp,
Value = cookie.Value,
Version = cookie.Version
}
);
}
response.Headers = webResponse.Headers.AllKeys
.Select(x => new HttpHeader(x, webResponse.Headers[x]))
.ToList();
var webResponseStream = webResponse.GetResponseStream();
if (webResponseStream != null)
ProcessResponseStream();
webResponse.Close();
return response;
void ProcessResponseStream()
{
if (AdvancedResponseWriter != null)
{
AdvancedResponseWriter(webResponseStream, response);
}
else
{
if (ResponseWriter == null)
response.RawBytes = webResponseStream.ReadAsBytes();
else
ResponseWriter(webResponseStream);
}
}
}
}
}