X Tutup
Skip to content

Commit eb4d20f

Browse files
author
Sebastiano Merlino
committed
Changed code to avoid patches on libmicrohttpd
Changed connection identification system. Now requestor IP and PORT are used.
1 parent 1f32013 commit eb4d20f

File tree

6 files changed

+144
-136
lines changed

6 files changed

+144
-136
lines changed

src/http_response.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ void http_response::get_raw_response_lp_receive(
232232
this->ws = ws;
233233
this->connection_id = MHD_get_connection_info(
234234
this->underlying_connection,
235-
MHD_CONNECTION_INFO_FD
236-
)->socket_fd;
235+
MHD_CONNECTION_INFO_CLIENT_ADDRESS
236+
)->client_addr;
237237

238238
*response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 80,
239239
&long_polling_receive_response::data_generator, (void*) this, NULL);

src/http_utils.cpp

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -226,42 +226,55 @@ void http_utils::standardize_url(const std::string& url, std::string& result)
226226
}
227227

228228
void get_ip_str(
229-
const struct sockaddr *sa,
230-
std::string& result,
231-
socklen_t maxlen
229+
const struct sockaddr *sa,
230+
std::string& result,
231+
socklen_t maxlen
232232
)
233233
{
234-
char to_ret[INET6_ADDRSTRLEN] = { '\0' };
235-
switch(sa->sa_family)
234+
if(sa)
236235
{
237-
case AF_INET:
238-
if(maxlen == 0)
239-
maxlen = INET_ADDRSTRLEN;
240-
241-
inet_ntop(AF_INET,
242-
&(((struct sockaddr_in *)sa)->sin_addr),
243-
to_ret,
244-
maxlen
245-
);
246-
247-
break;
248-
249-
case AF_INET6:
250-
if(maxlen == 0)
251-
maxlen = INET6_ADDRSTRLEN;
252-
253-
inet_ntop(AF_INET6,
254-
&(((struct sockaddr_in6 *)sa)->sin6_addr),
255-
to_ret,
256-
maxlen
257-
);
258-
259-
break;
260-
default:
261-
strncpy(to_ret, "Unknown AF", 11);
262-
return;
236+
char to_ret[INET6_ADDRSTRLEN] = { '\0' };
237+
switch(sa->sa_family)
238+
{
239+
case AF_INET:
240+
if(maxlen == 0)
241+
maxlen = INET_ADDRSTRLEN;
242+
243+
inet_ntop(AF_INET,
244+
&(((struct sockaddr_in *)sa)->sin_addr),
245+
to_ret,
246+
maxlen
247+
);
248+
249+
break;
250+
251+
case AF_INET6:
252+
if(maxlen == 0)
253+
maxlen = INET6_ADDRSTRLEN;
254+
255+
inet_ntop(AF_INET6,
256+
&(((struct sockaddr_in6 *)sa)->sin6_addr),
257+
to_ret,
258+
maxlen
259+
);
260+
261+
break;
262+
default:
263+
strncpy(to_ret, "Unknown AF", 11);
264+
return;
265+
}
266+
result = to_ret;
263267
}
264-
result = to_ret;
268+
}
269+
270+
std::string get_ip_str_new(
271+
const struct sockaddr* sa,
272+
socklen_t maxlen
273+
)
274+
{
275+
std::string to_ret;
276+
get_ip_str(sa, to_ret, maxlen);
277+
return to_ret;
265278
}
266279

267280
const struct sockaddr str_to_ip(const std::string& src)
@@ -280,15 +293,19 @@ const struct sockaddr str_to_ip(const std::string& src)
280293

281294
short get_port(const struct sockaddr* sa)
282295
{
283-
switch(sa->sa_family)
296+
if(sa)
284297
{
285-
case AF_INET:
286-
return ((struct sockaddr_in *)sa)->sin_port;
287-
case AF_INET6:
288-
return ((struct sockaddr_in *)sa)->sin_port;
289-
default:
290-
return 0;
298+
switch(sa->sa_family)
299+
{
300+
case AF_INET:
301+
return ((struct sockaddr_in *)sa)->sin_port;
302+
case AF_INET6:
303+
return ((struct sockaddr_in *)sa)->sin_port;
304+
default:
305+
return 0;
306+
}
291307
}
308+
return 0;
292309
}
293310

294311
size_t http_unescape (char *val)

src/httpserver/http_response.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class http_response
110110
enqueue_response(this, &http_response::enqueue_response_str),
111111
completed(false),
112112
ws(0x0),
113-
connection_id(-1)
113+
connection_id(0x0)
114114
{
115115
set_header(http_utils::http_header_content_type, content_type);
116116
}
@@ -388,7 +388,7 @@ class http_response
388388
bool completed;
389389

390390
webserver* ws;
391-
int connection_id;
391+
struct httpserver_ska connection_id;
392392

393393
void get_raw_response_str(MHD_Response** res, webserver* ws = 0x0);
394394
void get_raw_response_file(MHD_Response** res, webserver* ws = 0x0);
@@ -474,7 +474,7 @@ inline http_response::http_response<TYPE> \
474474
enqueue_response(this, &http_response::enqueue_response_## S3),\
475475
completed(false),\
476476
ws(0x0),\
477-
connection_id(-1)\
477+
connection_id(0x0)\
478478
{\
479479
set_header(http_utils::http_header_content_type, content_type);\
480480
}

src/httpserver/http_utils.hpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,11 @@ struct ip_representation
315315
* @return string containing the ip address
316316
**/
317317
void get_ip_str(const struct sockaddr *sa,
318-
std::string& result, socklen_t maxlen = 0
318+
std::string& result, socklen_t maxlen = 0
319+
);
320+
321+
std::string get_ip_str_new(const struct sockaddr* sa,
322+
socklen_t maxlen = 0
319323
);
320324
/**
321325
* Method used to get a port from a sockaddr
@@ -340,6 +344,44 @@ char* load_file (const char *filename);
340344

341345
size_t load_file (const char* filename, char** content);
342346

347+
struct httpserver_ska
348+
{
349+
httpserver_ska(struct sockaddr* addr):
350+
addr(addr),
351+
ip(get_ip_str_new(addr)),
352+
port(get_port(addr))
353+
{
354+
}
355+
356+
httpserver_ska(): addr(0x0) { }
357+
358+
httpserver_ska(const httpserver_ska& o): addr(o.addr) { }
359+
360+
bool operator<(const httpserver_ska& o) const
361+
{
362+
if(this->ip < o.ip)
363+
return true;
364+
else if(this->ip > o.ip)
365+
return false;
366+
else if(this->port < o.port)
367+
return true;
368+
else
369+
return false;
370+
}
371+
372+
httpserver_ska& operator=(const httpserver_ska& o)
373+
{
374+
this->addr = o.addr;
375+
return *this;
376+
}
377+
378+
struct sockaddr* addr;
379+
std::string ip;
380+
int port;
381+
};
382+
383+
384+
343385
};
344386
};
345387
#endif

src/httpserver/webserver.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,20 @@ class webserver
422422
void send_message_to_topic(const std::string& topic,
423423
const std::string& message
424424
);
425-
void send_message_to_consumer(int connection_id,
425+
void send_message_to_consumer(const httpserver_ska& connection_id,
426426
const std::string& message, bool to_lock = true
427427
);
428428
void register_to_topics(const std::vector<std::string>& topics,
429-
int connection_id, int keepalive_secs = -1,
429+
const httpserver_ska& connection_id, int keepalive_secs = -1,
430430
std::string keepalive_msg = ""
431431
);
432-
size_t read_message(int connection_id, std::string& message);
432+
size_t read_message(const httpserver_ska& connection_id,
433+
std::string& message
434+
);
433435
size_t get_topic_consumers(const std::string& topic,
434-
std::set<int>& consumers
436+
std::set<httpserver_ska>& consumers
435437
);
436-
bool pop_signaled(int consumer);
438+
bool pop_signaled(const httpserver_ska& consumer);
437439

438440
http_response* get_from_cache(const std::string& key, bool* valid,
439441
bool lock = false, bool write = false
@@ -569,12 +571,12 @@ class webserver
569571
std::set<ip_representation> allowances;
570572
#endif
571573

572-
std::map<int, std::deque<std::string> > q_messages;
573-
std::map<std::string, std::set<int> > q_waitings;
574-
std::map<int, std::pair<pthread_mutex_t, pthread_cond_t> > q_blocks;
575-
std::set<int> q_signal;
576-
std::map<int, long> q_keepalives;
577-
std::map<int, std::pair<int, std::string> > q_keepalives_mem;
574+
std::map<httpserver_ska, std::deque<std::string> > q_messages;
575+
std::map<std::string, std::set<httpserver_ska> > q_waitings;
576+
std::map<httpserver_ska, std::pair<pthread_mutex_t, pthread_cond_t> > q_blocks;
577+
std::set<httpserver_ska> q_signal;
578+
std::map<httpserver_ska, long> q_keepalives;
579+
std::map<httpserver_ska, std::pair<int, std::string> > q_keepalives_mem;
578580
pthread_rwlock_t comet_guard;
579581

580582
std::vector<details::daemon_item*> daemons;
@@ -586,9 +588,7 @@ class webserver
586588

587589
void init(render_ptr single_resource);
588590
static void* select(void* self);
589-
// void schedule_fd(int fd, fd_set* schedule_list, int* max);
590591
static void* cleaner(void* self);
591-
void clean_connections();
592592

593593
void register_resource(const std::string& resource,
594594
details::http_resource_mirror hrm, bool family = false

0 commit comments

Comments
 (0)
X Tutup