Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/FilesystemHttpHandler.cpp @ 4275:d7a50b7b8466
Dynamically access and/or change the verbosity of logging categories with the REST API
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 14:41:27 +0100 |
parents | bf7b9edf6b81 |
children | a01b1c9cbef4 |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3401
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
136 | 11 * |
0 | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
0 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
0 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
0 | 24 #include "FilesystemHttpHandler.h" |
25 | |
59 | 26 #include "../OrthancException.h" |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
27 #include "../SystemToolbox.h" |
217 | 28 #include "FilesystemHttpSender.h" |
0 | 29 |
30 #include <boost/filesystem.hpp> | |
31 | |
32 | |
59 | 33 namespace Orthanc |
0 | 34 { |
35 struct FilesystemHttpHandler::PImpl | |
36 { | |
37 UriComponents baseUri_; | |
38 boost::filesystem::path root_; | |
39 }; | |
40 | |
41 | |
42 | |
43 static void OutputDirectoryContent(HttpOutput& output, | |
1515
c94353fbd4e9
cont http compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
44 const IHttpHandler::Arguments& headers, |
0 | 45 const UriComponents& uri, |
46 const boost::filesystem::path& p) | |
47 { | |
48 namespace fs = boost::filesystem; | |
49 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
50 std::string s; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
51 s += "<html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
52 s += " <body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
53 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
|
54 s += " <ul>"; |
0 | 55 |
56 if (uri.size() > 0) | |
57 { | |
58 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
|
59 s += "<li><a href=\"" + h + "\">..</a></li>"; |
0 | 60 } |
61 | |
62 fs::directory_iterator end; | |
63 for (fs::directory_iterator it(p) ; it != end; ++it) | |
64 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
65 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 66 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
|
67 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
68 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
69 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
70 |
0 | 71 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
72 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
|
73 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
0 | 74 } |
75 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
76 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
77 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
|
78 s += " <ul>"; |
0 | 79 |
80 for (fs::directory_iterator it(p) ; it != end; ++it) | |
81 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
82 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 83 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
|
84 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
85 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
86 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
87 |
0 | 88 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
2140 | 89 if (SystemToolbox::IsRegularFile(it->path().string())) |
1911 | 90 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
91 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
1911 | 92 } |
0 | 93 } |
94 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
95 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
96 s += " </body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
97 s += "</html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
98 |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
99 output.SetContentType(MimeType_Html); |
1521 | 100 output.Answer(s); |
0 | 101 } |
102 | |
103 | |
104 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
105 const std::string& root) : pimpl_(new PImpl) | |
106 { | |
107 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
108 pimpl_->root_ = root; | |
109 listDirectoryContent_ = false; | |
110 | |
111 namespace fs = boost::filesystem; | |
112 if (!fs::exists(pimpl_->root_) || | |
113 !fs::is_directory(pimpl_->root_)) | |
114 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
115 throw OrthancException(ErrorCode_DirectoryExpected); |
0 | 116 } |
117 } | |
118 | |
119 | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
120 bool FilesystemHttpHandler::Handle( |
0 | 121 HttpOutput& output, |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
122 RequestOrigin /*origin*/, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
123 const char* /*remoteIp*/, |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1521
diff
changeset
|
124 const char* /*username*/, |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
125 HttpMethod method, |
0 | 126 const UriComponents& uri, |
127 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
|
128 const GetArguments& arguments, |
3401 | 129 const void* /*bodyData*/, |
1446
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
130 size_t /*bodySize*/) |
0 | 131 { |
901
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
132 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
|
133 { |
7d88f3f4a3b3
refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
895
diff
changeset
|
134 // 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
|
135 return false; |
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 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
138 if (method != HttpMethod_Get) |
0 | 139 { |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
908
diff
changeset
|
140 output.SendMethodNotAllowed("GET"); |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
141 return true; |
0 | 142 } |
143 | |
144 namespace fs = boost::filesystem; | |
145 | |
146 fs::path p = pimpl_->root_; | |
147 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
148 { | |
149 p /= uri[i]; | |
150 } | |
151 | |
2140 | 152 if (SystemToolbox::IsRegularFile(p.string())) |
0 | 153 { |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1517
diff
changeset
|
154 FilesystemHttpSender sender(p); |
3023
c9c2faf76bec
replaced 'var' by 'let' in Orthanc Explorer's JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2908
diff
changeset
|
155 sender.SetContentType(SystemToolbox::AutodetectMimeType(p.string())); |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1517
diff
changeset
|
156 output.Answer(sender); // TODO COMPRESSION |
0 | 157 } |
158 else if (listDirectoryContent_ && | |
159 fs::exists(p) && | |
160 fs::is_directory(p)) | |
161 { | |
1515
c94353fbd4e9
cont http compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
162 OutputDirectoryContent(output, headers, uri, p); |
0 | 163 } |
164 else | |
165 { | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
166 output.SendStatus(HttpStatus_404_NotFound); |
0 | 167 } |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
168 |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
169 return true; |
0 | 170 } |
171 } |