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