comparison OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp @ 4226:7bd5eab3ba25

prototyping IWebDavBucket
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 04 Oct 2020 13:23:53 +0200
parents
children c8c0bbaaace3
comparison
equal deleted inserted replaced
4225:81f2d1484886 4226:7bd5eab3ba25
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "../PrecompiledHeaders.h"
24 #include "IWebDavBucket.h"
25
26 #include "../OrthancException.h"
27 #include "../Toolbox.h"
28
29
30 static boost::posix_time::ptime GetNow()
31 {
32 return boost::posix_time::second_clock::universal_time();
33 }
34
35
36 static std::string AddTrailingSlash(const std::string& s)
37 {
38 if (s.empty() ||
39 s[s.size() - 1] != '/')
40 {
41 return s + '/';
42 }
43 else
44 {
45 return s;
46 }
47 }
48
49
50 namespace Orthanc
51 {
52 void IWebDavBucket::Resource::SetNameInternal(const std::string& name)
53 {
54 if (name.find('/') != std::string::npos ||
55 name.find('\\') != std::string::npos ||
56 name.find('\0') != std::string::npos)
57 {
58 throw OrthancException(ErrorCode_ParameterOutOfRange,
59 "Bad resource name for WebDAV: " + name);
60 }
61
62 name_ = name;
63 }
64
65
66 IWebDavBucket::Resource::Resource() :
67 hasModificationTime_(false),
68 creationTime_(GetNow()),
69 modificationTime_(GetNow())
70 {
71 }
72
73
74 void IWebDavBucket::Resource::SetCreationTime(const boost::posix_time::ptime& t)
75 {
76 creationTime_ = t;
77
78 if (!hasModificationTime_)
79 {
80 modificationTime_ = t;
81 }
82 }
83
84
85 void IWebDavBucket::Resource::SetModificationTime(const boost::posix_time::ptime& t)
86 {
87 modificationTime_ = t;
88 hasModificationTime_ = true;
89 }
90
91
92 void IWebDavBucket::Resource::Format(pugi::xml_node& node,
93 const std::string& parentPath) const
94 {
95 node.set_name("D:response");
96
97 std::string s = AddTrailingSlash(parentPath) + GetName();
98 node.append_child("D:href").append_child(pugi::node_pcdata).set_value(s.c_str());
99
100 pugi::xml_node propstat = node.append_child("D:propstat");
101 propstat.append_child("D:status").append_child(pugi::node_pcdata).
102 set_value("HTTP/1.1 200 OK");
103
104 pugi::xml_node prop = propstat.append_child("D:prop");
105
106 // IMPORTANT: The "Z" suffix is mandatory on Windows >= 7
107 s = boost::posix_time::to_iso_extended_string(GetCreationTime()) + "Z";
108 prop.append_child("D:creationdate").append_child(pugi::node_pcdata).set_value(s.c_str());
109
110 s = boost::posix_time::to_iso_extended_string(GetModificationTime()) + "Z";
111 prop.append_child("D:getlastmodified").append_child(pugi::node_pcdata).set_value(s.c_str());
112
113 #if 0
114 prop.append_child("D:lockdiscovery");
115 pugi::xml_node lock = prop.append_child("D:supportedlock");
116
117 pugi::xml_node lockentry = lock.append_child("D:lockentry");
118 lockentry.append_child("D:lockscope").append_child("D:exclusive");
119 lockentry.append_child("D:locktype").append_child("D:write");
120
121 lockentry = lock.append_child("D:lockentry");
122 lockentry.append_child("D:lockscope").append_child("D:shared");
123 lockentry.append_child("D:locktype").append_child("D:write");
124 #endif
125 }
126
127
128 IWebDavBucket::File::File(const std::string& name) :
129 contentLength_(0),
130 mime_(MimeType_Binary)
131 {
132 if (name.empty())
133 {
134 throw OrthancException(ErrorCode_ParameterOutOfRange,
135 "Cannot use an empty filename in WebDAV");
136 }
137
138 SetNameInternal(name);
139 }
140
141
142 void IWebDavBucket::File::Format(pugi::xml_node& node,
143 const std::string& parentPath) const
144 {
145 Resource::Format(node, parentPath);
146
147 pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
148 prop.append_child("D:resourcetype");
149
150 std::string s = boost::lexical_cast<std::string>(contentLength_);
151 prop.append_child("D:getcontentlength").append_child(pugi::node_pcdata).set_value(s.c_str());
152
153 s = EnumerationToString(mime_);
154 prop.append_child("D:getcontenttype").append_child(pugi::node_pcdata).set_value(s.c_str());
155
156 prop.append_child("D:displayname").append_child(pugi::node_pcdata).set_value(GetName().c_str());
157 }
158
159
160 void IWebDavBucket::Folder::Format(pugi::xml_node& node,
161 const std::string& parentPath) const
162 {
163 Resource::Format(node, parentPath);
164
165 pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
166 prop.append_child("D:resourcetype").append_child("D:collection");
167
168 //prop.append_child("D:getcontenttype").append_child(pugi::node_pcdata).set_value("httpd/unix-directory");
169
170 std::string s = GetName();
171 prop.append_child("D:displayname").append_child(pugi::node_pcdata).set_value(s.c_str());
172 }
173
174
175 IWebDavBucket::Collection::~Collection()
176 {
177 for (std::list<Resource*>::iterator it = resources_.begin(); it != resources_.end(); ++it)
178 {
179 assert(*it != NULL);
180 delete(*it);
181 }
182 }
183
184
185 void IWebDavBucket::Collection::AddResource(Resource* resource) // Takes ownership
186 {
187 if (resource == NULL)
188 {
189 throw OrthancException(ErrorCode_NullPointer);
190 }
191 else
192 {
193 resources_.push_back(resource);
194 }
195 }
196
197
198 void IWebDavBucket::Collection::Format(std::string& target,
199 const std::string& parentPath) const
200 {
201 pugi::xml_document doc;
202
203 pugi::xml_node root = doc.append_child("D:multistatus");
204 root.append_attribute("xmlns:D").set_value("DAV:");
205
206 for (std::list<Resource*>::const_iterator
207 it = resources_.begin(); it != resources_.end(); ++it)
208 {
209 assert(*it != NULL);
210 pugi::xml_node n = root.append_child();
211 (*it)->Format(n, parentPath);
212 }
213
214 pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
215 decl.append_attribute("version").set_value("1.0");
216 decl.append_attribute("encoding").set_value("UTF-8");
217
218 Toolbox::XmlToString(target, doc);
219 }
220 }