2
// MonoMac.CoreServices.CFHTTPStream
5
// Martin Baulig (martin.baulig@gmail.com)
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
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:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
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.
30
using MonoMac.Foundation;
31
using MonoMac.CoreFoundation;
32
using MonoMac.ObjCRuntime;
34
namespace MonoMac.CoreServices {
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;
45
static CFHTTPStream ()
47
var handle = Dlfcn.dlopen (Constants.CFNetworkLibrary, 0);
48
if (handle == IntPtr.Zero)
49
throw new InvalidOperationException ();
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");
67
Dlfcn.dlclose (handle);
71
static NSString GetStringConstant (IntPtr handle, string name)
73
var result = Dlfcn.GetStringConstant (handle, name);
75
throw new InvalidOperationException (
76
string.Format ("Cannot get '{0}' property.", name));
80
internal CFHTTPStream (IntPtr handle)
87
var handle = GetProperty (_FinalURL);
88
if (handle == IntPtr.Zero)
91
if (CFType.GetTypeID (handle) != CFUrl.GetTypeID ()) {
92
CFObject.CFRelease (handle);
93
throw new InvalidCastException ();
96
using (var url = new CFUrl (handle))
97
return new Uri (url.ToString ());
101
public CFHTTPMessage GetFinalRequest ()
103
var handle = GetProperty (_FinalRequest);
104
if (handle == IntPtr.Zero)
107
if (CFType.GetTypeID (handle) != CFHTTPMessage.GetTypeID ()) {
108
CFObject.CFRelease (handle);
109
throw new InvalidCastException ();
112
return new CFHTTPMessage (handle);
115
public CFHTTPMessage GetResponseHeader ()
117
var handle = GetProperty (_ResponseHeader);
118
if (handle == IntPtr.Zero)
121
if (CFType.GetTypeID (handle) != CFHTTPMessage.GetTypeID ()) {
122
CFObject.CFRelease (handle);
123
throw new InvalidCastException ();
125
return new CFHTTPMessage (handle);
128
public bool AttemptPersistentConnection {
130
var handle = GetProperty (_AttemptPersistentConnection);
131
if (handle == IntPtr.Zero)
133
else if (handle == CFBoolean.False.Handle)
135
else if (handle == CFBoolean.True.Handle)
138
throw new InvalidCastException ();
141
SetProperty (_AttemptPersistentConnection,
142
CFBoolean.FromBoolean (value));
146
public int RequestBytesWrittenCount {
148
var handle = GetProperty (_RequestBytesWrittenCount);
149
if (handle == IntPtr.Zero)
152
using (var number = new NSNumber (handle))
153
return number.Int32Value;
157
public bool ShouldAutoredirect {
159
var handle = GetProperty (_ShouldAutoredirect);
160
if (handle == IntPtr.Zero)
162
else if (handle == CFBoolean.False.Handle)
164
else if (handle == CFBoolean.True.Handle)
167
throw new InvalidCastException ();
170
SetProperty (_ShouldAutoredirect,
171
CFBoolean.FromBoolean (value));
175
internal CFDictionary Proxy {
177
SetProperty (_Proxy, value);