Mercurial > hg > orthanc
annotate Core/HttpServer/MongooseServer.cpp @ 409:63f707278fc8 lua-scripting
lua filtering of incoming http requests
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 03 May 2013 12:23:02 +0200 |
parents | bdd72233b105 |
children | 26290b46056b |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
398 | 3 * Copyright (C) 2012-2013 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 // http://en.highscore.de/cpp/boost/stringhandling.html | |
34 | |
35 #include "MongooseServer.h" | |
36 | |
37 #include <algorithm> | |
38 #include <string.h> | |
39 #include <boost/lexical_cast.hpp> | |
40 #include <boost/algorithm/string.hpp> | |
41 #include <iostream> | |
42 #include <string.h> | |
43 #include <stdio.h> | |
44 #include <boost/thread.hpp> | |
108 | 45 #include <glog/logging.h> |
0 | 46 |
59 | 47 #include "../OrthancException.h" |
0 | 48 #include "../ChunkedBuffer.h" |
324 | 49 #include "HttpOutput.h" |
0 | 50 #include "mongoose.h" |
51 | |
52 | |
59 | 53 #define ORTHANC_REALM "Orthanc Secure Area" |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
54 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
55 static const long LOCALHOST = (127ll << 24) + 1ll; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
56 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
57 |
59 | 58 namespace Orthanc |
0 | 59 { |
60 static const char multipart[] = "multipart/form-data; boundary="; | |
61 static unsigned int multipartLength = sizeof(multipart) / sizeof(char) - 1; | |
62 | |
63 | |
64 namespace | |
65 { | |
66 // Anonymous namespace to avoid clashes between compilation modules | |
67 class MongooseOutput : public HttpOutput | |
68 { | |
69 private: | |
70 struct mg_connection* connection_; | |
71 | |
72 public: | |
73 MongooseOutput(struct mg_connection* connection) : connection_(connection) | |
74 { | |
75 } | |
76 | |
77 virtual void Send(const void* buffer, size_t length) | |
78 { | |
217 | 79 if (length > 0) |
80 { | |
81 mg_write(connection_, buffer, length); | |
82 } | |
0 | 83 } |
84 }; | |
85 | |
86 | |
87 enum PostDataStatus | |
88 { | |
89 PostDataStatus_Success, | |
90 PostDataStatus_NoLength, | |
91 PostDataStatus_Pending, | |
92 PostDataStatus_Failure | |
93 }; | |
94 } | |
95 | |
96 | |
97 // TODO Move this to external file | |
98 | |
99 | |
100 class ChunkedFile : public ChunkedBuffer | |
101 { | |
102 private: | |
103 std::string filename_; | |
104 | |
105 public: | |
106 ChunkedFile(const std::string& filename) : | |
107 filename_(filename) | |
108 { | |
109 } | |
110 | |
111 const std::string& GetFilename() const | |
112 { | |
113 return filename_; | |
114 } | |
115 }; | |
116 | |
117 | |
118 | |
119 class ChunkStore | |
120 { | |
121 private: | |
122 typedef std::list<ChunkedFile*> Content; | |
123 Content content_; | |
124 unsigned int numPlaces_; | |
125 | |
126 boost::mutex mutex_; | |
127 std::set<std::string> discardedFiles_; | |
128 | |
129 void Clear() | |
130 { | |
131 for (Content::iterator it = content_.begin(); | |
132 it != content_.end(); it++) | |
133 { | |
134 delete *it; | |
135 } | |
136 } | |
137 | |
138 Content::iterator Find(const std::string& filename) | |
139 { | |
140 for (Content::iterator it = content_.begin(); | |
141 it != content_.end(); it++) | |
142 { | |
143 if ((*it)->GetFilename() == filename) | |
144 { | |
145 return it; | |
146 } | |
147 } | |
148 | |
149 return content_.end(); | |
150 } | |
151 | |
152 void Remove(const std::string& filename) | |
153 { | |
154 Content::iterator it = Find(filename); | |
155 if (it != content_.end()) | |
156 { | |
157 delete *it; | |
158 content_.erase(it); | |
159 } | |
160 } | |
161 | |
162 public: | |
163 ChunkStore() | |
164 { | |
165 numPlaces_ = 10; | |
166 } | |
167 | |
168 ~ChunkStore() | |
169 { | |
170 Clear(); | |
171 } | |
172 | |
173 PostDataStatus Store(std::string& completed, | |
174 const char* chunkData, | |
175 size_t chunkSize, | |
176 const std::string& filename, | |
177 size_t filesize) | |
178 { | |
179 boost::mutex::scoped_lock lock(mutex_); | |
180 | |
181 std::set<std::string>::iterator wasDiscarded = discardedFiles_.find(filename); | |
182 if (wasDiscarded != discardedFiles_.end()) | |
183 { | |
184 discardedFiles_.erase(wasDiscarded); | |
185 return PostDataStatus_Failure; | |
186 } | |
187 | |
188 ChunkedFile* f; | |
189 Content::iterator it = Find(filename); | |
190 if (it == content_.end()) | |
191 { | |
192 f = new ChunkedFile(filename); | |
193 | |
194 // Make some room | |
195 if (content_.size() >= numPlaces_) | |
196 { | |
197 discardedFiles_.insert(content_.front()->GetFilename()); | |
198 delete content_.front(); | |
199 content_.pop_front(); | |
200 } | |
201 | |
202 content_.push_back(f); | |
203 } | |
204 else | |
205 { | |
206 f = *it; | |
207 } | |
208 | |
209 f->AddChunk(chunkData, chunkSize); | |
210 | |
211 if (f->GetNumBytes() > filesize) | |
212 { | |
213 Remove(filename); | |
214 } | |
215 else if (f->GetNumBytes() == filesize) | |
216 { | |
217 f->Flatten(completed); | |
218 Remove(filename); | |
219 return PostDataStatus_Success; | |
220 } | |
221 | |
222 return PostDataStatus_Pending; | |
223 } | |
224 | |
225 /*void Print() | |
226 { | |
227 boost::mutex::scoped_lock lock(mutex_); | |
228 | |
229 printf("ChunkStore status:\n"); | |
230 for (Content::const_iterator i = content_.begin(); | |
231 i != content_.end(); i++) | |
232 { | |
233 printf(" [%s]: %d\n", (*i)->GetFilename().c_str(), (*i)->GetNumBytes()); | |
234 } | |
235 printf("-----\n"); | |
236 }*/ | |
237 }; | |
238 | |
239 | |
240 struct MongooseServer::PImpl | |
241 { | |
242 struct mg_context *context_; | |
243 ChunkStore chunkStore_; | |
244 }; | |
245 | |
246 | |
247 ChunkStore& MongooseServer::GetChunkStore() | |
248 { | |
249 return pimpl_->chunkStore_; | |
250 } | |
251 | |
252 | |
253 | |
254 HttpHandler* MongooseServer::FindHandler(const UriComponents& forUri) const | |
255 { | |
256 for (Handlers::const_iterator it = | |
257 handlers_.begin(); it != handlers_.end(); it++) | |
258 { | |
259 if ((*it)->IsServedUri(forUri)) | |
260 { | |
261 return *it; | |
262 } | |
263 } | |
264 | |
265 return NULL; | |
266 } | |
267 | |
268 | |
269 | |
270 | |
271 static PostDataStatus ReadPostData(std::string& postData, | |
272 struct mg_connection *connection, | |
273 const HttpHandler::Arguments& headers) | |
274 { | |
275 HttpHandler::Arguments::const_iterator cs = headers.find("content-length"); | |
276 if (cs == headers.end()) | |
277 { | |
278 return PostDataStatus_NoLength; | |
279 } | |
280 | |
281 int length; | |
282 try | |
283 { | |
284 length = boost::lexical_cast<int>(cs->second); | |
285 } | |
286 catch (boost::bad_lexical_cast) | |
287 { | |
288 return PostDataStatus_NoLength; | |
289 } | |
290 | |
291 if (length < 0) | |
292 { | |
293 length = 0; | |
294 } | |
295 | |
296 postData.resize(length); | |
297 | |
298 size_t pos = 0; | |
299 while (length > 0) | |
300 { | |
301 int r = mg_read(connection, &postData[pos], length); | |
302 if (r <= 0) | |
303 { | |
304 return PostDataStatus_Failure; | |
305 } | |
8 | 306 assert(r <= length); |
0 | 307 length -= r; |
308 pos += r; | |
309 } | |
310 | |
311 return PostDataStatus_Success; | |
312 } | |
313 | |
314 | |
315 | |
316 static PostDataStatus ParseMultipartPost(std::string &completedFile, | |
317 struct mg_connection *connection, | |
318 const HttpHandler::Arguments& headers, | |
319 const std::string& contentType, | |
320 ChunkStore& chunkStore) | |
321 { | |
322 std::string boundary = "--" + contentType.substr(multipartLength); | |
323 | |
324 std::string postData; | |
325 PostDataStatus status = ReadPostData(postData, connection, headers); | |
326 | |
327 if (status != PostDataStatus_Success) | |
328 { | |
329 return status; | |
330 } | |
331 | |
332 /*for (HttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) | |
333 { | |
334 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; | |
335 } | |
336 printf("CHUNK\n");*/ | |
337 | |
338 typedef HttpHandler::Arguments::const_iterator ArgumentIterator; | |
339 | |
340 ArgumentIterator requestedWith = headers.find("x-requested-with"); | |
341 ArgumentIterator fileName = headers.find("x-file-name"); | |
342 ArgumentIterator fileSizeStr = headers.find("x-file-size"); | |
343 | |
338
3a3b3ba8c1e0
fix for uploads through internet explorer 7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
333
diff
changeset
|
344 if (requestedWith != headers.end() && |
0 | 345 requestedWith->second != "XMLHttpRequest") |
346 { | |
347 return PostDataStatus_Failure; | |
348 } | |
349 | |
350 size_t fileSize = 0; | |
351 if (fileSizeStr != headers.end()) | |
352 { | |
353 try | |
354 { | |
355 fileSize = boost::lexical_cast<size_t>(fileSizeStr->second); | |
356 } | |
357 catch (boost::bad_lexical_cast) | |
358 { | |
359 return PostDataStatus_Failure; | |
360 } | |
361 } | |
362 | |
363 typedef boost::find_iterator<std::string::iterator> FindIterator; | |
10 | 364 typedef boost::iterator_range<char*> Range; |
0 | 365 |
366 //chunkStore.Print(); | |
367 | |
368 try | |
369 { | |
370 FindIterator last; | |
371 for (FindIterator it = | |
372 make_find_iterator(postData, boost::first_finder(boundary)); | |
373 it!=FindIterator(); | |
374 ++it) | |
375 { | |
376 if (last != FindIterator()) | |
377 { | |
10 | 378 Range part(&last->back(), &it->front()); |
0 | 379 Range content = boost::find_first(part, "\r\n\r\n"); |
345 | 380 if (/*content != Range()*/!content.empty()) |
0 | 381 { |
382 Range c(&content.back() + 1, &it->front() - 2); | |
383 size_t chunkSize = c.size(); | |
384 | |
385 if (chunkSize > 0) | |
386 { | |
387 const char* chunkData = &c.front(); | |
388 | |
389 if (fileName == headers.end()) | |
390 { | |
391 // This file is stored in a single chunk | |
392 completedFile.resize(chunkSize); | |
393 if (chunkSize > 0) | |
394 { | |
395 memcpy(&completedFile[0], chunkData, chunkSize); | |
396 } | |
397 return PostDataStatus_Success; | |
398 } | |
399 else | |
400 { | |
401 return chunkStore.Store(completedFile, chunkData, chunkSize, fileName->second, fileSize); | |
402 } | |
403 } | |
10 | 404 } |
0 | 405 } |
406 | |
407 last = it; | |
408 } | |
409 } | |
410 catch (std::length_error) | |
411 { | |
412 return PostDataStatus_Failure; | |
413 } | |
414 | |
415 return PostDataStatus_Pending; | |
416 } | |
417 | |
418 | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
419 static void SendUnauthorized(HttpOutput& output) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
420 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
421 std::string s = "HTTP/1.1 401 Unauthorized\r\n" |
59 | 422 "WWW-Authenticate: Basic realm=\"" ORTHANC_REALM "\"" |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
423 "\r\n\r\n"; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
424 output.Send(&s[0], s.size()); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
425 } |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
426 |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
427 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
428 static bool Authorize(const MongooseServer& that, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
429 const HttpHandler::Arguments& headers, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
430 HttpOutput& output) |
23 | 431 { |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
432 bool granted = false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
433 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
434 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
435 if (auth != headers.end()) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
436 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
437 std::string s = auth->second; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
438 if (s.substr(0, 6) == "Basic ") |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
439 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
440 std::string b64 = s.substr(6); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
441 granted = that.IsValidBasicHttpAuthentication(b64); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
442 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
443 } |
23 | 444 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
445 if (!granted) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
446 { |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
447 SendUnauthorized(output); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
448 return false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
449 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
450 else |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
451 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
452 return true; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
453 } |
23 | 454 } |
455 | |
456 | |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
457 static std::string GetAuthenticatedUsername(const HttpHandler::Arguments& headers) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
458 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
459 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
460 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
461 if (auth == headers.end()) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
462 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
463 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
464 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
465 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
466 std::string s = auth->second; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
467 if (s.substr(0, 6) != "Basic ") |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
468 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
469 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
470 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
471 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
472 std::string b64 = s.substr(6); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
473 std::string decoded = Toolbox::DecodeBase64(b64); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
474 size_t semicolons = decoded.find(':'); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
475 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
476 if (semicolons == std::string::npos) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
477 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
478 // Bad-formatted request |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
479 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
480 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
481 else |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
482 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
483 return decoded.substr(0, semicolons); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
484 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
485 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
486 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
487 |
0 | 488 |
489 static void* Callback(enum mg_event event, | |
490 struct mg_connection *connection, | |
491 const struct mg_request_info *request) | |
492 { | |
493 if (event == MG_NEW_REQUEST) | |
494 { | |
495 MongooseServer* that = (MongooseServer*) (request->user_data); | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
496 MongooseOutput output(connection); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
497 |
355 | 498 // Compute the method |
499 Orthanc_HttpMethod method; | |
500 if (!strcmp(request->request_method, "GET")) | |
501 { | |
502 method = Orthanc_HttpMethod_Get; | |
503 } | |
504 else if (!strcmp(request->request_method, "POST")) | |
505 { | |
506 method = Orthanc_HttpMethod_Post; | |
507 } | |
508 else if (!strcmp(request->request_method, "DELETE")) | |
509 { | |
510 method = Orthanc_HttpMethod_Delete; | |
511 } | |
512 else if (!strcmp(request->request_method, "PUT")) | |
513 { | |
514 method = Orthanc_HttpMethod_Put; | |
515 } | |
516 else | |
517 { | |
518 output.SendHeader(Orthanc_HttpStatus_405_MethodNotAllowed); | |
519 return (void*) ""; | |
520 } | |
521 | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
522 if (!that->IsRemoteAccessAllowed() && |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
523 request->remote_ip != LOCALHOST) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
524 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
525 SendUnauthorized(output); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
526 return (void*) ""; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
527 } |
0 | 528 |
529 HttpHandler::Arguments arguments, headers; | |
530 | |
531 for (int i = 0; i < request->num_headers; i++) | |
532 { | |
533 std::string name = request->http_headers[i].name; | |
534 std::transform(name.begin(), name.end(), name.begin(), ::tolower); | |
535 headers.insert(std::make_pair(name, request->http_headers[i].value)); | |
536 } | |
537 | |
23 | 538 // Authenticate this connection |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
539 if (that->IsAuthenticationEnabled() && |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
540 !Authorize(*that, headers, output)) |
23 | 541 { |
542 return (void*) ""; | |
543 } | |
544 | |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
545 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
546 // Apply the filter, if it is installed |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
547 const IIncomingHttpRequestFilter *filter = that->GetIncomingHttpRequestFilter(); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
548 if (filter != NULL) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
549 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
550 std::string username = GetAuthenticatedUsername(headers); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
551 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
552 char remoteIp[24]; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
553 sprintf(remoteIp, "%d.%d.%d.%d", |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
554 reinterpret_cast<const uint8_t*>(&request->remote_ip) [3], |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
555 reinterpret_cast<const uint8_t*>(&request->remote_ip) [2], |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
556 reinterpret_cast<const uint8_t*>(&request->remote_ip) [1], |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
557 reinterpret_cast<const uint8_t*>(&request->remote_ip) [0]); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
558 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
559 if (!filter->IsAllowed(method, request->uri, remoteIp, username.c_str())) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
560 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
561 SendUnauthorized(output); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
562 return (void*) ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
563 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
564 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
565 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
566 |
0 | 567 std::string postData; |
568 | |
355 | 569 if (method == Orthanc_HttpMethod_Get) |
0 | 570 { |
571 HttpHandler::ParseGetQuery(arguments, request->query_string); | |
572 } | |
355 | 573 else if (method == Orthanc_HttpMethod_Post || |
574 method == Orthanc_HttpMethod_Put) | |
0 | 575 { |
576 HttpHandler::Arguments::const_iterator ct = headers.find("content-type"); | |
577 if (ct == headers.end()) | |
578 { | |
59 | 579 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
0 | 580 return (void*) ""; |
581 } | |
582 | |
583 PostDataStatus status; | |
584 | |
585 std::string contentType = ct->second; | |
586 if (contentType.size() >= multipartLength && | |
587 !memcmp(contentType.c_str(), multipart, multipartLength)) | |
588 { | |
589 status = ParseMultipartPost(postData, connection, headers, contentType, that->GetChunkStore()); | |
590 } | |
591 else | |
592 { | |
593 status = ReadPostData(postData, connection, headers); | |
594 } | |
595 | |
596 switch (status) | |
597 { | |
598 case PostDataStatus_NoLength: | |
59 | 599 output.SendHeader(Orthanc_HttpStatus_411_LengthRequired); |
0 | 600 return (void*) ""; |
601 | |
602 case PostDataStatus_Failure: | |
59 | 603 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
0 | 604 return (void*) ""; |
605 | |
606 case PostDataStatus_Pending: | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
607 output.AnswerBufferWithContentType(NULL, 0, ""); |
0 | 608 return (void*) ""; |
609 | |
610 default: | |
611 break; | |
612 } | |
613 } | |
614 | |
615 UriComponents uri; | |
616 Toolbox::SplitUriComponents(uri, request->uri); | |
617 | |
618 HttpHandler* handler = that->FindHandler(uri); | |
619 if (handler) | |
620 { | |
621 try | |
622 { | |
355 | 623 handler->Handle(output, method, uri, headers, arguments, postData); |
0 | 624 } |
59 | 625 catch (OrthancException& e) |
0 | 626 { |
108 | 627 LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; |
59 | 628 output.SendHeader(Orthanc_HttpStatus_500_InternalServerError); |
0 | 629 } |
327
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
630 catch (boost::bad_lexical_cast&) |
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
631 { |
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
632 LOG(ERROR) << "MongooseServer Exception: Bad lexical cast"; |
333 | 633 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
634 } | |
635 catch (std::runtime_error&) | |
636 { | |
637 LOG(ERROR) << "MongooseServer Exception: Presumably a bad JSON request"; | |
638 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); | |
327
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
639 } |
0 | 640 } |
641 else | |
642 { | |
59 | 643 output.SendHeader(Orthanc_HttpStatus_404_NotFound); |
0 | 644 } |
645 | |
646 // Mark as processed | |
647 return (void*) ""; | |
648 } | |
649 else | |
650 { | |
651 return NULL; | |
652 } | |
653 } | |
654 | |
655 | |
656 bool MongooseServer::IsRunning() const | |
657 { | |
658 return (pimpl_->context_ != NULL); | |
659 } | |
660 | |
661 | |
662 MongooseServer::MongooseServer() : pimpl_(new PImpl) | |
663 { | |
664 pimpl_->context_ = NULL; | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
665 remoteAllowed_ = false; |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
666 authentication_ = false; |
23 | 667 ssl_ = false; |
0 | 668 port_ = 8000; |
669 } | |
670 | |
671 | |
672 MongooseServer::~MongooseServer() | |
673 { | |
674 Stop(); | |
675 ClearHandlers(); | |
676 } | |
677 | |
678 | |
128 | 679 void MongooseServer::SetPortNumber(uint16_t port) |
0 | 680 { |
681 Stop(); | |
682 port_ = port; | |
683 } | |
684 | |
685 void MongooseServer::Start() | |
686 { | |
687 if (!IsRunning()) | |
688 { | |
689 std::string port = boost::lexical_cast<std::string>(port_); | |
690 | |
23 | 691 if (ssl_) |
692 { | |
693 port += "s"; | |
694 } | |
695 | |
0 | 696 const char *options[] = { |
697 "listening_ports", port.c_str(), | |
23 | 698 ssl_ ? "ssl_certificate" : NULL, |
699 certificate_.c_str(), | |
0 | 700 NULL |
701 }; | |
702 | |
703 pimpl_->context_ = mg_start(&Callback, this, options); | |
704 if (!pimpl_->context_) | |
705 { | |
59 | 706 throw OrthancException("Unable to launch the Mongoose server"); |
0 | 707 } |
708 } | |
709 } | |
710 | |
711 void MongooseServer::Stop() | |
712 { | |
713 if (IsRunning()) | |
714 { | |
715 mg_stop(pimpl_->context_); | |
716 pimpl_->context_ = NULL; | |
717 } | |
718 } | |
719 | |
720 | |
721 void MongooseServer::RegisterHandler(HttpHandler* handler) | |
722 { | |
723 Stop(); | |
724 | |
725 handlers_.push_back(handler); | |
726 } | |
727 | |
728 | |
729 void MongooseServer::ClearHandlers() | |
730 { | |
731 Stop(); | |
732 | |
733 for (Handlers::iterator it = | |
734 handlers_.begin(); it != handlers_.end(); it++) | |
735 { | |
736 delete *it; | |
737 } | |
738 } | |
739 | |
23 | 740 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
741 void MongooseServer::ClearUsers() |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
742 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
743 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
744 registeredUsers_.clear(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
745 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
746 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
747 |
23 | 748 void MongooseServer::RegisterUser(const char* username, |
749 const char* password) | |
750 { | |
751 Stop(); | |
24 | 752 |
753 std::string tag = std::string(username) + ":" + std::string(password); | |
754 registeredUsers_.insert(Toolbox::EncodeBase64(tag)); | |
23 | 755 } |
756 | |
757 void MongooseServer::SetSslEnabled(bool enabled) | |
758 { | |
759 Stop(); | |
760 | |
59 | 761 #if ORTHANC_SSL_ENABLED == 0 |
23 | 762 if (enabled) |
763 { | |
59 | 764 throw OrthancException("Orthanc has been built without SSL support"); |
23 | 765 } |
766 else | |
767 { | |
768 ssl_ = false; | |
769 } | |
770 #else | |
771 ssl_ = enabled; | |
772 #endif | |
773 } | |
774 | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
775 void MongooseServer::SetAuthenticationEnabled(bool enabled) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
776 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
777 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
778 authentication_ = enabled; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
779 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
780 |
23 | 781 void MongooseServer::SetSslCertificate(const char* path) |
782 { | |
783 Stop(); | |
784 certificate_ = path; | |
785 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
786 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
787 void MongooseServer::SetRemoteAccessAllowed(bool allowed) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
788 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
789 Stop(); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
790 remoteAllowed_ = allowed; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
791 } |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
792 |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
793 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
794 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
795 Stop(); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
796 filter_ = &filter; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
797 } |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
798 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
799 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
800 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
801 return registeredUsers_.find(basic) != registeredUsers_.end(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
802 } |
0 | 803 } |