Mercurial > hg > orthanc
annotate Core/HttpServer/MongooseServer.cpp @ 1616:644c32c07306
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Sep 2015 16:00:50 +0200 |
parents | d73124f6b439 |
children | 939b921b2c81 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1213
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1213
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 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 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
38 #include "../Logging.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
39 #include "../OrthancException.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
40 #include "../ChunkedBuffer.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
41 #include "HttpToolbox.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
42 #include "mongoose.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1446
diff
changeset
|
43 |
0 | 44 #include <algorithm> |
45 #include <string.h> | |
46 #include <boost/lexical_cast.hpp> | |
47 #include <boost/algorithm/string.hpp> | |
48 #include <iostream> | |
49 #include <string.h> | |
50 #include <stdio.h> | |
51 #include <boost/thread.hpp> | |
52 | |
748
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
53 #if ORTHANC_SSL_ENABLED == 1 |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
54 #include <openssl/opensslv.h> |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
55 #endif |
0 | 56 |
59 | 57 #define ORTHANC_REALM "Orthanc Secure Area" |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
58 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
59 static const long LOCALHOST = (127ll << 24) + 1ll; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
60 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
61 |
59 | 62 namespace Orthanc |
0 | 63 { |
64 static const char multipart[] = "multipart/form-data; boundary="; | |
65 static unsigned int multipartLength = sizeof(multipart) / sizeof(char) - 1; | |
66 | |
67 | |
68 namespace | |
69 { | |
70 // Anonymous namespace to avoid clashes between compilation modules | |
911 | 71 class MongooseOutputStream : public IHttpOutputStream |
0 | 72 { |
73 private: | |
74 struct mg_connection* connection_; | |
75 | |
911 | 76 public: |
77 MongooseOutputStream(struct mg_connection* connection) : connection_(connection) | |
78 { | |
79 } | |
80 | |
909 | 81 virtual void Send(bool isHeader, const void* buffer, size_t length) |
0 | 82 { |
217 | 83 if (length > 0) |
84 { | |
1430
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
85 int status = mg_write(connection_, buffer, length); |
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
86 if (status != static_cast<int>(length)) |
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
87 { |
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
88 // status == 0 when the connection has been closed, -1 on error |
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
89 throw OrthancException(ErrorCode_NetworkProtocol); |
ad94a3583b07
Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1363
diff
changeset
|
90 } |
217 | 91 } |
0 | 92 } |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
901
diff
changeset
|
93 |
911 | 94 virtual void OnHttpStatusReceived(HttpStatus status) |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
901
diff
changeset
|
95 { |
911 | 96 // Ignore this |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
901
diff
changeset
|
97 } |
0 | 98 }; |
99 | |
100 | |
101 enum PostDataStatus | |
102 { | |
103 PostDataStatus_Success, | |
104 PostDataStatus_NoLength, | |
105 PostDataStatus_Pending, | |
106 PostDataStatus_Failure | |
107 }; | |
108 } | |
109 | |
110 | |
111 // TODO Move this to external file | |
112 | |
113 | |
114 class ChunkedFile : public ChunkedBuffer | |
115 { | |
116 private: | |
117 std::string filename_; | |
118 | |
119 public: | |
120 ChunkedFile(const std::string& filename) : | |
121 filename_(filename) | |
122 { | |
123 } | |
124 | |
125 const std::string& GetFilename() const | |
126 { | |
127 return filename_; | |
128 } | |
129 }; | |
130 | |
131 | |
132 | |
133 class ChunkStore | |
134 { | |
135 private: | |
136 typedef std::list<ChunkedFile*> Content; | |
137 Content content_; | |
138 unsigned int numPlaces_; | |
139 | |
140 boost::mutex mutex_; | |
141 std::set<std::string> discardedFiles_; | |
142 | |
143 void Clear() | |
144 { | |
145 for (Content::iterator it = content_.begin(); | |
656 | 146 it != content_.end(); ++it) |
0 | 147 { |
148 delete *it; | |
149 } | |
150 } | |
151 | |
152 Content::iterator Find(const std::string& filename) | |
153 { | |
154 for (Content::iterator it = content_.begin(); | |
656 | 155 it != content_.end(); ++it) |
0 | 156 { |
157 if ((*it)->GetFilename() == filename) | |
158 { | |
159 return it; | |
160 } | |
161 } | |
162 | |
163 return content_.end(); | |
164 } | |
165 | |
166 void Remove(const std::string& filename) | |
167 { | |
168 Content::iterator it = Find(filename); | |
169 if (it != content_.end()) | |
170 { | |
171 delete *it; | |
172 content_.erase(it); | |
173 } | |
174 } | |
175 | |
176 public: | |
177 ChunkStore() | |
178 { | |
179 numPlaces_ = 10; | |
180 } | |
181 | |
182 ~ChunkStore() | |
183 { | |
184 Clear(); | |
185 } | |
186 | |
187 PostDataStatus Store(std::string& completed, | |
188 const char* chunkData, | |
189 size_t chunkSize, | |
190 const std::string& filename, | |
191 size_t filesize) | |
192 { | |
193 boost::mutex::scoped_lock lock(mutex_); | |
194 | |
195 std::set<std::string>::iterator wasDiscarded = discardedFiles_.find(filename); | |
196 if (wasDiscarded != discardedFiles_.end()) | |
197 { | |
198 discardedFiles_.erase(wasDiscarded); | |
199 return PostDataStatus_Failure; | |
200 } | |
201 | |
202 ChunkedFile* f; | |
203 Content::iterator it = Find(filename); | |
204 if (it == content_.end()) | |
205 { | |
206 f = new ChunkedFile(filename); | |
207 | |
208 // Make some room | |
209 if (content_.size() >= numPlaces_) | |
210 { | |
211 discardedFiles_.insert(content_.front()->GetFilename()); | |
212 delete content_.front(); | |
213 content_.pop_front(); | |
214 } | |
215 | |
216 content_.push_back(f); | |
217 } | |
218 else | |
219 { | |
220 f = *it; | |
221 } | |
222 | |
223 f->AddChunk(chunkData, chunkSize); | |
224 | |
225 if (f->GetNumBytes() > filesize) | |
226 { | |
227 Remove(filename); | |
228 } | |
229 else if (f->GetNumBytes() == filesize) | |
230 { | |
231 f->Flatten(completed); | |
232 Remove(filename); | |
233 return PostDataStatus_Success; | |
234 } | |
235 | |
236 return PostDataStatus_Pending; | |
237 } | |
238 | |
239 /*void Print() | |
240 { | |
241 boost::mutex::scoped_lock lock(mutex_); | |
242 | |
243 printf("ChunkStore status:\n"); | |
244 for (Content::const_iterator i = content_.begin(); | |
245 i != content_.end(); i++) | |
246 { | |
247 printf(" [%s]: %d\n", (*i)->GetFilename().c_str(), (*i)->GetNumBytes()); | |
248 } | |
249 printf("-----\n"); | |
250 }*/ | |
251 }; | |
252 | |
253 | |
254 struct MongooseServer::PImpl | |
255 { | |
256 struct mg_context *context_; | |
257 ChunkStore chunkStore_; | |
258 }; | |
259 | |
260 | |
261 ChunkStore& MongooseServer::GetChunkStore() | |
262 { | |
263 return pimpl_->chunkStore_; | |
264 } | |
265 | |
266 | |
267 | |
416 | 268 static PostDataStatus ReadBody(std::string& postData, |
269 struct mg_connection *connection, | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
270 const IHttpHandler::Arguments& headers) |
0 | 271 { |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
272 IHttpHandler::Arguments::const_iterator cs = headers.find("content-length"); |
0 | 273 if (cs == headers.end()) |
274 { | |
275 return PostDataStatus_NoLength; | |
276 } | |
277 | |
278 int length; | |
279 try | |
280 { | |
281 length = boost::lexical_cast<int>(cs->second); | |
282 } | |
283 catch (boost::bad_lexical_cast) | |
284 { | |
285 return PostDataStatus_NoLength; | |
286 } | |
287 | |
288 if (length < 0) | |
289 { | |
290 length = 0; | |
291 } | |
292 | |
293 postData.resize(length); | |
294 | |
295 size_t pos = 0; | |
296 while (length > 0) | |
297 { | |
298 int r = mg_read(connection, &postData[pos], length); | |
299 if (r <= 0) | |
300 { | |
301 return PostDataStatus_Failure; | |
302 } | |
418
b79bf2f4ab2e
execution of lua through REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
417
diff
changeset
|
303 |
8 | 304 assert(r <= length); |
0 | 305 length -= r; |
306 pos += r; | |
307 } | |
308 | |
309 return PostDataStatus_Success; | |
310 } | |
311 | |
312 | |
313 | |
314 static PostDataStatus ParseMultipartPost(std::string &completedFile, | |
315 struct mg_connection *connection, | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
316 const IHttpHandler::Arguments& headers, |
0 | 317 const std::string& contentType, |
318 ChunkStore& chunkStore) | |
319 { | |
320 std::string boundary = "--" + contentType.substr(multipartLength); | |
321 | |
322 std::string postData; | |
416 | 323 PostDataStatus status = ReadBody(postData, connection, headers); |
0 | 324 |
325 if (status != PostDataStatus_Success) | |
326 { | |
327 return status; | |
328 } | |
329 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
330 /*for (IHttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++) |
0 | 331 { |
332 std::cout << "Header [" << i->first << "] = " << i->second << "\n"; | |
333 } | |
334 printf("CHUNK\n");*/ | |
335 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
336 typedef IHttpHandler::Arguments::const_iterator ArgumentIterator; |
0 | 337 |
338 ArgumentIterator requestedWith = headers.find("x-requested-with"); | |
339 ArgumentIterator fileName = headers.find("x-file-name"); | |
340 ArgumentIterator fileSizeStr = headers.find("x-file-size"); | |
341 | |
338
3a3b3ba8c1e0
fix for uploads through internet explorer 7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
333
diff
changeset
|
342 if (requestedWith != headers.end() && |
0 | 343 requestedWith->second != "XMLHttpRequest") |
344 { | |
345 return PostDataStatus_Failure; | |
346 } | |
347 | |
348 size_t fileSize = 0; | |
349 if (fileSizeStr != headers.end()) | |
350 { | |
351 try | |
352 { | |
353 fileSize = boost::lexical_cast<size_t>(fileSizeStr->second); | |
354 } | |
355 catch (boost::bad_lexical_cast) | |
356 { | |
357 return PostDataStatus_Failure; | |
358 } | |
359 } | |
360 | |
361 typedef boost::find_iterator<std::string::iterator> FindIterator; | |
10 | 362 typedef boost::iterator_range<char*> Range; |
0 | 363 |
364 //chunkStore.Print(); | |
365 | |
366 try | |
367 { | |
368 FindIterator last; | |
369 for (FindIterator it = | |
370 make_find_iterator(postData, boost::first_finder(boundary)); | |
371 it!=FindIterator(); | |
372 ++it) | |
373 { | |
374 if (last != FindIterator()) | |
375 { | |
10 | 376 Range part(&last->back(), &it->front()); |
0 | 377 Range content = boost::find_first(part, "\r\n\r\n"); |
345 | 378 if (/*content != Range()*/!content.empty()) |
0 | 379 { |
380 Range c(&content.back() + 1, &it->front() - 2); | |
381 size_t chunkSize = c.size(); | |
382 | |
383 if (chunkSize > 0) | |
384 { | |
385 const char* chunkData = &c.front(); | |
386 | |
387 if (fileName == headers.end()) | |
388 { | |
389 // This file is stored in a single chunk | |
390 completedFile.resize(chunkSize); | |
391 if (chunkSize > 0) | |
392 { | |
393 memcpy(&completedFile[0], chunkData, chunkSize); | |
394 } | |
395 return PostDataStatus_Success; | |
396 } | |
397 else | |
398 { | |
399 return chunkStore.Store(completedFile, chunkData, chunkSize, fileName->second, fileSize); | |
400 } | |
401 } | |
10 | 402 } |
0 | 403 } |
404 | |
405 last = it; | |
406 } | |
407 } | |
408 catch (std::length_error) | |
409 { | |
410 return PostDataStatus_Failure; | |
411 } | |
412 | |
413 return PostDataStatus_Pending; | |
414 } | |
415 | |
416 | |
1202
476a17cfdf42
Fix crash when bad HTTP credentials are provided
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1122
diff
changeset
|
417 static bool IsAccessGranted(const MongooseServer& that, |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
418 const IHttpHandler::Arguments& headers) |
23 | 419 { |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
420 bool granted = false; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
421 |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
422 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
423 if (auth != headers.end()) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
424 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
425 std::string s = auth->second; |
1213 | 426 if (s.size() > 6 && |
427 s.substr(0, 6) == "Basic ") | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
428 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
429 std::string b64 = s.substr(6); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
430 granted = that.IsValidBasicHttpAuthentication(b64); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
431 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
432 } |
23 | 433 |
1202
476a17cfdf42
Fix crash when bad HTTP credentials are provided
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1122
diff
changeset
|
434 return granted; |
23 | 435 } |
436 | |
437 | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
438 static std::string GetAuthenticatedUsername(const IHttpHandler::Arguments& headers) |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
439 { |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
440 IHttpHandler::Arguments::const_iterator auth = headers.find("authorization"); |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
441 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
442 if (auth == headers.end()) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
443 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
444 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
445 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
446 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
447 std::string s = auth->second; |
1213 | 448 if (s.size() <= 6 || |
449 s.substr(0, 6) != "Basic ") | |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
450 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
451 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
452 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
453 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
454 std::string b64 = s.substr(6); |
809
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
455 std::string decoded; |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
456 Toolbox::DecodeBase64(decoded, b64); |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
457 size_t semicolons = decoded.find(':'); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
458 |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
459 if (semicolons == std::string::npos) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
460 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
461 // Bad-formatted request |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
462 return ""; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
463 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
464 else |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
465 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
466 return decoded.substr(0, semicolons); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
467 } |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
468 } |
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 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
471 static bool ExtractMethod(HttpMethod& method, |
414 | 472 const struct mg_request_info *request, |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
473 const IHttpHandler::Arguments& headers, |
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
474 const IHttpHandler::GetArguments& argumentsGET) |
414 | 475 { |
476 std::string overriden; | |
477 | |
478 // Check whether some PUT/DELETE faking is done | |
479 | |
480 // 1. Faking with Google's approach | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
481 IHttpHandler::Arguments::const_iterator methodOverride = |
414 | 482 headers.find("x-http-method-override"); |
483 | |
484 if (methodOverride != headers.end()) | |
485 { | |
486 overriden = methodOverride->second; | |
487 } | |
488 else if (!strcmp(request->request_method, "GET")) | |
489 { | |
490 // 2. Faking with Ruby on Rail's approach | |
491 // GET /my/resource?_method=delete <=> DELETE /my/resource | |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
492 for (size_t i = 0; i < argumentsGET.size(); i++) |
414 | 493 { |
1363
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
494 if (argumentsGET[i].first == "_method") |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
495 { |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
496 overriden = argumentsGET[i].second; |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
497 break; |
feaf2840917c
Plugins now receive duplicated GET arguments in their REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
498 } |
414 | 499 } |
500 } | |
501 | |
502 if (overriden.size() > 0) | |
503 { | |
504 // A faking has been done within this request | |
505 Toolbox::ToUpperCase(overriden); | |
506 | |
416 | 507 LOG(INFO) << "HTTP method faking has been detected for " << overriden; |
508 | |
414 | 509 if (overriden == "PUT") |
510 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
511 method = HttpMethod_Put; |
416 | 512 return true; |
414 | 513 } |
514 else if (overriden == "DELETE") | |
515 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
516 method = HttpMethod_Delete; |
416 | 517 return true; |
414 | 518 } |
519 else | |
520 { | |
521 return false; | |
522 } | |
523 } | |
524 | |
525 // No PUT/DELETE faking was present | |
526 if (!strcmp(request->request_method, "GET")) | |
527 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
528 method = HttpMethod_Get; |
414 | 529 } |
530 else if (!strcmp(request->request_method, "POST")) | |
531 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
532 method = HttpMethod_Post; |
414 | 533 } |
534 else if (!strcmp(request->request_method, "DELETE")) | |
535 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
536 method = HttpMethod_Delete; |
414 | 537 } |
538 else if (!strcmp(request->request_method, "PUT")) | |
539 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
418
diff
changeset
|
540 method = HttpMethod_Put; |
414 | 541 } |
542 else | |
543 { | |
544 return false; | |
545 } | |
546 | |
547 return true; | |
548 } | |
549 | |
550 | |
1517 | 551 static void ConfigureHttpCompression(HttpOutput& output, |
552 const IHttpHandler::Arguments& headers) | |
553 { | |
554 // Look if the client wishes HTTP compression | |
555 // https://en.wikipedia.org/wiki/HTTP_compression | |
556 IHttpHandler::Arguments::const_iterator it = headers.find("accept-encoding"); | |
557 if (it != headers.end()) | |
558 { | |
559 std::vector<std::string> encodings; | |
560 Toolbox::TokenizeString(encodings, it->second, ','); | |
561 | |
562 for (size_t i = 0; i < encodings.size(); i++) | |
563 { | |
564 std::string s = Toolbox::StripSpaces(encodings[i]); | |
565 | |
566 if (s == "deflate") | |
567 { | |
568 output.SetDeflateAllowed(true); | |
569 } | |
570 else if (s == "gzip") | |
571 { | |
572 output.SetGzipAllowed(true); | |
573 } | |
574 } | |
575 } | |
576 } | |
577 | |
578 | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
579 static void InternalCallback(struct mg_connection *connection, |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
580 const struct mg_request_info *request) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
581 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
582 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data); |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
583 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
584 MongooseOutputStream stream(connection); |
1115
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
585 HttpOutput output(stream, that->IsKeepAliveEnabled()); |
1592
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
586 output.SetDescribeErrorsEnabled(that->IsDescribeErrorsEnabled()); |
0 | 587 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
588 // Check remote calls |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
589 if (!that->IsRemoteAccessAllowed() && |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
590 request->remote_ip != LOCALHOST) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
591 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
592 output.SendUnauthorized(ORTHANC_REALM); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
593 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
594 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
595 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
596 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
597 // Extract the HTTP headers |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
598 IHttpHandler::Arguments headers; |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
599 for (int i = 0; i < request->num_headers; i++) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
600 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
601 std::string name = request->http_headers[i].name; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
602 std::transform(name.begin(), name.end(), name.begin(), ::tolower); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
603 headers.insert(std::make_pair(name, request->http_headers[i].value)); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
604 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
605 |
1517 | 606 if (that->IsHttpCompressionEnabled()) |
607 { | |
608 ConfigureHttpCompression(output, headers); | |
609 } | |
610 | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
611 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
612 // Extract the GET arguments |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
613 IHttpHandler::GetArguments argumentsGET; |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
614 if (!strcmp(request->request_method, "GET")) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
615 { |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
616 HttpToolbox::ParseGetArguments(argumentsGET, request->query_string); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
617 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
618 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
619 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
620 // Compute the HTTP method, taking method faking into consideration |
1122
1d60316c3618
simplifications in FileStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1115
diff
changeset
|
621 HttpMethod method = HttpMethod_Get; |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
622 if (!ExtractMethod(method, request, headers, argumentsGET)) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
623 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1112
diff
changeset
|
624 output.SendStatus(HttpStatus_400_BadRequest); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
625 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
626 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
627 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
628 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
629 // Authenticate this connection |
1202
476a17cfdf42
Fix crash when bad HTTP credentials are provided
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1122
diff
changeset
|
630 if (that->IsAuthenticationEnabled() && !IsAccessGranted(*that, headers)) |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
631 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1112
diff
changeset
|
632 output.SendUnauthorized(ORTHANC_REALM); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
633 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
634 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
635 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
636 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
637 // Apply the filter, if it is installed |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
638 char remoteIp[24]; |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
639 sprintf(remoteIp, "%d.%d.%d.%d", |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
640 reinterpret_cast<const uint8_t*>(&request->remote_ip) [3], |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
641 reinterpret_cast<const uint8_t*>(&request->remote_ip) [2], |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
642 reinterpret_cast<const uint8_t*>(&request->remote_ip) [1], |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
643 reinterpret_cast<const uint8_t*>(&request->remote_ip) [0]); |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
644 |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
645 std::string username = GetAuthenticatedUsername(headers); |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
646 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
647 const IIncomingHttpRequestFilter *filter = that->GetIncomingHttpRequestFilter(); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
648 if (filter != NULL) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
649 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
650 if (!filter->IsAllowed(method, request->uri, remoteIp, username.c_str())) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
651 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
652 output.SendUnauthorized(ORTHANC_REALM); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
653 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
654 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
655 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
656 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
657 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
658 // Extract the body of the request for PUT and POST |
1446
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1443
diff
changeset
|
659 |
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1443
diff
changeset
|
660 // TODO Avoid unneccessary memcopy of the body |
8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1443
diff
changeset
|
661 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
662 std::string body; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
663 if (method == HttpMethod_Post || |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
664 method == HttpMethod_Put) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
665 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
666 PostDataStatus status; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
667 |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1438
diff
changeset
|
668 IHttpHandler::Arguments::const_iterator ct = headers.find("content-type"); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
669 if (ct == headers.end()) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
670 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
671 // No content-type specified. Assume no multi-part content occurs at this point. |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
672 status = ReadBody(body, connection, headers); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
673 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
674 else |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
675 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
676 std::string contentType = ct->second; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
677 if (contentType.size() >= multipartLength && |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
678 !memcmp(contentType.c_str(), multipart, multipartLength)) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
679 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
680 status = ParseMultipartPost(body, connection, headers, contentType, that->GetChunkStore()); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
681 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
682 else |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
683 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
684 status = ReadBody(body, connection, headers); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
685 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
686 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
687 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
688 switch (status) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
689 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
690 case PostDataStatus_NoLength: |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1112
diff
changeset
|
691 output.SendStatus(HttpStatus_411_LengthRequired); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
692 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
693 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
694 case PostDataStatus_Failure: |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1112
diff
changeset
|
695 output.SendStatus(HttpStatus_400_BadRequest); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
696 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
697 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
698 case PostDataStatus_Pending: |
1521 | 699 output.AnswerEmpty(); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
700 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
701 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
702 default: |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
703 break; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
704 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
705 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
706 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
707 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
708 // Decompose the URI into its components |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
709 UriComponents uri; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
710 try |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
711 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
712 Toolbox::SplitUriComponents(uri, request->uri); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
713 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
714 catch (OrthancException) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
715 { |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1112
diff
changeset
|
716 output.SendStatus(HttpStatus_400_BadRequest); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
717 return; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
718 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
719 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
720 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
721 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
722 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
723 |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
724 try |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
725 { |
1569 | 726 bool found = false; |
727 | |
728 try | |
1570 | 729 { |
730 if (that->HasHandler()) | |
731 { | |
1571
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
732 found = that->GetHandler().Handle(output, RequestOrigin_Http, remoteIp, username.c_str(), |
3232f1c995a5
provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1570
diff
changeset
|
733 method, uri, headers, argumentsGET, body.c_str(), body.size()); |
1570 | 734 } |
735 } | |
1569 | 736 catch (boost::bad_lexical_cast&) |
1570 | 737 { |
1575
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
738 throw OrthancException(ErrorCode_BadParameterType, HttpStatus_400_BadRequest); |
1570 | 739 } |
1569 | 740 catch (std::runtime_error&) |
1570 | 741 { |
742 // Presumably an error while parsing the JSON body | |
1575
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
743 throw OrthancException(ErrorCode_BadRequest, HttpStatus_400_BadRequest); |
1570 | 744 } |
1569 | 745 |
746 if (!found) | |
1570 | 747 { |
748 throw OrthancException(ErrorCode_UnknownResource); | |
749 } | |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
750 } |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
751 catch (OrthancException& e) |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
752 { |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
753 // Using this candidate handler results in an exception |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
754 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
755 |
1575
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
756 Json::Value message = Json::objectValue; |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
757 message["HttpError"] = EnumerationToString(e.GetHttpStatus()); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
758 message["HttpStatus"] = e.GetHttpStatus(); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
759 message["Message"] = e.What(); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
760 message["Method"] = EnumerationToString(method); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
761 message["OrthancError"] = EnumerationToString(e.GetErrorCode()); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
762 message["OrthancStatus"] = e.GetErrorCode(); |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
763 message["Uri"] = request->uri; |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
764 |
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
765 std::string info = message.toStyledString(); |
1569 | 766 |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
767 try |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
768 { |
1575
f8aae45011c9
exceptions now carry HTTP status
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
769 output.SendStatus(e.GetHttpStatus(), info); |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
770 } |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
771 catch (OrthancException&) |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
772 { |
1569 | 773 // An exception here reflects the fact that the status code |
774 // was already set by the HTTP handler. | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
775 } |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
776 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
777 return; |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
778 } |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
779 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
780 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
781 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
782 #if MONGOOSE_USE_CALLBACKS == 0 |
0 | 783 static void* Callback(enum mg_event event, |
784 struct mg_connection *connection, | |
785 const struct mg_request_info *request) | |
786 { | |
787 if (event == MG_NEW_REQUEST) | |
788 { | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
789 InternalCallback(connection, request); |
0 | 790 |
791 // Mark as processed | |
792 return (void*) ""; | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
793 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
794 else |
0 | 795 { |
796 return NULL; | |
797 } | |
798 } | |
799 | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
800 #elif MONGOOSE_USE_CALLBACKS == 1 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
801 static int Callback(struct mg_connection *connection) |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
802 { |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
803 struct mg_request_info *request = mg_get_request_info(connection); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
804 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
805 InternalCallback(connection, request); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
806 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
807 return 1; // Do not let Mongoose handle the request by itself |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
808 } |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
809 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
810 #else |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
811 #error Please set MONGOOSE_USE_CALLBACKS |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
812 #endif |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
813 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
814 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
815 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
816 |
0 | 817 |
818 bool MongooseServer::IsRunning() const | |
819 { | |
820 return (pimpl_->context_ != NULL); | |
821 } | |
822 | |
823 | |
824 MongooseServer::MongooseServer() : pimpl_(new PImpl) | |
825 { | |
826 pimpl_->context_ = NULL; | |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
827 handler_ = NULL; |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
828 remoteAllowed_ = false; |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
829 authentication_ = false; |
23 | 830 ssl_ = false; |
0 | 831 port_ = 8000; |
417 | 832 filter_ = NULL; |
1115
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
833 keepAlive_ = false; |
1517 | 834 httpCompression_ = true; |
1592
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
835 describeErrors_ = true; |
748
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
836 |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
837 #if ORTHANC_SSL_ENABLED == 1 |
749 | 838 // 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
|
839 // 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
|
840 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
|
841 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
|
842 { |
749 | 843 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
|
844 } |
de9763f63510
upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
845 #endif |
0 | 846 } |
847 | |
848 | |
849 MongooseServer::~MongooseServer() | |
850 { | |
851 Stop(); | |
852 } | |
853 | |
854 | |
128 | 855 void MongooseServer::SetPortNumber(uint16_t port) |
0 | 856 { |
857 Stop(); | |
858 port_ = port; | |
859 } | |
860 | |
861 void MongooseServer::Start() | |
862 { | |
863 if (!IsRunning()) | |
864 { | |
865 std::string port = boost::lexical_cast<std::string>(port_); | |
866 | |
23 | 867 if (ssl_) |
868 { | |
869 port += "s"; | |
870 } | |
871 | |
0 | 872 const char *options[] = { |
1110
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
873 // Set the TCP port for the HTTP server |
0 | 874 "listening_ports", port.c_str(), |
1110
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
875 |
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
876 // Optimization reported by Chris Hafey |
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
877 // https://groups.google.com/d/msg/orthanc-users/CKueKX0pJ9E/_UCbl8T-VjIJ |
1115
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
878 "enable_keep_alive", (keepAlive_ ? "yes" : "no"), |
1110
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
879 |
becde5351e47
preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
880 // Set the SSL certificate, if any. This must be the last option. |
23 | 881 ssl_ ? "ssl_certificate" : NULL, |
882 certificate_.c_str(), | |
0 | 883 NULL |
884 }; | |
885 | |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
886 #if MONGOOSE_USE_CALLBACKS == 0 |
0 | 887 pimpl_->context_ = mg_start(&Callback, this, options); |
1112
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
888 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
889 #elif MONGOOSE_USE_CALLBACKS == 1 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
890 struct mg_callbacks callbacks; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
891 memset(&callbacks, 0, sizeof(callbacks)); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
892 callbacks.begin_request = Callback; |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
893 pimpl_->context_ = mg_start(&callbacks, this, options); |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
894 |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
895 #else |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
896 #error Please set MONGOOSE_USE_CALLBACKS |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
897 #endif |
a119f9ae3640
upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1110
diff
changeset
|
898 |
0 | 899 if (!pimpl_->context_) |
900 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1575
diff
changeset
|
901 throw OrthancException(ErrorCode_HttpPortInUse); |
0 | 902 } |
903 } | |
904 } | |
905 | |
906 void MongooseServer::Stop() | |
907 { | |
908 if (IsRunning()) | |
909 { | |
910 mg_stop(pimpl_->context_); | |
911 pimpl_->context_ = NULL; | |
912 } | |
913 } | |
914 | |
915 | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
916 void MongooseServer::ClearUsers() |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
917 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
918 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
919 registeredUsers_.clear(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
920 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
921 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
922 |
23 | 923 void MongooseServer::RegisterUser(const char* username, |
924 const char* password) | |
925 { | |
926 Stop(); | |
24 | 927 |
928 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
|
929 std::string encoded; |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
930 Toolbox::EncodeBase64(encoded, tag); |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
749
diff
changeset
|
931 registeredUsers_.insert(encoded); |
23 | 932 } |
933 | |
934 void MongooseServer::SetSslEnabled(bool enabled) | |
935 { | |
936 Stop(); | |
937 | |
59 | 938 #if ORTHANC_SSL_ENABLED == 0 |
23 | 939 if (enabled) |
940 { | |
59 | 941 throw OrthancException("Orthanc has been built without SSL support"); |
23 | 942 } |
943 else | |
944 { | |
945 ssl_ = false; | |
946 } | |
947 #else | |
948 ssl_ = enabled; | |
949 #endif | |
950 } | |
951 | |
1115
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
952 |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
953 void MongooseServer::SetKeepAliveEnabled(bool enabled) |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
954 { |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
955 Stop(); |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
956 keepAlive_ = enabled; |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
957 LOG(WARNING) << "HTTP keep alive is " << (enabled ? "enabled" : "disabled"); |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
958 } |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
959 |
da56a7916e8a
Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
960 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
961 void MongooseServer::SetAuthenticationEnabled(bool enabled) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
962 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
963 Stop(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
964 authentication_ = enabled; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
965 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
966 |
23 | 967 void MongooseServer::SetSslCertificate(const char* path) |
968 { | |
969 Stop(); | |
970 certificate_ = path; | |
971 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
972 |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
973 void MongooseServer::SetRemoteAccessAllowed(bool allowed) |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
974 { |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
975 Stop(); |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
976 remoteAllowed_ = allowed; |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
977 } |
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
978 |
1517 | 979 void MongooseServer::SetHttpCompressionEnabled(bool enabled) |
980 { | |
981 Stop(); | |
982 httpCompression_ = enabled; | |
1518 | 983 LOG(WARNING) << "HTTP compression is " << (enabled ? "enabled" : "disabled"); |
1517 | 984 } |
1592
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
985 |
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
986 void MongooseServer::SetDescribeErrorsEnabled(bool enabled) |
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
987 { |
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
988 describeErrors_ = enabled; |
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
989 LOG(INFO) << "Description of the errors in the HTTP answers is " << (enabled ? "enabled" : "disabled"); |
d73124f6b439
configuration option HttpDescribeErrors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
990 } |
1517 | 991 |
409
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
992 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter) |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
993 { |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
994 Stop(); |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
995 filter_ = &filter; |
63f707278fc8
lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
996 } |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
997 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
998 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
999 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
1000 return registeredUsers_.find(basic) != registeredUsers_.end(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
1001 } |
1443
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1002 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1003 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1004 void MongooseServer::Register(IHttpHandler& handler) |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1005 { |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1006 Stop(); |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1007 handler_ = &handler; |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1008 } |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1009 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1010 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1011 IHttpHandler& MongooseServer::GetHandler() const |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1012 { |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1013 if (handler_ == NULL) |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1014 { |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1015 throw OrthancException(ErrorCode_InternalError); |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1016 } |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1017 |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1018 return *handler_; |
895ab369d63c
refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1441
diff
changeset
|
1019 } |
0 | 1020 } |