Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 1198:1169528a9a5f db-changes
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 Oct 2014 13:52:01 +0200 |
parents | ba5c0908600c |
children | 6e7e5ed91c2d |
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 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
58 output.SetContentType("text/html"); |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
59 |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
60 std::string s; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
61 s += "<html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
62 s += " <body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
63 s += " <h1>Subdirectories</h1>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
64 s += " <ul>"; |
0 | 65 |
66 if (uri.size() > 0) | |
67 { | |
68 std::string h = Toolbox::FlattenUri(uri) + "/.."; | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
69 s += "<li><a href=\"" + h + "\">..</a></li>"; |
0 | 70 } |
71 | |
72 fs::directory_iterator end; | |
73 for (fs::directory_iterator it(p) ; it != end; ++it) | |
74 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 76 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
|
77 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
78 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
79 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
80 |
0 | 81 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
82 if (fs::is_directory(it->status())) | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
83 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
0 | 84 } |
85 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
86 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
87 s += " <h1>Files</h1>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
88 s += " <ul>"; |
0 | 89 |
90 for (fs::directory_iterator it(p) ; it != end; ++it) | |
91 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
92 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 93 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
|
94 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
95 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
96 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
97 |
0 | 98 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
99 if (fs::is_regular_file(it->status())) | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
100 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
0 | 101 } |
102 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
103 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
104 s += " </body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
105 s += "</html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
106 |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
107 output.SendBody(s); |
0 | 108 } |
109 | |
110 | |
111 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
112 const std::string& root) : pimpl_(new PImpl) | |
113 { | |
114 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
115 pimpl_->root_ = root; | |
116 listDirectoryContent_ = false; | |
117 | |
118 namespace fs = boost::filesystem; | |
119 if (!fs::exists(pimpl_->root_) || | |
120 !fs::is_directory(pimpl_->root_)) | |
121 { | |
59 | 122 throw OrthancException("The path does not point to a directory"); |
0 | 123 } |
124 } | |
125 | |
126 | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
127 bool FilesystemHttpHandler::Handle( |
0 | 128 HttpOutput& output, |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
129 HttpMethod method, |
0 | 130 const UriComponents& uri, |
131 const Arguments& headers, | |
132 const Arguments& arguments, | |
133 const std::string&) | |
134 { | |
901
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
135 if (!Toolbox::IsChildUri(pimpl_->baseUri_, uri)) |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
136 { |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
137 // This URI is not served by this handler |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
138 return false; |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
139 } |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
140 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
141 if (method != HttpMethod_Get) |
0 | 142 { |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
908
diff
changeset
|
143 output.SendMethodNotAllowed("GET"); |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
144 return true; |
0 | 145 } |
146 | |
147 namespace fs = boost::filesystem; | |
148 | |
149 fs::path p = pimpl_->root_; | |
150 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
151 { | |
152 p /= uri[i]; | |
153 } | |
154 | |
155 if (fs::exists(p) && fs::is_regular_file(p)) | |
156 { | |
217 | 157 FilesystemHttpSender(p).Send(output); |
158 | |
159 //output.AnswerFileAutodetectContentType(p.string()); | |
0 | 160 } |
161 else if (listDirectoryContent_ && | |
162 fs::exists(p) && | |
163 fs::is_directory(p)) | |
164 { | |
165 OutputDirectoryContent(output, uri, p); | |
166 } | |
167 else | |
168 { | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
169 output.SendStatus(HttpStatus_404_NotFound); |
0 | 170 } |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
171 |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
172 return true; |
0 | 173 } |
174 } |