From fd501eaf8afe7a21eebec90d31a0439e435d94e9 Mon Sep 17 00:00:00 2001 From: Alejandro Huerta <1011415huerta@gmail.com> Date: Tue, 14 Jun 2016 13:29:13 -0700 Subject: [PATCH 1/9] Change namespace to prevent name conflicts --- src/CookieJar.cs | 2 +- src/DiskCache.cs | 5 ++--- src/FormDataStream.cs | 2 +- src/Request.cs | 6 +++--- src/Response.cs | 2 +- src/ResponseCallbackDispatcher.cs | 4 ++-- src/StreamedWWWForm.cs | 2 +- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/CookieJar.cs b/src/CookieJar.cs index 8d6cc7a..d226e5c 100644 --- a/src/CookieJar.cs +++ b/src/CookieJar.cs @@ -4,7 +4,7 @@ // Based on node-cookiejar (https://github.com/bmeck/node-cookiejar) -namespace HTTP +namespace UnityHTTP { public class CookieAccessInfo { diff --git a/src/DiskCache.cs b/src/DiskCache.cs index 45ac010..f5357e3 100644 --- a/src/DiskCache.cs +++ b/src/DiskCache.cs @@ -2,9 +2,8 @@ using System.Collections; using System.IO; using System; -using HTTP; -namespace HTTP +namespace UnityHTTP { public class DiskCacheOperation { @@ -91,7 +90,7 @@ public DiskCacheOperation Fetch (Request request) IEnumerator DownloadAndSave (Request request, string filename, DiskCacheOperation handle) { var useCachedVersion = File.Exists(filename); - Action< HTTP.Request > callback = request.completedCallback; + Action< UnityHTTP.Request > callback = request.completedCallback; request.Send(); // will clear the completedCallback while (!request.isDone) yield return new WaitForEndOfFrame (); diff --git a/src/FormDataStream.cs b/src/FormDataStream.cs index 5e11922..02b44ca 100644 --- a/src/FormDataStream.cs +++ b/src/FormDataStream.cs @@ -2,7 +2,7 @@ using System.IO; using System.Text; using System.Collections.Generic; -namespace HTTP { +namespace UnityHTTP { public class FormPart { byte[] header; diff --git a/src/Request.cs b/src/Request.cs index 7c3126f..2d86473 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -11,7 +11,7 @@ using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; -namespace HTTP +namespace UnityHTTP { public class HTTPException : Exception { @@ -49,7 +49,7 @@ public class Request public bool synchronous = false; public int bufferSize = 4 * 1024; - public Action< HTTP.Request > completedCallback = null; + public Action< UnityHTTP.Request > completedCallback = null; Dictionary> headers = new Dictionary> (); static Dictionary etags = new Dictionary (); @@ -271,7 +271,7 @@ private void GetResponse() { } } - public virtual void Send( Action< HTTP.Request > callback = null) + public virtual void Send( Action< UnityHTTP.Request > callback = null) { if (!synchronous && callback != null && ResponseCallbackDispatcher.Singleton == null ) diff --git a/src/Response.cs b/src/Response.cs index 7a1a9d7..22d51f1 100644 --- a/src/Response.cs +++ b/src/Response.cs @@ -6,7 +6,7 @@ using System.Globalization; using Ionic.Zlib; -namespace HTTP +namespace UnityHTTP { public class Response { diff --git a/src/ResponseCallbackDispatcher.cs b/src/ResponseCallbackDispatcher.cs index 2f857aa..d49a31e 100644 --- a/src/ResponseCallbackDispatcher.cs +++ b/src/ResponseCallbackDispatcher.cs @@ -2,7 +2,7 @@ using System; using System.Collections; -namespace HTTP +namespace UnityHTTP { public class ResponseCallbackDispatcher : MonoBehaviour { @@ -43,7 +43,7 @@ public void Update() { while( requests.Count > 0 ) { - HTTP.Request request = (Request)requests.Dequeue(); + UnityHTTP.Request request = (Request)requests.Dequeue(); request.completedCallback( request ); } } diff --git a/src/StreamedWWWForm.cs b/src/StreamedWWWForm.cs index f8ed954..0510e48 100644 --- a/src/StreamedWWWForm.cs +++ b/src/StreamedWWWForm.cs @@ -3,7 +3,7 @@ using System.Text; using System.Collections; -namespace HTTP +namespace UnityHTTP { public class StreamedWWWForm { string boundary; From bcee48fa41d53d8ddd4684cc8bdb013a85857246 Mon Sep 17 00:00:00 2001 From: andzdroid Date: Wed, 29 Jun 2016 16:03:41 +0100 Subject: [PATCH 2/9] Don't read body in HEAD requests --- src/Response.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Response.cs b/src/Response.cs index 22d51f1..f57b342 100644 --- a/src/Response.cs +++ b/src/Response.cs @@ -210,7 +210,7 @@ public void ReadFromStream( Stream inputStream ) AddHeader( parts[0], parts[1] ); } - } else { + } else if (request.method != "head") { // Read Body int contentLength = 0; From 3c8db7b606a9d13acb9c86bc4893a001ae6332a5 Mon Sep 17 00:00:00 2001 From: andzdroid Date: Wed, 29 Jun 2016 16:15:51 +0100 Subject: [PATCH 3/9] Fix case-sensitive request method check --- src/Response.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Response.cs b/src/Response.cs index f57b342..ac48e70 100644 --- a/src/Response.cs +++ b/src/Response.cs @@ -210,7 +210,7 @@ public void ReadFromStream( Stream inputStream ) AddHeader( parts[0], parts[1] ); } - } else if (request.method != "head") { + } else if (request.method.ToUpper() != "HEAD") { // Read Body int contentLength = 0; From f623acf0f1e6565c581e7f3c0dd51920bc2520ce Mon Sep 17 00:00:00 2001 From: Dmitry Radkovskiy Date: Wed, 19 Jul 2017 20:04:11 +0300 Subject: [PATCH 4/9] Fix Unity 2017 compiler define --- src/Request.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request.cs b/src/Request.cs index 2d86473..e61bdfa 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -89,7 +89,7 @@ public Request( string method, string uri, WWWForm form ) this.method = method; this.uri = new Uri (uri); this.byteStream = new MemoryStream(form.data); -#if UNITY_5 +#if UNITY_5 || UNITY_5_3_OR_NEWER foreach ( var entry in form.headers ) { this.AddHeader( entry.Key, entry.Value ); From bf07dfaa34b9f1ccdb548298e1d44a62db673e17 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:03:33 +0300 Subject: [PATCH 5/9] Added ILogger, ConsoleLogger, UnityLogger --- src/Logger.cs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Logger.cs diff --git a/src/Logger.cs b/src/Logger.cs new file mode 100644 index 0000000..cdf9f0e --- /dev/null +++ b/src/Logger.cs @@ -0,0 +1,52 @@ +using System; + +namespace UnityHTTP +{ + public interface ILogger + { + void Log(string msg); + void LogError(string msg); + void LogException(Exception e); + void LogWarning(string msg); + } +#if UNITY_EDITOR + public class UnityLogger : ILogger + { + public void Log(string msg) + { + UnityEngine.Debug.Log(msg); + } + public void LogError(string msg) + { + UnityEngine.Debug.LogError(msg); + } + public void LogException(Exception e) + { + UnityEngine.Debug.LogException(msg); + } + public void LogWarning(string msg) + { + UnityEngine.Debug.LogWarning(msg); + } + } +#endif + public class ConsoleLogger : ILogger + { + public void Log(string msg) + { + Console.WriteLine(msg); + } + public void LogError(string msg) + { + Console.WriteLine(msg); + } + public void LogException(Exception e) + { + Console.WriteLine(msg); + } + public void LogWarning(string msg) + { + Console.WriteLine(msg); + } + } +} \ No newline at end of file From 5663f4a0e5b8d17a22e74291bb8d776fc40ba25a Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:04:03 +0300 Subject: [PATCH 6/9] refactored Request to use UnityLogger/ConsoleLogger where appropriate --- src/Request.cs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Request.cs b/src/Request.cs index e61bdfa..38970b1 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -30,7 +30,14 @@ public class Request public static bool LogAllRequests = false; public static bool VerboseLogging = false; public static string unityVersion = Application.unityVersion; - public static string operatingSystem = SystemInfo.operatingSystem; + public static string operatingSystem = SystemInfo.operatingSystem; + public static ILogger logger = +#if !UNITY_EDITOR + new ConsoleLogger() +#else + new UnityLogger() +#endif + ; public CookieJar cookieJar = CookieJar.Instance; public string method = "GET"; @@ -184,13 +191,8 @@ private void GetResponse() { var ssl = ostream as SslStream; ssl.AuthenticateAsClient (uri.Host); } catch (Exception e) { -#if !UNITY_EDITOR - Console.WriteLine ("SSL authentication failed."); - Console.WriteLine (e); -#else - Debug.LogError ("SSL authentication failed."); - Debug.LogException(e); -#endif + logger.LogError ("SSL authentication failed."); + logger.LogException(e); return; } } @@ -220,13 +222,8 @@ private void GetResponse() { } } catch (Exception e) { -#if !UNITY_EDITOR - Console.WriteLine ("Unhandled Exception, aborting request."); - Console.WriteLine (e); -#else - Debug.LogError("Unhandled Exception, aborting request."); - Debug.LogException(e); -#endif + logger.LogError("Unhandled Exception, aborting request."); + logger.LogException(e); exception = e; response = null; } @@ -253,19 +250,19 @@ private void GetResponse() { if ( LogAllRequests ) { #if !UNITY_EDITOR - System.Console.WriteLine("NET: " + InfoString( VerboseLogging )); + logger.Log("NET: " + InfoString( VerboseLogging )); #else if ( response != null && response.status >= 200 && response.status < 300 ) { - Debug.Log( InfoString( VerboseLogging ) ); + logger.Log( InfoString( VerboseLogging ) ); } else if ( response != null && response.status >= 400 ) { - Debug.LogError( InfoString( VerboseLogging ) ); + logger.LogError( InfoString( VerboseLogging ) ); } else { - Debug.LogWarning( InfoString( VerboseLogging ) ); + logger.LogWarning( InfoString( VerboseLogging ) ); } #endif } @@ -340,9 +337,9 @@ public string Text { public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { #if !UNITY_EDITOR - System.Console.WriteLine( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); + logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); #else - Debug.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); + logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); #endif return true; } From 51b0b3da5d348ab783aaeffac2844981aa25dc60 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:05:59 +0300 Subject: [PATCH 7/9] added loggers for every request --- src/Request.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Request.cs b/src/Request.cs index 38970b1..78a9478 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -55,6 +55,8 @@ public class Request public long responseTime = 0; // in milliseconds public bool synchronous = false; public int bufferSize = 4 * 1024; + + public ILogger logger = Request.logger; public Action< UnityHTTP.Request > completedCallback = null; From 2ba38524bdc5cd46134389f63d4e6361a8717a25 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:19:25 +0300 Subject: [PATCH 8/9] compiler errors fixes --- src/Logger.cs | 4 ++-- src/Request.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Logger.cs b/src/Logger.cs index cdf9f0e..193096f 100644 --- a/src/Logger.cs +++ b/src/Logger.cs @@ -22,7 +22,7 @@ public void LogError(string msg) } public void LogException(Exception e) { - UnityEngine.Debug.LogException(msg); + UnityEngine.Debug.LogException(e); } public void LogWarning(string msg) { @@ -42,7 +42,7 @@ public void LogError(string msg) } public void LogException(Exception e) { - Console.WriteLine(msg); + Console.WriteLine(e); } public void LogWarning(string msg) { diff --git a/src/Request.cs b/src/Request.cs index 78a9478..429a52d 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -31,7 +31,7 @@ public class Request public static bool VerboseLogging = false; public static string unityVersion = Application.unityVersion; public static string operatingSystem = SystemInfo.operatingSystem; - public static ILogger logger = + public static ILogger Logger = #if !UNITY_EDITOR new ConsoleLogger() #else @@ -56,7 +56,7 @@ public class Request public bool synchronous = false; public int bufferSize = 4 * 1024; - public ILogger logger = Request.logger; + public ILogger logger = Request.Logger; public Action< UnityHTTP.Request > completedCallback = null; @@ -339,9 +339,9 @@ public string Text { public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { #if !UNITY_EDITOR - logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); + Logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); #else - logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); + Logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); #endif return true; } From f41bad5587ab6fdb7703c1c60e4bd5490f638665 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:21:30 +0300 Subject: [PATCH 9/9] added DiscardLogger --- src/Logger.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Logger.cs b/src/Logger.cs index 193096f..d466a6f 100644 --- a/src/Logger.cs +++ b/src/Logger.cs @@ -49,4 +49,23 @@ public void LogWarning(string msg) Console.WriteLine(msg); } } + public class DiscardLogger : ILogger + { + public void Log(string msg) + { + // discard logs + } + public void LogError(string msg) + { + // discard logs + } + public void LogException(Exception e) + { + // discard logs + } + public void LogWarning(string msg) + { + // discard logs + } + } } \ No newline at end of file