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