Mercurial > hg > orthanc
annotate Core/HttpServer/MongooseServer.cpp @ 896:c4053ac5db04 plugins
better plugni api
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jun 2014 09:57:02 +0200 |
parents | 7e8cde5905fd |
children | bb0a51561016 |
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 | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
258 void MongooseServer::FindHandlers(std::list<HttpHandler*>& result, |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
259 const UriComponents& forUri) const |
0 | 260 { |
261 for (Handlers::const_iterator it = | |
656 | 262 handlers_.begin(); it != handlers_.end(); ++it) |
0 | 263 { |
264 if ((*it)->IsServedUri(forUri)) | |
265 { | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
266 result.push_back(*it); |
0 | 267 } |
268 } | |
269 } | |
270 | |
271 | |
272 | |
273 | |
416 | 274 static PostDataStatus ReadBody(std::string& postData, |
275 struct mg_connection *connection, | |
276 const HttpHandler::Arguments& headers) | |
0 | 277 { |
278 HttpHandler::Arguments::const_iterator cs = headers.find("content-length"); | |
279 if (cs == headers.end()) | |
280 { | |
281 return PostDataStatus_NoLength; | |
282 } | |
283 | |
284 int length; | |
285 try | |
286 { | |
287 length = boost::lexical_cast<int>(cs->second); | |
288 } | |
289 catch (boost::bad_lexical_cast) | |
290 { | |
291 return PostDataStatus_NoLength; | |
292 } | |
293 | |
294 if (length < 0) | |
295 { | |
296 length = 0; | |
297 } | |
298 | |
299 postData.resize(length); | |
300 | |
301 size_t pos = 0; | |
302 while (length > 0) | |
303 { | |
304 int r = mg_read(connection, &postData[pos], length); | |
305 if (r <= 0) | |
306 { | |
307 return PostDataStatus_Failure; | |
308 } | |
418
b79bf2f4ab2e
execution of lua through REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
417
diff
changeset
|
309 |
8 | 310 assert(r <= length); |
0 | 311 length -= r; |
312 pos += r; | |
313 } | |
314 | |
315 return PostDataStatus_Success; | |
316 } | |
317 | |
318 | |
319 | |
320 static PostDataStatus ParseMultipartPost(std::string &completedFile, | |
321 struct mg_connection *connection, | |
322 const HttpHandler::Arguments& headers, | |
323 const std::string& contentType, | |
324 ChunkStore& chunkStore) | |
325 { | |
326 std::string boundary = "--" + contentType.substr(multipartLength); | |
327 | |
328 std::string postData; | |
416 | 329 PostDataStatus status = ReadBody(postData, connection, headers); |
0 | 330 |
331 if (status != PostDataStatus_Success) | |
332 { | |
333 return status; | |
334 } | |
335 | |
336 /*for (HttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) | |
337 { | |
338 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; | |
339 } | |
340 printf("CHUNK\n");*/ | |
341 | |
342 typedef HttpHandler::Arguments::const_iterator ArgumentIterator; | |
343 | |
344 ArgumentIterator requestedWith = headers.find("x-requested-with"); | |
345 ArgumentIterator fileName = headers.find("x-file-name"); | |
346 ArgumentIterator fileSizeStr = headers.find("x-file-size"); | |
347 | |
338
3a3b3ba8c1e0
fix for uploads through internet explorer 7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
333
diff
changeset
|
348 if (requestedWith != headers.end() && |
0 | 349 requestedWith->second != "XMLHttpRequest") |
350 { | |
351 return PostDataStatus_Failure; | |
352 } | |
353 | |
354 size_t fileSize = 0; | |
355 if (fileSizeStr != headers.end()) | |
356 { | |
357 try | |
358 { | |
359 fileSize = boost::lexical_cast<size_t>(fileSizeStr->second); | |
360 } | |
361 catch (boost::bad_lexical_cast) | |
362 { | |
363 return PostDataStatus_Failure; | |
364 } | |
365 } | |
366 | |
367 typedef boost::find_iterator<std::string::iterator> FindIterator; | |
10 | 368 typedef boost::iterator_range<char*> Range; |
0 | 369 |
370 //chunkStore.Print(); | |
371 | |
372 try | |
373 { | |
374 FindIterator last; | |
375 for (FindIterator it = | |
376 make_find_iterator(postData, boost::first_finder(boundary)); | |
377 it!=FindIterator(); | |
378 ++it) | |
379 { | |
380 if (last != FindIterator()) | |
381 { | |
10 | 382 Range part(&last->back(), &it->front()); |
0 | 383 Range content = boost::find_first(part, "\r\n\r\n"); |
345 | 384 if (/*content != Range()*/!content.empty()) |
0 | 385 { |
386 Range c(&content.back() + 1, &it->front() - 2); | |
387 size_t chunkSize = c.size(); | |
388 | |
389 if (chunkSize > 0) | |
390 { | |
391 const char* chunkData = &c.front(); | |
392 | |
393 if (fileName == headers.end()) | |
394 { | |
395 // This file is stored in a single chunk | |
396 completedFile.resize(chunkSize); | |
397 if (chunkSize > 0) | |
398 { | |
399 memcpy(&completedFile[0], chunkData, chunkSize); | |
400 } | |
401 return PostDataStatus_Success; | |
402 } | |
403 else | |
404 { | |
405 return chunkStore.Store(completedFile, chunkData, chunkSize, fileName->second, fileSize); | |
406 } | |
407 } | |
10 | 408 } |
0 | 409 } |
410 | |
411 last = it; | |
412 } | |
413 } | |
414 catch (std::length_error) | |
415 { | |
416 return PostDataStatus_Failure; | |
417 } | |
418 | |
419 return PostDataStatus_Pending; | |
420 } | |
421 | |
422 | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
423 static void SendUnauthorized(HttpOutput& output) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
424 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
425 std::string s = "HTTP/1.1 401 Unauthorized\r\n" |
59 | 426 "WWW-Authenticate: Basic realm=\"" ORTHANC_REALM "\"" |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
427 "\r\n\r\n"; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
428 output.Send(&s[0], s.size()); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
429 } |
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 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
432 static bool Authorize(const MongooseServer& that, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
433 const HttpHandler::Arguments& headers, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
434 HttpOutput& output) |
23 | 435 { |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
436 bool granted = false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
437 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
438 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
439 if (auth != headers.end()) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
440 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
441 std::string s = auth->second; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
442 if (s.substr(0, 6) == "Basic ") |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
443 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
444 std::string b64 = s.substr(6); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
445 granted = that.IsValidBasicHttpAuthentication(b64); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
446 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
447 } |
23 | 448 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
449 if (!granted) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
450 { |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
451 SendUnauthorized(output); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
452 return false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
453 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
454 else |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
455 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
456 return true; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
457 } |
23 | 458 } |
459 | |
460 | |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
461 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
|
462 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
463 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
|
464 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
465 if (auth == headers.end()) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
466 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
467 return ""; |
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 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
470 std::string s = auth->second; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
471 if (s.substr(0, 6) != "Basic ") |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
472 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
473 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
474 } |
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 std::string b64 = s.substr(6); |
809
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
477 std::string decoded; |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
478 Toolbox::DecodeBase64(decoded, b64); |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
479 size_t semicolons = decoded.find(':'); |
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 if (semicolons == std::string::npos) |
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 // Bad-formatted request |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
484 return ""; |
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 else |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
487 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
488 return decoded.substr(0, semicolons); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
489 } |
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 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
493 static bool ExtractMethod(HttpMethod& method, |
414 | 494 const struct mg_request_info *request, |
495 const HttpHandler::Arguments& headers, | |
496 const HttpHandler::Arguments& argumentsGET) | |
497 { | |
498 std::string overriden; | |
499 | |
500 // Check whether some PUT/DELETE faking is done | |
501 | |
502 // 1. Faking with Google's approach | |
503 HttpHandler::Arguments::const_iterator methodOverride = | |
504 headers.find("x-http-method-override"); | |
505 | |
506 if (methodOverride != headers.end()) | |
507 { | |
508 overriden = methodOverride->second; | |
509 } | |
510 else if (!strcmp(request->request_method, "GET")) | |
511 { | |
512 // 2. Faking with Ruby on Rail's approach | |
513 // GET /my/resource?_method=delete <=> DELETE /my/resource | |
514 methodOverride = argumentsGET.find("_method"); | |
515 if (methodOverride != argumentsGET.end()) | |
516 { | |
517 overriden = methodOverride->second; | |
518 } | |
519 } | |
520 | |
521 if (overriden.size() > 0) | |
522 { | |
523 // A faking has been done within this request | |
524 Toolbox::ToUpperCase(overriden); | |
525 | |
416 | 526 LOG(INFO) << "HTTP method faking has been detected for " << overriden; |
527 | |
414 | 528 if (overriden == "PUT") |
529 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
530 method = HttpMethod_Put; |
416 | 531 return true; |
414 | 532 } |
533 else if (overriden == "DELETE") | |
534 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
535 method = HttpMethod_Delete; |
416 | 536 return true; |
414 | 537 } |
538 else | |
539 { | |
540 return false; | |
541 } | |
542 } | |
543 | |
544 // No PUT/DELETE faking was present | |
545 if (!strcmp(request->request_method, "GET")) | |
546 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
547 method = HttpMethod_Get; |
414 | 548 } |
549 else if (!strcmp(request->request_method, "POST")) | |
550 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
551 method = HttpMethod_Post; |
414 | 552 } |
553 else if (!strcmp(request->request_method, "DELETE")) | |
554 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
555 method = HttpMethod_Delete; |
414 | 556 } |
557 else if (!strcmp(request->request_method, "PUT")) | |
558 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
559 method = HttpMethod_Put; |
414 | 560 } |
561 else | |
562 { | |
563 return false; | |
564 } | |
565 | |
566 return true; | |
567 } | |
568 | |
569 | |
0 | 570 |
571 static void* Callback(enum mg_event event, | |
572 struct mg_connection *connection, | |
573 const struct mg_request_info *request) | |
574 { | |
575 if (event == MG_NEW_REQUEST) | |
576 { | |
656 | 577 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
|
578 MongooseOutput output(connection); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
579 |
414 | 580 // Check remote calls |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
581 if (!that->IsRemoteAccessAllowed() && |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
582 request->remote_ip != LOCALHOST) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
583 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
584 SendUnauthorized(output); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
585 return (void*) ""; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
586 } |
0 | 587 |
588 | |
414 | 589 // Extract the HTTP headers |
590 HttpHandler::Arguments headers; | |
0 | 591 for (int i = 0; i < request->num_headers; i++) |
592 { | |
593 std::string name = request->http_headers[i].name; | |
594 std::transform(name.begin(), name.end(), name.begin(), ::tolower); | |
595 headers.insert(std::make_pair(name, request->http_headers[i].value)); | |
596 } | |
597 | |
414 | 598 |
599 // Extract the GET arguments | |
600 HttpHandler::Arguments argumentsGET; | |
601 if (!strcmp(request->request_method, "GET")) | |
602 { | |
603 HttpHandler::ParseGetQuery(argumentsGET, request->query_string); | |
604 } | |
605 | |
606 | |
607 // 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
|
608 HttpMethod method; |
414 | 609 if (!ExtractMethod(method, request, headers, argumentsGET)) |
610 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
611 output.SendHeader(HttpStatus_400_BadRequest); |
414 | 612 return (void*) ""; |
613 } | |
614 | |
615 | |
23 | 616 // Authenticate this connection |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
617 if (that->IsAuthenticationEnabled() && |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
618 !Authorize(*that, headers, output)) |
23 | 619 { |
620 return (void*) ""; | |
621 } | |
622 | |
409
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 // Apply the filter, if it is installed |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
625 const IIncomingHttpRequestFilter *filter = that->GetIncomingHttpRequestFilter(); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
626 if (filter != NULL) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
627 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
628 std::string username = GetAuthenticatedUsername(headers); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
629 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
630 char remoteIp[24]; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
631 sprintf(remoteIp, "%d.%d.%d.%d", |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
632 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
|
633 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
|
634 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
|
635 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
|
636 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
637 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
|
638 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
639 SendUnauthorized(output); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
640 return (void*) ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
641 } |
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 |
414 | 645 // Extract the body of the request for PUT and POST |
646 std::string body; | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
647 if (method == HttpMethod_Post || |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
648 method == HttpMethod_Put) |
0 | 649 { |
416 | 650 PostDataStatus status; |
651 | |
0 | 652 HttpHandler::Arguments::const_iterator ct = headers.find("content-type"); |
653 if (ct == headers.end()) | |
654 { | |
416 | 655 // No content-type specified. Assume no multi-part content occurs at this point. |
656 status = ReadBody(body, connection, headers); | |
0 | 657 } |
658 else | |
659 { | |
416 | 660 std::string contentType = ct->second; |
661 if (contentType.size() >= multipartLength && | |
662 !memcmp(contentType.c_str(), multipart, multipartLength)) | |
663 { | |
664 status = ParseMultipartPost(body, connection, headers, contentType, that->GetChunkStore()); | |
665 } | |
666 else | |
667 { | |
668 status = ReadBody(body, connection, headers); | |
669 } | |
0 | 670 } |
671 | |
672 switch (status) | |
673 { | |
416 | 674 case PostDataStatus_NoLength: |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
675 output.SendHeader(HttpStatus_411_LengthRequired); |
416 | 676 return (void*) ""; |
0 | 677 |
416 | 678 case PostDataStatus_Failure: |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
679 output.SendHeader(HttpStatus_400_BadRequest); |
416 | 680 return (void*) ""; |
0 | 681 |
416 | 682 case PostDataStatus_Pending: |
683 output.AnswerBufferWithContentType(NULL, 0, ""); | |
684 return (void*) ""; | |
0 | 685 |
416 | 686 default: |
687 break; | |
0 | 688 } |
689 } | |
690 | |
414 | 691 |
896 | 692 // Decompose the URI into its components |
0 | 693 UriComponents uri; |
415 | 694 try |
695 { | |
696 Toolbox::SplitUriComponents(uri, request->uri); | |
697 } | |
698 catch (OrthancException) | |
699 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
700 output.SendHeader(HttpStatus_400_BadRequest); |
415 | 701 return (void*) ""; |
702 } | |
703 | |
0 | 704 |
896 | 705 // Locate the candidate handlers for this URI |
706 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
707 std::list<HttpHandler*> handlers; |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
708 that->FindHandlers(handlers, uri); |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
709 |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
710 bool found = false; |
896 | 711 bool isError = false; |
712 HttpStatus errorStatus; | |
713 std::string errorDescription; | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
714 |
896 | 715 |
716 // Loop over the candidate handlers for this URI | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
717 for (std::list<HttpHandler*>::const_iterator |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
718 it = handlers.begin(); it != handlers.end() && !found; it++) |
0 | 719 { |
720 try | |
721 { | |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
722 found = (*it)->Handle(output, method, uri, headers, argumentsGET, body); |
0 | 723 } |
59 | 724 catch (OrthancException& e) |
0 | 725 { |
896 | 726 // Using this candidate handler results in an exception, try |
727 // another handler before failing | |
728 isError = true; | |
729 errorStatus = HttpStatus_500_InternalServerError; | |
730 errorDescription = e.What(); | |
0 | 731 } |
327
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
732 catch (boost::bad_lexical_cast&) |
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
733 { |
896 | 734 isError = true; |
735 errorStatus = HttpStatus_400_BadRequest; | |
736 errorDescription = "Bad lexical cast"; | |
333 | 737 } |
738 catch (std::runtime_error&) | |
739 { | |
896 | 740 isError = true; |
741 errorStatus = HttpStatus_400_BadRequest; | |
742 errorDescription = "Presumably a bad JSON request"; | |
327
4564e908bba9
handling of bad lexical casts in http server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
324
diff
changeset
|
743 } |
0 | 744 } |
895
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
745 |
7e8cde5905fd
allow superposition of REST handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
746 if (!found) |
0 | 747 { |
896 | 748 if (isError) |
749 { | |
750 LOG(ERROR) << "Exception in the HTTP handler: " << errorDescription; | |
751 output.SendHeader(errorStatus); | |
752 } | |
753 else | |
754 { | |
755 output.SendHeader(HttpStatus_404_NotFound); | |
756 } | |
0 | 757 } |
758 | |
896 | 759 |
0 | 760 // Mark as processed |
761 return (void*) ""; | |
762 } | |
763 else | |
764 { | |
765 return NULL; | |
766 } | |
767 } | |
768 | |
769 | |
770 bool MongooseServer::IsRunning() const | |
771 { | |
772 return (pimpl_->context_ != NULL); | |
773 } | |
774 | |
775 | |
776 MongooseServer::MongooseServer() : pimpl_(new PImpl) | |
777 { | |
778 pimpl_->context_ = NULL; | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
779 remoteAllowed_ = false; |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
780 authentication_ = false; |
23 | 781 ssl_ = false; |
0 | 782 port_ = 8000; |
417 | 783 filter_ = NULL; |
748
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
784 |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
785 #if ORTHANC_SSL_ENABLED == 1 |
749 | 786 // 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
|
787 // 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
|
788 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
|
789 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
|
790 { |
749 | 791 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
|
792 } |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
793 #endif |
0 | 794 } |
795 | |
796 | |
797 MongooseServer::~MongooseServer() | |
798 { | |
799 Stop(); | |
800 ClearHandlers(); | |
801 } | |
802 | |
803 | |
128 | 804 void MongooseServer::SetPortNumber(uint16_t port) |
0 | 805 { |
806 Stop(); | |
807 port_ = port; | |
808 } | |
809 | |
810 void MongooseServer::Start() | |
811 { | |
812 if (!IsRunning()) | |
813 { | |
814 std::string port = boost::lexical_cast<std::string>(port_); | |
815 | |
23 | 816 if (ssl_) |
817 { | |
818 port += "s"; | |
819 } | |
820 | |
0 | 821 const char *options[] = { |
822 "listening_ports", port.c_str(), | |
23 | 823 ssl_ ? "ssl_certificate" : NULL, |
824 certificate_.c_str(), | |
0 | 825 NULL |
826 }; | |
827 | |
828 pimpl_->context_ = mg_start(&Callback, this, options); | |
829 if (!pimpl_->context_) | |
830 { | |
59 | 831 throw OrthancException("Unable to launch the Mongoose server"); |
0 | 832 } |
833 } | |
834 } | |
835 | |
836 void MongooseServer::Stop() | |
837 { | |
838 if (IsRunning()) | |
839 { | |
840 mg_stop(pimpl_->context_); | |
841 pimpl_->context_ = NULL; | |
842 } | |
843 } | |
844 | |
845 | |
846 void MongooseServer::RegisterHandler(HttpHandler* handler) | |
847 { | |
848 Stop(); | |
849 | |
850 handlers_.push_back(handler); | |
851 } | |
852 | |
853 | |
854 void MongooseServer::ClearHandlers() | |
855 { | |
856 Stop(); | |
857 | |
858 for (Handlers::iterator it = | |
656 | 859 handlers_.begin(); it != handlers_.end(); ++it) |
0 | 860 { |
861 delete *it; | |
862 } | |
863 } | |
864 | |
23 | 865 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
866 void MongooseServer::ClearUsers() |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
867 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
868 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
869 registeredUsers_.clear(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
870 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
871 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
872 |
23 | 873 void MongooseServer::RegisterUser(const char* username, |
874 const char* password) | |
875 { | |
876 Stop(); | |
24 | 877 |
878 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
|
879 std::string encoded; |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
880 Toolbox::EncodeBase64(encoded, tag); |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
881 registeredUsers_.insert(encoded); |
23 | 882 } |
883 | |
884 void MongooseServer::SetSslEnabled(bool enabled) | |
885 { | |
886 Stop(); | |
887 | |
59 | 888 #if ORTHANC_SSL_ENABLED == 0 |
23 | 889 if (enabled) |
890 { | |
59 | 891 throw OrthancException("Orthanc has been built without SSL support"); |
23 | 892 } |
893 else | |
894 { | |
895 ssl_ = false; | |
896 } | |
897 #else | |
898 ssl_ = enabled; | |
899 #endif | |
900 } | |
901 | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
902 void MongooseServer::SetAuthenticationEnabled(bool enabled) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
903 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
904 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
905 authentication_ = enabled; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
906 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
907 |
23 | 908 void MongooseServer::SetSslCertificate(const char* path) |
909 { | |
910 Stop(); | |
911 certificate_ = path; | |
912 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
913 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
914 void MongooseServer::SetRemoteAccessAllowed(bool allowed) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
915 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
916 Stop(); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
917 remoteAllowed_ = allowed; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
918 } |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
919 |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
920 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
921 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
922 Stop(); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
923 filter_ = &filter; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
924 } |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
925 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
926 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
927 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
928 return registeredUsers_.find(basic) != registeredUsers_.end(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
929 } |
0 | 930 } |