RuntimeOptions.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Net.Http;
  5. #if XAMCORE_2_0
  6. using Foundation;
  7. using ObjCRuntime;
  8. #elif MONOMAC
  9. using MonoMac.Foundation;
  10. using MonoMac.ObjCRuntime;
  11. #else
  12. #error Unknown platform
  13. #endif
  14. namespace System.Net.Http {
  15. class RuntimeOptions
  16. {
  17. const string HttpClientHandlerValue = "HttpClientHandler";
  18. const string CFNetworkHandlerValue = "CFNetworkHandler";
  19. const string NSUrlSessionHandlerValue = "NSUrlSessionHandler";
  20. const string DefaultTlsProviderValue = "default";
  21. const string LegacyTlsProviderValue = "legacy";
  22. const string AppleTlsProviderValue = "appletls";
  23. string http_message_handler;
  24. internal static RuntimeOptions Read ()
  25. {
  26. // for iOS NSBundle.ResourcePath returns the path to the root of the app bundle
  27. // for macOS apps NSBundle.ResourcePath returns foo.app/Contents/Resources
  28. // for macOS frameworks NSBundle.ResourcePath returns foo.app/Versions/Current/Resources
  29. Class bundle_finder = new Class (typeof (NSObject.NSObject_Disposer));
  30. var resource_dir = NSBundle.FromClass (bundle_finder).ResourcePath;
  31. var plist_path = GetFileName (resource_dir);
  32. if (!File.Exists (plist_path))
  33. return null;
  34. using (var plist = NSDictionary.FromFile (plist_path)) {
  35. var options = new RuntimeOptions ();
  36. options.http_message_handler = (NSString) plist ["HttpMessageHandler"];
  37. return options;
  38. }
  39. }
  40. #if MONOMAC
  41. [Preserve]
  42. #endif
  43. internal static HttpMessageHandler GetHttpMessageHandler ()
  44. {
  45. RuntimeOptions options = null;
  46. try {
  47. options = RuntimeOptions.Read ();
  48. } catch (FileNotFoundException){
  49. // this happens on the Mono SDKs since we don't have a real Xamarin.iOS.dll so we can't resolve NSObject
  50. }
  51. if (options == null) {
  52. #if MONOTOUCH_WATCH
  53. return new NSUrlSessionHandler ();
  54. #else
  55. return new HttpClientHandler ();
  56. #endif
  57. }
  58. // all types will be present as this is executed only when the linker is not enabled
  59. var handler_name = options.http_message_handler;
  60. var t = Type.GetType (handler_name, false);
  61. HttpMessageHandler handler = null;
  62. if (t != null)
  63. handler = Activator.CreateInstance (t) as HttpMessageHandler;
  64. if (handler != null)
  65. return handler;
  66. #if MONOTOUCH_WATCH
  67. Console.WriteLine ("{0} is not a valid HttpMessageHandler, defaulting to NSUrlSessionHandler", handler_name);
  68. return new NSUrlSessionHandler ();
  69. #else
  70. Console.WriteLine ("{0} is not a valid HttpMessageHandler, defaulting to System.Net.Http.HttpClientHandler", handler_name);
  71. return new HttpClientHandler ();
  72. #endif
  73. }
  74. // Use either Create() or Read().
  75. RuntimeOptions ()
  76. {
  77. }
  78. static string GetFileName (string resource_dir)
  79. {
  80. return Path.Combine (resource_dir, "runtime-options.plist");
  81. }
  82. }
  83. }