annotate Core/HttpServer/MongooseServer.cpp @ 1478:1011bd6ab00b

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 01 Aug 2015 21:49:20 +0200
parents 8dc80ba768aa
children f967bdf8534e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
10 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
11 * In addition, as a special exception, the copyright holders of this
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
12 * program give permission to link the code of its release with the
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
15 * the linked executables. You must obey the GNU General Public License
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
19 * you do not wish to do so, delete this exception statement from your
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
20 * version. If you delete this exception statement from all source files
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 128
diff changeset
21 * in the program, then also delete it here.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 // http://en.highscore.de/cpp/boost/stringhandling.html
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 809
diff changeset
35 #include "../PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "MongooseServer.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <algorithm>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <boost/lexical_cast.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <boost/algorithm/string.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include <iostream>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include <stdio.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 #include <boost/thread.hpp>
108
a6e41de88a53 using glog
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
46 #include <glog/logging.h>
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
48 #include "../OrthancException.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 #include "../ChunkedBuffer.h"
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1438
diff changeset
50 #include "HttpToolbox.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 #include "mongoose.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
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
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
62 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 static const char multipart[] = "multipart/form-data; boundary=";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 static unsigned int multipartLength = sizeof(multipart) / sizeof(char) - 1;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 namespace
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 // Anonymous namespace to avoid clashes between compilation modules
911
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
71 class MongooseOutputStream : public IHttpOutputStream
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 private:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 struct mg_connection* connection_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
911
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
76 public:
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
77 MongooseOutputStream(struct mg_connection* connection) : connection_(connection)
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
78 {
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
79 }
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
80
909
ef71057d8b26 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
81 virtual void Send(bool isHeader, const void* buffer, size_t length)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 {
217
1ac3aacd10a5 simplifications
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 207
diff changeset
83 if (length > 0)
1ac3aacd10a5 simplifications
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 207
diff changeset
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
1ac3aacd10a5 simplifications
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 207
diff changeset
91 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
908
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
93
911
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
94 virtual void OnHttpStatusReceived(HttpStatus status)
908
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
95 {
911
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 909
diff changeset
96 // Ignore this
908
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
97 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 enum PostDataStatus
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 PostDataStatus_Success,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 PostDataStatus_NoLength,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 PostDataStatus_Pending,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 PostDataStatus_Failure
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 // TODO Move this to external file
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 class ChunkedFile : public ChunkedBuffer
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 private:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 std::string filename_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 ChunkedFile(const std::string& filename) :
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 filename_(filename)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 const std::string& GetFilename() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 return filename_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 class ChunkStore
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 private:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 typedef std::list<ChunkedFile*> Content;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 Content content_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 unsigned int numPlaces_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 boost::mutex mutex_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 std::set<std::string> discardedFiles_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 void Clear()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 for (Content::iterator it = content_.begin();
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
146 it != content_.end(); ++it)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 delete *it;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 Content::iterator Find(const std::string& filename)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 for (Content::iterator it = content_.begin();
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
155 it != content_.end(); ++it)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if ((*it)->GetFilename() == filename)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 return it;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 return content_.end();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 void Remove(const std::string& filename)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 Content::iterator it = Find(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 if (it != content_.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 delete *it;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 content_.erase(it);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 ChunkStore()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 numPlaces_ = 10;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 ~ChunkStore()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 Clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 PostDataStatus Store(std::string& completed,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 const char* chunkData,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 size_t chunkSize,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 const std::string& filename,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 size_t filesize)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 boost::mutex::scoped_lock lock(mutex_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 std::set<std::string>::iterator wasDiscarded = discardedFiles_.find(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 if (wasDiscarded != discardedFiles_.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 discardedFiles_.erase(wasDiscarded);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 return PostDataStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 ChunkedFile* f;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 Content::iterator it = Find(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 if (it == content_.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 f = new ChunkedFile(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 // Make some room
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 if (content_.size() >= numPlaces_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 discardedFiles_.insert(content_.front()->GetFilename());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 delete content_.front();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 content_.pop_front();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 content_.push_back(f);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 f = *it;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 f->AddChunk(chunkData, chunkSize);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 if (f->GetNumBytes() > filesize)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 Remove(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 else if (f->GetNumBytes() == filesize)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 f->Flatten(completed);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 Remove(filename);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 return PostDataStatus_Success;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 return PostDataStatus_Pending;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 /*void Print()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 boost::mutex::scoped_lock lock(mutex_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 printf("ChunkStore status:\n");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 for (Content::const_iterator i = content_.begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 i != content_.end(); i++)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 printf(" [%s]: %d\n", (*i)->GetFilename().c_str(), (*i)->GetNumBytes());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 printf("-----\n");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }*/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 struct MongooseServer::PImpl
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 struct mg_context *context_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 ChunkStore chunkStore_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 ChunkStore& MongooseServer::GetChunkStore()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 return pimpl_->chunkStore_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
416
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
268 static PostDataStatus ReadBody(std::string& postData,
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 if (cs == headers.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 return PostDataStatus_NoLength;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 int length;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 length = boost::lexical_cast<int>(cs->second);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 catch (boost::bad_lexical_cast)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 return PostDataStatus_NoLength;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 if (length < 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 length = 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 postData.resize(length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 size_t pos = 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 while (length > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 int r = mg_read(connection, &postData[pos], length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 if (r <= 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 return PostDataStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 }
418
b79bf2f4ab2e execution of lua through REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 417
diff changeset
303
8
3a584803783e build with visual studio
jodogne
parents: 0
diff changeset
304 assert(r <= length);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 length -= r;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 pos += r;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 return PostDataStatus_Success;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 static PostDataStatus ParseMultipartPost(std::string &completedFile,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 const std::string& contentType,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 ChunkStore& chunkStore)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 std::string boundary = "--" + contentType.substr(multipartLength);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 std::string postData;
416
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
323 PostDataStatus status = ReadBody(postData, connection, headers);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 if (status != PostDataStatus_Success)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 return status;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 std::cout << "Header [" << i->first << "] = " << i->second << "\n";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 printf("CHUNK\n");*/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 ArgumentIterator requestedWith = headers.find("x-requested-with");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 ArgumentIterator fileName = headers.find("x-file-name");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 ArgumentIterator fileSizeStr = headers.find("x-file-size");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 requestedWith->second != "XMLHttpRequest")
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 return PostDataStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 size_t fileSize = 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 if (fileSizeStr != headers.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 fileSize = boost::lexical_cast<size_t>(fileSizeStr->second);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 catch (boost::bad_lexical_cast)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 return PostDataStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 typedef boost::find_iterator<std::string::iterator> FindIterator;
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
362 typedef boost::iterator_range<char*> Range;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 //chunkStore.Print();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 FindIterator last;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 for (FindIterator it =
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 make_find_iterator(postData, boost::first_finder(boundary));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 it!=FindIterator();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 ++it)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 if (last != FindIterator())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 {
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
376 Range part(&last->back(), &it->front());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 Range content = boost::find_first(part, "\r\n\r\n");
345
jodogne
parents: 338
diff changeset
378 if (/*content != Range()*/!content.empty())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 Range c(&content.back() + 1, &it->front() - 2);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 size_t chunkSize = c.size();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 if (chunkSize > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 const char* chunkData = &c.front();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 if (fileName == headers.end())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 // This file is stored in a single chunk
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 completedFile.resize(chunkSize);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 if (chunkSize > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 memcpy(&completedFile[0], chunkData, chunkSize);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 return PostDataStatus_Success;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 return chunkStore.Store(completedFile, chunkData, chunkSize, fileName->second, fileSize);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 }
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
402 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 last = it;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 catch (std::length_error)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 return PostDataStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 return PostDataStatus_Pending;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1202
diff changeset
426 if (s.size() > 6 &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1202
diff changeset
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
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
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
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
435 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
436
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1202
diff changeset
448 if (s.size() <= 6 ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1202
diff changeset
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
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
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
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
475 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
476 std::string overriden;
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
477
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
478 // Check whether some PUT/DELETE faking is done
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
479
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
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
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
482 headers.find("x-http-method-override");
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
483
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
484 if (methodOverride != headers.end())
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
485 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
486 overriden = methodOverride->second;
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
487 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
488 else if (!strcmp(request->request_method, "GET"))
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
489 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
490 // 2. Faking with Ruby on Rail's approach
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
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
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
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
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
499 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
500 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
501
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
502 if (overriden.size() > 0)
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
503 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
504 // A faking has been done within this request
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
505 Toolbox::ToUpperCase(overriden);
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
506
416
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
507 LOG(INFO) << "HTTP method faking has been detected for " << overriden;
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
508
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
509 if (overriden == "PUT")
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
510 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
511 method = HttpMethod_Put;
416
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
512 return true;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
513 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
514 else if (overriden == "DELETE")
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
515 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
516 method = HttpMethod_Delete;
416
1188cb0ddaa5 http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 415
diff changeset
517 return true;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
518 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
519 else
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
520 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
521 return false;
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
522 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
523 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
524
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
525 // No PUT/DELETE faking was present
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
526 if (!strcmp(request->request_method, "GET"))
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
527 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
528 method = HttpMethod_Get;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
529 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
530 else if (!strcmp(request->request_method, "POST"))
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
531 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
532 method = HttpMethod_Post;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
533 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
534 else if (!strcmp(request->request_method, "DELETE"))
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
535 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
536 method = HttpMethod_Delete;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
537 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
538 else if (!strcmp(request->request_method, "PUT"))
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
539 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
540 method = HttpMethod_Put;
414
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
541 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
542 else
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
543 {
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
544 return false;
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
545 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
546
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
547 return true;
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
548 }
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
549
26290b46056b http method faking
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 409
diff changeset
550
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
551 static void InternalCallback(struct mg_connection *connection,
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
552 const struct mg_request_info *request)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
553 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
554 MongooseServer* that = reinterpret_cast<MongooseServer*>(request->user_data);
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
555
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
556 MongooseOutputStream stream(connection);
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
557 HttpOutput output(stream, that->IsKeepAliveEnabled());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
559 // Check remote calls
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
560 if (!that->IsRemoteAccessAllowed() &&
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
561 request->remote_ip != LOCALHOST)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
562 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
563 output.SendUnauthorized(ORTHANC_REALM);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
564 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
565 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
566
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
567
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
568 // Extract the HTTP headers
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1438
diff changeset
569 IHttpHandler::Arguments headers;
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
570 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
571 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
572 std::string name = request->http_headers[i].name;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
573 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
574 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
575 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
576
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
577
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
578 // Extract the GET arguments
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1438
diff changeset
579 IHttpHandler::GetArguments argumentsGET;
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
580 if (!strcmp(request->request_method, "GET"))
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
581 {
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1438
diff changeset
582 HttpToolbox::ParseGetArguments(argumentsGET, request->query_string);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
583 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
584
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
585
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
586 // Compute the HTTP method, taking method faking into consideration
1122
1d60316c3618 simplifications in FileStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1115
diff changeset
587 HttpMethod method = HttpMethod_Get;
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
588 if (!ExtractMethod(method, request, headers, argumentsGET))
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
589 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
590 output.SendStatus(HttpStatus_400_BadRequest);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
591 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
592 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
593
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 // Authenticate this connection
1202
476a17cfdf42 Fix crash when bad HTTP credentials are provided
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
596 if (that->IsAuthenticationEnabled() && !IsAccessGranted(*that, headers))
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
597 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
598 output.SendUnauthorized(ORTHANC_REALM);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
599 return;
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
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
602
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
603 // Apply the filter, if it is installed
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
604 const IIncomingHttpRequestFilter *filter = that->GetIncomingHttpRequestFilter();
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
605 if (filter != NULL)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
606 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
607 std::string username = GetAuthenticatedUsername(headers);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
608
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
609 char remoteIp[24];
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
610 sprintf(remoteIp, "%d.%d.%d.%d",
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
611 reinterpret_cast<const uint8_t*>(&request->remote_ip) [3],
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
612 reinterpret_cast<const uint8_t*>(&request->remote_ip) [2],
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
613 reinterpret_cast<const uint8_t*>(&request->remote_ip) [1],
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
614 reinterpret_cast<const uint8_t*>(&request->remote_ip) [0]);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
615
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
616 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
617 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
618 output.SendUnauthorized(ORTHANC_REALM);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
619 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
620 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
621 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
622
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
623
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
624 // 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
625
8dc80ba768aa refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
626 // 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
627
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
628 std::string body;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
629 if (method == HttpMethod_Post ||
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
630 method == HttpMethod_Put)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
631 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
632 PostDataStatus status;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
633
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1438
diff changeset
634 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
635 if (ct == headers.end())
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 // 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
638 status = ReadBody(body, connection, headers);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
639 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
640 else
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
641 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
642 std::string contentType = ct->second;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
643 if (contentType.size() >= multipartLength &&
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
644 !memcmp(contentType.c_str(), multipart, multipartLength))
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
645 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
646 status = ParseMultipartPost(body, connection, headers, contentType, that->GetChunkStore());
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
647 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
648 else
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 status = ReadBody(body, connection, headers);
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 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
653
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
654 switch (status)
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 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
657 output.SendStatus(HttpStatus_411_LengthRequired);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
658 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
659
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
660 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
661 output.SendStatus(HttpStatus_400_BadRequest);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
662 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
663
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
664 case PostDataStatus_Pending:
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
665 output.SendBody();
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
666 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
667
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
668 default:
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
669 break;
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 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
672
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 // Decompose the URI into its components
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
675 UriComponents uri;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
676 try
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
677 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
678 Toolbox::SplitUriComponents(uri, request->uri);
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 catch (OrthancException)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
681 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
682 output.SendStatus(HttpStatus_400_BadRequest);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
683 return;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
684 }
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 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri);
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
688
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
689 bool found = false;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
690
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
691 try
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
692 {
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
693 if (that->HasHandler())
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
694 {
1446
8dc80ba768aa refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
695 found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body.c_str(), body.size());
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
696 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
697 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
698 catch (OrthancException& e)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
699 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
700 // Using this candidate handler results in an exception
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
701 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
702
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
703 try
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
704 {
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
705 switch (e.GetErrorCode())
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
706 {
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
707 case ErrorCode_InexistentFile:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
708 case ErrorCode_InexistentItem:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
709 case ErrorCode_UnknownResource:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
710 output.SendStatus(HttpStatus_404_NotFound);
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
711 break;
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
712
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
713 case ErrorCode_BadRequest:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
714 case ErrorCode_UriSyntax:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
715 output.SendStatus(HttpStatus_400_BadRequest);
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
716 break;
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
717
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
718 default:
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
719 output.SendStatus(HttpStatus_500_InternalServerError);
1438
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
720 }
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
721 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
722 catch (OrthancException&)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
723 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
724 // An exception here reflects the fact that an exception was
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
725 // triggered after the status code was sent by the HTTP handler.
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
726 }
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
727
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
728 return;
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
729 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
730 catch (boost::bad_lexical_cast&)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
731 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
732 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast";
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
733 output.SendStatus(HttpStatus_400_BadRequest);
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
734 return;
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
735 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
736 catch (std::runtime_error&)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
737 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
738 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request";
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
739 output.SendStatus(HttpStatus_400_BadRequest);
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
740 return;
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
741 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
742
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
743 if (!found)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
744 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1112
diff changeset
745 output.SendStatus(HttpStatus_404_NotFound);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
746 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
747 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
748
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
749
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
750 #if MONGOOSE_USE_CALLBACKS == 0
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
751 static void* Callback(enum mg_event event,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
752 struct mg_connection *connection,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
753 const struct mg_request_info *request)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
754 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
755 if (event == MG_NEW_REQUEST)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
756 {
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
757 InternalCallback(connection, request);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
758
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
759 // Mark as processed
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
760 return (void*) "";
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
761 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
762 else
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
763 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
764 return NULL;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
765 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
766 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
767
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
768 #elif MONGOOSE_USE_CALLBACKS == 1
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
769 static int Callback(struct mg_connection *connection)
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
770 {
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
771 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
772
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
773 InternalCallback(connection, request);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
774
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
775 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
776 }
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
777
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
778 #else
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
779 #error Please set MONGOOSE_USE_CALLBACKS
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
780 #endif
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
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
783
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
784
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
785
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
786 bool MongooseServer::IsRunning() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
787 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
788 return (pimpl_->context_ != NULL);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
789 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
790
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
791
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
792 MongooseServer::MongooseServer() : pimpl_(new PImpl)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
793 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
794 pimpl_->context_ = NULL;
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
795 handler_ = NULL;
34
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
796 remoteAllowed_ = false;
25
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
797 authentication_ = false;
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
798 ssl_ = false;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
799 port_ = 8000;
417
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 416
diff changeset
800 filter_ = NULL;
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
801 keepAlive_ = false;
748
de9763f63510 upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
802
de9763f63510 upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
803 #if ORTHANC_SSL_ENABLED == 1
749
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 748
diff changeset
804 // 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
805 // 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
806 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
807 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
808 {
749
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 748
diff changeset
809 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
810 }
de9763f63510 upgrade to openssl-1.0.1g because of heartbeat exploit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
811 #endif
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
812 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
813
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
814
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
815 MongooseServer::~MongooseServer()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
816 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
817 Stop();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
818 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
819
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
820
128
2a24f43d9dca fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
821 void MongooseServer::SetPortNumber(uint16_t port)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
822 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
823 Stop();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
824 port_ = port;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
825 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
826
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
827 void MongooseServer::Start()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
828 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829 if (!IsRunning())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
830 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
831 std::string port = boost::lexical_cast<std::string>(port_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
832
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
833 if (ssl_)
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
834 {
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
835 port += "s";
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
836 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
837
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
838 const char *options[] = {
1110
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
839 // Set the TCP port for the HTTP server
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
840 "listening_ports", port.c_str(),
1110
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
841
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
842 // Optimization reported by Chris Hafey
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
843 // 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
844 "enable_keep_alive", (keepAlive_ ? "yes" : "no"),
1110
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
845
becde5351e47 preparing to update mongoose
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
846 // Set the SSL certificate, if any. This must be the last option.
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
847 ssl_ ? "ssl_certificate" : NULL,
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
848 certificate_.c_str(),
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
849 NULL
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
850 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
851
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
852 #if MONGOOSE_USE_CALLBACKS == 0
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
853 pimpl_->context_ = mg_start(&Callback, this, options);
1112
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
854
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
855 #elif MONGOOSE_USE_CALLBACKS == 1
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
856 struct mg_callbacks callbacks;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
857 memset(&callbacks, 0, sizeof(callbacks));
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
858 callbacks.begin_request = Callback;
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
859 pimpl_->context_ = mg_start(&callbacks, this, options);
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
860
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
861 #else
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
862 #error Please set MONGOOSE_USE_CALLBACKS
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
863 #endif
a119f9ae3640 upgrade to Mongoose 3.8
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1110
diff changeset
864
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
865 if (!pimpl_->context_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
866 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
867 throw OrthancException("Unable to launch the Mongoose server");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
868 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
869 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
870 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
871
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
872 void MongooseServer::Stop()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
873 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
874 if (IsRunning())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
875 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
876 mg_stop(pimpl_->context_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
877 pimpl_->context_ = NULL;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
878 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
879 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
880
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
881
25
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
882 void MongooseServer::ClearUsers()
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
883 {
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
884 Stop();
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
885 registeredUsers_.clear();
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
886 }
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
887
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
888
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
889 void MongooseServer::RegisterUser(const char* username,
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
890 const char* password)
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
891 {
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
892 Stop();
24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
893
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
894 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
895 std::string encoded;
8ce2f69436ca do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 749
diff changeset
896 Toolbox::EncodeBase64(encoded, tag);
8ce2f69436ca do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 749
diff changeset
897 registeredUsers_.insert(encoded);
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
898 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
899
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
900 void MongooseServer::SetSslEnabled(bool enabled)
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
901 {
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
902 Stop();
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
903
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
904 #if ORTHANC_SSL_ENABLED == 0
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
905 if (enabled)
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
906 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
907 throw OrthancException("Orthanc has been built without SSL support");
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
908 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
909 else
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
910 {
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
911 ssl_ = false;
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
912 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
913 #else
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
914 ssl_ = enabled;
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
915 #endif
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
916 }
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
917
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
918
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
919 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
920 {
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
921 Stop();
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
922 keepAlive_ = enabled;
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
923 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
924 }
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
925
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
926
25
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
927 void MongooseServer::SetAuthenticationEnabled(bool enabled)
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
928 {
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
929 Stop();
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
930 authentication_ = enabled;
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
931 }
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
932
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
933 void MongooseServer::SetSslCertificate(const char* path)
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
934 {
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
935 Stop();
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
936 certificate_ = path;
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
937 }
25
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
938
34
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
939 void MongooseServer::SetRemoteAccessAllowed(bool allowed)
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
940 {
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
941 Stop();
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
942 remoteAllowed_ = allowed;
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
943 }
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
944
409
63f707278fc8 lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
945 void MongooseServer::SetIncomingHttpRequestFilter(IIncomingHttpRequestFilter& filter)
63f707278fc8 lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
946 {
63f707278fc8 lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
947 Stop();
63f707278fc8 lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
948 filter_ = &filter;
63f707278fc8 lua filtering of incoming http requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
949 }
34
96e57b863dd9 option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 25
diff changeset
950
25
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
951 bool MongooseServer::IsValidBasicHttpAuthentication(const std::string& basic) const
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
952 {
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
953 return registeredUsers_.find(basic) != registeredUsers_.end();
dd1489098265 basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
954 }
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
955
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
956
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
957 void MongooseServer::Register(IHttpHandler& handler)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
958 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
959 Stop();
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
960 handler_ = &handler;
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
961 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
962
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
963
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
964 IHttpHandler& MongooseServer::GetHandler() const
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
965 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
966 if (handler_ == NULL)
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
967 {
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
968 throw OrthancException(ErrorCode_InternalError);
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
969 }
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
970
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
971 return *handler_;
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
972 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
973 }