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 =>
{
{
//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
//
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;
}
}
+
public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
{
if (String.IsNullOrWhiteSpace(Token))
<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">
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;
}
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();
--- /dev/null
+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);
+ }
+ }
+ }
+}