Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portable versions of NativeMessageHandler and NativeCookieHandler #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/ModernHttpClient/Android/NativeCookieHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SetCookies(IEnumerable<Cookie> cookies)
}
}

public List<Cookie> Cookies {
public IReadOnlyList<Cookie> Cookies {
get {
return cookieManager.CookieStore.Cookies
.Select(ToNetCookie)
Expand Down
56 changes: 0 additions & 56 deletions src/ModernHttpClient/Facades.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,9 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Net;

namespace ModernHttpClient
{
public class NativeMessageHandler : HttpClientHandler
{
const string wrongVersion = "You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version";

public bool DisableCaching { get; set; }

/// <summary>
/// Initializes a new instance of the <see
/// cref="ModernHttpClient.Portable.NativeMessageHandler"/> class.
/// </summary>
public NativeMessageHandler(): base()
{
}

/// <summary>
/// Initializes a new instance of the <see
/// cref="ModernHttpClient.Portable.NativeMessageHandler"/> class.
/// </summary>
/// <param name="throwOnCaptiveNetwork">If set to <c>true</c> throw on
/// captive network (ie: a captive network is usually a wifi network
/// where an authentication html form is shown instead of the real
/// content).</param>
/// <param name="customSSLVerification">Enable custom SSL certificate
/// verification via ServicePointManager. Disabled by default for
/// performance reasons (i.e. the OS default certificate verification
/// will take place)</param>
/// <param name="cookieHandler">Enable native cookie handling.
/// </param>
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null) : base()
{
}

public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
{
throw new Exception(wrongVersion);
}
}

public class ProgressStreamContent : StreamContent
{
const string wrongVersion = "You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version";
Expand All @@ -67,18 +25,4 @@ public ProgressDelegate Progress {
}

public delegate void ProgressDelegate(long bytes, long totalBytes, long totalBytesExpected);

public class NativeCookieHandler
{
const string wrongVersion = "You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version";

public void SetCookies(IEnumerable<Cookie> cookies)
{
throw new Exception(wrongVersion);
}

public List<Cookie> Cookies {
get { throw new Exception(wrongVersion); }
}
}
}
2 changes: 2 additions & 0 deletions src/ModernHttpClient/ModernHttpClient.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Facades.cs" />
<Compile Include="CaptiveNetworkException.cs" />
<Compile Include="NativeCookieHandler.cs" />
<Compile Include="NativeMessageHandler.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<ItemGroup>
Expand Down
53 changes: 53 additions & 0 deletions src/ModernHttpClient/NativeCookieHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace ModernHttpClient
{
public class NativeCookieHandler
{
static readonly char[] PortDelimiters = new[] { ',', '"' };

readonly HashSet<string> authorities = new HashSet<string>(StringComparer.Ordinal);
internal readonly CookieContainer CookieContainer = new CookieContainer();

public void SetCookies(IEnumerable<Cookie> cookies)
{
foreach (var cookie in cookies) {
var uriSb = new StringBuilder();
uriSb.Append(cookie.Secure ? "https://" : "http://");
uriSb.Append(cookie.Domain);
if (cookie.Port.Length > 0) {
var ports = cookie.Port.Split(PortDelimiters, StringSplitOptions.RemoveEmptyEntries);
if (ports.Length > 0) uriSb.Append(':').Append(ports[0]);
}
uriSb.Append(cookie.Path);

Uri uri;
if (!Uri.TryCreate(uriSb.ToString(), UriKind.Absolute, out uri)) {
throw new CookieException();
}

CookieContainer.Add(uri, cookie);
Add(uri);
}
}

public IReadOnlyList<Cookie> Cookies {
get {
var cookies = new CookieCollection();
foreach (var uri in authorities.Select(a => new Uri("https://" + a))) {
cookies.Add(CookieContainer.GetCookies(uri));
}
return cookies.Cast<Cookie>().ToList();
}
}

internal void Add(Uri uri)
{
authorities.Add(uri.Authority);
}
}
}
68 changes: 68 additions & 0 deletions src/ModernHttpClient/NativeMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace ModernHttpClient
{
public class NativeMessageHandler : HttpClientHandler
{
const string wrongVersion = "You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version";

readonly bool throwOnCaptiveNetwork;
readonly NativeCookieHandler cookieHandler;

/// <summary>
/// Initializes a new instance of the <see
/// cref="ModernHttpClient.Portable.NativeMessageHandler"/> class.
/// </summary>
public NativeMessageHandler(): this(false, false)
{
}

/// <summary>
/// Initializes a new instance of the <see
/// cref="ModernHttpClient.Portable.NativeMessageHandler"/> class.
/// </summary>
/// <param name="throwOnCaptiveNetwork">If set to <c>true</c> throw on
/// captive network (ie: a captive network is usually a wifi network
/// where an authentication html form is shown instead of the real
/// content).</param>
/// <param name="customSSLVerification">Enable custom SSL certificate
/// verification via ServicePointManager. Disabled by default for
/// performance reasons (i.e. the OS default certificate verification
/// will take place)</param>
/// <param name="cookieHandler">Enable native cookie handling.
/// </param>
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null) : base()
{
this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;
this.cookieHandler = cookieHandler;

UseCookies = cookieHandler != null;
if (cookieHandler != null) {
CookieContainer = cookieHandler.CookieContainer;
}
}

public bool DisableCaching { get; set; }

public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
{
throw new Exception(wrongVersion);
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var reqUri = request.RequestUri;
var response = await base.SendAsync(request, cancellationToken);
var newUri = response.RequestMessage.RequestUri;
if (throwOnCaptiveNetwork && reqUri.Host != newUri.Host) {
throw new CaptiveNetworkException(reqUri, newUri);
}
cookieHandler.Add(reqUri);
cookieHandler.Add(newUri);
return response;
}
}
}
2 changes: 1 addition & 1 deletion src/ModernHttpClient/iOS/NativeCookieHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void SetCookies(IEnumerable<Cookie> cookies)
}
}

public List<Cookie> Cookies {
public IReadOnlyList<Cookie> Cookies {
get {
return NSHttpCookieStorage.SharedStorage.Cookies
.Select(ToNetCookie)
Expand Down