-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHttpUtil.cs
More file actions
284 lines (260 loc) · 10.7 KB
/
HttpUtil.cs
File metadata and controls
284 lines (260 loc) · 10.7 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
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
//-----------------------------------------------------------------------
// <copyright file="HttpUtil.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2012-2015 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
/// <summary>
/// Http utily class
/// </summary>
public class HttpUtil
{
/// <summary>
/// Url of http request, immutable.
/// </summary>
private readonly string url;
/// <summary>
/// Cache settings of http request, immutable.
/// </summary>
private readonly System.Net.Cache.RequestCacheLevel cachesettings;
private readonly string postdata;
/// <summary>
/// HTTP backgroundworker thread
/// </summary>
private BackgroundWorker httpthread;
/// <summary>
/// Initializes a new instance of the HttpUtil class.
/// </summary>
/// <param name="url">The url of the request to make</param>
/// <param name="cachesettings">The cache settings (important note: this is always NoCacheNoStore under Mono)</param>
/// <param name="postdata">POST data for a POST Http request.</param>
/// <returns></returns>
public HttpUtil(string url, System.Net.Cache.RequestCacheLevel cachesettings, string postdata)
{
String protocolhandler = "";
if (!url.Contains("://"))
{
protocolhandler = "https:";
if (!Settings.ProgramHttpsLinks)
{
protocolhandler = "http:";
}
}
url = protocolhandler + url;
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
this.url = url;
}
else
{
Log.Write(LogType.exception, "Invalid url.");
}
if (!String.IsNullOrEmpty(postdata))
{
this.postdata = postdata;
}
else
{
this.postdata = null;
}
this.cachesettings = cachesettings;
this.httpthread = new BackgroundWorker();
this.httpthread.DoWork += new DoWorkEventHandler(this.httpthread_DoWork);
}
/// <summary>
/// Initializes a new instance of the HttpUtil class. Using no http POST.
/// </summary>
/// <param name="url"></param>
/// <param name="cachesettings"></param>
public HttpUtil(string url, System.Net.Cache.RequestCacheLevel cachesettings) : this(url, cachesettings, null)
{
}
/// <summary>
/// Create a new Http request only if DownloadCompleet event is assigned.
/// </summary>
/// <param name="workcompleethandler">The RunWorkerCompletedEventHandler</param>
/// <returns>True if http background worker succesfully started</returns>
public bool Start(RunWorkerCompletedEventHandler workcompleethandler)
{
if (!this.IsNetworkConnected())
{
return false;
}
else
{
this.httpthread.RunWorkerCompleted += workcompleethandler;
this.httpthread.RunWorkerAsync();
return true;
}
}
/// <summary>
/// Instantly stop the http worker, if still running.
/// </summary>
public void Stop()
{
if (this.httpthread != null)
{
this.httpthread.CancelAsync();
}
}
// get network status
[System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
private static extern bool InternetGetConnectedState(out int description, int ReservedValue);
/// <summary>
/// Http background worker thread reading stream
/// and writing to memory in string.
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="doworkevtarg">DoWorkEvent arguments</param>
private void httpthread_DoWork(object sender, DoWorkEventArgs doworkevtarg)
{
HttpWebRequest request = this.CreateHttpWebRequest(this.url, this.cachesettings);
if (request == null)
{
return;
}
WebResponse webresponse = null;
string response = null;
try
{
webresponse = (WebResponse)request.GetResponse();
using (BufferedStream bufferedstream = new BufferedStream(webresponse.GetResponseStream()))
{
using (StreamReader streamreader = new StreamReader(bufferedstream, System.Text.Encoding.UTF8))
{
response = streamreader.ReadToEnd();
}
}
doworkevtarg.Result = response;
}
catch (WebException webexc)
{
Log.Write(LogType.exception, webexc.Message + webexc.StackTrace);
}
finally
{
if (webresponse != null)
{
webresponse.Close();
}
}
}
/// <summary>
/// Create a WebRequest object
/// </summary>
/// <param name="url">Http request url.</param>
/// <param name="cachesettings">Http cache settings</param>
/// <returns>A new httpwebrequest object.</returns>
private HttpWebRequest CreateHttpWebRequest(string url, System.Net.Cache.RequestCacheLevel cachesettings)
{
HttpWebRequest request = null;
if (String.IsNullOrEmpty(url))
{
Log.Write(LogType.exception, "Url is null or empty.");
return request;
}
System.Net.ServicePointManager.Expect100Continue = false;
System.Net.ServicePointManager.EnableDnsRoundRobin = true;
System.Net.ServicePointManager.DnsRefreshTimeout = 3 * 60 * 1000; // 180000 ms = 180 s = 3 minutes
System.Net.ServicePointManager.DefaultConnectionLimit = 8;
// .NET framework 3.5 and lower do not have TLS 1.1 and TLS 1.2 support only vulnerable TLS 1.0 and SSL 3.0.
// .NET framework 4.0 has TLS 1.1 and TLS 1.2 support but has no enum available for it.
// SSL 3.0 and TLS 1.0 are vulnerable and should be avoided.
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
System.Net.ServicePointManager.CheckCertificateRevocationList = true;
Log.Write(LogType.info, "Making request to '" + url + "'");
try
{
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
if (this.postdata == null)
{
request.Method = "GET";
}
else
{
request.Method = "POST";
}
request.ContentType = "text/xml";
request.ProtocolVersion = HttpVersion.Version11; // HTTP 1.1>= is required, required sending host: header for notefly.org
request.UserAgent = Program.AssemblyTitle + " " + Program.AssemblyVersionAsString;
request.AllowAutoRedirect = false;
request.Timeout = Settings.NetworkConnectionTimeout;
request.KeepAlive = true;
if (Settings.NetworkProxyEnabled && !string.IsNullOrEmpty(Settings.NetworkProxyAddress))
{
request.Proxy = new WebProxy(Settings.NetworkProxyAddress, Settings.NetworkProxyPort);
}
else
{
// set proxy to nothing, otherwise HttpWebRequest has issues, details: https://holyhoehle.wordpress.com/2010/01/12/webrequest-slow/
request.Proxy = null;
}
if (Settings.NetworkUseGzip)
{
request.Headers["Accept-Encoding"] = "gzip";
request.AutomaticDecompression = DecompressionMethods.GZip;
}
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(cachesettings);
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
request.PreAuthenticate = false;
if (this.postdata != null)
{
request.ContentType = "application/x-www-form-urlencoded";
byte[] dataenc = System.Text.Encoding.UTF8.GetBytes(this.postdata); // utf-8 on server.
request.ContentLength = dataenc.Length;
Stream stream = request.GetRequestStream();
stream.Write(dataenc, 0, dataenc.Length);
}
}
catch (System.Net.WebException webexc)
{
Log.Write(LogType.exception, webexc.Message + webexc.StackTrace);
}
return request;
}
/// <summary>
/// Check if there is internet connection, if not warn user.
/// Uses windows API, other platforms return always true at the moment.
/// </summary>
/// <remarks>Decreated, used for send to twitter/facebook</remarks>
/// <returns>true if there is a connection, otherwise return false</returns>
private bool IsNetworkConnected()
{
if (Program.CurrentOS == Program.OS.WINDOWS)
{
int desc;
if (InternetGetConnectedState(out desc, 0))
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
}
}