Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 797:37adac56017a
ImageAccessor abstraction
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 May 2014 12:47:26 +0200 |
parents | 2d0a347e8cfc |
children | a811bdf8b8eb |
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 | |
33 #include "FilesystemHttpHandler.h" | |
34 | |
59 | 35 #include "../OrthancException.h" |
217 | 36 #include "FilesystemHttpSender.h" |
0 | 37 |
38 #include <boost/filesystem.hpp> | |
39 | |
40 | |
59 | 41 namespace Orthanc |
0 | 42 { |
43 struct FilesystemHttpHandler::PImpl | |
44 { | |
45 UriComponents baseUri_; | |
46 boost::filesystem::path root_; | |
47 }; | |
48 | |
49 | |
50 | |
51 static void OutputDirectoryContent(HttpOutput& output, | |
52 const UriComponents& uri, | |
53 const boost::filesystem::path& p) | |
54 { | |
55 namespace fs = boost::filesystem; | |
56 | |
330 | 57 output.SendOkHeader("text/html", false, 0, NULL); |
0 | 58 output.SendString("<html>"); |
59 output.SendString(" <body>"); | |
60 output.SendString(" <h1>Subdirectories</h1>"); | |
61 output.SendString(" <ul>"); | |
62 | |
63 if (uri.size() > 0) | |
64 { | |
65 std::string h = Toolbox::FlattenUri(uri) + "/.."; | |
66 output.SendString("<li><a href=\"" + h + "\">..</a></li>"); | |
67 } | |
68 | |
69 fs::directory_iterator end; | |
70 for (fs::directory_iterator it(p) ; it != end; ++it) | |
71 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
72 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 73 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
|
74 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
77 |
0 | 78 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
79 if (fs::is_directory(it->status())) | |
80 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
81 } | |
82 | |
83 output.SendString(" </ul>"); | |
84 output.SendString(" <h1>Files</h1>"); | |
85 output.SendString(" <ul>"); | |
86 | |
87 for (fs::directory_iterator it(p) ; it != end; ++it) | |
88 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
89 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 90 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
|
91 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
92 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
93 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
94 |
0 | 95 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
96 if (fs::is_regular_file(it->status())) | |
97 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
98 } | |
99 | |
100 output.SendString(" </ul>"); | |
101 output.SendString(" </body>"); | |
102 output.SendString("</html>"); | |
103 } | |
104 | |
105 | |
106 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
107 const std::string& root) : pimpl_(new PImpl) | |
108 { | |
109 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
110 pimpl_->root_ = root; | |
111 listDirectoryContent_ = false; | |
112 | |
113 namespace fs = boost::filesystem; | |
114 if (!fs::exists(pimpl_->root_) || | |
115 !fs::is_directory(pimpl_->root_)) | |
116 { | |
59 | 117 throw OrthancException("The path does not point to a directory"); |
0 | 118 } |
119 } | |
120 | |
121 | |
122 bool FilesystemHttpHandler::IsServedUri(const UriComponents& uri) | |
123 { | |
124 return Toolbox::IsChildUri(pimpl_->baseUri_, uri); | |
125 } | |
126 | |
127 | |
128 void FilesystemHttpHandler::Handle( | |
129 HttpOutput& output, | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
130 HttpMethod method, |
0 | 131 const UriComponents& uri, |
132 const Arguments& headers, | |
133 const Arguments& arguments, | |
134 const std::string&) | |
135 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
136 if (method != HttpMethod_Get) |
0 | 137 { |
138 output.SendMethodNotAllowedError("GET"); | |
139 return; | |
140 } | |
141 | |
142 namespace fs = boost::filesystem; | |
143 | |
144 fs::path p = pimpl_->root_; | |
145 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
146 { | |
147 p /= uri[i]; | |
148 } | |
149 | |
150 if (fs::exists(p) && fs::is_regular_file(p)) | |
151 { | |
217 | 152 FilesystemHttpSender(p).Send(output); |
153 | |
154 //output.AnswerFileAutodetectContentType(p.string()); | |
0 | 155 } |
156 else if (listDirectoryContent_ && | |
157 fs::exists(p) && | |
158 fs::is_directory(p)) | |
159 { | |
160 OutputDirectoryContent(output, uri, p); | |
161 } | |
162 else | |
163 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
164 output.SendHeader(HttpStatus_404_NotFound); |
0 | 165 } |
166 } | |
167 } |