Skip to content

Commit d3b3027

Browse files
author
Sebastiano Merlino
committed
Linearized code to make it more readable
1 parent 262819d commit d3b3027

File tree

1 file changed

+97
-120
lines changed

1 file changed

+97
-120
lines changed

src/webserver.cpp

Lines changed: 97 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,12 @@ void webserver::request_completed (
211211
)
212212
{
213213
details::modded_request* mr = static_cast<details::modded_request*>(*con_cls);
214-
if (mr != 0x0)
215-
{
216-
if(mr->ws != 0x0)
217-
{
218-
mr->ws->internal_comet_manager->complete_request(mr->dhrs->connection_id);
219-
}
214+
if (mr == 0x0) return;
220215

221-
delete mr;
222-
mr = 0x0;
223-
}
216+
if (mr->ws != 0x0) mr->ws->internal_comet_manager->complete_request(mr->dhrs->connection_id);
217+
218+
delete mr;
219+
mr = 0x0;
224220
}
225221

226222
bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family)
@@ -246,12 +242,12 @@ MHD_socket create_socket (int domain, int type, int protocol)
246242
int sock_cloexec = SOCK_CLOEXEC;
247243
int ctype = SOCK_STREAM | sock_cloexec;
248244

249-
/* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
245+
/* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
250246
* implementations do not set ai_socktype, e.g. RHL6.2. */
251247
MHD_socket fd = socket(domain, ctype, protocol);
252248

253249
#ifdef _WINDOWS
254-
if (fd == INVALID_SOCKET)
250+
if (fd == INVALID_SOCKET)
255251
#else
256252
if ((fd == -1) &&
257253
(errno == EINVAL || errno == EPROTONOSUPPORT) && (sock_cloexec != 0)
@@ -398,33 +394,28 @@ bool webserver::is_running()
398394

399395
bool webserver::stop()
400396
{
401-
if(this->running)
397+
if(!this->running) return false;
398+
399+
pthread_mutex_lock(&mutexwait);
400+
this->running = false;
401+
pthread_cond_signal(&mutexcond);
402+
pthread_mutex_unlock(&mutexwait);
403+
for(unsigned int i = 0; i < threads.size(); ++i)
402404
{
403-
pthread_mutex_lock(&mutexwait);
404-
this->running = false;
405-
pthread_cond_signal(&mutexcond);
406-
pthread_mutex_unlock(&mutexwait);
407-
for(unsigned int i = 0; i < threads.size(); ++i)
408-
{
409-
void* t_res;
410-
pthread_join(threads[i], &t_res);
411-
free(t_res);
412-
}
413-
threads.clear();
414-
typedef vector<details::daemon_item*>::const_iterator daemon_item_it;
405+
void* t_res;
406+
pthread_join(threads[i], &t_res);
407+
free(t_res);
408+
}
409+
threads.clear();
410+
typedef vector<details::daemon_item*>::const_iterator daemon_item_it;
415411

416-
for(daemon_item_it it = daemons.begin(); it != daemons.end(); ++it)
417-
delete *it;
418-
daemons.clear();
412+
for(daemon_item_it it = daemons.begin(); it != daemons.end(); ++it)
413+
delete *it;
414+
daemons.clear();
419415

420-
shutdown(bind_socket, 2);
416+
shutdown(bind_socket, 2);
421417

422-
return true;
423-
}
424-
else
425-
{
426-
return false;
427-
}
418+
return true;
428419
}
429420

430421
void webserver::unregister_resource(const string& resource)
@@ -535,18 +526,20 @@ int webserver::build_request_args (
535526

536527
int policy_callback (void *cls, const struct sockaddr* addr, socklen_t addrlen)
537528
{
538-
if((static_cast<webserver*>(cls))->ban_system_enabled)
529+
if(!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;
530+
531+
if((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
532+
((static_cast<webserver*>(cls))->bans.count(addr)) &&
533+
(!(static_cast<webserver*>(cls))->allowances.count(addr))
534+
) ||
535+
(((static_cast<webserver*>(cls))->default_policy == http_utils::REJECT)
536+
&& ((!(static_cast<webserver*>(cls))->allowances.count(addr)) ||
537+
((static_cast<webserver*>(cls))->bans.count(addr)))
538+
))
539539
{
540-
if((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
541-
((static_cast<webserver*>(cls))->bans.count(addr)) &&
542-
(!(static_cast<webserver*>(cls))->allowances.count(addr))
543-
) ||
544-
(((static_cast<webserver*>(cls))->default_policy == http_utils::REJECT)
545-
&& ((!(static_cast<webserver*>(cls))->allowances.count(addr)) ||
546-
((static_cast<webserver*>(cls))->bans.count(addr)))
547-
))
548-
return MHD_NO;
540+
return MHD_NO;
549541
}
542+
550543
return MHD_YES;
551544
}
552545

@@ -561,14 +554,12 @@ void* uri_log(void* cls, const char* uri)
561554
void error_log(void* cls, const char* fmt, va_list ap)
562555
{
563556
webserver* dws = static_cast<webserver*>(cls);
564-
if(dws->log_error != 0x0)
565-
dws->log_error(fmt);
557+
if(dws->log_error != 0x0) dws->log_error(fmt);
566558
}
567559

568560
void access_log(webserver* dws, string uri)
569561
{
570-
if(dws->log_access != 0x0)
571-
dws->log_access(uri);
562+
if(dws->log_access != 0x0) dws->log_access(uri);
572563
}
573564

574565
size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s)
@@ -590,10 +581,8 @@ size_t internal_unescaper(void* cls, char* s)
590581
dws->unescaper(s);
591582
return strlen(s);
592583
}
593-
else
594-
{
595-
return http_unescape(s);
596-
}
584+
585+
return http_unescape(s);
597586
}
598587

599588
int webserver::post_iterator (void *cls, enum MHD_ValueKind kind,
@@ -704,21 +693,16 @@ int webserver::bodyfull_requests_answer_second_step(
704693
size_t* upload_data_size, struct details::modded_request* mr
705694
)
706695
{
707-
if ( 0 != *upload_data_size)
708-
{
696+
if (0 == *upload_data_size) return complete_request(connection, mr, version, method);
697+
709698
#ifdef DEBUG
710-
cout << "Writing content: " << upload_data << endl;
699+
cout << "Writing content: " << upload_data << endl;
711700
#endif //DEBUG
712-
mr->dhr->grow_content(upload_data, *upload_data_size);
713-
if (mr->pp != NULL)
714-
{
715-
MHD_post_process(mr->pp, upload_data, *upload_data_size);
716-
}
717-
*upload_data_size = 0;
718-
return MHD_YES;
719-
}
701+
mr->dhr->grow_content(upload_data, *upload_data_size);
720702

721-
return complete_request(connection, mr, version, method);
703+
if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
704+
*upload_data_size = 0;
705+
return MHD_YES;
722706
}
723707

724708
void webserver::end_request_construction(
@@ -987,62 +971,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
987971
struct details::modded_request* mr =
988972
static_cast<struct details::modded_request*>(*con_cls);
989973

990-
if(mr->second == false)
991-
{
992-
mr->standardized_url = new string();
993-
internal_unescaper((void*) static_cast<webserver*>(cls), (char*) url);
994-
http_utils::standardize_url(url, *mr->standardized_url);
995-
996-
bool body = false;
997-
998-
access_log(
999-
static_cast<webserver*>(cls),
1000-
*(mr->complete_uri) + " METHOD: " + method
1001-
);
1002-
1003-
if( 0 == strcasecmp(method, http_utils::http_method_get.c_str()))
1004-
{
1005-
mr->callback = &http_resource::render_GET;
1006-
}
1007-
else if (0 == strcmp(method, http_utils::http_method_post.c_str()))
1008-
{
1009-
mr->callback = &http_resource::render_POST;
1010-
body = true;
1011-
}
1012-
else if (0 == strcasecmp(method, http_utils::http_method_put.c_str()))
1013-
{
1014-
mr->callback = &http_resource::render_PUT;
1015-
body = true;
1016-
}
1017-
else if (0 == strcasecmp(method,http_utils::http_method_delete.c_str()))
1018-
{
1019-
mr->callback = &http_resource::render_DELETE;
1020-
}
1021-
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
1022-
{
1023-
mr->callback = &http_resource::render_HEAD;
1024-
}
1025-
else if (0 ==strcasecmp(method,http_utils::http_method_connect.c_str()))
1026-
{
1027-
mr->callback = &http_resource::render_CONNECT;
1028-
}
1029-
else if (0 == strcasecmp(method, http_utils::http_method_trace.c_str()))
1030-
{
1031-
mr->callback = &http_resource::render_TRACE;
1032-
}
1033-
else if (0 ==strcasecmp(method,http_utils::http_method_options.c_str()))
1034-
{
1035-
mr->callback = &http_resource::render_OPTIONS;
1036-
}
1037-
1038-
if(body)
1039-
return static_cast<webserver*>(cls)->
1040-
bodyfull_requests_answer_first_step(connection, mr);
1041-
else
1042-
return static_cast<webserver*>(cls)->
1043-
bodyless_requests_answer(connection, method, version, mr);
1044-
}
1045-
else
974+
if(mr->second != false)
1046975
{
1047976
return static_cast<webserver*>(cls)->
1048977
bodyfull_requests_answer_second_step(
@@ -1054,6 +983,54 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
1054983
mr
1055984
);
1056985
}
986+
987+
mr->standardized_url = new string();
988+
internal_unescaper((void*) static_cast<webserver*>(cls), (char*) url);
989+
http_utils::standardize_url(url, *mr->standardized_url);
990+
991+
bool body = false;
992+
993+
access_log(
994+
static_cast<webserver*>(cls),
995+
*(mr->complete_uri) + " METHOD: " + method
996+
);
997+
998+
if( 0 == strcasecmp(method, http_utils::http_method_get.c_str()))
999+
{
1000+
mr->callback = &http_resource::render_GET;
1001+
}
1002+
else if (0 == strcmp(method, http_utils::http_method_post.c_str()))
1003+
{
1004+
mr->callback = &http_resource::render_POST;
1005+
body = true;
1006+
}
1007+
else if (0 == strcasecmp(method, http_utils::http_method_put.c_str()))
1008+
{
1009+
mr->callback = &http_resource::render_PUT;
1010+
body = true;
1011+
}
1012+
else if (0 == strcasecmp(method,http_utils::http_method_delete.c_str()))
1013+
{
1014+
mr->callback = &http_resource::render_DELETE;
1015+
}
1016+
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
1017+
{
1018+
mr->callback = &http_resource::render_HEAD;
1019+
}
1020+
else if (0 ==strcasecmp(method,http_utils::http_method_connect.c_str()))
1021+
{
1022+
mr->callback = &http_resource::render_CONNECT;
1023+
}
1024+
else if (0 == strcasecmp(method, http_utils::http_method_trace.c_str()))
1025+
{
1026+
mr->callback = &http_resource::render_TRACE;
1027+
}
1028+
else if (0 ==strcasecmp(method,http_utils::http_method_options.c_str()))
1029+
{
1030+
mr->callback = &http_resource::render_OPTIONS;
1031+
}
1032+
1033+
return body ? static_cast<webserver*>(cls)->bodyfull_requests_answer_first_step(connection, mr) : static_cast<webserver*>(cls)->bodyless_requests_answer(connection, method, version, mr);
10571034
}
10581035

10591036
void webserver::send_message_to_topic(

0 commit comments

Comments
 (0)