Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/FilesystemHttpHandler.cpp @ 5577:9e74e761b108 find-refactoring
integration mainline->find-refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2024 17:43:22 +0200 |
parents | 48b8dae6dc77 |
children | f7adfb22e20e |
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 |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5407
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5407
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
136 | 12 * |
0 | 13 * This program is distributed in the hope that it will be useful, but |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
0 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
0 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
0 | 25 #include "FilesystemHttpHandler.h" |
26 | |
59 | 27 #include "../OrthancException.h" |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
28 #include "../SystemToolbox.h" |
217 | 29 #include "FilesystemHttpSender.h" |
0 | 30 |
31 #include <boost/filesystem.hpp> | |
32 | |
33 | |
59 | 34 namespace Orthanc |
0 | 35 { |
36 struct FilesystemHttpHandler::PImpl | |
37 { | |
38 UriComponents baseUri_; | |
39 boost::filesystem::path root_; | |
40 }; | |
41 | |
42 | |
43 | |
44 static void OutputDirectoryContent(HttpOutput& output, | |
4330
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
45 const HttpToolbox::Arguments& headers, |
0 | 46 const UriComponents& uri, |
47 const boost::filesystem::path& p) | |
48 { | |
49 namespace fs = boost::filesystem; | |
50 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
51 std::string s; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
52 s += "<html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
53 s += " <body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
54 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
|
55 s += " <ul>"; |
0 | 56 |
57 if (uri.size() > 0) | |
58 { | |
59 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
|
60 s += "<li><a href=\"" + h + "\">..</a></li>"; |
0 | 61 } |
62 | |
63 fs::directory_iterator end; | |
64 for (fs::directory_iterator it(p) ; it != end; ++it) | |
65 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
66 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 67 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
|
68 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
69 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
70 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
71 |
0 | 72 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
73 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
|
74 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
0 | 75 } |
76 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
77 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
78 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
|
79 s += " <ul>"; |
0 | 80 |
81 for (fs::directory_iterator it(p) ; it != end; ++it) | |
82 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
83 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 84 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
|
85 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
86 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
87 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
88 |
0 | 89 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
2140 | 90 if (SystemToolbox::IsRegularFile(it->path().string())) |
1911 | 91 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
92 s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; |
1911 | 93 } |
0 | 94 } |
95 | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
96 s += " </ul>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
97 s += " </body>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
98 s += "</html>"; |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
99 |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
100 output.SetContentType(MimeType_Html); |
1521 | 101 output.Answer(s); |
0 | 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 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
116 throw OrthancException(ErrorCode_DirectoryExpected); |
0 | 117 } |
118 } | |
119 | |
120 | |
4330
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
121 bool FilesystemHttpHandler::Handle(HttpOutput& output, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
122 RequestOrigin /*origin*/, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
123 const char* /*remoteIp*/, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
124 const char* /*username*/, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
125 HttpMethod method, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
126 const UriComponents& uri, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
127 const HttpToolbox::Arguments& headers, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
128 const HttpToolbox::GetArguments& arguments, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
129 const void* /*bodyData*/, |
a01b1c9cbef4
moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
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())); |
5407
3206537cbb56
HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
156 output.Answer(sender); |
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 } |