|
2 | 2 | using System.IO;
|
3 | 3 | using System.Text;
|
4 | 4 |
|
5 |
| -#if MTOUCH || MMP || BUNDLER |
6 |
| -using Mono.Cecil; |
7 |
| -using Xamarin.Linker; |
8 |
| -#else |
9 | 5 | using System.Net.Http;
|
10 | 6 | using Foundation;
|
11 | 7 | using ObjCRuntime;
|
12 |
| -#endif |
13 | 8 |
|
14 | 9 | #nullable enable
|
15 | 10 |
|
16 |
| -#if MMP || MMP_TEST || MTOUCH || BUNDLER |
17 |
| -namespace Xamarin.Bundler { |
18 |
| -#else |
19 | 11 | namespace ObjCRuntime {
|
20 |
| -#endif |
21 | 12 | class RuntimeOptions {
|
22 |
| -#if !LEGACY_TOOLS |
23 | 13 | const string SocketsHandlerValue = "SocketsHttpHandler";
|
24 |
| -#else |
25 |
| - const string HttpClientHandlerValue = "HttpClientHandler"; |
26 |
| -#endif |
27 | 14 | const string CFNetworkHandlerValue = "CFNetworkHandler";
|
28 | 15 | const string NSUrlSessionHandlerValue = "NSUrlSessionHandler";
|
29 | 16 |
|
30 |
| - string? http_message_handler; |
31 |
| - |
32 |
| -#if MTOUCH || MMP || BUNDLER |
33 |
| - /* |
34 |
| - * This section is only used by the tools |
35 |
| - */ |
36 |
| - internal static RuntimeOptions Create (Application app, string? http_message_handler, string? tls_provider) |
37 |
| - { |
38 |
| - var options = new RuntimeOptions (); |
39 |
| - options.http_message_handler = ParseHttpMessageHandler (app, http_message_handler); |
40 |
| - return options; |
41 |
| - } |
42 |
| - |
43 |
| - static string ParseHttpMessageHandler (Application app, string? value) |
44 |
| - { |
45 |
| - switch (value) { |
46 |
| - // default |
47 |
| - case null: |
48 |
| -#if !LEGACY_TOOLS |
49 |
| - return NSUrlSessionHandlerValue; |
50 |
| -#else |
51 |
| - return HttpClientHandlerValue; |
52 |
| -#endif |
53 |
| - case CFNetworkHandlerValue: |
54 |
| -#if !LEGACY_TOOLS |
55 |
| - case SocketsHandlerValue: |
56 |
| -#else |
57 |
| - case HttpClientHandlerValue: |
58 |
| -#endif |
59 |
| - return value; |
60 |
| - case NSUrlSessionHandlerValue: |
61 |
| - return value; |
62 |
| - default: |
63 |
| - throw ErrorHelper.CreateError (2010, Errors.MT2010, value); |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| - internal void Write (string app_dir) |
68 |
| - { |
69 |
| - // note: we always create the file because the simulator won't remove old files |
70 |
| - // that might become useful if we add new options in the future |
71 |
| - var content = new StringBuilder (); |
72 |
| - content.AppendLine ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
73 |
| - content.AppendLine ("<!DOCTYPE plist PUBLIC \\\"-//Apple//DTD PLIST 1.0//EN\\\" \\\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\\\">"); |
74 |
| - content.AppendLine ("<plist version=\\\"1.0\\\">"); |
75 |
| - content.AppendLine ("<dict>"); |
76 |
| - content.AppendLine ("<key>HttpMessageHandler</key>"); |
77 |
| - content.Append ("<string>"); |
78 |
| - content.Append (http_message_handler); |
79 |
| - content.AppendLine ("</string>"); |
80 |
| - content.AppendLine ("</dict>"); |
81 |
| - content.AppendLine ("</plist>"); |
82 |
| - |
83 |
| - var file_name = GetFileName (app_dir); |
84 |
| - Xamarin.Bundler.Driver.WriteIfDifferent (file_name, content.ToString ()); |
85 |
| - } |
86 |
| - |
87 |
| - // Called from CoreHttpMessageHandler |
88 |
| - internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOptions options, ModuleDefinition httpModule, ModuleDefinition? platformModule = null) |
89 |
| - { |
90 |
| - string? handler; |
91 |
| - |
92 |
| - if (options is not null) { |
93 |
| - handler = options.http_message_handler; |
94 |
| - } else { |
95 |
| -#if !LEGACY_TOOLS |
96 |
| - handler = NSUrlSessionHandlerValue; |
97 |
| -#else |
98 |
| - handler = HttpClientHandlerValue; |
99 |
| -#endif |
100 |
| - } |
101 |
| - TypeDefinition type; |
102 |
| - switch (handler) { |
103 |
| -#if MONOMAC |
104 |
| - case HttpClientHandlerValue: |
105 |
| - type = httpModule.GetType ("System.Net.Http", "HttpClientHandler"); |
106 |
| - break; |
107 |
| - case CFNetworkHandlerValue: |
108 |
| - type = platformModule!.GetType ("System.Net.Http", "CFNetworkHandler"); |
109 |
| - break; |
110 |
| - case NSUrlSessionHandlerValue: |
111 |
| - type = platformModule!.GetType ("Foundation", "NSUrlSessionHandler"); |
112 |
| - break; |
113 |
| -#else |
114 |
| -#if !LEGACY_TOOLS |
115 |
| - case SocketsHandlerValue: |
116 |
| - type = httpModule.GetType ("System.Net.Http", "SocketsHttpHandler"); |
117 |
| - break; |
118 |
| -#else |
119 |
| - case HttpClientHandlerValue: |
120 |
| - type = httpModule.GetType ("System.Net.Http", "HttpClientHandler"); |
121 |
| - break; |
122 |
| -#endif |
123 |
| - case CFNetworkHandlerValue: |
124 |
| - type = platformModule!.GetType ("System.Net.Http", "CFNetworkHandler"); |
125 |
| - break; |
126 |
| - case NSUrlSessionHandlerValue: |
127 |
| - type = platformModule!.GetType ("System.Net.Http", "NSUrlSessionHandler"); |
128 |
| - break; |
129 |
| -#endif |
130 |
| - default: |
131 |
| - throw new InvalidOperationException (string.Format ("Unknown HttpMessageHandler `{0}`.", handler)); |
132 |
| - } |
133 |
| - if (type is null) |
134 |
| - throw new InvalidOperationException (string.Format ("Cannot load HttpMessageHandler `{0}`.", handler)); |
135 |
| - return type; |
136 |
| - } |
137 |
| -#else |
138 |
| - |
139 |
| - internal static RuntimeOptions? Read () |
140 |
| - { |
141 |
| - // for iOS NSBundle.ResourcePath returns the path to the root of the app bundle |
142 |
| - // for macOS apps NSBundle.ResourcePath returns foo.app/Contents/Resources |
143 |
| - // for macOS frameworks NSBundle.ResourcePath returns foo.app/Versions/Current/Resources |
144 |
| - Class bundle_finder = new Class (typeof (NSObject.NSObject_Disposer)); |
145 |
| - var resource_dir = NSBundle.FromClass (bundle_finder).ResourcePath; |
146 |
| - var plist_path = GetFileName (resource_dir); |
147 |
| - |
148 |
| - if (!File.Exists (plist_path)) |
149 |
| - return null; |
150 |
| - |
151 |
| - using (var plist = NSMutableDictionary.FromFile (plist_path)) { |
152 |
| - var options = new RuntimeOptions (); |
153 |
| - options.http_message_handler = (NSString) plist ["HttpMessageHandler"]; |
154 |
| - return options; |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 | 17 | // This is invoked by
|
159 | 18 | // System.Net.Http.dll!System.Net.Http.HttpClient.cctor
|
| 19 | + // https://github.com/dotnet/runtime/blob/6be6c5de821e389c986b0926fb7334017decee54/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs#L146-L152 |
160 | 20 | internal static HttpMessageHandler GetHttpMessageHandler ()
|
161 | 21 | {
|
162 |
| - var options = RuntimeOptions.Read (); |
163 |
| - // all types will be present as this is executed only when the linker is not enabled |
164 |
| - var handler_name = options?.http_message_handler; |
165 |
| -#if !LEGACY_TOOLS |
166 |
| - // Note: no need to handle SocketsHandlerValue here because System.Net.Http handles |
167 |
| - // creating a SocketsHttpHandler when configured to do so. |
168 |
| - switch (handler_name) { |
169 |
| - case CFNetworkHandlerValue: |
170 |
| -#pragma warning disable CA1422 // This call site is reachable on: 'ios' 12.2 and later, 'maccatalyst' 12.2 and later, 'macOS/OSX' 12.0 and later, 'tvos' 12.2 and later. 'CFNetworkHandler' is obsoleted on: 'ios' all versions, 'maccatalyst' all versions, 'macOS/OSX' all versions, 'tvos' all versions. |
171 |
| - return new CFNetworkHandler (); |
172 |
| -#pragma warning restore CA1422 |
173 |
| - default: |
174 |
| - if (handler_name is not null && handler_name != NSUrlSessionHandlerValue) |
175 |
| - Runtime.NSLog ($"{handler_name} is not a valid HttpMessageHandler, defaulting to System.Net.Http.NSUrlSessionHandlerValue"); |
| 22 | + if (Runtime.UseNSUrlSessionHandler) |
176 | 23 | return new NSUrlSessionHandler ();
|
177 |
| - } |
178 |
| -#else |
179 |
| - switch (handler_name) { |
180 |
| - case CFNetworkHandlerValue: |
181 |
| - return new CFNetworkHandler (); |
182 |
| - case NSUrlSessionHandlerValue: |
183 |
| - return new NSUrlSessionHandler (); |
184 |
| - default: |
185 |
| - if (handler_name is not null && handler_name != HttpClientHandlerValue) |
186 |
| - Runtime.NSLog ($"{handler_name} is not a valid HttpMessageHandler, defaulting to System.Net.Http.HttpClientHandler"); |
187 |
| - return new HttpClientHandler (); |
188 |
| - } |
189 |
| -#endif |
190 |
| - } |
191 |
| -#endif |
192 | 24 |
|
193 |
| - // Use either Create() or Read(). |
194 |
| - RuntimeOptions () |
195 |
| - { |
196 |
| - } |
| 25 | + if (Runtime.UseCFNetworkHandler) |
| 26 | + return new CFNetworkHandler (); |
197 | 27 |
|
198 |
| - static string GetFileName (string resource_dir) |
199 |
| - { |
200 |
| - return Path.Combine (resource_dir, "runtime-options.plist"); |
| 28 | + return new HttpClientHandler (); |
201 | 29 | }
|
202 | 30 | }
|
203 | 31 | }
|
0 commit comments