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