~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/CFNetwork/MessageHandler.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MonoMac.CFNetwork.MessageHandler
 
3
//
 
4
// Authors:
 
5
//      Martin Baulig (martin.baulig@gmail.com)
 
6
//
 
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
 
8
//
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining
 
11
// a copy of this software and associated documentation files (the
 
12
// "Software"), to deal in the Software without restriction, including
 
13
// without limitation the rights to use, copy, modify, merge, publish,
 
14
// distribute, sublicense, and/or sell copies of the Software, and to
 
15
// permit persons to whom the Software is furnished to do so, subject to
 
16
// the following conditions:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
//
 
29
 
 
30
using System;
 
31
using System.Net;
 
32
using System.Text.RegularExpressions;
 
33
using System.Collections.Generic;
 
34
using System.Threading;
 
35
using System.Threading.Tasks;
 
36
using System.Net.Http;
 
37
using MonoMac.CoreFoundation;
 
38
using MonoMac.CoreServices;
 
39
using MonoMac.Foundation;
 
40
 
 
41
namespace MonoMac.CFNetwork {
 
42
 
 
43
        public class MessageHandler : HttpClientHandler {
 
44
                public MessageHandler ()
 
45
                {
 
46
                }
 
47
 
 
48
                public MessageHandler (WorkerThread worker)
 
49
                {
 
50
                        WorkerThread = worker;
 
51
                }
 
52
 
 
53
                public WorkerThread WorkerThread {
 
54
                        get;
 
55
                        private set;
 
56
                }
 
57
 
 
58
                /*
 
59
                 * CFNetwork supports two ways of authentication:
 
60
                 * 
 
61
                 * a) You send a normal request to the server and when it responds with
 
62
                 *    a 401 or 407, then you call CFHTTPMessageAddAuthentication on the
 
63
                 *    returned response CFHTTPMessage.  When the call succeeds, you resend
 
64
                 *    the request.
 
65
                 * 
 
66
                 * b) You do the same thing for the first request, but call
 
67
                 *    CFHTTPAuthenticationCreateFromResponse on the returned response to
 
68
                 *    get a CFHTTPAuthentication object which can persist multiple requests.
 
69
                 * 
 
70
                 *    On subsequent requests, you can then resue that object by calling
 
71
                 *    CFHTTPAuthenticationAppliesToRequest, CFHTTPAuthenticationIsValid and
 
72
                 *    (if both succeed) CFHTTPMessageApplyCredentials prior to sending the
 
73
                 *    request.
 
74
                 * 
 
75
                 */
 
76
                CFHTTPAuthentication auth;
 
77
 
 
78
                #region implemented abstract members of HttpMessageHandler
 
79
                protected override async Task<HttpResponseMessage> SendAsync (HttpRequestMessage request,
 
80
                                                                              CancellationToken cancellationToken)
 
81
                {
 
82
                        if (!request.RequestUri.IsAbsoluteUri)
 
83
                                throw new InvalidOperationException ();
 
84
 
 
85
                        using (var message = CreateRequest (request)) {
 
86
                                var body = await CreateBody (request, message, cancellationToken);
 
87
                                return await ProcessRequest (request, message, body, true, cancellationToken);
 
88
                        }
 
89
                }
 
90
                #endregion
 
91
 
 
92
                CFHTTPMessage CreateRequest (HttpRequestMessage request)
 
93
                {
 
94
                        var message = CFHTTPMessage.CreateRequest (
 
95
                                request.RequestUri, request.Method.Method, request.Version);
 
96
 
 
97
                        SetupRequest (request, message);
 
98
 
 
99
                        if ((auth == null) || (Credentials == null) || !PreAuthenticate)
 
100
                                return message;
 
101
 
 
102
                        if (!auth.AppliesToRequest (message))
 
103
                                return message;
 
104
 
 
105
                        var method = auth.GetMethod ();
 
106
                        var credential = Credentials.GetCredential (request.RequestUri, method);
 
107
                        if (credential == null)
 
108
                                return message;
 
109
 
 
110
                        message.ApplyCredentials (auth, credential);
 
111
                        return message;
 
112
                }
 
113
 
 
114
                void SetupRequest (HttpRequestMessage request, CFHTTPMessage message)
 
115
                {
 
116
                        string accept_encoding = null;
 
117
                        if ((AutomaticDecompression & DecompressionMethods.GZip) != 0)
 
118
                                accept_encoding = "gzip";
 
119
                        if ((AutomaticDecompression & DecompressionMethods.Deflate) != 0)
 
120
                                accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
 
121
                        if (accept_encoding != null)
 
122
                                message.SetHeaderFieldValue ("Accept-Encoding", accept_encoding);
 
123
 
 
124
                        if (request.Content != null) {
 
125
                                foreach (var header in request.Content.Headers) {
 
126
                                        var value = string.Join (",", header.Value);
 
127
                                        message.SetHeaderFieldValue (header.Key, value);
 
128
                                }
 
129
                        }
 
130
 
 
131
                        foreach (var header in request.Headers) {
 
132
                                if ((accept_encoding != null) && header.Key.Equals ("Accept-Encoding"))
 
133
                                        continue;
 
134
                                var value = string.Join (",", header.Value);
 
135
                                message.SetHeaderFieldValue (header.Key, value);
 
136
                        }
 
137
 
 
138
                        if (UseCookies && (CookieContainer != null)) {
 
139
                                string cookieHeader = CookieContainer.GetCookieHeader (request.RequestUri);
 
140
                                if (cookieHeader != "")
 
141
                                        message.SetHeaderFieldValue ("Cookie", cookieHeader);
 
142
                        }
 
143
                }
 
144
 
 
145
                async Task<WebRequestStream> CreateBody (HttpRequestMessage request, CFHTTPMessage message,
 
146
                                                         CancellationToken cancellationToken)
 
147
                {
 
148
                        if (request.Content == null)
 
149
                                return null;
 
150
 
 
151
                        /*
 
152
                         * There are two ways of sending the body:
 
153
                         * 
 
154
                         * - CFHTTPMessageSetBody() sets the full body contents
 
155
                         *   We use this by default.
 
156
                         *
 
157
                         * - CFReadStreamCreateForStreamedHTTPRequest() should be used
 
158
                         *   if the body is too large to fit in memory.  It also uses
 
159
                         *   chunked transfer encoding.
 
160
                         *
 
161
                         *   We use this if the user either gave us a StreamContent, or we
 
162
                         *   don't have any Content-Length, so we'll have to use chunked
 
163
                         *   transfer anyways.
 
164
                         *
 
165
                         */
 
166
                        var length = request.Content.Headers.ContentLength;
 
167
                        if ((request.Content is StreamContent) || (length == null)) {
 
168
                                var stream = await request.Content.ReadAsStreamAsync ().ConfigureAwait (false);
 
169
                                return new WebRequestStream (stream, cancellationToken);
 
170
                        }
 
171
 
 
172
                        var text = await request.Content.ReadAsByteArrayAsync ().ConfigureAwait (false);
 
173
                        message.SetBody (text);
 
174
                        return null;
 
175
                }
 
176
 
 
177
                bool GetKeepAlive (HttpRequestMessage request)
 
178
                {
 
179
                        if (request.Version != HttpVersion.Version10)
 
180
                                return request.Headers.ConnectionClose != true;
 
181
 
 
182
                        foreach (var header in request.Headers.Connection) {
 
183
                                if (string.Equals (header, "Keep-Alive", StringComparison.OrdinalIgnoreCase))
 
184
                                        return true;
 
185
                        }
 
186
 
 
187
                        return request.Headers.Contains ("Keep-Alive");
 
188
                }
 
189
 
 
190
                async Task<HttpResponseMessage> ProcessRequest (HttpRequestMessage request,
 
191
                                                                CFHTTPMessage message,
 
192
                                                                WebRequestStream body,
 
193
                                                                bool retryWithCredentials,
 
194
                                                                CancellationToken cancellationToken)
 
195
                {
 
196
                        cancellationToken.ThrowIfCancellationRequested ();
 
197
 
 
198
                        WebResponseStream stream;
 
199
                        if (body != null)
 
200
                                stream = WebResponseStream.Create (message, body);
 
201
                        else
 
202
                                stream = WebResponseStream.Create (message);
 
203
                        if (stream == null)
 
204
                                throw new HttpRequestException (string.Format (
 
205
                                        "Failed to create web request for '{0}'.",
 
206
                                        request.RequestUri)
 
207
                                );
 
208
 
 
209
                        stream.Stream.ShouldAutoredirect = AllowAutoRedirect;
 
210
                        stream.Stream.AttemptPersistentConnection = GetKeepAlive (request);
 
211
 
 
212
                        var response = await stream.Open (
 
213
                                WorkerThread, cancellationToken).ConfigureAwait (false);
 
214
 
 
215
                        var status = (HttpStatusCode)response.ResponseStatusCode;
 
216
 
 
217
                        if (retryWithCredentials && (body == null) &&
 
218
                            (status == HttpStatusCode.Unauthorized) ||
 
219
                            (status == HttpStatusCode.ProxyAuthenticationRequired)) {
 
220
                                if (HandleAuthentication (request.RequestUri, message, response)) {
 
221
                                        stream.Dispose ();
 
222
                                        return await ProcessRequest (
 
223
                                                request, message, null, false, cancellationToken);
 
224
                                }
 
225
                        }
 
226
 
 
227
                        // The Content object takes ownership of the stream, so we don't
 
228
                        // dispose it here.
 
229
 
 
230
                        var retval = new HttpResponseMessage ();
 
231
                        retval.StatusCode = response.ResponseStatusCode;
 
232
                        retval.ReasonPhrase = GetReasonPhrase (response);
 
233
                        retval.Version = response.Version;
 
234
 
 
235
                        var content = new Content (stream);
 
236
                        retval.Content = content;
 
237
 
 
238
                        DecodeHeaders (response, retval, content);
 
239
                        return retval;
 
240
                }
 
241
 
 
242
                string GetReasonPhrase (CFHTTPMessage response)
 
243
                {
 
244
                        var line = response.ResponseStatusLine;
 
245
                        var match = Regex.Match (line, "HTTP/1.(0|1) (\\d+) (.*)");
 
246
                        if (!match.Success)
 
247
                                return line;
 
248
 
 
249
                        return match.Groups [3].Value;
 
250
                }
 
251
 
 
252
                bool HandleAuthentication (Uri uri, CFHTTPMessage request, CFHTTPMessage response)
 
253
                {
 
254
                        if (Credentials == null)
 
255
                                return false;
 
256
 
 
257
                        if (PreAuthenticate) {
 
258
                                FindAuthenticationObject (response);
 
259
                                return HandlePreAuthentication (uri, request);
 
260
                        }
 
261
 
 
262
                        var basic = Credentials.GetCredential (uri, "Basic");
 
263
                        var digest = Credentials.GetCredential (uri, "Digest");
 
264
 
 
265
                        bool ok = false;
 
266
                        if ((basic != null) && (digest == null))
 
267
                                ok = HandleAuthentication (
 
268
                                        request, response, CFHTTPMessage.AuthenticationScheme.Basic, basic);
 
269
                        if ((digest != null) && (basic == null))
 
270
                                ok = HandleAuthentication (
 
271
                                        request, response, CFHTTPMessage.AuthenticationScheme.Digest, digest);
 
272
                        if (ok)
 
273
                                return true;
 
274
 
 
275
                        FindAuthenticationObject (response);
 
276
                        return HandlePreAuthentication (uri, request);
 
277
                }
 
278
 
 
279
                bool HandlePreAuthentication (Uri uri, CFHTTPMessage message)
 
280
                {
 
281
                        var method = auth.GetMethod ();
 
282
                        var credential = Credentials.GetCredential (uri, method);
 
283
                        if (credential == null)
 
284
                                return false;
 
285
 
 
286
                        message.ApplyCredentials (auth, credential);
 
287
                        return true;
 
288
                }
 
289
 
 
290
                bool HandleAuthentication (CFHTTPMessage request, CFHTTPMessage response,
 
291
                                           CFHTTPMessage.AuthenticationScheme scheme,
 
292
                                           NetworkCredential credential)
 
293
                {
 
294
                        bool forProxy = response.ResponseStatusCode == HttpStatusCode.ProxyAuthenticationRequired;
 
295
 
 
296
                        return request.AddAuthentication (
 
297
                                response, (NSString)credential.UserName, (NSString)credential.Password,
 
298
                                scheme, forProxy);
 
299
                }
 
300
 
 
301
                void FindAuthenticationObject (CFHTTPMessage response)
 
302
                {
 
303
                        if (auth != null) {
 
304
                                if (auth.IsValid)
 
305
                                        return;
 
306
                                auth.Dispose ();
 
307
                                auth = null;
 
308
                        }
 
309
 
 
310
                        if (auth == null) {
 
311
                                auth = CFHTTPAuthentication.CreateFromResponse (response);
 
312
                                if (auth == null)
 
313
                                        throw new HttpRequestException ("Failed to create CFHTTPAuthentication");
 
314
                        }
 
315
 
 
316
                        if (!auth.IsValid)
 
317
                                throw new HttpRequestException ("Failed to validate CFHTTPAuthentication");
 
318
                }
 
319
 
 
320
                void DecodeHeaders (CFHTTPMessage message, HttpResponseMessage response, Content content)
 
321
                {
 
322
                        using (var dict = message.GetAllHeaderFields ()) {
 
323
                                foreach (var entry in dict) {
 
324
                                        DecodeHeader (response, content, entry);
 
325
                                }
 
326
                        }
 
327
                }
 
328
 
 
329
                void DecodeHeader (HttpResponseMessage response, Content content,
 
330
                                   KeyValuePair<NSObject,NSObject> entry)
 
331
                {
 
332
                        string key = (NSString)entry.Key;
 
333
                        string value = (NSString)entry.Value;
 
334
 
 
335
                        try {
 
336
                                if (content.DecodeHeader (key, value))
 
337
                                        return;
 
338
 
 
339
                                response.Headers.Add (key, value);
 
340
                                return;
 
341
                        } catch {
 
342
                                ;
 
343
                        }
 
344
 
 
345
                        /*
 
346
                         * FIXME: .NET automatically fixes an invalid date header
 
347
                         *        by setting it to the current time.  Mono does not.
 
348
                         */
 
349
                        if (key.Equals ("Date"))
 
350
                                response.Headers.Date = DateTime.Now;
 
351
                }
 
352
 
 
353
                protected override void Dispose (bool disposing)
 
354
                {
 
355
                        if (disposing) {
 
356
                                if (auth != null) {
 
357
                                        auth.Dispose ();
 
358
                                        auth = null;
 
359
                                }
 
360
                        }
 
361
 
 
362
                        base.Dispose (disposing);
 
363
                }
 
364
        }
 
365
}