Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 2186:8b51b133bb8b
move of third party downloads to the main server
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 29 Nov 2016 12:22:59 +0100 |
parents | fd5875662670 |
children | a3a65de1840f |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 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" |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
37 #include "../SystemToolbox.h" |
217 | 38 #include "FilesystemHttpSender.h" |
0 | 39 |
40 #include <boost/filesystem.hpp> | |
41 | |
42 | |
59 | 43 namespace Orthanc |
0 | 44 { |
45 struct FilesystemHttpHandler::PImpl | |
46 { | |
47 UriComponents baseUri_; | |
48 boost::filesystem::path root_; | |
49 }; | |
50 | |
51 | |
52 | |
53 static void OutputDirectoryContent(HttpOutput& output, | |
1515
c94353fbd4e9
cont http compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
54 const IHttpHandler::Arguments& headers, |
0 | 55 const UriComponents& uri, |
56 const boost::filesystem::path& p) | |
57 { | |
58 namespace fs = boost::filesystem; | |
59 | |
1113
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; |
2140 | 99 if (SystemToolbox::IsRegularFile(it->path().string())) |
1911 | 100 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
101 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
1911 | 102 } |
0 | 103 } |
104 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
105 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
106 s += " </body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
107 s += "</html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
108 |
1517 | 109 output.SetContentType("text/html"); |
1521 | 110 output.Answer(s); |
0 | 111 } |
112 | |
113 | |
114 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
115 const std::string& root) : pimpl_(new PImpl) | |
116 { | |
117 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
118 pimpl_->root_ = root; | |
119 listDirectoryContent_ = false; | |
120 | |
121 namespace fs = boost::filesystem; | |
122 if (!fs::exists(pimpl_->root_) || | |
123 !fs::is_directory(pimpl_->root_)) | |
124 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
125 throw OrthancException(ErrorCode_DirectoryExpected); |
0 | 126 } |
127 } | |
128 | |
129 | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
130 bool FilesystemHttpHandler::Handle( |
0 | 131 HttpOutput& output, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
132 RequestOrigin /*origin*/, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
133 const char* /*remoteIp*/, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
134 const char* /*username*/, |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
135 HttpMethod method, |
0 | 136 const UriComponents& uri, |
137 const Arguments& headers, | |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
138 const GetArguments& arguments, |
1446
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
139 const char* /*bodyData*/, |
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
140 size_t /*bodySize*/) |
0 | 141 { |
901
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
142 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
|
143 { |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
144 // 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
|
145 return false; |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
146 } |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
147 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
148 if (method != HttpMethod_Get) |
0 | 149 { |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
908
diff
changeset
|
150 output.SendMethodNotAllowed("GET"); |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
151 return true; |
0 | 152 } |
153 | |
154 namespace fs = boost::filesystem; | |
155 | |
156 fs::path p = pimpl_->root_; | |
157 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
158 { | |
159 p /= uri[i]; | |
160 } | |
161 | |
2140 | 162 if (SystemToolbox::IsRegularFile(p.string())) |
0 | 163 { |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1517
diff
changeset
|
164 FilesystemHttpSender sender(p); |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1517
diff
changeset
|
165 output.Answer(sender); // TODO COMPRESSION |
0 | 166 } |
167 else if (listDirectoryContent_ && | |
168 fs::exists(p) && | |
169 fs::is_directory(p)) | |
170 { | |
1515
c94353fbd4e9
cont http compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
171 OutputDirectoryContent(output, headers, uri, p); |
0 | 172 } |
173 else | |
174 { | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
175 output.SendStatus(HttpStatus_404_NotFound); |
0 | 176 } |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
177 |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 return true; |
0 | 179 } |
180 } |