Added WebExtensions.cs with methods for logging request and response contents
authorpkanavos <pkanavos@gmail.com>
Tue, 27 Mar 2012 14:33:17 +0000 (17:33 +0300)
committerpkanavos <pkanavos@gmail.com>
Tue, 27 Mar 2012 14:33:17 +0000 (17:33 +0300)
Modified the maximum error response size for HttpWebRequest

trunk/Pithos.Network/CloudFilesClient.cs
trunk/Pithos.Network/Pithos.Network.csproj
trunk/Pithos.Network/RestClient.cs
trunk/Pithos.Network/Signature.cs
trunk/Pithos.Network/WebExtensions.cs [new file with mode: 0644]

index 3601d62..d2993ce 100644 (file)
@@ -1035,7 +1035,8 @@ namespace Pithos.Network
             client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
             var jsonHash = hash.ToJson();
             var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
-            
+            if (Log.IsDebugEnabled)
+                Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
             return uploadTask.ContinueWith(t =>
             {
 
@@ -1059,9 +1060,8 @@ namespace Pithos.Network
                     {
                         //In case of 409 the missing parts will be in the response content                        
                         using (var stream = response.GetResponseStream())
-                        using(var reader=new StreamReader(stream))
+                        using(var reader=stream.GetLoggedReader(Log))
                         {
-                            Debug.Assert(stream.Position == 0);
                             //We used to have to cleanup the content before returning it because it contains
                             //error content after the list of hashes
                             //
@@ -1070,24 +1070,12 @@ namespace Pithos.Network
                             
                             var serializer = new JsonSerializer();                            
                             serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
-                            if (Log.IsDebugEnabled)
-                            {
-                                var body=reader.ReadToEnd();
-                                Log.DebugFormat("JSON response: {0}",body);
-                                using (var strReader = new StringReader(body))
-                                {
-                                    var hashes = (List<string>)serializer.Deserialize(strReader, typeof(List<string>));
-                                    return hashes;
-                                }
-                            }
-                            else
-                            {
-                                var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
-                                return hashes;
-                            }
+                            var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
+                            return hashes;
                         }                        
                     }                    
                     //Any other status code is unexpected and the exception should be rethrown
+                    Log.LogError(response);
                     throw ex;
                     
                 }
@@ -1099,6 +1087,7 @@ namespace Pithos.Network
 
         }
 
+        
         public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
         {
             if (String.IsNullOrWhiteSpace(Token))
index 513e2db..02abe61 100644 (file)
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Signature.cs" />
     <Compile Include="TreeHash.cs" />
+    <Compile Include="WebExtensions.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Libraries\Json40r2\Source\Src\Newtonsoft.Json\Newtonsoft.Json.csproj">
index 70b5740..36f6e0d 100644 (file)
@@ -96,7 +96,8 @@ namespace Pithos.Network
 
         public RestClient():base()
         {
-            
+            //The maximum error response must be large because missing server hashes are return as a Conflivt (409) error response
+            HttpWebRequest.DefaultMaximumErrorResponseLength = 16*1024*1024;
         }
 
        
index 634fea5..c8faac8 100644 (file)
@@ -79,7 +79,7 @@ namespace Pithos.Network
 
             string hash;
             using (var hasher = MD5.Create())
-            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
+            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, true))
             {
                 var hashBytes = hasher.ComputeHash(stream);
                 hash = hashBytes.ToHashString();
diff --git a/trunk/Pithos.Network/WebExtensions.cs b/trunk/Pithos.Network/WebExtensions.cs
new file mode 100644 (file)
index 0000000..7aed40b
--- /dev/null
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.IO;
+using log4net;
+
+namespace Pithos.Network
+{
+    public static class WebExtensions
+    {
+        public static string ReadToEnd(this HttpWebResponse response)
+        {
+            using (var stream = response.GetResponseStream())
+            {
+                if (stream == null)
+                    return null;
+                using (var reader = new StreamReader(stream))
+                {
+                    var body = reader.ReadToEnd();
+                    return body;
+                }
+            }
+        }
+    
+        public static void LogError(this ILog log,HttpWebResponse response)
+        {
+            if (log.IsDebugEnabled)
+            {
+                if (response != null)
+                {
+                    var body = response.ReadToEnd();
+                    log.ErrorFormat("Headers:\n{0}\nBody:{1}", response.Headers,body);
+                }
+            }
+        }
+
+        public static TextReader GetLoggedReader(this Stream stream,ILog log)
+        {
+            TextReader reader = new StreamReader(stream);
+            if (!log.IsDebugEnabled)
+                return reader;
+            
+            using (reader)
+            {
+                var body = reader.ReadToEnd();
+                log.DebugFormat("JSON response: {0}", body);
+                return new StringReader(body);
+            }
+        }
+    }
+}