comparison OrthancFramework/Sources/HttpServer/FilesystemHttpHandler.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/HttpServer/FilesystemHttpHandler.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "FilesystemHttpHandler.h"
36
37 #include "../OrthancException.h"
38 #include "../SystemToolbox.h"
39 #include "FilesystemHttpSender.h"
40
41 #include <boost/filesystem.hpp>
42
43
44 namespace Orthanc
45 {
46 struct FilesystemHttpHandler::PImpl
47 {
48 UriComponents baseUri_;
49 boost::filesystem::path root_;
50 };
51
52
53
54 static void OutputDirectoryContent(HttpOutput& output,
55 const IHttpHandler::Arguments& headers,
56 const UriComponents& uri,
57 const boost::filesystem::path& p)
58 {
59 namespace fs = boost::filesystem;
60
61 std::string s;
62 s += "<html>";
63 s += " <body>";
64 s += " <h1>Subdirectories</h1>";
65 s += " <ul>";
66
67 if (uri.size() > 0)
68 {
69 std::string h = Toolbox::FlattenUri(uri) + "/..";
70 s += "<li><a href=\"" + h + "\">..</a></li>";
71 }
72
73 fs::directory_iterator end;
74 for (fs::directory_iterator it(p) ; it != end; ++it)
75 {
76 #if BOOST_HAS_FILESYSTEM_V3 == 1
77 std::string f = it->path().filename().string();
78 #else
79 std::string f = it->path().filename();
80 #endif
81
82 std::string h = Toolbox::FlattenUri(uri) + "/" + f;
83 if (fs::is_directory(it->status()))
84 s += "<li><a href=\"" + h + "\">" + f + "</a></li>";
85 }
86
87 s += " </ul>";
88 s += " <h1>Files</h1>";
89 s += " <ul>";
90
91 for (fs::directory_iterator it(p) ; it != end; ++it)
92 {
93 #if BOOST_HAS_FILESYSTEM_V3 == 1
94 std::string f = it->path().filename().string();
95 #else
96 std::string f = it->path().filename();
97 #endif
98
99 std::string h = Toolbox::FlattenUri(uri) + "/" + f;
100 if (SystemToolbox::IsRegularFile(it->path().string()))
101 {
102 s += "<li><a href=\"" + h + "\">" + f + "</a></li>";
103 }
104 }
105
106 s += " </ul>";
107 s += " </body>";
108 s += "</html>";
109
110 output.SetContentType(MimeType_Html);
111 output.Answer(s);
112 }
113
114
115 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri,
116 const std::string& root) : pimpl_(new PImpl)
117 {
118 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri);
119 pimpl_->root_ = root;
120 listDirectoryContent_ = false;
121
122 namespace fs = boost::filesystem;
123 if (!fs::exists(pimpl_->root_) ||
124 !fs::is_directory(pimpl_->root_))
125 {
126 throw OrthancException(ErrorCode_DirectoryExpected);
127 }
128 }
129
130
131 bool FilesystemHttpHandler::Handle(
132 HttpOutput& output,
133 RequestOrigin /*origin*/,
134 const char* /*remoteIp*/,
135 const char* /*username*/,
136 HttpMethod method,
137 const UriComponents& uri,
138 const Arguments& headers,
139 const GetArguments& arguments,
140 const void* /*bodyData*/,
141 size_t /*bodySize*/)
142 {
143 if (!Toolbox::IsChildUri(pimpl_->baseUri_, uri))
144 {
145 // This URI is not served by this handler
146 return false;
147 }
148
149 if (method != HttpMethod_Get)
150 {
151 output.SendMethodNotAllowed("GET");
152 return true;
153 }
154
155 namespace fs = boost::filesystem;
156
157 fs::path p = pimpl_->root_;
158 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++)
159 {
160 p /= uri[i];
161 }
162
163 if (SystemToolbox::IsRegularFile(p.string()))
164 {
165 FilesystemHttpSender sender(p);
166 sender.SetContentType(SystemToolbox::AutodetectMimeType(p.string()));
167 output.Answer(sender); // TODO COMPRESSION
168 }
169 else if (listDirectoryContent_ &&
170 fs::exists(p) &&
171 fs::is_directory(p))
172 {
173 OutputDirectoryContent(output, headers, uri, p);
174 }
175 else
176 {
177 output.SendStatus(HttpStatus_404_NotFound);
178 }
179
180 return true;
181 }
182 }