Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 833:30efdc8fac70
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 02 Jun 2014 12:17:47 +0200 |
parents | a811bdf8b8eb |
children | 7e8cde5905fd |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
0 | 34 #include "FilesystemHttpHandler.h" |
35 | |
59 | 36 #include "../OrthancException.h" |
217 | 37 #include "FilesystemHttpSender.h" |
0 | 38 |
39 #include <boost/filesystem.hpp> | |
40 | |
41 | |
59 | 42 namespace Orthanc |
0 | 43 { |
44 struct FilesystemHttpHandler::PImpl | |
45 { | |
46 UriComponents baseUri_; | |
47 boost::filesystem::path root_; | |
48 }; | |
49 | |
50 | |
51 | |
52 static void OutputDirectoryContent(HttpOutput& output, | |
53 const UriComponents& uri, | |
54 const boost::filesystem::path& p) | |
55 { | |
56 namespace fs = boost::filesystem; | |
57 | |
330 | 58 output.SendOkHeader("text/html", false, 0, NULL); |
0 | 59 output.SendString("<html>"); |
60 output.SendString(" <body>"); | |
61 output.SendString(" <h1>Subdirectories</h1>"); | |
62 output.SendString(" <ul>"); | |
63 | |
64 if (uri.size() > 0) | |
65 { | |
66 std::string h = Toolbox::FlattenUri(uri) + "/.."; | |
67 output.SendString("<li><a href=\"" + h + "\">..</a></li>"); | |
68 } | |
69 | |
70 fs::directory_iterator end; | |
71 for (fs::directory_iterator it(p) ; it != end; ++it) | |
72 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
73 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 74 std::string f = it->path().filename().string(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
77 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
78 |
0 | 79 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
80 if (fs::is_directory(it->status())) | |
81 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
82 } | |
83 | |
84 output.SendString(" </ul>"); | |
85 output.SendString(" <h1>Files</h1>"); | |
86 output.SendString(" <ul>"); | |
87 | |
88 for (fs::directory_iterator it(p) ; it != end; ++it) | |
89 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
90 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 91 std::string f = it->path().filename().string(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
92 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
93 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
94 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
95 |
0 | 96 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
97 if (fs::is_regular_file(it->status())) | |
98 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
99 } | |
100 | |
101 output.SendString(" </ul>"); | |
102 output.SendString(" </body>"); | |
103 output.SendString("</html>"); | |
104 } | |
105 | |
106 | |
107 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
108 const std::string& root) : pimpl_(new PImpl) | |
109 { | |
110 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
111 pimpl_->root_ = root; | |
112 listDirectoryContent_ = false; | |
113 | |
114 namespace fs = boost::filesystem; | |
115 if (!fs::exists(pimpl_->root_) || | |
116 !fs::is_directory(pimpl_->root_)) | |
117 { | |
59 | 118 throw OrthancException("The path does not point to a directory"); |
0 | 119 } |
120 } | |
121 | |
122 | |
123 bool FilesystemHttpHandler::IsServedUri(const UriComponents& uri) | |
124 { | |
125 return Toolbox::IsChildUri(pimpl_->baseUri_, uri); | |
126 } | |
127 | |
128 | |
129 void FilesystemHttpHandler::Handle( | |
130 HttpOutput& output, | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
131 HttpMethod method, |
0 | 132 const UriComponents& uri, |
133 const Arguments& headers, | |
134 const Arguments& arguments, | |
135 const std::string&) | |
136 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
137 if (method != HttpMethod_Get) |
0 | 138 { |
139 output.SendMethodNotAllowedError("GET"); | |
140 return; | |
141 } | |
142 | |
143 namespace fs = boost::filesystem; | |
144 | |
145 fs::path p = pimpl_->root_; | |
146 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
147 { | |
148 p /= uri[i]; | |
149 } | |
150 | |
151 if (fs::exists(p) && fs::is_regular_file(p)) | |
152 { | |
217 | 153 FilesystemHttpSender(p).Send(output); |
154 | |
155 //output.AnswerFileAutodetectContentType(p.string()); | |
0 | 156 } |
157 else if (listDirectoryContent_ && | |
158 fs::exists(p) && | |
159 fs::is_directory(p)) | |
160 { | |
161 OutputDirectoryContent(output, uri, p); | |
162 } | |
163 else | |
164 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
165 output.SendHeader(HttpStatus_404_NotFound); |
0 | 166 } |
167 } | |
168 } |