9 using MonoMac.CoreFoundation;
10 using MonoMac.Foundation;
11 using MonoMac.ObjCRuntime;
13 using MonoTouch.CoreFoundation;
14 using MonoTouch.Foundation;
15 using MonoTouch.ObjCRuntime;
18 #if SYSTEM_NET_HTTP && !MONOMAC
19 namespace System.Net.Http {
21 public partial class HttpClient {
24 : this (GetDefaultHandler (), true)
28 // note: the linker will re-write this to only reference the selected handler
29 // but we want this to work "as expected" even if the application is not being linked
30 static HttpMessageHandler GetDefaultHandler ()
32 return RuntimeOptions.GetHttpMessageHandler ();
36 // due to historical reasons (around CFNetwork) Xamarin.Mac includes this inside it's profile assembly
37 // and not a custom System.Net.Http assembly
38 namespace Foundation {
41 partial class NSUrlSessionHandler {
43 bool allowAutoRedirect;
44 ICredentials credentials;
47 public bool AllowAutoRedirect {
49 return allowAutoRedirect;
52 EnsureModifiability ();
53 allowAutoRedirect = value;
57 public ICredentials Credentials {
62 EnsureModifiability ();
67 internal void EnsureModifiability ()
70 throw new InvalidOperationException (
71 "This instance has already started one or more requests. " +
72 "Properties can only be modified before sending the first request.");
75 // almost identical to ModernHttpClient version but it uses the constants from monotouch.dll | Xamarin.[iOS|WatchOS|TVOS].dll
76 static Exception createExceptionForNSError(NSError error)
78 var webExceptionStatus = WebExceptionStatus.UnknownError;
80 var innerException = new NSErrorException(error);
82 // errors that exists in both share the same error code, so we can use a single switch/case
83 // this also ease watchOS integration as if does not expose CFNetwork but (I would not be
84 // surprised if it)could return some of it's error codes
86 if (error.Domain == NSError.NSUrlErrorDomain) {
88 if ((error.Domain == NSError.NSUrlErrorDomain) || (error.Domain == NSError.CFNetworkErrorDomain)) {
90 // Parse the enum into a web exception status or exception. Some
91 // of these values don't necessarily translate completely to
92 // what WebExceptionStatus supports, so made some best guesses
93 // here. For your reading pleasure, compare these:
95 // Apple docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes
96 // .NET docs: http://msdn.microsoft.com/en-us/library/system.net.webexceptionstatus(v=vs.110).aspx
97 switch ((NSUrlError) (long) error.Code) {
98 case NSUrlError.Cancelled:
99 case NSUrlError.UserCancelledAuthentication:
101 case (NSUrlError) NSNetServicesStatus.CancelledError:
103 // No more processing is required so just return.
104 return new OperationCanceledException(error.LocalizedDescription, innerException);
105 case NSUrlError.BadURL:
106 case NSUrlError.UnsupportedURL:
107 case NSUrlError.CannotConnectToHost:
108 case NSUrlError.ResourceUnavailable:
109 case NSUrlError.NotConnectedToInternet:
110 case NSUrlError.UserAuthenticationRequired:
111 case NSUrlError.InternationalRoamingOff:
112 case NSUrlError.CallIsActive:
113 case NSUrlError.DataNotAllowed:
115 case (NSUrlError) CFNetworkErrors.Socks5BadCredentials:
116 case (NSUrlError) CFNetworkErrors.Socks5UnsupportedNegotiationMethod:
117 case (NSUrlError) CFNetworkErrors.Socks5NoAcceptableMethod:
118 case (NSUrlError) CFNetworkErrors.HttpAuthenticationTypeUnsupported:
119 case (NSUrlError) CFNetworkErrors.HttpBadCredentials:
120 case (NSUrlError) CFNetworkErrors.HttpBadURL:
122 webExceptionStatus = WebExceptionStatus.ConnectFailure;
124 case NSUrlError.TimedOut:
126 case (NSUrlError) CFNetworkErrors.NetServiceTimeout:
128 webExceptionStatus = WebExceptionStatus.Timeout;
130 case NSUrlError.CannotFindHost:
131 case NSUrlError.DNSLookupFailed:
133 case (NSUrlError) CFNetworkErrors.HostNotFound:
134 case (NSUrlError) CFNetworkErrors.NetServiceDnsServiceFailure:
136 webExceptionStatus = WebExceptionStatus.NameResolutionFailure;
138 case NSUrlError.DataLengthExceedsMaximum:
139 webExceptionStatus = WebExceptionStatus.MessageLengthLimitExceeded;
141 case NSUrlError.NetworkConnectionLost:
143 case (NSUrlError) CFNetworkErrors.HttpConnectionLost:
145 webExceptionStatus = WebExceptionStatus.ConnectionClosed;
147 case NSUrlError.HTTPTooManyRedirects:
148 case NSUrlError.RedirectToNonExistentLocation:
150 case (NSUrlError) CFNetworkErrors.HttpRedirectionLoopDetected:
152 webExceptionStatus = WebExceptionStatus.ProtocolError;
154 case NSUrlError.RequestBodyStreamExhausted:
156 case (NSUrlError) CFNetworkErrors.SocksUnknownClientVersion:
157 case (NSUrlError) CFNetworkErrors.SocksUnsupportedServerVersion:
158 case (NSUrlError) CFNetworkErrors.HttpParseFailure:
160 webExceptionStatus = WebExceptionStatus.SendFailure;
162 case NSUrlError.BadServerResponse:
163 case NSUrlError.ZeroByteResource:
164 case NSUrlError.CannotDecodeRawData:
165 case NSUrlError.CannotDecodeContentData:
166 case NSUrlError.CannotParseResponse:
167 case NSUrlError.FileDoesNotExist:
168 case NSUrlError.FileIsDirectory:
169 case NSUrlError.NoPermissionsToReadFile:
170 case NSUrlError.CannotLoadFromNetwork:
171 case NSUrlError.CannotCreateFile:
172 case NSUrlError.CannotOpenFile:
173 case NSUrlError.CannotCloseFile:
174 case NSUrlError.CannotWriteToFile:
175 case NSUrlError.CannotRemoveFile:
176 case NSUrlError.CannotMoveFile:
177 case NSUrlError.DownloadDecodingFailedMidStream:
178 case NSUrlError.DownloadDecodingFailedToComplete:
180 case (NSUrlError) CFNetworkErrors.Socks4RequestFailed:
181 case (NSUrlError) CFNetworkErrors.Socks4IdentdFailed:
182 case (NSUrlError) CFNetworkErrors.Socks4IdConflict:
183 case (NSUrlError) CFNetworkErrors.Socks4UnknownStatusCode:
184 case (NSUrlError) CFNetworkErrors.Socks5BadState:
185 case (NSUrlError) CFNetworkErrors.Socks5BadResponseAddr:
186 case (NSUrlError) CFNetworkErrors.CannotParseCookieFile:
187 case (NSUrlError) CFNetworkErrors.NetServiceUnknown:
188 case (NSUrlError) CFNetworkErrors.NetServiceCollision:
189 case (NSUrlError) CFNetworkErrors.NetServiceNotFound:
190 case (NSUrlError) CFNetworkErrors.NetServiceInProgress:
191 case (NSUrlError) CFNetworkErrors.NetServiceBadArgument:
192 case (NSUrlError) CFNetworkErrors.NetServiceInvalid:
194 webExceptionStatus = WebExceptionStatus.ReceiveFailure;
196 case NSUrlError.SecureConnectionFailed:
197 webExceptionStatus = WebExceptionStatus.SecureChannelFailure;
199 case NSUrlError.ServerCertificateHasBadDate:
200 case NSUrlError.ServerCertificateHasUnknownRoot:
201 case NSUrlError.ServerCertificateNotYetValid:
202 case NSUrlError.ServerCertificateUntrusted:
203 case NSUrlError.ClientCertificateRejected:
204 case NSUrlError.ClientCertificateRequired:
205 webExceptionStatus = WebExceptionStatus.TrustFailure;
208 case (NSUrlError) CFNetworkErrors.HttpProxyConnectionFailure:
209 case (NSUrlError) CFNetworkErrors.HttpBadProxyCredentials:
210 case (NSUrlError) CFNetworkErrors.PacFileError:
211 case (NSUrlError) CFNetworkErrors.PacFileAuth:
212 case (NSUrlError) CFNetworkErrors.HttpsProxyConnectionFailure:
213 case (NSUrlError) CFNetworkErrors.HttpsProxyFailureUnexpectedResponseToConnectMethod:
214 webExceptionStatus = WebExceptionStatus.RequestProhibitedByProxy;
220 // Always create a WebException so that it can be handled by the client.
221 return new WebException(error.LocalizedDescription, innerException); //, webExceptionStatus, response: null);