Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CFNetworkHandler.cs « System.Net.Http « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6daf9e10a279d7ea4e3092d07dbb97036b3ba1ed (plain)
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
//
// CFNetworkHandler.cs
//
// Authors:
//	Marek Safar  <marek.safar@gmail.com>
//
// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Threading;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;
using System.Net;

#if XAMCORE_4_0
using CFNetwork;
using CoreFoundation;
using CF=CoreFoundation;
#elif XAMCORE_2_0
using CoreServices;
using CoreFoundation;
using CF=CoreFoundation;
#else
using MonoTouch.CoreServices;
using MonoTouch.CoreFoundation;
using CF=MonoTouch.CoreFoundation;
#endif

namespace System.Net.Http
{
	public class CFNetworkHandler : HttpMessageHandler
	{
		class StreamBucket
		{
			public TaskCompletionSource<HttpResponseMessage> Response;
			public HttpRequestMessage Request;
			public CancellationTokenRegistration CancellationTokenRegistration;
			public CFContentStream ContentStream;

			public void Close ()
			{
				CancellationTokenRegistration.Dispose ();
                                if (ContentStream != null) {
                                    ContentStream.Close ();
                                }
			}
		}

		bool allowAutoRedirect;
		bool sentRequest;
		bool useSystemProxy;
		CookieContainer cookies;

		Dictionary<IntPtr, StreamBucket> streamBuckets;

		public CFNetworkHandler ()
		{
			allowAutoRedirect = true;
			streamBuckets = new Dictionary<IntPtr, StreamBucket> ();
		}

		void EnsureModifiability ()
		{
			if (sentRequest)
				throw new InvalidOperationException (
					"This instance has already started one or more requests. " +
					"Properties can only be modified before sending the first request.");
		}

		public bool AllowAutoRedirect {
			get {
				return allowAutoRedirect;
			}
			set {
				EnsureModifiability ();
				allowAutoRedirect = value;
			}
		}

		public CookieContainer CookieContainer {
			get {
				return cookies ?? (cookies = new CookieContainer ());
			}
			set {
				EnsureModifiability ();
				cookies = value;
			}
		}

		public bool UseSystemProxy {
			get {
				return useSystemProxy;
			}
			set {
				EnsureModifiability ();
				useSystemProxy = value;
			}
		}

		// TODO: Add more properties

		protected override void Dispose (bool disposing)
		{
			// TODO: CloseStream remaining stream buckets if there are any

			base.Dispose (disposing);
		}

		CFHTTPMessage CreateWebRequestAsync (HttpRequestMessage request)
		{
			var req = CFHTTPMessage.CreateRequest (request.RequestUri, request.Method.Method, request.Version);

			// TODO:
/*
			if (wr.ProtocolVersion == HttpVersion.Version10) {
				wr.KeepAlive = request.Headers.ConnectionKeepAlive;
			} else {
				wr.KeepAlive = request.Headers.ConnectionClose != true;
			}

			if (useDefaultCredentials) {
				wr.UseDefaultCredentials = true;
			} else {
				wr.Credentials = credentials;
			}

			if (useProxy) {
				wr.Proxy = proxy;
			}
*/
			if (cookies != null) {
				string cookieHeader = cookies.GetCookieHeader (request.RequestUri);
				if (cookieHeader != "")
					req.SetHeaderFieldValue ("Cookie", cookieHeader);
			}

			foreach (var header in request.Headers) {
				foreach (var value in header.Value) {
					req.SetHeaderFieldValue (header.Key, value);
				}
			}

			if (request.Content != null) {
				foreach (var header in request.Content.Headers) {
					foreach (var value in header.Value) {
						req.SetHeaderFieldValue (header.Key, value);
					}
				}
			}

			return req;
		}

		protected internal override async Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken)
		{
			sentRequest = true;

			CFHTTPStream stream;
			using (var message = CreateWebRequestAsync (request))
			{
				if (request.Content != null) {
					var data = await request.Content.ReadAsByteArrayAsync ().ConfigureAwait (false);
					message.SetBody (data);
				}

				stream = CFHTTPStream.CreateForHTTPRequest (message);
			}

			if (useSystemProxy) {
				var proxies = CF.CFNetwork.GetSystemProxySettings ();
				if (proxies.HTTPEnable) {
					stream.SetProxy (proxies);
				}
			}

			stream.ShouldAutoredirect = allowAutoRedirect;
			stream.HasBytesAvailableEvent += HandleHasBytesAvailableEvent;
			stream.ErrorEvent += HandleErrorEvent;
			stream.ClosedEvent += HandleClosedEvent;

			var response = new TaskCompletionSource<HttpResponseMessage> ();

			if (cancellationToken.IsCancellationRequested) {
				response.SetCanceled ();
				return await response.Task;
			}

			var bucket = new StreamBucket () {
				Request = request,
				Response = response,
			};

			streamBuckets.Add (stream.Handle, bucket);

			//
			// Always schedule stream events handling on main-loop. Due to ConfigureAwait (false) we may end up
			// on any thread-pool thread which may not have run-loop running
			//
#if XAMCORE_2_0
			stream.EnableEvents (CF.CFRunLoop.Main, CF.CFRunLoop.ModeCommon);
#else
			stream.EnableEvents (CF.CFRunLoop.Main, CF.CFRunLoop.CFRunLoopCommonModes);
#endif

			stream.Open ();

			bucket.CancellationTokenRegistration = cancellationToken.Register (() => {
				StreamBucket bucket2;
				if (!streamBuckets.TryGetValue (stream.Handle, out bucket2))
					return;

				bucket2.Response.TrySetCanceled ();
				CloseStream (stream);
			});

			return await response.Task;
		}

		void HandleErrorEvent (object sender, CFStream.StreamEventArgs e)
		{
			var stream = (CFHTTPStream)sender;

			StreamBucket bucket;
			if (!streamBuckets.TryGetValue (stream.Handle, out bucket))
				return;

			bucket.Response.TrySetException (stream.GetError ());
			CloseStream (stream);
		}

		void HandleClosedEvent (object sender, CFStream.StreamEventArgs e)
		{
			var stream = (CFHTTPStream)sender;
			CloseStream (stream);
		}

		void CloseStream (CFHTTPStream stream)
		{
			StreamBucket bucket;
			if (streamBuckets.TryGetValue (stream.Handle, out bucket)) {
				bucket.Close ();
				streamBuckets.Remove (stream.Handle);
			}

			stream.Close ();
		}

		void HandleHasBytesAvailableEvent (object sender, CFStream.StreamEventArgs e)
		{
			var stream = (CFHTTPStream) sender;

			StreamBucket bucket;
			if (!streamBuckets.TryGetValue (stream.Handle, out bucket))
				return;

			if (bucket.Response.Task.IsCompleted) {
				bucket.ContentStream.ReadStreamData ();
				return;
			}

			var header = stream.GetResponseHeader ();

			// Is this possible?
			if (!header.IsHeaderComplete)
				throw new NotImplementedException ();

			bucket.ContentStream = new CFContentStream (stream);
				
			var response_msg = new HttpResponseMessage (header.ResponseStatusCode);
			response_msg.RequestMessage = bucket.Request;
			response_msg.ReasonPhrase = header.ResponseStatusLine;
			response_msg.Content = bucket.ContentStream;

			var fields = header.GetAllHeaderFields ();
			if (fields != null) {
				foreach (var entry in fields) {
					if (entry.Key == null)
						continue;

					var key = entry.Key.ToString ();
					var value = entry.Value == null ? string.Empty : entry.Value.ToString ();
					HttpHeaders item_headers;
					if (HttpHeaders.GetKnownHeaderKind (key) == Headers.HttpHeaderKind.Content) {
						item_headers = response_msg.Content.Headers;
					} else {
						item_headers = response_msg.Headers;

						if (cookies != null && (key == "Set-Cookie" || key == "Set-Cookie2"))
							AddCookie (value, bucket.Request.RequestUri, key);
					}

					item_headers.TryAddWithoutValidation (key, value);
				}
			}

			bucket.Response.TrySetResult (response_msg);

			bucket.ContentStream.ReadStreamData ();
		}

		void AddCookie (string value, Uri uri, string header)
		{
			CookieCollection cookies1 = null;
			try {
				cookies1 = cookies.CookieCutter (uri, header, value, false);
			} catch {
			}

			if (cookies1 != null && cookies1.Count != 0) 
				cookies.Add (cookies1);
		}
	}
}