/
server.cpp
344 lines (292 loc) · 9.22 KB
/
server.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Copyright (c) 2016, Human Brain Project
* Stefan.Eilemann@epfl.ch
*/
#include <zeq/http/server.h>
#include "../log.h"
#include "../detail/broker.h"
#include "../detail/sender.h"
#include "../detail/socket.h"
#include <servus/serializable.h>
#include <httpxx/BufferedMessage.hpp>
#include <httpxx/Error.hpp>
#include <httpxx/Message.hpp>
#include <httpxx/ResponseBuilder.hpp>
#include <algorithm>
namespace httpxx = ::http; // avoid confusion between httpxx and zeq::http
namespace
{
std::string _toLower( std::string value )
{
std::transform( value.begin(), value.end(), value.begin(), ::tolower );
return value;
}
}
namespace zeq
{
namespace http
{
class Server::Impl : public detail::Sender
{
public:
Impl() : Impl( URI( )) {}
Impl( const URI& uri_ )
: detail::Sender( uri_, 0, ZMQ_STREAM )
{
const std::string& zmqURI = buildZmqURI( uri );
if( ::zmq_bind( socket, zmqURI.c_str( )) == -1 )
{
ZEQTHROW( std::runtime_error(
std::string( "Cannot bind http server socket '" ) +
zmqURI + "': " +
zmq_strerror( zmq_errno( )) +
( zmq_errno() == ENODEV ?
": host name instead of device used?" : "" )));
}
initURI();
}
bool subscribe( servus::Serializable& serializable )
{
const std::string& name = _toLower( serializable.getTypeName( ));
if( _subscriptions.count( name ) != 0 )
return false;
_subscriptions[ name ] = &serializable;
return true;
}
bool unsubscribe( const servus::Serializable& serializable )
{
return _subscriptions.erase(
_toLower( serializable.getTypeName( ))) != 0;
}
bool register_( servus::Serializable& serializable )
{
const std::string& name = _toLower( serializable.getTypeName( ));
if( _registrations.count( name ) != 0 )
return false;
_registrations[ name ] = &serializable;
return true;
}
bool unregister( const servus::Serializable& serializable )
{
return _registrations.erase(
_toLower( serializable.getTypeName( ))) != 0;
}
void addSockets( std::vector< detail::Socket >& entries )
{
detail::Socket entry;
entry.socket = socket;
entry.events = ZMQ_POLLIN;
entries.push_back( entry );
}
void process( detail::Socket& )
{
// Read request and body
httpxx::BufferedRequest request;
std::string body;
uint8_t id[256];
int idSize = 0;
while( !request.complete( ))
{
// id of client (used for reply)
idSize = ::zmq_recv( socket, id, sizeof( id ), 0 );
if( idSize <= 0 )
{
ZEQWARN << "HTTP server receive failed: "
<< zmq_strerror( zmq_errno( )) << std::endl;
return;
}
// msg body
zmq_msg_t msg;
zmq_msg_init( &msg );
zmq_msg_recv( &msg, socket, 0 );
const char* data = (const char*)zmq_msg_data( &msg );
const size_t msgSize = zmq_msg_size( &msg );
if( msgSize == 0 )
{
ZEQWARN << "HTTP server receive failed: "
<< zmq_strerror( zmq_errno( )) << std::endl;
return;
}
size_t consumed = 0;
try
{
while( !request.complete() && msgSize > consumed )
consumed += request.feed( data + consumed,
msgSize - consumed );
}
catch( const httpxx::Error& )
{
zmq_msg_close( &msg );
return; // garbage from client, ignore
}
zmq_msg_close( &msg );
}
// Handle
httpxx::ResponseBuilder response;
if( request.method() == httpxx::Method::get( ))
body = _processGet( request, response );
else if( request.method() == httpxx::Method::put( ))
{
if( request.has_header( "Content-Length" ))
_processPut( request, response );
else
response.set_status( 411 ); // Content-Length required
body.clear(); // no response body
}
else
{
response.set_status( 405 ); // Method Not Allowed
body.clear(); // no response body
}
// response header
if( response.status() >= 400 && response.status() < 500 )
body = response.to_string();
response.headers()[ "Content-Length" ] = std::to_string( body.length( ));
const std::string& rep = response.to_string();
const int more = body.empty() ? 0 : ZMQ_SNDMORE;
if( ::zmq_send( socket, id, idSize, ZMQ_SNDMORE ) != idSize ||
::zmq_send( socket, rep.c_str(), rep.length(), more ) !=
int( rep.length( )))
{
ZEQWARN << "Could not send HTTP response header: "
<< zmq_strerror( zmq_errno( )) << std::endl;
return;
}
// response body
if( !body.empty() &&
( ::zmq_send( socket, id, idSize, ZMQ_SNDMORE ) != idSize ||
::zmq_send( socket, body.c_str(), body.length(), 0 ) !=
int( body.length( ))))
{
ZEQWARN << "Could not send HTTP response body: "
<< zmq_strerror( zmq_errno( )) << std::endl;
}
}
protected:
typedef std::map< std::string, servus::Serializable* > SerializableMap;
SerializableMap _subscriptions;
SerializableMap _registrations;
std::string _getTypeName( const std::string& url )
{
if( url.empty( ))
return url;
std::string name = url.substr( 1 );
while( true )
{
const size_t pos = name.find( '/' );
if( pos == std::string::npos )
return name;
name = name.substr( 0, pos ) + "::" + name.substr( pos + 1 );
}
}
std::string _processGet( const httpxx::BufferedRequest& request,
httpxx::ResponseBuilder& response )
{
const std::string& type = _toLower( _getTypeName( request.url( )));
const auto& i = _registrations.find( type );
if( i == _registrations.end( ))
{
response.set_status( 404 );
return std::string();
}
response.set_status( 200 );
i->second->notifyRequested();
return i->second->toJSON();
}
void _processPut( const httpxx::BufferedRequest& request,
httpxx::ResponseBuilder& response )
{
const std::string& type = _toLower( _getTypeName( request.url( )));
const auto& i = _subscriptions.find( type );
if( i == _subscriptions.end( ))
response.set_status( 404 );
else
{
if( i->second->fromJSON( request.body( )))
{
i->second->notifyUpdated();
response.set_status( 200 );
}
else
response.set_status( 400 );
}
}
};
namespace
{
std::string _getServerParameter( const int argc, const char* const* argv )
{
for( int i = 0; i < argc; ++i )
{
if( std::string( argv[i] ) == "--zeq-http-server" )
{
if( i == argc - 1 || argv[ i + 1 ][0] == '-' )
return "tcp://";
return argv[i+1];
}
}
return std::string();
}
}
Server::Server( const URI& uri, Receiver& shared )
: Receiver( shared )
, _impl( new Impl( uri ))
{}
Server::Server( const URI& uri )
: Receiver()
, _impl( new Impl( uri ))
{}
Server::Server( Receiver& shared )
: Receiver( shared )
, _impl( new Impl )
{}
Server::Server()
: Receiver()
, _impl( new Impl )
{}
Server::~Server()
{}
std::unique_ptr< Server > Server::parse( const int argc, const char* const* argv )
{
const std::string& param = _getServerParameter( argc, argv );
if( param.empty( ))
return nullptr;
return std::unique_ptr< Server >( new Server( URI( param )));
}
std::unique_ptr< Server > Server::parse( const int argc, const char* const* argv,
Receiver& shared )
{
const std::string& param = _getServerParameter( argc, argv );
if( param.empty( ))
return nullptr;
return std::unique_ptr< Server >( new Server( URI( param ), shared ));
}
const servus::URI& Server::getURI() const
{
return _impl->uri.toServusURI();
}
bool Server::subscribe( servus::Serializable& object )
{
return _impl->subscribe( object );
}
bool Server::unsubscribe( const servus::Serializable& object )
{
return _impl->unsubscribe( object );
}
bool Server::register_( servus::Serializable& object )
{
return _impl->register_( object );
}
bool Server::unregister( const servus::Serializable& object )
{
return _impl->unregister( object );
}
void Server::addSockets( std::vector< detail::Socket >& entries )
{
_impl->addSockets( entries );
}
void Server::process( detail::Socket& socket )
{
_impl->process( socket );
}
}
}