Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 139:3ad78369fcc4
start threaded messages
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Oct 2012 17:57:03 +0200 |
parents | fe180eae201d |
children | 1ac3aacd10a5 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
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 | |
33 #include "FilesystemHttpHandler.h" | |
34 | |
59 | 35 #include "../OrthancException.h" |
0 | 36 |
37 #include <boost/filesystem.hpp> | |
38 | |
39 | |
59 | 40 namespace Orthanc |
0 | 41 { |
42 struct FilesystemHttpHandler::PImpl | |
43 { | |
44 UriComponents baseUri_; | |
45 boost::filesystem::path root_; | |
46 }; | |
47 | |
48 | |
49 | |
50 static void OutputDirectoryContent(HttpOutput& output, | |
51 const UriComponents& uri, | |
52 const boost::filesystem::path& p) | |
53 { | |
54 namespace fs = boost::filesystem; | |
55 | |
56 output.SendOkHeader("text/html"); | |
57 output.SendString("<html>"); | |
58 output.SendString(" <body>"); | |
59 output.SendString(" <h1>Subdirectories</h1>"); | |
60 output.SendString(" <ul>"); | |
61 | |
62 if (uri.size() > 0) | |
63 { | |
64 std::string h = Toolbox::FlattenUri(uri) + "/.."; | |
65 output.SendString("<li><a href=\"" + h + "\">..</a></li>"); | |
66 } | |
67 | |
68 fs::directory_iterator end; | |
69 for (fs::directory_iterator it(p) ; it != end; ++it) | |
70 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
71 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 72 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
|
73 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
74 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 |
0 | 77 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
78 if (fs::is_directory(it->status())) | |
79 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
80 } | |
81 | |
82 output.SendString(" </ul>"); | |
83 output.SendString(" <h1>Files</h1>"); | |
84 output.SendString(" <ul>"); | |
85 | |
86 for (fs::directory_iterator it(p) ; it != end; ++it) | |
87 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
88 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 89 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
|
90 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
91 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
92 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
93 |
0 | 94 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
95 if (fs::is_regular_file(it->status())) | |
96 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
97 } | |
98 | |
99 output.SendString(" </ul>"); | |
100 output.SendString(" </body>"); | |
101 output.SendString("</html>"); | |
102 } | |
103 | |
104 | |
105 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
106 const std::string& root) : pimpl_(new PImpl) | |
107 { | |
108 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
109 pimpl_->root_ = root; | |
110 listDirectoryContent_ = false; | |
111 | |
112 namespace fs = boost::filesystem; | |
113 if (!fs::exists(pimpl_->root_) || | |
114 !fs::is_directory(pimpl_->root_)) | |
115 { | |
59 | 116 throw OrthancException("The path does not point to a directory"); |
0 | 117 } |
118 } | |
119 | |
120 | |
121 bool FilesystemHttpHandler::IsServedUri(const UriComponents& uri) | |
122 { | |
123 return Toolbox::IsChildUri(pimpl_->baseUri_, uri); | |
124 } | |
125 | |
126 | |
127 void FilesystemHttpHandler::Handle( | |
128 HttpOutput& output, | |
129 const std::string& method, | |
130 const UriComponents& uri, | |
131 const Arguments& headers, | |
132 const Arguments& arguments, | |
133 const std::string&) | |
134 { | |
135 if (method != "GET") | |
136 { | |
137 output.SendMethodNotAllowedError("GET"); | |
138 return; | |
139 } | |
140 | |
141 namespace fs = boost::filesystem; | |
142 | |
143 fs::path p = pimpl_->root_; | |
144 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
145 { | |
146 p /= uri[i]; | |
147 } | |
148 | |
149 if (fs::exists(p) && fs::is_regular_file(p)) | |
150 { | |
151 output.AnswerFileAutodetectContentType(p.string()); | |
152 } | |
153 else if (listDirectoryContent_ && | |
154 fs::exists(p) && | |
155 fs::is_directory(p)) | |
156 { | |
157 OutputDirectoryContent(output, uri, p); | |
158 } | |
159 else | |
160 { | |
59 | 161 output.SendHeader(Orthanc_HttpStatus_404_NotFound); |
0 | 162 } |
163 } | |
164 } |