Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build/
*.xam
packages
.vs
*Resource.designer.cs
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand All @@ -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
4 changes: 2 additions & 2 deletions ModernHttpClient.nuspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<version>2.5.1</version>
<version>2.6.5</version>
<authors>Switch Media, Paul Betts</authors>
<owners>Switch Media, Paul Betts</owners>
<licenseUrl>https://github.com/paulcbetts/ModernHttpClient/blob/master/COPYING</licenseUrl>
Expand All @@ -14,7 +14,7 @@
<copyright>Copyright Paul Betts © 2012</copyright>
<dependencies>
<group targetFramework="MonoAndroid">
<dependency id="Square.OkHttp" version="2.4.0.3" />
<dependency id="Square.OkHttp3" version="4.2.2"/>
</group>
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion ModernHttpClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 8 additions & 37 deletions src/ModernHttpClient/Android/OkHttpNetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -118,12 +121,12 @@ protected override async Task<HttpResponseMessage> 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()));
}
}
Expand Down Expand Up @@ -160,38 +163,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
}

public static class AwaitableOkHttp
{
public static Task<Response> EnqueueAsync(this Call This)
{
var cb = new OkTaskCallback();
This.Enqueue(cb);

return cb.Task;
}

class OkTaskCallback : Java.Lang.Object, ICallback
{
readonly TaskCompletionSource<Response> tcs = new TaskCompletionSource<Response>();
public Task<Response> 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);
Expand Down
23 changes: 18 additions & 5 deletions src/ModernHttpClient/ModernHttpClient.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AssemblyName>ModernHttpClient</AssemblyName>
<TargetFrameworkVersion>v4.4</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -56,13 +56,26 @@
<Compile Include="Utility.cs" />
<Compile Include="CaptiveNetworkException.cs" />
<Compile Include="Android\NativeCookieHandler.cs" />
<Reference Include="Java.Interop" />
<None Include="packages.config" />
<Reference Include="Xamarin.Jetbrains.Annotations">
<HintPath>..\..\packages\Xamarin.Jetbrains.Annotations.13.0.0.1\lib\monoandroid90\Xamarin.Jetbrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Kotlin.StdLib.Common">
<HintPath>..\..\packages\Xamarin.Kotlin.StdLib.Common.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.Common.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Kotlin.StdLib">
<HintPath>..\..\packages\Xamarin.Kotlin.StdLib.1.3.50.1\lib\monoandroid90\Xamarin.Kotlin.StdLib.dll</HintPath>
</Reference>
<Reference Include="Square.OkIO">
<HintPath>..\..\packages\Square.OkIO.1.5.0.0\lib\MonoAndroid\Square.OkIO.dll</HintPath>
<HintPath>..\..\packages\Square.OkIO.2.2.2\lib\monoandroid90\Square.OkIO.dll</HintPath>
</Reference>
<None Include="packages.config" />
<Reference Include="Square.OkHttp">
<HintPath>..\..\packages\Square.OkHttp.2.4.0.3\lib\MonoAndroid\Square.OkHttp.dll</HintPath>
<Reference Include="Square.OkHttp3">
<HintPath>..\..\packages\Square.OkHttp3.4.2.2\lib\monoandroid90\Square.OkHttp3.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\..\packages\Xamarin.Jetbrains.Annotations.13.0.0.1\build\monoandroid90\Xamarin.Jetbrains.Annotations.targets" Condition="Exists('..\..\packages\Xamarin.Jetbrains.Annotations.13.0.0.1\build\monoandroid90\Xamarin.Jetbrains.Annotations.targets')" />
<Import Project="..\..\packages\Xamarin.Kotlin.StdLib.Common.1.3.50.1\build\monoandroid90\Xamarin.Kotlin.StdLib.Common.targets" Condition="Exists('..\..\packages\Xamarin.Kotlin.StdLib.Common.1.3.50.1\build\monoandroid90\Xamarin.Kotlin.StdLib.Common.targets')" />
<Import Project="..\..\packages\Xamarin.Kotlin.StdLib.1.3.50.1\build\monoandroid90\Xamarin.Kotlin.StdLib.targets" Condition="Exists('..\..\packages\Xamarin.Kotlin.StdLib.1.3.50.1\build\monoandroid90\Xamarin.Kotlin.StdLib.targets')" />
</Project>
48 changes: 0 additions & 48 deletions src/ModernHttpClient/ModernHttpClient.Portable.csproj

This file was deleted.

20 changes: 20 additions & 0 deletions src/ModernHttpClient/NetStandard/ModernHttpClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>..\bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>..\bin\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\CaptiveNetworkException.cs">
<Link>CaptiveNetworkException.cs</Link>
</Compile>
</ItemGroup>
</Project>
57 changes: 0 additions & 57 deletions src/ModernHttpClient/Resources/Resource.designer.cs

This file was deleted.

21 changes: 13 additions & 8 deletions src/ModernHttpClient/iOS/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/ModernHttpClient/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Square.OkHttp" version="2.4.0.3" targetFramework="MonoAndroid23" />
<package id="Square.OkIO" version="1.5.0.0" targetFramework="MonoAndroid23" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Square.OkHttp3" version="4.2.2" targetFramework="monoandroid10.0" />
<package id="Square.OkIO" version="2.2.2" targetFramework="monoandroid10.0" />
<package id="Xamarin.Jetbrains.Annotations" version="13.0.0.1" targetFramework="monoandroid10.0" />
<package id="Xamarin.Kotlin.StdLib" version="1.3.50.1" targetFramework="monoandroid10.0" />
<package id="Xamarin.Kotlin.StdLib.Common" version="1.3.50.1" targetFramework="monoandroid10.0" />
</packages>
4 changes: 2 additions & 2 deletions src/Playground.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading