diff --git a/.gitignore b/.gitignore index ffac3b9..fe8de75 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ build/ *.xam packages .vs +*Resource.designer.cs diff --git a/Makefile b/Makefile index f00056e..6c99ea0 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ MDTOOL ?= /Applications/Visual\ Studio.app/Contents/MacOS/vstool .PHONY: all clean -all: ModernHttpClient.iOS64.dll ModernHttpClient.Android.dll ModernHttpClient.Portable.dll +all: ModernHttpClient.iOS64.dll ModernHttpClient.Android.dll ModernHttpClient.dll -package: ModernHttpClient.iOS64.dll ModernHttpClient.Android.dll ModernHttpClient.Portable.dll +package: ModernHttpClient.iOS64.dll ModernHttpClient.Android.dll ModernHttpClient.dll nuget pack mv modernhttpclient*.nupkg ./build/ @@ -18,12 +18,12 @@ ModernHttpClient.iOS64.dll: mkdir -p ./build/Xamarin.iOS10 mv ./src/ModernHttpClient/bin/Release/Xamarin.iOS10/Modern* ./build/Xamarin.iOS10 -ModernHttpClient.Portable.dll: - $(MDTOOL) build -c:Release ./src/ModernHttpClient/ModernHttpClient.Portable.csproj - mkdir -p ./build/Portable-Net45+WinRT45+WP8+WPA81 - mv ./src/ModernHttpClient/bin/Release/Portable-Net45+WinRT45+WP8+WPA81/Modern* ./build/Portable-Net45+WinRT45+WP8+WPA81 +ModernHttpClient.dll: + dotnet build ./src/ModernHttpClient/NetStandard/ModernHttpClient.csproj -c:Release + mkdir -p ./build/netstandard2.0 + mv ./src/ModernHttpClient/bin/Release/netstandard2.0/Modern* ./build/netstandard2.0 clean: $(MDTOOL) build -t:Clean ModernHttpClient.sln - rm *.dll + dotnet clean ./src/ModernHttpClient/NetStandard/ModernHttpClient.csproj rm -rf build diff --git a/ModernHttpClient.nuspec b/ModernHttpClient.nuspec index fafc804..9fd3f69 100644 --- a/ModernHttpClient.nuspec +++ b/ModernHttpClient.nuspec @@ -1,7 +1,7 @@ - 2.5.1 + 2.6.5 Switch Media, Paul Betts Switch Media, Paul Betts https://github.com/paulcbetts/ModernHttpClient/blob/master/COPYING @@ -14,7 +14,7 @@ Copyright Paul Betts © 2012 - + diff --git a/ModernHttpClient.sln b/ModernHttpClient.sln index 7c3f91f..7a3744a 100644 --- a/ModernHttpClient.sln +++ b/ModernHttpClient.sln @@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground.Android", "src\P EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground.iOS", "src\Playground.iOS\Playground.iOS.csproj", "{2CE837CD-9B97-4058-9156-339697EEE228}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernHttpClient.Portable", "src\ModernHttpClient\ModernHttpClient.Portable.csproj", "{6DD98593-4498-463D-B16E-F6CC5A928B79}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernHttpClient", "src\ModernHttpClient\NetStandard\ModernHttpClient.csproj", "{6DD98593-4498-463D-B16E-F6CC5A928B79}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernHttpClient.iOS64", "src\ModernHttpClient\ModernHttpClient.iOS64.csproj", "{6941F069-AC02-40EE-BBF3-9FE3331030EF}" EndProject diff --git a/README.md b/README.md index d7da13a..570b6f0 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ System.Net.Http, but drop this library in and it will go drastically faster. This is made possible by two native libraries: * On iOS, [NSURLSession](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html) -* On Android, via [OkHttp](http://square.github.io/okhttp/) +* On Android, via [OkHttp v3](http://square.github.io/okhttp/) ## Usage diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 3edbd65..f7c59d7 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Net; using System.Net.Http; -using Square.OkHttp; +using Square.OkHttp3; using Javax.Net.Ssl; using System.Text.RegularExpressions; using Java.IO; @@ -36,7 +36,10 @@ public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerificati { this.throwOnCaptiveNetwork = throwOnCaptiveNetwork; - if (customSSLVerification) client.SetHostnameVerifier(new HostnameVerifier()); + var clientBuilder = new OkHttpClient.Builder (); + if (customSSLVerification) clientBuilder.HostnameVerifier(new HostnameVerifier()); + client = clientBuilder.Build (); + noCacheCacheControl = (new CacheControl.Builder()).NoCache().Build(); } @@ -118,12 +121,12 @@ protected override async Task SendAsync(HttpRequestMessage var resp = default(Response); try { - resp = await call.EnqueueAsync().ConfigureAwait(false); + resp = await call.ExecuteAsync().ConfigureAwait(false); var newReq = resp.Request(); - var newUri = newReq == null ? null : newReq.Uri(); + var newUri = newReq == null ? null : newReq.Url(); request.RequestUri = new Uri(newUri.ToString()); if (throwOnCaptiveNetwork && newUri != null) { - if (url.Host != newUri.Host) { + if (url.Host != newUri.Host()) { throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString())); } } @@ -160,38 +163,6 @@ protected override async Task SendAsync(HttpRequestMessage } } - public static class AwaitableOkHttp - { - public static Task EnqueueAsync(this Call This) - { - var cb = new OkTaskCallback(); - This.Enqueue(cb); - - return cb.Task; - } - - class OkTaskCallback : Java.Lang.Object, ICallback - { - readonly TaskCompletionSource tcs = new TaskCompletionSource(); - public Task Task { get { return tcs.Task; } } - - public void OnFailure(Request p0, Java.IO.IOException p1) - { - // Kind of a hack, but the simplest way to find out that server cert. validation failed - if (p1.Message == String.Format("Hostname '{0}' was not verified", p0.Url().Host)) { - tcs.TrySetException(new WebException(p1.LocalizedMessage, WebExceptionStatus.TrustFailure)); - } else { - tcs.TrySetException(p1); - } - } - - public void OnResponse(Response p0) - { - tcs.TrySetResult(p0); - } - } - } - class HostnameVerifier : Java.Lang.Object, IHostnameVerifier { static readonly Regex cnRegex = new Regex(@"CN\s*=\s*([^,]*)", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Singleline); diff --git a/src/ModernHttpClient/ModernHttpClient.Android.csproj b/src/ModernHttpClient/ModernHttpClient.Android.csproj index 91110e4..78dfaef 100644 --- a/src/ModernHttpClient/ModernHttpClient.Android.csproj +++ b/src/ModernHttpClient/ModernHttpClient.Android.csproj @@ -14,7 +14,7 @@ Resources Assets ModernHttpClient - v4.4 + v10.0 true @@ -56,13 +56,26 @@ + + + + ..\..\packages\Xamarin.Jetbrains.Annotations.13.0.0.1\lib\monoandroid90\Xamarin.Jetbrains.Annotations.dll + + + ..\..\packages\Xamarin.Kotlin.StdLib.Common.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.Common.dll + + + ..\..\packages\Xamarin.Kotlin.StdLib.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.dll + - ..\..\packages\Square.OkIO.1.5.0.0\lib\MonoAndroid\Square.OkIO.dll + ..\..\packages\Square.OkIO.2.2.2\lib\monoandroid90\Square.OkIO.dll - - - ..\..\packages\Square.OkHttp.2.4.0.3\lib\MonoAndroid\Square.OkHttp.dll + + ..\..\packages\Square.OkHttp3.4.2.2\lib\monoandroid90\Square.OkHttp3.dll + + + diff --git a/src/ModernHttpClient/ModernHttpClient.Portable.csproj b/src/ModernHttpClient/ModernHttpClient.Portable.csproj deleted file mode 100644 index 791c717..0000000 --- a/src/ModernHttpClient/ModernHttpClient.Portable.csproj +++ /dev/null @@ -1,48 +0,0 @@ - - - - Debug - AnyCPU - {6DD98593-4498-463D-B16E-F6CC5A928B79} - {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Library - ModernHttpClient.Portable - ModernHttpClient - Profile78 - v4.5 - 8.0.30703 - 2.0 - - - true - full - false - bin\Debug\Portable-Net45+WinRT45+WP8+WPA81 - obj\Debug\Portable-Net45+WinRT45+WP8+WPA81 - DEBUG; - prompt - 4 - false - - - full - true - bin\Release\Portable-Net45+WinRT45+WP8+WPA81 - obj\Release\Portable-Net45+WinRT45+WP8+WPA81 - prompt - 4 - false - - - - - - - - - - ..\..\ext\portable-headers\System.Net.Http.dll - false - - - diff --git a/src/ModernHttpClient/Facades.cs b/src/ModernHttpClient/NetStandard/Facades.cs similarity index 100% rename from src/ModernHttpClient/Facades.cs rename to src/ModernHttpClient/NetStandard/Facades.cs diff --git a/src/ModernHttpClient/NetStandard/ModernHttpClient.csproj b/src/ModernHttpClient/NetStandard/ModernHttpClient.csproj new file mode 100644 index 0000000..87d8bc7 --- /dev/null +++ b/src/ModernHttpClient/NetStandard/ModernHttpClient.csproj @@ -0,0 +1,20 @@ + + + + netstandard2.0 + + + ..\bin\Debug\ + + + ..\bin\Release\ + + + + + + + CaptiveNetworkException.cs + + + diff --git a/src/ModernHttpClient/Resources/Resource.designer.cs b/src/ModernHttpClient/Resources/Resource.designer.cs deleted file mode 100644 index 861eaa2..0000000 --- a/src/ModernHttpClient/Resources/Resource.designer.cs +++ /dev/null @@ -1,57 +0,0 @@ -#pragma warning disable 1591 -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -[assembly: global::Android.Runtime.ResourceDesignerAttribute("ModernHttpClient.Resource", IsApplication=false)] - -namespace ModernHttpClient -{ - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] - public partial class Resource - { - - static Resource() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - public partial class Attribute - { - - static Attribute() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Attribute() - { - } - } - - public partial class String - { - - // aapt resource value: 0x7f020000 - public static int library_name = 2130837504; - - static String() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private String() - { - } - } - } -} -#pragma warning restore 1591 diff --git a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs index 610999d..4f2c950 100644 --- a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs +++ b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs @@ -54,20 +54,25 @@ public class NativeMessageHandler : HttpClientHandler public bool DisableCaching { get; set; } public NativeMessageHandler(): this(false, false) { } - public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null, SslProtocol? minimumSSLProtocol = null) + public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null) { var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration; // System.Net.ServicePointManager.SecurityProtocol provides a mechanism for specifying supported protocol types // for System.Net. Since iOS only provides an API for a minimum and maximum protocol we are not able to port // this configuration directly and instead use the specified minimum value when one is specified. - if (minimumSSLProtocol.HasValue) { - configuration.TLSMinimumSupportedProtocol = minimumSSLProtocol.Value; - } - - session = NSUrlSession.FromConfiguration( - NSUrlSessionConfiguration.DefaultSessionConfiguration, - new DataTaskDelegate(this), null); + var sp = ServicePointManager.SecurityProtocol; + if ((sp & SecurityProtocolType.Ssl3) != 0) + configuration.TLSMinimumSupportedProtocol = SslProtocol.Ssl_3_0; + else if ((sp & SecurityProtocolType.Tls) != 0) + configuration.TLSMinimumSupportedProtocol = SslProtocol.Tls_1_0; + else if ((sp & SecurityProtocolType.Tls11) != 0) + configuration.TLSMinimumSupportedProtocol = SslProtocol.Tls_1_1; + else if ((sp & SecurityProtocolType.Tls12) != 0) + configuration.TLSMinimumSupportedProtocol = SslProtocol.Tls_1_2; + + INSUrlSessionDelegate sessionDelegate = new DataTaskDelegate (this); + session = NSUrlSession.FromConfiguration (NSUrlSessionConfiguration.DefaultSessionConfiguration, sessionDelegate, null); this.throwOnCaptiveNetwork = throwOnCaptiveNetwork; this.customSSLVerification = customSSLVerification; diff --git a/src/ModernHttpClient/packages.config b/src/ModernHttpClient/packages.config index 683dfae..e231900 100644 --- a/src/ModernHttpClient/packages.config +++ b/src/ModernHttpClient/packages.config @@ -1,5 +1,8 @@ - - - - + + + + + + + \ No newline at end of file diff --git a/src/Playground.Android/MainActivity.cs b/src/Playground.Android/MainActivity.cs index 94a4b83..6d0337e 100644 --- a/src/Playground.Android/MainActivity.cs +++ b/src/Playground.Android/MainActivity.cs @@ -52,8 +52,8 @@ protected override void OnCreate (Bundle bundle) //This API is only available in Mono and Xamarin products. //You can filter and/or re-order the ciphers suites that the SSL/TLS server will accept from a client. //The following example removes weak (export) ciphers from the list that will be offered to the server. - ServicePointManager.ClientCipherSuitesCallback += (protocol, allCiphers) => - allCiphers.Where(x => !x.Contains("EXPORT")).ToList(); + //ServicePointManager.ClientCipherSuitesCallback += (protocol, allCiphers) => + // allCiphers.Where(x => !x.Contains("EXPORT")).ToList(); //Here we accept any certificate and just print the cert's data. ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { diff --git a/src/Playground.Android/Playground.Android.csproj b/src/Playground.Android/Playground.Android.csproj index b1712e9..0b50a60 100644 --- a/src/Playground.Android/Playground.Android.csproj +++ b/src/Playground.Android/Playground.Android.csproj @@ -15,7 +15,7 @@ Resources Assets Playground.Android - v4.4 + v10.0 Properties\AndroidManifest.xml @@ -55,10 +55,20 @@ - ..\..\packages\Square.OkIO.1.5.0.0\lib\MonoAndroid\Square.OkIO.dll + ..\..\packages\Square.OkIO.2.2.2\lib\monoandroid90\Square.OkIO.dll - - ..\..\packages\Square.OkHttp.2.4.0.3\lib\MonoAndroid\Square.OkHttp.dll + + ..\..\packages\Square.OkHttp3.4.2.2\lib\monoandroid90\Square.OkHttp3.dll + + + ..\..\packages\Xamarin.Jetbrains.Annotations.13.0.0.1\lib\monoandroid90\Xamarin.Jetbrains.Annotations.dll + + + + ..\..\packages\Xamarin.Kotlin.StdLib.Common.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.Common.dll + + + ..\..\packages\Xamarin.Kotlin.StdLib.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.dll @@ -84,4 +94,7 @@ ModernHttpClient.Android + + + diff --git a/src/Playground.Android/Properties/AndroidManifest.xml b/src/Playground.Android/Properties/AndroidManifest.xml index 3df56b0..ce5f0b4 100644 --- a/src/Playground.Android/Properties/AndroidManifest.xml +++ b/src/Playground.Android/Properties/AndroidManifest.xml @@ -1,6 +1,6 @@  - + \ No newline at end of file diff --git a/src/Playground.Android/Resources/Resource.designer.cs b/src/Playground.Android/Resources/Resource.designer.cs deleted file mode 100644 index b947db6..0000000 --- a/src/Playground.Android/Resources/Resource.designer.cs +++ /dev/null @@ -1,131 +0,0 @@ -#pragma warning disable 1591 -// ------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Mono Runtime Version: 4.0.30319.17020 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -// ------------------------------------------------------------------------------ - -[assembly: Android.Runtime.ResourceDesignerAttribute("Playground.Android.Resource", IsApplication=true)] - -namespace Playground.Android -{ - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] - public partial class Resource - { - - static Resource() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - public static void UpdateIdValues() - { - global::ModernHttpClient.Resource.String.library_name = global::Playground.Android.Resource.String.library_name; - } - - public partial class Attribute - { - - static Attribute() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Attribute() - { - } - } - - public partial class Drawable - { - - // aapt resource value: 0x7f020000 - public const int Icon = 2130837504; - - static Drawable() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Drawable() - { - } - } - - public partial class Id - { - - // aapt resource value: 0x7f050001 - public const int cancelButton = 2131034113; - - // aapt resource value: 0x7f050000 - public const int doIt = 2131034112; - - // aapt resource value: 0x7f050004 - public const int md5sum = 2131034116; - - // aapt resource value: 0x7f050005 - public const int progress = 2131034117; - - // aapt resource value: 0x7f050003 - public const int result = 2131034115; - - // aapt resource value: 0x7f050002 - public const int status = 2131034114; - - static Id() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Id() - { - } - } - - public partial class Layout - { - - // aapt resource value: 0x7f030000 - public const int Main = 2130903040; - - static Layout() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Layout() - { - } - } - - public partial class String - { - - // aapt resource value: 0x7f040002 - public const int app_name = 2130968578; - - // aapt resource value: 0x7f040001 - public const int hello = 2130968577; - - // aapt resource value: 0x7f040000 - public const int library_name = 2130968576; - - static String() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private String() - { - } - } - } -} -#pragma warning restore 1591 diff --git a/src/Playground.Android/packages.config b/src/Playground.Android/packages.config index 5125da1..e231900 100644 --- a/src/Playground.Android/packages.config +++ b/src/Playground.Android/packages.config @@ -1,5 +1,8 @@ - - - - + + + + + + + \ No newline at end of file