-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathConnection.cs
More file actions
275 lines (245 loc) · 8.67 KB
/
Copy pathConnection.cs
File metadata and controls
275 lines (245 loc) · 8.67 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
using System;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Nest
{
public class Connection : IConnection
{
const int BUFFER_SIZE = 1024;
private IConnectionSettings _ConnectionSettings { get; set; }
private Semaphore _ResourceLock;
public Connection(IConnectionSettings settings)
{
if (settings == null)
throw new ArgumentNullException("settings");
this._ConnectionSettings = settings;
this._ResourceLock = new Semaphore(settings.MaximumAsyncConnections, settings.MaximumAsyncConnections);
}
public ConnectionStatus GetSync(string path)
{
return this.HeaderOnlyRequest(path, "GET");
}
public ConnectionStatus HeadSync(string path)
{
return this.HeaderOnlyRequest(path, "HEAD");
}
public ConnectionStatus PostSync(string path, string data)
{
return this.BodyRequest(path, data, "POST");
}
public ConnectionStatus PutSync(string path, string data)
{
return this.BodyRequest(path, data, "PUT");
}
public ConnectionStatus DeleteSync(string path)
{
var connection = this.CreateConnection(path, "DELETE");
return this.DoSynchronousRequest(connection);
}
public ConnectionStatus DeleteSync(string path, string data)
{
var connection = this.CreateConnection(path, "DELETE");
return this.DoSynchronousRequest(connection, data);
}
public Task<ConnectionStatus> Get(string path)
{
var r = this.CreateConnection(path, "GET");
return this.DoAsyncRequest(r);
}
public Task<ConnectionStatus> Head(string path)
{
var r = this.CreateConnection(path, "HEAD");
return this.DoAsyncRequest(r);
}
public Task<ConnectionStatus> Post(string path, string data)
{
var r = this.CreateConnection(path, "POST");
return this.DoAsyncRequest(r, data);
}
public Task<ConnectionStatus> Put(string path, string data)
{
var r = this.CreateConnection(path, "PUT");
return this.DoAsyncRequest(r, data);
}
public Task<ConnectionStatus> Delete(string path, string data)
{
var r = this.CreateConnection(path, "DELETE");
return this.DoAsyncRequest(r, data);
}
public Task<ConnectionStatus> Delete(string path)
{
var r = this.CreateConnection(path, "DELETE");
return this.DoAsyncRequest(r);
}
private static void ThreadTimeoutCallback(object state, bool timedOut)
{
if (timedOut)
{
HttpWebRequest request = state as HttpWebRequest;
if (request != null)
{
request.Abort();
}
}
}
private ConnectionStatus HeaderOnlyRequest(string path, string method)
{
var connection = this.CreateConnection(path, method);
return this.DoSynchronousRequest(connection);
}
private ConnectionStatus BodyRequest(string path, string data, string method)
{
var connection = this.CreateConnection(path, method);
return this.DoSynchronousRequest(connection, data);
}
private HttpWebRequest CreateConnection(string path, string method)
{
var timeout = this._ConnectionSettings.Timeout;
var url = this._CreateUriString(path);
if (this._ConnectionSettings.UsesPrettyResponses)
{
var uri = new Uri(url);
url += (string.IsNullOrEmpty(uri.Query) ? "?" : "&") + "pretty=true";
}
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Accept = "application/json";
myReq.ContentType = "application/json";
myReq.Timeout = timeout; // 1 minute timeout.
myReq.ReadWriteTimeout = timeout; // 1 minute timeout.
myReq.Method = method;
if (!string.IsNullOrEmpty(this._ConnectionSettings.ProxyAddress))
{
var proxy = new WebProxy();
var uri = new Uri(this._ConnectionSettings.ProxyAddress);
var credentials = new NetworkCredential(this._ConnectionSettings.Username, this._ConnectionSettings.Password);
proxy.Address = uri;
proxy.Credentials = credentials;
myReq.Proxy = proxy;
}
return myReq;
}
protected virtual ConnectionStatus DoSynchronousRequest(HttpWebRequest request, string data = null)
{
var timeout = this._ConnectionSettings.Timeout;
var task = this.DoAsyncRequest(request, data);
task.Wait(timeout);
if (task.Result == null && task.IsCanceled)
{
var m = "Operation did not complete before the set timeout of " + timeout + "ms";
return new ConnectionStatus(new TimeoutException(m));
}
return task.Result;
}
protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
{
var tcs = new TaskCompletionSource<ConnectionStatus>();
this.Iterate(this._AsyncSteps(request, tcs, data), tcs);
if (tcs.Task != null && tcs.Task.Result != null)
{
tcs.Task.Result.Request = data;
tcs.Task.Result.RequestUrl = request.RequestUri.ToString();
tcs.Task.Result.RequestMethod = request.Method;
}
return tcs.Task;
}
private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
{
var timeout = this._ConnectionSettings.Timeout;
if (!this._ResourceLock.WaitOne(timeout))
{
var m = "Could not start the operation before the timeout of " + timeout + "ms completed while waiting for the semaphore";
tcs.SetResult(new ConnectionStatus(new TimeoutException(m)));
yield break;
}
try
{
var state = new ConnectionState { Connection = request };
if (data != null)
{
var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
yield return getRequestStream;
var requestStream = getRequestStream.Result;
try
{
byte[] buffer = Encoding.UTF8.GetBytes(data);
var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, buffer, 0, buffer.Length, state);
yield return writeToRequestStream;
}
finally
{
requestStream.Close();
}
}
// Get the response
var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
yield return getResponse;
// Get the response stream
using (var response = (HttpWebResponse)getResponse.Result)
using (var responseStream = response.GetResponseStream())
{
// Copy all data from the response stream
var output = new MemoryStream();
var buffer = new byte[BUFFER_SIZE];
while (responseStream != null)
{
var read = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, buffer, 0, BUFFER_SIZE, null);
yield return read;
if (read.Result == 0) break;
output.Write(buffer, 0, read.Result);
}
// Decode the data and store the result
var result = Encoding.UTF8.GetString(output.ToArray());
var cs = new ConnectionStatus(result) { Request = data, RequestUrl = request.RequestUri.ToString(), RequestMethod = request.Method };
tcs.TrySetResult(cs);
}
}
finally
{
this._ResourceLock.Release();
}
}
public void Iterate(IEnumerable<Task> asyncIterator, TaskCompletionSource<ConnectionStatus> tcs)
{
var enumerator = asyncIterator.GetEnumerator();
Action<Task> recursiveBody = null;
recursiveBody = completedTask =>
{
if (completedTask != null && completedTask.IsFaulted)
{
var exception = completedTask.Exception.InnerException;
//cleanly exit from exceptions in stages
//none of the individual steps in _AsyncSteps run in parallel for 1 request
//as this would be impossible we can assume Aggregate Exception.InnerException
tcs.SetResult(new ConnectionStatus(exception));
//tcs.TrySetException();
enumerator.Dispose();
}
else if (enumerator.MoveNext())
{
enumerator.Current.ContinueWith(recursiveBody, TaskContinuationOptions.ExecuteSynchronously);
}
else enumerator.Dispose();
};
recursiveBody(null);
}
private string _CreateUriString(string path)
{
var s = this._ConnectionSettings;
if (!string.IsNullOrEmpty(s.Host))
{
if (!path.StartsWith("/"))
path = "/" + path;
return string.Format("http://{0}:{1}{2}", s.Host, s.Port, path);
}
if (!s.Uri.ToString().EndsWith("/") && !path.StartsWith("/"))
path = "/" + path;
return s.Uri + path;
}
}
}