Now closing web request immediatelly after executing a request that has no content.
[pithos-ms-client] / trunk / Pithos.Core / Agents / NetworkAgent.cs
index e609dd6..40f84d6 100644 (file)
@@ -478,8 +478,10 @@ namespace Pithos.Core.Agents
             var fileAgent = GetFileAgent(accountInfo);
             foreach (var trashObject in trashObjects)
             {
-                var relativePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
-                //and remove any matching objects from the list, adding them to the commonObjects list
+                var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
+                //HACK: Assume only the "pithos" container is used. Must find out what happens when
+                //deleting a file from a different container
+                var relativePath = Path.Combine("pithos", barePath);
                 fileAgent.Delete(relativePath);                                
             }
         }
@@ -747,12 +749,12 @@ namespace Pithos.Core.Agents
             try
             {
                 if (action == null)
-                    throw new ArgumentNullException("action");            
+                    throw new ArgumentNullException("action");
                 Contract.EndContractBlock();
 
-                var accountInfo=action.AccountInfo;
-            
-                var fileInfo=action.LocalFile;                        
+                var accountInfo = action.AccountInfo;
+
+                var fileInfo = action.LocalFile;
 
                 if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
                     return;
@@ -760,27 +762,27 @@ namespace Pithos.Core.Agents
                 var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
                 if (relativePath.StartsWith(FolderConstants.OthersFolder))
                 {
-                    var parts=relativePath.Split('\\');
+                    var parts = relativePath.Split('\\');
                     var accountName = parts[1];
                     var oldName = accountInfo.UserName;
                     var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
-                    var nameIndex=absoluteUri.IndexOf(oldName);
-                    var root=absoluteUri.Substring(0, nameIndex);
+                    var nameIndex = absoluteUri.IndexOf(oldName);
+                    var root = absoluteUri.Substring(0, nameIndex);
 
                     accountInfo = new AccountInfo
                     {
                         UserName = accountName,
                         AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
                         StorageUri = new Uri(root + accountName),
-                        BlockHash=accountInfo.BlockHash,
-                        BlockSize=accountInfo.BlockSize,
-                        Token=accountInfo.Token
+                        BlockHash = accountInfo.BlockHash,
+                        BlockSize = accountInfo.BlockSize,
+                        Token = accountInfo.Token
                     };
                 }
 
 
                 var fullFileName = fileInfo.FullName;
-                using(var gate=NetworkGate.Acquire(fullFileName,NetworkOperation.Uploading))
+                using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
                 {
                     //Abort if the file is already being uploaded or downloaded
                     if (gate.Failed)
@@ -791,14 +793,14 @@ namespace Pithos.Core.Agents
 
                     var client = new CloudFilesClient(accountInfo);
                     //Even if GetObjectInfo times out, we can proceed with the upload            
-                    var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);                
+                    var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
                     var cloudHash = info.Hash.ToLower();
 
                     var hash = action.LocalHash.Value;
                     var topHash = action.TopHash.Value;
 
                     //If the file hashes match, abort the upload
-                    if (hash == cloudHash  || topHash ==cloudHash)
+                    if (hash == cloudHash || topHash == cloudHash)
                     {
                         //but store any metadata changes 
                         this.StatusKeeper.StoreInfo(fullFileName, info);
@@ -806,7 +808,7 @@ namespace Pithos.Core.Agents
                         return;
                     }
 
-                    if (info.AllowedTo=="read")
+                    if (info.AllowedTo == "read")
                         return;
 
                     //Mark the file as modified while we upload it
@@ -818,9 +820,9 @@ namespace Pithos.Core.Agents
 
                     //First, calculate the tree hash
                     var treeHash = await Signature.CalculateTreeHashAsync(fileInfo.FullName, accountInfo.BlockSize,
-                        accountInfo.BlockHash);                
-                    
-                    await UploadWithHashMap(accountInfo,cloudFile,fileInfo,cloudFile.Name,treeHash);
+                        accountInfo.BlockHash);
+
+                    await UploadWithHashMap(accountInfo, cloudFile, fileInfo, cloudFile.Name, treeHash);
 
                     //If everything succeeds, change the file and overlay status to normal
                     this.StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
@@ -834,22 +836,40 @@ namespace Pithos.Core.Agents
                 var exc = ex.InnerException as WebException;
                 if (exc == null)
                     throw ex.InnerException;
-                var response = exc.Response as HttpWebResponse;
-                if (response == null)
-                    throw exc;
-                if (response.StatusCode == HttpStatusCode.Unauthorized)
-                {
-                    Log.Error("Not allowed to upload file", exc);
-                    var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
-                    StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
-                    StatusNotification.NotifyChange(message, TraceLevel.Warning);
+                if (HandleUploadWebException(action, exc)) 
                     return;
-                }
+                throw;
+            }
+            catch (WebException ex)
+            {
+                if (HandleUploadWebException(action, ex))
+                    return;
+                throw;
+            }
+            catch (Exception ex)
+            {
+                Log.Error("Unexpected error while uploading file", ex);
                 throw;
             }
 
         }
 
+        private bool HandleUploadWebException(CloudAction action, WebException exc)
+        {
+            var response = exc.Response as HttpWebResponse;
+            if (response == null)
+                throw exc;
+            if (response.StatusCode == HttpStatusCode.Unauthorized)
+            {
+                Log.Error("Not allowed to upload file", exc);
+                var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
+                StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
+                StatusNotification.NotifyChange(message, TraceLevel.Warning);
+                return true;
+            }
+            return false;
+        }
+
         public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
         {
             if (accountInfo == null)