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

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreServices/CFHTTPStream.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.CoreServices.CFHTTPStream
 
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
using System;
 
30
using MonoMac.Foundation;
 
31
using MonoMac.CoreFoundation;
 
32
using MonoMac.ObjCRuntime;
 
33
 
 
34
namespace MonoMac.CoreServices {
 
35
 
 
36
        public class CFHTTPStream : CFReadStream {
 
37
                static readonly NSString _AttemptPersistentConnection;
 
38
                static readonly NSString _FinalURL;
 
39
                static readonly NSString _FinalRequest;
 
40
                static readonly NSString _Proxy;
 
41
                static readonly NSString _RequestBytesWrittenCount;
 
42
                static readonly NSString _ResponseHeader;
 
43
                static readonly NSString _ShouldAutoredirect;
 
44
 
 
45
                static CFHTTPStream ()
 
46
                {
 
47
                        var handle = Dlfcn.dlopen (Constants.CFNetworkLibrary, 0);
 
48
                        if (handle == IntPtr.Zero)
 
49
                                throw new InvalidOperationException ();
 
50
 
 
51
                        try {
 
52
                                _AttemptPersistentConnection = GetStringConstant (
 
53
                                        handle, "kCFStreamPropertyHTTPAttemptPersistentConnection");
 
54
                                _FinalURL = GetStringConstant (
 
55
                                        handle, "kCFStreamPropertyHTTPFinalURL");
 
56
                                _FinalRequest = GetStringConstant (
 
57
                                        handle, "kCFStreamPropertyHTTPFinalRequest");
 
58
                                _Proxy = GetStringConstant (
 
59
                                        handle, "kCFStreamPropertyHTTPProxy");
 
60
                                _RequestBytesWrittenCount = GetStringConstant (
 
61
                                        handle, "kCFStreamPropertyHTTPRequestBytesWrittenCount");
 
62
                                _ResponseHeader = GetStringConstant (
 
63
                                        handle, "kCFStreamPropertyHTTPResponseHeader");
 
64
                                _ShouldAutoredirect = GetStringConstant (
 
65
                                        handle, "kCFStreamPropertyHTTPShouldAutoredirect");
 
66
                        } finally {
 
67
                                Dlfcn.dlclose (handle);
 
68
                        }
 
69
                }
 
70
 
 
71
                static NSString GetStringConstant (IntPtr handle, string name)
 
72
                {
 
73
                        var result = Dlfcn.GetStringConstant (handle, name);
 
74
                        if (result == null)
 
75
                                throw new InvalidOperationException (
 
76
                                        string.Format ("Cannot get '{0}' property.", name));
 
77
                        return result;
 
78
                }
 
79
 
 
80
                internal CFHTTPStream (IntPtr handle)
 
81
                        : base (handle)
 
82
                {
 
83
                }
 
84
 
 
85
                public Uri FinalURL {
 
86
                        get {
 
87
                                var handle = GetProperty (_FinalURL);
 
88
                                if (handle == IntPtr.Zero)
 
89
                                        return null;
 
90
 
 
91
                                if (CFType.GetTypeID (handle) != CFUrl.GetTypeID ()) {
 
92
                                        CFObject.CFRelease (handle);
 
93
                                        throw new InvalidCastException ();
 
94
                                }
 
95
 
 
96
                                using (var url = new CFUrl (handle))
 
97
                                        return new Uri (url.ToString ());
 
98
                        }
 
99
                }
 
100
 
 
101
                public CFHTTPMessage GetFinalRequest ()
 
102
                {
 
103
                        var handle = GetProperty (_FinalRequest);
 
104
                        if (handle == IntPtr.Zero)
 
105
                                return null;
 
106
 
 
107
                        if (CFType.GetTypeID (handle) != CFHTTPMessage.GetTypeID ()) {
 
108
                                CFObject.CFRelease (handle);
 
109
                                throw new InvalidCastException ();
 
110
                        }
 
111
 
 
112
                        return new CFHTTPMessage (handle);
 
113
                }
 
114
 
 
115
                public CFHTTPMessage GetResponseHeader ()
 
116
                {
 
117
                        var handle = GetProperty (_ResponseHeader);
 
118
                        if (handle == IntPtr.Zero)
 
119
                                return null;
 
120
 
 
121
                        if (CFType.GetTypeID (handle) != CFHTTPMessage.GetTypeID ()) {
 
122
                                CFObject.CFRelease (handle);
 
123
                                throw new InvalidCastException ();
 
124
                        }
 
125
                        return new CFHTTPMessage (handle);
 
126
                }
 
127
 
 
128
                public bool AttemptPersistentConnection {
 
129
                        get {
 
130
                                var handle = GetProperty (_AttemptPersistentConnection);
 
131
                                if (handle == IntPtr.Zero)
 
132
                                        return false;
 
133
                                else if (handle == CFBoolean.False.Handle)
 
134
                                        return false;
 
135
                                else if (handle == CFBoolean.True.Handle)
 
136
                                        return true;
 
137
                                else
 
138
                                        throw new InvalidCastException ();
 
139
                        }
 
140
                        set {
 
141
                                SetProperty (_AttemptPersistentConnection,
 
142
                                             CFBoolean.FromBoolean (value));
 
143
                        }
 
144
                }
 
145
 
 
146
                public int RequestBytesWrittenCount {
 
147
                        get {
 
148
                                var handle = GetProperty (_RequestBytesWrittenCount);
 
149
                                if (handle == IntPtr.Zero)
 
150
                                        return 0;
 
151
 
 
152
                                using (var number = new NSNumber (handle))
 
153
                                        return number.Int32Value;
 
154
                        }
 
155
                }
 
156
 
 
157
                public bool ShouldAutoredirect {
 
158
                        get {
 
159
                                var handle = GetProperty (_ShouldAutoredirect);
 
160
                                if (handle == IntPtr.Zero)
 
161
                                        return false;
 
162
                                else if (handle == CFBoolean.False.Handle)
 
163
                                        return false;
 
164
                                else if (handle == CFBoolean.True.Handle)
 
165
                                        return true;
 
166
                                else
 
167
                                        throw new InvalidCastException ();
 
168
                        }
 
169
                        set {
 
170
                                SetProperty (_ShouldAutoredirect,
 
171
                                             CFBoolean.FromBoolean (value));
 
172
                        }
 
173
                }
 
174
 
 
175
                internal CFDictionary Proxy {
 
176
                        set {
 
177
                                SetProperty (_Proxy, value);
 
178
                        }
 
179
                }
 
180
        }
 
181
}