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/Logger.cs b/src/Logger.cs new file mode 100644 index 0000000..d466a6f --- /dev/null +++ b/src/Logger.cs @@ -0,0 +1,71 @@ +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(e); + } + 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(e); + } + 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 diff --git a/src/Request.cs b/src/Request.cs index 7c3126f..429a52d 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 { @@ -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"; @@ -48,8 +55,10 @@ 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< HTTP.Request > completedCallback = null; + public Action< UnityHTTP.Request > completedCallback = null; Dictionary> headers = new Dictionary> (); static Dictionary etags = new Dictionary (); @@ -89,7 +98,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 ); @@ -184,13 +193,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 +224,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,25 +252,25 @@ 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 } } - 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 ) @@ -340,9 +339,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; } diff --git a/src/Response.cs b/src/Response.cs index 7a1a9d7..ac48e70 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 { @@ -210,7 +210,7 @@ public void ReadFromStream( Stream inputStream ) AddHeader( parts[0], parts[1] ); } - } else { + } else if (request.method.ToUpper() != "HEAD") { // Read Body int contentLength = 0; 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;