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