changeset 4246:6c3721ff284c

simplification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 13 Oct 2020 10:39:47 +0200
parents c70df925151e
children 112951171852
files OrthancFramework/Sources/HttpServer/HttpServer.cpp OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp OrthancFramework/Sources/HttpServer/IWebDavBucket.h OrthancServer/Sources/OrthancWebDav.cpp
diffstat 4 files changed, 73 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Sat Oct 10 11:23:11 2020 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Tue Oct 13 10:39:47 2020 +0200
@@ -821,19 +821,17 @@
             MimeType mime;
             std::string content;
             boost::posix_time::ptime modificationTime = boost::posix_time::second_clock::universal_time();
-            
+
             if (bucket->second->IsExistingFolder(path))
             {
               if (depth == 0)
               {
                 IWebDavBucket::Collection c;
-                c.AddResource(new IWebDavBucket::Folder(""));
-                c.Format(answer, uri, true /* include display name */);
+                c.Format(answer, uri);
               }
               else if (depth == 1)
               {
                 IWebDavBucket::Collection c;
-                c.AddResource(new IWebDavBucket::Folder(""));  // Necessary for empty folders
               
                 if (!bucket->second->ListCollection(c, path))
                 {
@@ -841,7 +839,7 @@
                   return true;
                 }
                 
-                c.Format(answer, uri, true /* include display name */);
+                c.Format(answer, uri);
               }
               else
               {
@@ -869,12 +867,8 @@
                   throw OrthancException(ErrorCode_InternalError);
                 }
 
-                // Nautilus doesn't work on DELETE, if "D:displayname"
-                // is included in the multi-status answer of PROPFIND at depth 1
-                const bool includeDisplayName = (depth == 0);
-                
                 p.resize(p.size() - 1);
-                c.Format(answer, Toolbox::FlattenUri(p), includeDisplayName);
+                c.Format(answer, Toolbox::FlattenUri(p));
               }
               else
               {
--- a/OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp	Sat Oct 10 11:23:11 2020 +0200
+++ b/OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp	Tue Oct 13 10:39:47 2020 +0200
@@ -74,46 +74,61 @@
 
   void IWebDavBucket::Resource::SetCreationTime(const boost::posix_time::ptime& t)
   {
-    creationTime_ = t;
-
-    if (!hasModificationTime_)
+    if (t.is_special())
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange, "Not a valid date-time");
+    }
+    else
     {
-      modificationTime_ = t;
+      creationTime_ = t;
+      
+      if (!hasModificationTime_)
+      {
+        modificationTime_ = t;
+      }
     }
   }
 
 
   void IWebDavBucket::Resource::SetModificationTime(const boost::posix_time::ptime& t)
   {
-    modificationTime_ = t;
-    hasModificationTime_ = true;
+    if (t.is_special())
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange, "Not a valid date-time");
+    }
+    else
+    {
+      modificationTime_ = t;
+      hasModificationTime_ = true;
+    }
   }
 
 
-  void IWebDavBucket::Resource::Format(pugi::xml_node& node,
-                                       const std::string& parentPath,
-                                       bool includeDisplayName) const
+  static void FormatInternal(pugi::xml_node& node,
+                             const std::string& href,
+                             const std::string& displayName,
+                             const boost::posix_time::ptime& creationTime,
+                             const boost::posix_time::ptime& modificationTime)
   {
     node.set_name("D:response");
 
-    std::string s;
-    Toolbox::UriEncode(s, AddTrailingSlash(parentPath) + GetName());
-    node.append_child("D:href").append_child(pugi::node_pcdata).set_value(s.c_str());
+    node.append_child("D:href").append_child(pugi::node_pcdata).set_value(href.c_str());
 
     pugi::xml_node propstat = node.append_child("D:propstat");
 
     static const HttpStatus status = HttpStatus_200_Ok;
-    s = ("HTTP/1.1 " + boost::lexical_cast<std::string>(status) + " " +
-         std::string(EnumerationToString(status)));
+    std::string s = ("HTTP/1.1 " + boost::lexical_cast<std::string>(status) + " " +
+                     std::string(EnumerationToString(status)));
     propstat.append_child("D:status").append_child(pugi::node_pcdata).set_value(s.c_str());
 
     pugi::xml_node prop = propstat.append_child("D:prop");
+    prop.append_child("D:displayname").append_child(pugi::node_pcdata).set_value(displayName.c_str());
 
     // IMPORTANT: The "Z" suffix is mandatory on Windows >= 7 (it indicates UTC)
-    s = boost::posix_time::to_iso_extended_string(GetCreationTime()) + "Z";
+    s = boost::posix_time::to_iso_extended_string(creationTime) + "Z";
     prop.append_child("D:creationdate").append_child(pugi::node_pcdata).set_value(s.c_str());
 
-    s = boost::posix_time::to_iso_extended_string(GetModificationTime()) + "Z";
+    s = boost::posix_time::to_iso_extended_string(modificationTime) + "Z";
     prop.append_child("D:getlastmodified").append_child(pugi::node_pcdata).set_value(s.c_str());
 
 #if 0
@@ -136,7 +151,7 @@
 #endif
   }
 
-
+  
   IWebDavBucket::File::File(const std::string& name) :
     contentLength_(0),
     mime_(MimeType_Binary)
@@ -152,10 +167,11 @@
 
   
   void IWebDavBucket::File::Format(pugi::xml_node& node,
-                                   const std::string& parentPath,
-                                   bool includeDisplayName) const
+                                   const std::string& parentPath) const
   {
-    Resource::Format(node, parentPath, includeDisplayName);
+    std::string href;
+    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetName());
+    FormatInternal(node, href, GetName(), GetCreationTime(), GetModificationTime());
 
     pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
     prop.append_child("D:resourcetype");
@@ -165,23 +181,20 @@
 
     s = EnumerationToString(mime_);
     prop.append_child("D:getcontenttype").append_child(pugi::node_pcdata).set_value(s.c_str());
-
-    prop.append_child("D:displayname").append_child(pugi::node_pcdata).set_value(GetName().c_str());
   }
 
 
   void IWebDavBucket::Folder::Format(pugi::xml_node& node,
-                                     const std::string& parentPath,
-                                     bool includeDisplayName) const
+                                     const std::string& parentPath) const
   {
-    Resource::Format(node, parentPath, includeDisplayName);
+    std::string href;
+    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetName());
+    FormatInternal(node, href, GetName(), GetCreationTime(), GetModificationTime());
         
     pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
     prop.append_child("D:resourcetype").append_child("D:collection");
 
     //prop.append_child("D:getcontenttype").append_child(pugi::node_pcdata).set_value("httpd/unix-directory");
-
-    prop.append_child("D:displayname").append_child(pugi::node_pcdata).set_value(GetName().c_str());
   }
 
 
@@ -209,20 +222,41 @@
 
 
   void IWebDavBucket::Collection::Format(std::string& target,
-                                         const std::string& parentPath,
-                                         bool includeDisplayName) const
+                                         const std::string& parentPath) const
   {
     pugi::xml_document doc;
 
     pugi::xml_node root = doc.append_child("D:multistatus");
     root.append_attribute("xmlns:D").set_value("DAV:");
 
+    {
+      pugi::xml_node self = root.append_child();
+
+      std::string folder;
+      size_t lastSlash = parentPath.rfind('/');
+      if (lastSlash == std::string::npos)
+      {
+        folder = parentPath;
+      }
+      else
+      {
+        folder = parentPath.substr(lastSlash + 1);
+      }
+      
+      std::string href;
+      Toolbox::UriEncode(href, AddTrailingSlash(parentPath));
+      FormatInternal(self, href, folder, GetNow(), GetNow());
+
+      pugi::xml_node prop = self.first_element_by_path("D:propstat/D:prop");
+      prop.append_child("D:resourcetype").append_child("D:collection");
+    }
+
     for (std::list<Resource*>::const_iterator
            it = resources_.begin(); it != resources_.end(); ++it)
     {
       assert(*it != NULL);
       pugi::xml_node n = root.append_child();
-      (*it)->Format(n, parentPath, includeDisplayName);
+      (*it)->Format(n, parentPath);
     }
 
     pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
--- a/OrthancFramework/Sources/HttpServer/IWebDavBucket.h	Sat Oct 10 11:23:11 2020 +0200
+++ b/OrthancFramework/Sources/HttpServer/IWebDavBucket.h	Tue Oct 13 10:39:47 2020 +0200
@@ -84,8 +84,7 @@
       }
 
       virtual void Format(pugi::xml_node& node,
-                          const std::string& parentPath,
-                          bool includeDisplayName) const;
+                          const std::string& parentPath) const = 0;
     };
 
 
@@ -121,8 +120,7 @@
       void SetCreated(bool created);
 
       virtual void Format(pugi::xml_node& node,
-                          const std::string& parentPath,
-                          bool includeDisplayName) const ORTHANC_OVERRIDE;
+                          const std::string& parentPath) const ORTHANC_OVERRIDE;
     };
 
 
@@ -135,8 +133,7 @@
       }
       
       virtual void Format(pugi::xml_node& node,
-                          const std::string& parentPath,
-                          bool includeDisplayName) const ORTHANC_OVERRIDE;
+                          const std::string& parentPath) const ORTHANC_OVERRIDE;
     };
 
 
@@ -156,8 +153,7 @@
       void AddResource(Resource* resource);  // Takes ownership
 
       void Format(std::string& target,
-                  const std::string& parentPath,
-                  bool includeDisplayName) const;
+                  const std::string& parentPath) const;
     };
 
 
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Sat Oct 10 11:23:11 2020 +0200
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Tue Oct 13 10:39:47 2020 +0200
@@ -1169,7 +1169,7 @@
   {
     MimeType mime;
     std::string content;
-    boost::posix_time::ptime modification;
+    boost::posix_time::ptime modification;  // Unused, let the date be set to "GetNow()"
 
     UriComponents p = path;
     p.push_back(filename);
@@ -1179,7 +1179,6 @@
       std::unique_ptr<File> f(new File(filename));
       f->SetMimeType(mime);
       f->SetContentLength(content.size());
-      f->SetCreationTime(modification);
       collection.AddResource(f.release());
     }
   }