Mercurial > hg > orthanc
annotate Core/HttpServer/MongooseServer.cpp @ 232:5368bbe813cf
refactoring of attachments
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 30 Nov 2012 14:22:27 +0100 |
parents | 1ac3aacd10a5 |
children | 64925c94825c |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 // http://en.highscore.de/cpp/boost/stringhandling.html | |
34 | |
35 #include "MongooseServer.h" | |
36 | |
37 #include <algorithm> | |
38 #include <string.h> | |
39 #include <boost/lexical_cast.hpp> | |
40 #include <boost/algorithm/string.hpp> | |
41 #include <iostream> | |
42 #include <string.h> | |
43 #include <stdio.h> | |
44 #include <boost/thread.hpp> | |
108 | 45 #include <glog/logging.h> |
0 | 46 |
59 | 47 #include "../OrthancException.h" |
0 | 48 #include "../ChunkedBuffer.h" |
49 #include "mongoose.h" | |
50 | |
51 | |
59 | 52 #define ORTHANC_REALM "Orthanc Secure Area" |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
53 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
54 static const long LOCALHOST = (127ll << 24) + 1ll; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
55 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
56 |
59 | 57 namespace Orthanc |
0 | 58 { |
59 static const char multipart[] = "multipart/form-data; boundary="; | |
60 static unsigned int multipartLength = sizeof(multipart) / sizeof(char) - 1; | |
61 | |
62 | |
63 namespace | |
64 { | |
65 // Anonymous namespace to avoid clashes between compilation modules | |
66 class MongooseOutput : public HttpOutput | |
67 { | |
68 private: | |
69 struct mg_connection* connection_; | |
70 | |
71 public: | |
72 MongooseOutput(struct mg_connection* connection) : connection_(connection) | |
73 { | |
74 } | |
75 | |
76 virtual void Send(const void* buffer, size_t length) | |
77 { | |
217 | 78 if (length > 0) |
79 { | |
80 mg_write(connection_, buffer, length); | |
81 } | |
0 | 82 } |
83 }; | |
84 | |
85 | |
86 enum PostDataStatus | |
87 { | |
88 PostDataStatus_Success, | |
89 PostDataStatus_NoLength, | |
90 PostDataStatus_Pending, | |
91 PostDataStatus_Failure | |
92 }; | |
93 } | |
94 | |
95 | |
96 // TODO Move this to external file | |
97 | |
98 | |
99 class ChunkedFile : public ChunkedBuffer | |
100 { | |
101 private: | |
102 std::string filename_; | |
103 | |
104 public: | |
105 ChunkedFile(const std::string& filename) : | |
106 filename_(filename) | |
107 { | |
108 } | |
109 | |
110 const std::string& GetFilename() const | |
111 { | |
112 return filename_; | |
113 } | |
114 }; | |
115 | |
116 | |
117 | |
118 class ChunkStore | |
119 { | |
120 private: | |
121 typedef std::list<ChunkedFile*> Content; | |
122 Content content_; | |
123 unsigned int numPlaces_; | |
124 | |
125 boost::mutex mutex_; | |
126 std::set<std::string> discardedFiles_; | |
127 | |
128 void Clear() | |
129 { | |
130 for (Content::iterator it = content_.begin(); | |
131 it != content_.end(); it++) | |
132 { | |
133 delete *it; | |
134 } | |
135 } | |
136 | |
137 Content::iterator Find(const std::string& filename) | |
138 { | |
139 for (Content::iterator it = content_.begin(); | |
140 it != content_.end(); it++) | |
141 { | |
142 if ((*it)->GetFilename() == filename) | |
143 { | |
144 return it; | |
145 } | |
146 } | |
147 | |
148 return content_.end(); | |
149 } | |
150 | |
151 void Remove(const std::string& filename) | |
152 { | |
153 Content::iterator it = Find(filename); | |
154 if (it != content_.end()) | |
155 { | |
156 delete *it; | |
157 content_.erase(it); | |
158 } | |
159 } | |
160 | |
161 public: | |
162 ChunkStore() | |
163 { | |
164 numPlaces_ = 10; | |
165 } | |
166 | |
167 ~ChunkStore() | |
168 { | |
169 Clear(); | |
170 } | |
171 | |
172 PostDataStatus Store(std::string& completed, | |
173 const char* chunkData, | |
174 size_t chunkSize, | |
175 const std::string& filename, | |
176 size_t filesize) | |
177 { | |
178 boost::mutex::scoped_lock lock(mutex_); | |
179 | |
180 std::set<std::string>::iterator wasDiscarded = discardedFiles_.find(filename); | |
181 if (wasDiscarded != discardedFiles_.end()) | |
182 { | |
183 discardedFiles_.erase(wasDiscarded); | |
184 return PostDataStatus_Failure; | |
185 } | |
186 | |
187 ChunkedFile* f; | |
188 Content::iterator it = Find(filename); | |
189 if (it == content_.end()) | |
190 { | |
191 f = new ChunkedFile(filename); | |
192 | |
193 // Make some room | |
194 if (content_.size() >= numPlaces_) | |
195 { | |
196 discardedFiles_.insert(content_.front()->GetFilename()); | |
197 delete content_.front(); | |
198 content_.pop_front(); | |
199 } | |
200 | |
201 content_.push_back(f); | |
202 } | |
203 else | |
204 { | |
205 f = *it; | |
206 } | |
207 | |
208 f->AddChunk(chunkData, chunkSize); | |
209 | |
210 if (f->GetNumBytes() > filesize) | |
211 { | |
212 Remove(filename); | |
213 } | |
214 else if (f->GetNumBytes() == filesize) | |
215 { | |
216 f->Flatten(completed); | |
217 Remove(filename); | |
218 return PostDataStatus_Success; | |
219 } | |
220 | |
221 return PostDataStatus_Pending; | |
222 } | |
223 | |
224 /*void Print() | |
225 { | |
226 boost::mutex::scoped_lock lock(mutex_); | |
227 | |
228 printf("ChunkStore status:\n"); | |
229 for (Content::const_iterator i = content_.begin(); | |
230 i != content_.end(); i++) | |
231 { | |
232 printf(" [%s]: %d\n", (*i)->GetFilename().c_str(), (*i)->GetNumBytes()); | |
233 } | |
234 printf("-----\n"); | |
235 }*/ | |
236 }; | |
237 | |
238 | |
239 struct MongooseServer::PImpl | |
240 { | |
241 struct mg_context *context_; | |
242 ChunkStore chunkStore_; | |
243 }; | |
244 | |
245 | |
246 ChunkStore& MongooseServer::GetChunkStore() | |
247 { | |
248 return pimpl_->chunkStore_; | |
249 } | |
250 | |
251 | |
252 | |
253 HttpHandler* MongooseServer::FindHandler(const UriComponents& forUri) const | |
254 { | |
255 for (Handlers::const_iterator it = | |
256 handlers_.begin(); it != handlers_.end(); it++) | |
257 { | |
258 if ((*it)->IsServedUri(forUri)) | |
259 { | |
260 return *it; | |
261 } | |
262 } | |
263 | |
264 return NULL; | |
265 } | |
266 | |
267 | |
268 | |
269 | |
270 static PostDataStatus ReadPostData(std::string& postData, | |
271 struct mg_connection *connection, | |
272 const HttpHandler::Arguments& headers) | |
273 { | |
274 HttpHandler::Arguments::const_iterator cs = headers.find("content-length"); | |
275 if (cs == headers.end()) | |
276 { | |
277 return PostDataStatus_NoLength; | |
278 } | |
279 | |
280 int length; | |
281 try | |
282 { | |
283 length = boost::lexical_cast<int>(cs->second); | |
284 } | |
285 catch (boost::bad_lexical_cast) | |
286 { | |
287 return PostDataStatus_NoLength; | |
288 } | |
289 | |
290 if (length < 0) | |
291 { | |
292 length = 0; | |
293 } | |
294 | |
295 postData.resize(length); | |
296 | |
297 size_t pos = 0; | |
298 while (length > 0) | |
299 { | |
300 int r = mg_read(connection, &postData[pos], length); | |
301 if (r <= 0) | |
302 { | |
303 return PostDataStatus_Failure; | |
304 } | |
8 | 305 assert(r <= length); |
0 | 306 length -= r; |
307 pos += r; | |
308 } | |
309 | |
310 return PostDataStatus_Success; | |
311 } | |
312 | |
313 | |
314 | |
315 static PostDataStatus ParseMultipartPost(std::string &completedFile, | |
316 struct mg_connection *connection, | |
317 const HttpHandler::Arguments& headers, | |
318 const std::string& contentType, | |
319 ChunkStore& chunkStore) | |
320 { | |
321 std::string boundary = "--" + contentType.substr(multipartLength); | |
322 | |
323 std::string postData; | |
324 PostDataStatus status = ReadPostData(postData, connection, headers); | |
325 | |
326 if (status != PostDataStatus_Success) | |
327 { | |
328 return status; | |
329 } | |
330 | |
331 /*for (HttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) | |
332 { | |
333 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; | |
334 } | |
335 printf("CHUNK\n");*/ | |
336 | |
337 typedef HttpHandler::Arguments::const_iterator ArgumentIterator; | |
338 | |
339 ArgumentIterator requestedWith = headers.find("x-requested-with"); | |
340 ArgumentIterator fileName = headers.find("x-file-name"); | |
341 ArgumentIterator fileSizeStr = headers.find("x-file-size"); | |
342 | |
343 if (requestedWith == headers.end() || | |
344 requestedWith->second != "XMLHttpRequest") | |
345 { | |
346 return PostDataStatus_Failure; | |
347 } | |
348 | |
349 size_t fileSize = 0; | |
350 if (fileSizeStr != headers.end()) | |
351 { | |
352 try | |
353 { | |
354 fileSize = boost::lexical_cast<size_t>(fileSizeStr->second); | |
355 } | |
356 catch (boost::bad_lexical_cast) | |
357 { | |
358 return PostDataStatus_Failure; | |
359 } | |
360 } | |
361 | |
362 typedef boost::find_iterator<std::string::iterator> FindIterator; | |
10 | 363 typedef boost::iterator_range<char*> Range; |
0 | 364 |
365 //chunkStore.Print(); | |
366 | |
367 try | |
368 { | |
369 FindIterator last; | |
370 for (FindIterator it = | |
371 make_find_iterator(postData, boost::first_finder(boundary)); | |
372 it!=FindIterator(); | |
373 ++it) | |
374 { | |
375 if (last != FindIterator()) | |
376 { | |
10 | 377 Range part(&last->back(), &it->front()); |
0 | 378 Range content = boost::find_first(part, "\r\n\r\n"); |
379 if (content != Range()) | |
380 { | |
381 Range c(&content.back() + 1, &it->front() - 2); | |
382 size_t chunkSize = c.size(); | |
383 | |
384 if (chunkSize > 0) | |
385 { | |
386 const char* chunkData = &c.front(); | |
387 | |
388 if (fileName == headers.end()) | |
389 { | |
390 // This file is stored in a single chunk | |
391 completedFile.resize(chunkSize); | |
392 if (chunkSize > 0) | |
393 { | |
394 memcpy(&completedFile[0], chunkData, chunkSize); | |
395 } | |
396 return PostDataStatus_Success; | |
397 } | |
398 else | |
399 { | |
400 return chunkStore.Store(completedFile, chunkData, chunkSize, fileName->second, fileSize); | |
401 } | |
402 } | |
10 | 403 } |
0 | 404 } |
405 | |
406 last = it; | |
407 } | |
408 } | |
409 catch (std::length_error) | |
410 { | |
411 return PostDataStatus_Failure; | |
412 } | |
413 | |
414 return PostDataStatus_Pending; | |
415 } | |
416 | |
417 | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
418 static void SendUnauthorized(HttpOutput& output) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
419 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
420 std::string s = "HTTP/1.1 401 Unauthorized\r\n" |
59 | 421 "WWW-Authenticate: Basic realm=\"" ORTHANC_REALM "\"" |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
422 "\r\n\r\n"; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
423 output.Send(&s[0], s.size()); |
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 |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
426 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
427 static bool Authorize(const MongooseServer& that, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
428 const HttpHandler::Arguments& headers, |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
429 HttpOutput& output) |
23 | 430 { |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
431 bool granted = false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
432 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
433 HttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
434 if (auth != headers.end()) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
435 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
436 std::string s = auth->second; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
437 if (s.substr(0, 6) == "Basic ") |
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 std::string b64 = s.substr(6); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
440 granted = that.IsValidBasicHttpAuthentication(b64); |
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 } |
23 | 443 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
444 if (!granted) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
445 { |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
446 SendUnauthorized(output); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
447 return false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
448 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
449 else |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
450 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
451 return true; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
452 } |
23 | 453 } |
454 | |
455 | |
0 | 456 |
457 static void* Callback(enum mg_event event, | |
458 struct mg_connection *connection, | |
459 const struct mg_request_info *request) | |
460 { | |
461 if (event == MG_NEW_REQUEST) | |
462 { | |
463 MongooseServer* that = (MongooseServer*) (request->user_data); | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
464 MongooseOutput output(connection); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
465 |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
466 if (!that->IsRemoteAccessAllowed() && |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
467 request->remote_ip != LOCALHOST) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
468 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
469 SendUnauthorized(output); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
470 return (void*) ""; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
471 } |
0 | 472 |
473 HttpHandler::Arguments arguments, headers; | |
474 | |
475 for (int i = 0; i < request->num_headers; i++) | |
476 { | |
477 std::string name = request->http_headers[i].name; | |
478 std::transform(name.begin(), name.end(), name.begin(), ::tolower); | |
479 headers.insert(std::make_pair(name, request->http_headers[i].value)); | |
480 } | |
481 | |
23 | 482 // Authenticate this connection |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
483 if (that->IsAuthenticationEnabled() && |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
484 !Authorize(*that, headers, output)) |
23 | 485 { |
486 return (void*) ""; | |
487 } | |
488 | |
0 | 489 std::string postData; |
490 | |
491 if (!strcmp(request->request_method, "GET")) | |
492 { | |
493 HttpHandler::ParseGetQuery(arguments, request->query_string); | |
494 } | |
207 | 495 else if (!strcmp(request->request_method, "POST") || |
496 !strcmp(request->request_method, "PUT")) | |
0 | 497 { |
498 HttpHandler::Arguments::const_iterator ct = headers.find("content-type"); | |
499 if (ct == headers.end()) | |
500 { | |
59 | 501 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
0 | 502 return (void*) ""; |
503 } | |
504 | |
505 PostDataStatus status; | |
506 | |
507 std::string contentType = ct->second; | |
508 if (contentType.size() >= multipartLength && | |
509 !memcmp(contentType.c_str(), multipart, multipartLength)) | |
510 { | |
511 status = ParseMultipartPost(postData, connection, headers, contentType, that->GetChunkStore()); | |
512 } | |
513 else | |
514 { | |
515 status = ReadPostData(postData, connection, headers); | |
516 } | |
517 | |
518 switch (status) | |
519 { | |
520 case PostDataStatus_NoLength: | |
59 | 521 output.SendHeader(Orthanc_HttpStatus_411_LengthRequired); |
0 | 522 return (void*) ""; |
523 | |
524 case PostDataStatus_Failure: | |
59 | 525 output.SendHeader(Orthanc_HttpStatus_400_BadRequest); |
0 | 526 return (void*) ""; |
527 | |
528 case PostDataStatus_Pending: | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
529 output.AnswerBufferWithContentType(NULL, 0, ""); |
0 | 530 return (void*) ""; |
531 | |
532 default: | |
533 break; | |
534 } | |
535 } | |
536 | |
537 UriComponents uri; | |
538 Toolbox::SplitUriComponents(uri, request->uri); | |
539 | |
540 HttpHandler* handler = that->FindHandler(uri); | |
541 if (handler) | |
542 { | |
543 try | |
544 { | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
545 handler->Handle(output, std::string(request->request_method), |
0 | 546 uri, headers, arguments, postData); |
547 } | |
59 | 548 catch (OrthancException& e) |
0 | 549 { |
108 | 550 LOG(ERROR) << "MongooseServer Exception [" << e.What() << "]"; |
59 | 551 output.SendHeader(Orthanc_HttpStatus_500_InternalServerError); |
0 | 552 } |
553 } | |
554 else | |
555 { | |
59 | 556 output.SendHeader(Orthanc_HttpStatus_404_NotFound); |
0 | 557 } |
558 | |
559 // Mark as processed | |
560 return (void*) ""; | |
561 } | |
562 else | |
563 { | |
564 return NULL; | |
565 } | |
566 } | |
567 | |
568 | |
569 bool MongooseServer::IsRunning() const | |
570 { | |
571 return (pimpl_->context_ != NULL); | |
572 } | |
573 | |
574 | |
575 MongooseServer::MongooseServer() : pimpl_(new PImpl) | |
576 { | |
577 pimpl_->context_ = NULL; | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
578 remoteAllowed_ = false; |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
579 authentication_ = false; |
23 | 580 ssl_ = false; |
0 | 581 port_ = 8000; |
582 } | |
583 | |
584 | |
585 MongooseServer::~MongooseServer() | |
586 { | |
587 Stop(); | |
588 ClearHandlers(); | |
589 } | |
590 | |
591 | |
128 | 592 void MongooseServer::SetPortNumber(uint16_t port) |
0 | 593 { |
594 Stop(); | |
595 port_ = port; | |
596 } | |
597 | |
598 void MongooseServer::Start() | |
599 { | |
600 if (!IsRunning()) | |
601 { | |
602 std::string port = boost::lexical_cast<std::string>(port_); | |
603 | |
23 | 604 if (ssl_) |
605 { | |
606 port += "s"; | |
607 } | |
608 | |
0 | 609 const char *options[] = { |
610 "listening_ports", port.c_str(), | |
23 | 611 ssl_ ? "ssl_certificate" : NULL, |
612 certificate_.c_str(), | |
0 | 613 NULL |
614 }; | |
615 | |
616 pimpl_->context_ = mg_start(&Callback, this, options); | |
617 if (!pimpl_->context_) | |
618 { | |
59 | 619 throw OrthancException("Unable to launch the Mongoose server"); |
0 | 620 } |
621 } | |
622 } | |
623 | |
624 void MongooseServer::Stop() | |
625 { | |
626 if (IsRunning()) | |
627 { | |
628 mg_stop(pimpl_->context_); | |
629 pimpl_->context_ = NULL; | |
630 } | |
631 } | |
632 | |
633 | |
634 void MongooseServer::RegisterHandler(HttpHandler* handler) | |
635 { | |
636 Stop(); | |
637 | |
638 handlers_.push_back(handler); | |
639 } | |
640 | |
641 | |
642 void MongooseServer::ClearHandlers() | |
643 { | |
644 Stop(); | |
645 | |
646 for (Handlers::iterator it = | |
647 handlers_.begin(); it != handlers_.end(); it++) | |
648 { | |
649 delete *it; | |
650 } | |
651 } | |
652 | |
23 | 653 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
654 void MongooseServer::ClearUsers() |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
655 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
656 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
657 registeredUsers_.clear(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
658 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
659 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
660 |
23 | 661 void MongooseServer::RegisterUser(const char* username, |
662 const char* password) | |
663 { | |
664 Stop(); | |
24 | 665 |
666 std::string tag = std::string(username) + ":" + std::string(password); | |
667 registeredUsers_.insert(Toolbox::EncodeBase64(tag)); | |
23 | 668 } |
669 | |
670 void MongooseServer::SetSslEnabled(bool enabled) | |
671 { | |
672 Stop(); | |
673 | |
59 | 674 #if ORTHANC_SSL_ENABLED == 0 |
23 | 675 if (enabled) |
676 { | |
59 | 677 throw OrthancException("Orthanc has been built without SSL support"); |
23 | 678 } |
679 else | |
680 { | |
681 ssl_ = false; | |
682 } | |
683 #else | |
684 ssl_ = enabled; | |
685 #endif | |
686 } | |
687 | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
688 void MongooseServer::SetAuthenticationEnabled(bool enabled) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
689 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
690 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
691 authentication_ = enabled; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
692 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
693 |
23 | 694 void MongooseServer::SetSslCertificate(const char* path) |
695 { | |
696 Stop(); | |
697 certificate_ = path; | |
698 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
699 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
700 void MongooseServer::SetRemoteAccessAllowed(bool allowed) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
701 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
702 Stop(); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
703 remoteAllowed_ = allowed; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
704 } |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
705 |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
706 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
707 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
708 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
709 return registeredUsers_.find(basic) != registeredUsers_.end(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
710 } |
0 | 711 } |