Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@


namespace Mapbox.Experimental.Platform
{

using System;
using System.Net;
using System.Net.Http;
using System.Threading;


public interface IMapboxHttpClient : IDisposable
{
HttpClient HttpClient { get; }
string BaseUrl { get; set; }
IMapboxHttpRequest Request(string url);

bool IsDisposed { get; }
}


public class MapboxHttpClient : IMapboxHttpClient, IDisposable
{

private readonly Lazy<HttpClient> _client;

public MapboxHttpClient(string baseUrl = null)
{

UnityEngine.Debug.LogWarning("MAPBOX_EXPERIMENTAL TODO: add proxy - settings??");

BaseUrl = baseUrl;

// DIRTY HACK! to get around missing Mono certificates:
// System.Net.WebException: Error: SecureChannelFailure (The authentication or decryption has failed.) ---> System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
// with newer Mono versions (+3.12.0) that *shouldn't* be necessary anymore:
// http://www.mono-project.com/docs/about-mono/releases/3.12.0/#cert-sync
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
var cookies = new CookieContainer();

_client = new Lazy<HttpClient>(() =>
new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
//AutomaticRedirection = true, // not available
CookieContainer = cookies,

//not implemented??
//MaxConnectionsPerServer = 2,

//UseProxy = true,
//Proxy = new WebProxy("192.168.1.125", 8888)
}
)
{
Timeout = Timeout.InfiniteTimeSpan
});

//_client.DefaultRequestHeaders.Add("Accept", "text/html, application/json");
//_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
//_client.DefaultRequestHeaders.Add("Accept", "text/html, application/json");
//_client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");

}

public string BaseUrl { get; set; }


public HttpClient HttpClient => _client.Value;

public IMapboxHttpRequest Request(string url)
{
return new MapboxHttpRequest(url);
}


public bool IsDisposed { get; private set; }


public virtual void Dispose()
{
if (IsDisposed) { return; }

//todo for httpmessagehandler
if (_client.IsValueCreated) { _client.Value.Dispose(); }

IsDisposed = true;
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


namespace Mapbox.Experimental.Platform
{

using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http;
using System.Threading;

public interface IMapboxHttpClientFactory : IDisposable
{
IMapboxHttpClient Get(string url);
}


public class MapboxHttpClientFactory : IMapboxHttpClientFactory
{

private readonly ConcurrentDictionary<string, IMapboxHttpClient> _clients = new ConcurrentDictionary<string, IMapboxHttpClient>();


public IMapboxHttpClient Get(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
throw new ArgumentNullException("url");
}

return
_clients.AddOrUpdate(
getCacheKey(url)
, u => create(u)
, (u, client) => client.IsDisposed ? create(u) : client
);
}


private string getCacheKey(string url) => new Uri(url).Host;

private IMapboxHttpClient create(string url) => new MapboxHttpClient();


public void Dispose()
{
foreach (var kv in _clients)
{
if (!kv.Value.IsDisposed)
{
kv.Value.Dispose();
}
}
_clients.Clear();
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@


namespace Mapbox.Experimental.Platform
{

using Mapbox.Unity;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public interface IMapboxHttpRequest
{
IMapboxHttpClient Client { get; set; }
string Url { get; set; }
HttpMethod Verb { get; }
Task<MapboxHttpResponse> SendAsync(
object id
, HttpMethod verb
, HttpContent content = null
, Dictionary<string, string> headers = null
, CancellationToken? cancellationToken = null
, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead
);
}

public class MapboxHttpRequest : IMapboxHttpRequest
{


private static IMapboxHttpClient _client = null;
private HttpMethod _verb;

public MapboxHttpRequest(string url)
{
Url = url;
}


//_client.DefaultRequestHeaders.Add("Accept", "text/html, application/json");
//_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
//_client.DefaultRequestHeaders.Add("Accept", "text/html, application/json");
//_client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");


public IMapboxHttpClient Client
{
get
{
return (null != _client) ? Client :
(!string.IsNullOrWhiteSpace(Url)) ? MapboxAccess.Instance.HttpClientFactory.Get(Url) :
null;
}
set { _client = value; }
}


public string Url { get; set; }


public HttpMethod Verb { get { return _verb; } }
public async Task<MapboxHttpResponse> SendAsync(
object id
, HttpMethod verb
, HttpContent content = null
, Dictionary<string, string> headers = null
, CancellationToken? cancellationToken = null
, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead
)
{

_verb = verb;

string accessTokenQuery = $"&access_token={MapboxAccess.Instance.Configuration.AccessToken}";
UriBuilder uriBuilder = new UriBuilder(Url);
if (!string.IsNullOrWhiteSpace(uriBuilder.Query) && uriBuilder.Query.Length > 1)
{
uriBuilder.Query = $"{uriBuilder.Query.Substring(1)}&{accessTokenQuery}";
}
else
{
uriBuilder.Query = accessTokenQuery;
}
HttpRequestMessage request = new HttpRequestMessage
{
Method = verb,
Content = content,
RequestUri = uriBuilder.Uri
};

if (null != headers)
{
foreach (var hdr in headers)
{
request.Headers.Add(hdr.Key, hdr.Value);
}
}


var userToken = cancellationToken ?? CancellationToken.None;

var cts = CancellationTokenSource.CreateLinkedTokenSource(userToken);
cts.CancelAfter(MapboxAccess.Instance.Configuration.DefaultTimeout * 1000);
var token = cts.Token;

try
{
using (HttpResponseMessage resp = await Client.HttpClient.SendAsync(request, completionOption, token).ConfigureAwait(false))
{
return await MapboxHttpResponse.FromWebResponse(this, resp, null);
}
}
catch (Exception ex)
{
UnityEngine.Debug.LogError(ex);
if (ex is OperationCanceledException && !token.IsCancellationRequested)
{
return await MapboxHttpResponse.FromWebResponse(this, null, new TimeoutException());
}
return await MapboxHttpResponse.FromWebResponse(this, null, ex);
}

}







}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading