forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeHttpServer.cpp
278 lines (239 loc) · 9.43 KB
/
SafeHttpServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file SafeHttpServer.cpp
* @authors:
* Marek
* @date 2015
*/
#include <microhttpd.h>
#include <sstream>
#include <jsonrpccpp/common/specificationparser.h>
#include <libdevcore/Log.h>
#include "SafeHttpServer.h"
#include "DfsFileServer.h"
using namespace std;
using namespace dev;
using namespace dev::rpc::fs;
/// structure copied from libjson-rpc-cpp httpserver version 0.6.0
struct mhd_coninfo
{
struct MHD_PostProcessor *postprocessor;
MHD_Connection* connection;
stringstream request;
TrustHttpServer* server;
int code;
};
TrustHttpServer::TrustHttpServer(eth::Client* _eth, int _port, const std::string& _sslrootca, const std::string& _sslcert, const std::string& _sslkey, int _threads) :
AbstractServerConnector(),
m_port(_port),
m_threads(_threads),
m_running(false),
m_path_sslcert(_sslcert),
m_path_sslkey(_sslkey),
m_path_sslrootca(_sslrootca),
m_eth(_eth),
daemon(NULL),
m_DfsNodeGroupId(""),
m_DfsNodeId(""),
m_DfsStoragePath("")
{
}
jsonrpc::IClientConnectionHandler *TrustHttpServer::GetHandler(const std::string &_url)
{
if (AbstractServerConnector::GetHandler() != NULL)
return AbstractServerConnector::GetHandler();
map<string, jsonrpc::IClientConnectionHandler*>::iterator it = this->m_urlhandler.find(_url);
if (it != this->m_urlhandler.end())
return it->second;
return NULL;
}
bool TrustHttpServer::StartListening()
{
if(!this->m_running)
{
//start the fileserver
if (0 != DfsFileServer::getInstance()->init(m_DfsStoragePath, m_DfsNodeGroupId, m_DfsNodeId, m_eth))
{
LOG(ERROR) << "init DfsFileServer failed !";
//return false;
}
if (this->m_path_sslcert != "" && this->m_path_sslkey != "" && this->m_path_sslrootca != "")
{
LOG(TRACE) << "*****#### HTTPS / SSL start, but not support ###******";
}
LOG(TRACE) << "*****#### HTTP start ###******";
this->daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, this->m_port, NULL, NULL, \
TrustHttpServer::callback, this, \
MHD_OPTION_THREAD_POOL_SIZE, this->m_threads, \
MHD_OPTION_NOTIFY_COMPLETED, DfsFileServer::request_completed, NULL, \
MHD_OPTION_END);
if (this->daemon != NULL)
this->m_running = true;
}
return this->m_running;
}
bool TrustHttpServer::StopListening()
{
if(this->m_running)
{
MHD_stop_daemon(this->daemon);
DfsFileServer::getInstance()->destory();
this->m_running = false;
}
return true;
}
bool TrustHttpServer::SendResponse(const string& _response, void* _addInfo)
{
struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
struct MHD_Response *result = MHD_create_response_from_buffer(_response.size(),(void *) _response.c_str(), MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(result, "Content-Type", "application/json");
MHD_add_response_header(result, "Access-Control-Allow-Origin", "*");
int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
MHD_destroy_response(result);
return ret == MHD_YES;
}
bool TrustHttpServer::SendOptionsResponse(void* _addInfo)
{
struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
struct MHD_Response *result = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(result, "Allow", "POST, OPTIONS");
MHD_add_response_header(result, "Access-Control-Allow-Origin", "*");
MHD_add_response_header(result, "Access-Control-Allow-Headers", "origin, content-type, accept");
MHD_add_response_header(result, "DAV", "1");
int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
MHD_destroy_response(result);
return ret == MHD_YES;
}
void TrustHttpServer::SetUrlHandler(const string &_url, jsonrpc::IClientConnectionHandler *_handler)
{
this->m_urlhandler[_url] = _handler;
this->SetHandler(NULL);
}
void TrustHttpServer::setGroup(const std::string& group)
{
m_DfsNodeGroupId = group;
}
void TrustHttpServer::setNode(const std::string& node)
{
m_DfsNodeId = node;
}
void TrustHttpServer::setStoragePath(const std::string& storage)
{
m_DfsStoragePath = storage;
}
int TrustHttpServer::callback(void *_cls, MHD_Connection *_connection, const char *_url, const char *_method, const char *_version, const char *_upload_data, size_t *_upload_data_size, void **_con_cls)
{
(void)_version;
std::string account = "";
string strUrl(_url);
string strMethod(_method);
bool isFileProcess = DfsFileServer::getInstance()->filter(strUrl, strMethod) == 0;
//huanggaofeng: filter file process request
if (isFileProcess)
{
DfsHttpInfo objHttpInfo;
objHttpInfo.cls = _cls;
objHttpInfo.connection = _connection;
objHttpInfo.url = (char*)_url;
objHttpInfo.method = (char*)_method;
objHttpInfo.version = (char*)_version;
objHttpInfo.upload_data = (char*)_upload_data;
objHttpInfo.upload_data_size = _upload_data_size;
objHttpInfo.ptr = _con_cls;
int ret = DfsFileServer::getInstance()->handle(&objHttpInfo);
*_upload_data_size = 0;
return ret;
}
//not file process http request
if (*_con_cls == NULL)//request first comes to here
{
struct mhd_coninfo* client_connection = new mhd_coninfo;
client_connection->connection = _connection;
client_connection->server = static_cast<TrustHttpServer*>(_cls);
*_con_cls = client_connection;
return MHD_YES;
}
struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(*_con_cls);
if (string("POST") == _method)
{
if (*_upload_data_size != 0)
{
client_connection->request.write(_upload_data, *_upload_data_size);
*_upload_data_size = 0;
return MHD_YES;
}
else
{
string response;
jsonrpc::IClientConnectionHandler* handler = client_connection->server->GetHandler(string(_url));
if (handler == NULL)
{
//debug print
LOG(TRACE) << "***** http get null handler *******";
client_connection->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
client_connection->server->SendResponse("No client conneciton handler found", client_connection);
}
else
{
//debug print
//LOG(TRACE) << "***** http get handler and response *******";
client_connection->code = MHD_HTTP_OK;
handler->HandleRequest(client_connection->request.str(), response);
client_connection->server->SendResponse(response, client_connection);
}
}
}
else if (string("OPTIONS") == _method) {
//debug print
//LOG(TRACE) << "***** http option process ******";
client_connection->code = MHD_HTTP_OK;
client_connection->server->SendOptionsResponse(client_connection);
}
else
{
//debug print
//LOG(TRACE) << "***** http else method: " << _method << " *******";
client_connection->code = MHD_HTTP_METHOD_NOT_ALLOWED;
client_connection->server->SendResponse("Not allowed HTTP Method", client_connection);
}
delete client_connection;
*_con_cls = NULL;
return MHD_YES;
}
bool SafeHttpServer::SendResponse(string const& _response, void* _addInfo)
{
struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
struct MHD_Response *result = MHD_create_response_from_buffer(
_response.size(),
static_cast<void *>(const_cast<char *>(_response.c_str())),
MHD_RESPMEM_MUST_COPY
);
MHD_add_response_header(result, "Content-Type", "application/json");
MHD_add_response_header(result, "Access-Control-Allow-Origin", m_allowedOrigin.c_str());
int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
MHD_destroy_response(result);
return ret == MHD_YES;
}
bool SafeHttpServer::SendOptionsResponse(void* _addInfo)
{
struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
struct MHD_Response *result = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(result, "Allow", "POST, OPTIONS");
MHD_add_response_header(result, "Access-Control-Allow-Origin", m_allowedOrigin.c_str());
MHD_add_response_header(result, "Access-Control-Allow-Headers", "origin, content-type, accept");
MHD_add_response_header(result, "DAV", "1");
int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
MHD_destroy_response(result);
return ret == MHD_YES;
}