forked from canneverbe/Ketarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebClient.cs
298 lines (258 loc) · 10.9 KB
/
WebClient.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
using CDBurnerXP;
namespace Ketarin
{
/// <summary>
/// The modified WebClient used by Ketarin for
/// downloads. Submits a valid user agent by default.
/// </summary>
internal class WebClient : System.Net.WebClient
{
private static string defaultUserAgent;
private string replacementString = string.Empty;
#region Properties
/// <summary>
/// If the WebClient has been redirected after a request,
/// this specifies the new URL.
/// </summary>
public Uri ResponseUri { get; private set; }
/// <summary>
/// Gets the plain POST data which is being ursed for a request.
/// </summary>
public string PostData { get; private set; } = string.Empty;
/// <summary>
/// Default user agent for all requests.
/// </summary>
public static string DefaultUserAgent
{
get { return defaultUserAgent ?? (defaultUserAgent = Settings.GetValue("DefaultUserAgent", "Mozilla/4.0 (compatible; Ketarin; +https://ketarin.org/)") as string); }
set { defaultUserAgent = value; }
}
#endregion
public WebClient()
: this(null)
{
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
}
public WebClient(string userAgent)
{
this.Headers.Add("User-Agent", string.IsNullOrEmpty(userAgent) ? DefaultUserAgent : userAgent);
// MS Bugfix - https://connect.microsoft.com/VisualStudio/feedback/details/386695/system-uri-incorrectly-strips-trailing-dots?wa=wsignin1.0#
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
}
protected override WebRequest GetWebRequest(Uri address)
{
address = FixNoProtocolUri(address);
WebRequest request = base.GetWebRequest(address);
HttpWebRequest httpReq = request as HttpWebRequest;
if (httpReq != null)
{
// DownloadString will not decompress automatically
httpReq.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate);
httpReq.Accept = "*/*";
}
// Make sure that the user defined timeout is used for all web requests!
request.Timeout = Convert.ToInt32(Settings.GetValue("ConnectionTimeout", 10)) * 1000; // 10 seconds by default
Updater.AddRequestToCancel(request);
// Need to append POST data?
if (!string.IsNullOrEmpty(this.PostData))
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream newStream = request.GetRequestStream();
byte[] bytes = Encoding.ASCII.GetBytes(this.PostData);
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
}
return request;
}
public new string DownloadString(string address)
{
try
{
return this.DownloadString(new Uri(address));
}
catch (UriFormatException)
{
throw new UriFormatException("The format of the URI \"" + address + "\" cannot be determined.");
}
}
public new string DownloadString(Uri address)
{
this.replacementString = string.Empty;
try
{
return base.DownloadString(address);
}
catch (WebException ex)
{
// If only SSL3 is supported, use this temporarily.
if (ex.Status == WebExceptionStatus.SecureChannelFailure && ServicePointManager.SecurityProtocol != SecurityProtocolType.Ssl3)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
try
{
return base.DownloadString(address);
}
finally
{
ServicePointManager.SecurityProtocol = Updater.DefaultHttpProtocols;
}
}
if (!string.IsNullOrEmpty(this.replacementString))
{
return this.replacementString;
}
throw;
}
}
protected override WebResponse GetWebResponse(WebRequest request)
{
FtpWebRequest ftpRequest = request as FtpWebRequest;
if (ftpRequest != null)
{
if (request.RequestUri.LocalPath.EndsWith("/"))
{
ftpRequest.Method = "LIST";
}
}
WebResponse response = base.GetWebResponse(request);
HttpWebResponse httpResponse = response as HttpWebResponse;
if (httpResponse != null)
{
this.ResponseUri = httpResponse.ResponseUri;
}
// If binary contents are sent, output information about the download
if (httpResponse != null && response.ContentType == "application/octet-stream" && response.ContentLength > 100000)
{
this.replacementString = "ResponseUri: " + httpResponse.ResponseUri + "\r\n";
this.replacementString += httpResponse.Headers.ToString();
return null;
}
return response;
}
/// <summary>
/// Works around the HTTP to FTP redirection limitation.
/// </summary>
public static WebResponse GetResponse(WebRequest request)
{
try
{
return request.GetResponse();
}
catch (WebException ex)
{
// If only SSL3 is supported, use this temporarily.
if (ex.Status == WebExceptionStatus.SecureChannelFailure && ServicePointManager.SecurityProtocol != SecurityProtocolType.Ssl3)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
try
{
return GetResponse(request);
}
finally
{
ServicePointManager.SecurityProtocol = Updater.DefaultHttpProtocols;
}
}
if (ex.Response == null || ex.Status != WebExceptionStatus.ProtocolError)
{
throw;
}
// Create a new WebRequest, starting with the FTP URL
string nextUrl = ex.Response.Headers["location"];
if (string.IsNullOrEmpty(nextUrl))
{
throw;
}
WebRequest nextRequest = WebRequest.CreateDefault(FixNoProtocolUri(new Uri(nextUrl)));
nextRequest.Timeout = request.Timeout;
if (request.Credentials != null)
{
nextRequest.Credentials = request.Credentials;
}
nextRequest.Proxy = request.Proxy;
return nextRequest.GetResponse();
}
}
/// <summary>
/// Sets the POST data which is sent
/// along with the request. Replaces variables.
/// </summary>
/// <param name="variable">The variable, which sends the request</param>
public void SetPostData(UrlVariable variable)
{
string[][] pairs = GetKeyValuePairs(variable.PostData);
if (variable.Parent != null)
{
foreach (string[] keyValue in pairs)
{
keyValue[0] = variable.Parent.ReplaceAllInString(keyValue[0]);
keyValue[1] = variable.Parent.ReplaceAllInString(keyValue[1]);
}
}
StringBuilder sb = new StringBuilder();
foreach (string[] keyValue in pairs)
{
sb.Append(HttpUtility.UrlEncode(keyValue[0]) + "=" + HttpUtility.UrlEncode(keyValue[1]) + "&");
}
this.PostData = sb.ToString().TrimEnd('&');
}
/// <summary>
/// Determines the key-value pairs from a post data string.
/// </summary>
internal static string[][] GetKeyValuePairs(string postData)
{
List<string[]> results = new List<string[]>();
// No data, no efforts
if (postData == null) return results.ToArray();
string[] pairs = postData.Split('&');
foreach (string pair in pairs)
{
string[] keyValue = pair.Split('=');
if (keyValue.Length == 2)
{
keyValue[0] = HttpUtility.UrlDecode(keyValue[0]);
keyValue[1] = HttpUtility.UrlDecode(keyValue[1]);
results.Add(keyValue);
}
}
return results.ToArray();
}
/// <summary>
/// Not using a protocol will default to file:// which is looking for a network resource.
/// This won't work properly in Ketarin's context, but looks like missing protocols
/// have become hip http://www.paulirish.com/2010/the-protocol-relative-url/ lately.
/// </summary>
public static Uri FixNoProtocolUri(Uri urlToRequest)
{
if (urlToRequest.OriginalString.StartsWith("//") && urlToRequest.Scheme == "file")
{
return new Uri("http:" + urlToRequest.OriginalString);
}
return urlToRequest;
}
}
}