Mercurial > hg > orthanc
annotate Core/HttpServer/FilesystemHttpHandler.cpp @ 111:0fc3b69c0357
preparing for release
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 Oct 2012 12:45:24 +0200 |
parents | fd7b0a3e6260 |
children | fe180eae201d |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
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. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "FilesystemHttpHandler.h" | |
22 | |
59 | 23 #include "../OrthancException.h" |
0 | 24 |
25 #include <boost/filesystem.hpp> | |
26 | |
27 | |
59 | 28 namespace Orthanc |
0 | 29 { |
30 struct FilesystemHttpHandler::PImpl | |
31 { | |
32 UriComponents baseUri_; | |
33 boost::filesystem::path root_; | |
34 }; | |
35 | |
36 | |
37 | |
38 static void OutputDirectoryContent(HttpOutput& output, | |
39 const UriComponents& uri, | |
40 const boost::filesystem::path& p) | |
41 { | |
42 namespace fs = boost::filesystem; | |
43 | |
44 output.SendOkHeader("text/html"); | |
45 output.SendString("<html>"); | |
46 output.SendString(" <body>"); | |
47 output.SendString(" <h1>Subdirectories</h1>"); | |
48 output.SendString(" <ul>"); | |
49 | |
50 if (uri.size() > 0) | |
51 { | |
52 std::string h = Toolbox::FlattenUri(uri) + "/.."; | |
53 output.SendString("<li><a href=\"" + h + "\">..</a></li>"); | |
54 } | |
55 | |
56 fs::directory_iterator end; | |
57 for (fs::directory_iterator it(p) ; it != end; ++it) | |
58 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
59 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 60 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
|
61 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
62 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
63 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
64 |
0 | 65 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
66 if (fs::is_directory(it->status())) | |
67 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
68 } | |
69 | |
70 output.SendString(" </ul>"); | |
71 output.SendString(" <h1>Files</h1>"); | |
72 output.SendString(" <ul>"); | |
73 | |
74 for (fs::directory_iterator it(p) ; it != end; ++it) | |
75 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 77 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
|
78 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
79 std::string f = it->path().filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
80 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
81 |
0 | 82 std::string h = Toolbox::FlattenUri(uri) + "/" + f; |
83 if (fs::is_regular_file(it->status())) | |
84 output.SendString("<li><a href=\"" + h + "\">" + f + "</a></li>"); | |
85 } | |
86 | |
87 output.SendString(" </ul>"); | |
88 output.SendString(" </body>"); | |
89 output.SendString("</html>"); | |
90 } | |
91 | |
92 | |
93 FilesystemHttpHandler::FilesystemHttpHandler(const std::string& baseUri, | |
94 const std::string& root) : pimpl_(new PImpl) | |
95 { | |
96 Toolbox::SplitUriComponents(pimpl_->baseUri_, baseUri); | |
97 pimpl_->root_ = root; | |
98 listDirectoryContent_ = false; | |
99 | |
100 namespace fs = boost::filesystem; | |
101 if (!fs::exists(pimpl_->root_) || | |
102 !fs::is_directory(pimpl_->root_)) | |
103 { | |
59 | 104 throw OrthancException("The path does not point to a directory"); |
0 | 105 } |
106 } | |
107 | |
108 | |
109 bool FilesystemHttpHandler::IsServedUri(const UriComponents& uri) | |
110 { | |
111 return Toolbox::IsChildUri(pimpl_->baseUri_, uri); | |
112 } | |
113 | |
114 | |
115 void FilesystemHttpHandler::Handle( | |
116 HttpOutput& output, | |
117 const std::string& method, | |
118 const UriComponents& uri, | |
119 const Arguments& headers, | |
120 const Arguments& arguments, | |
121 const std::string&) | |
122 { | |
123 if (method != "GET") | |
124 { | |
125 output.SendMethodNotAllowedError("GET"); | |
126 return; | |
127 } | |
128 | |
129 namespace fs = boost::filesystem; | |
130 | |
131 fs::path p = pimpl_->root_; | |
132 for (size_t i = pimpl_->baseUri_.size(); i < uri.size(); i++) | |
133 { | |
134 p /= uri[i]; | |
135 } | |
136 | |
137 if (fs::exists(p) && fs::is_regular_file(p)) | |
138 { | |
139 output.AnswerFileAutodetectContentType(p.string()); | |
140 } | |
141 else if (listDirectoryContent_ && | |
142 fs::exists(p) && | |
143 fs::is_directory(p)) | |
144 { | |
145 OutputDirectoryContent(output, uri, p); | |
146 } | |
147 else | |
148 { | |
59 | 149 output.SendHeader(Orthanc_HttpStatus_404_NotFound); |
0 | 150 } |
151 } | |
152 } |