|
44 | 44 | #include <iosfwd> |
45 | 45 | #include <iostream> |
46 | 46 | #include <memory> |
| 47 | +#include <mutex> |
47 | 48 | #include <set> |
48 | 49 | #include <stdexcept> |
49 | 50 | #include <string> |
@@ -196,6 +197,7 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr |
196 | 197 |
|
197 | 198 | details::http_endpoint idx(resource, family, true, regex_checking); |
198 | 199 |
|
| 200 | + std::unique_lock registered_resources_lock(registered_resources_mutex); |
199 | 201 | pair<map<details::http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(map<details::http_endpoint, http_resource*>::value_type(idx, hrm)); |
200 | 202 |
|
201 | 203 | if (!family && result.second) { |
@@ -370,6 +372,7 @@ bool webserver::stop() { |
370 | 372 | void webserver::unregister_resource(const string& resource) { |
371 | 373 | // family does not matter - it just checks the url_normalized anyhow |
372 | 374 | details::http_endpoint he(resource, false, true, regex_checking); |
| 375 | + std::unique_lock registered_resources_lock(registered_resources_mutex); |
373 | 376 | registered_resources.erase(he); |
374 | 377 | registered_resources.erase(he.get_url_complete()); |
375 | 378 | registered_resources_str.erase(he.get_url_complete()); |
@@ -676,51 +679,54 @@ MHD_Result webserver::finalize_answer(MHD_Connection* connection, struct details |
676 | 679 |
|
677 | 680 | bool found = false; |
678 | 681 | struct MHD_Response* raw_response; |
679 | | - if (!single_resource) { |
680 | | - const char* st_url = mr->standardized_url->c_str(); |
681 | | - fe = registered_resources_str.find(st_url); |
682 | | - if (fe == registered_resources_str.end()) { |
683 | | - if (regex_checking) { |
684 | | - map<details::http_endpoint, http_resource*>::iterator found_endpoint; |
685 | | - |
686 | | - details::http_endpoint endpoint(st_url, false, false, false); |
687 | | - |
688 | | - map<details::http_endpoint, http_resource*>::iterator it; |
689 | | - |
690 | | - size_t len = 0; |
691 | | - size_t tot_len = 0; |
692 | | - for (it = registered_resources.begin(); it != registered_resources.end(); ++it) { |
693 | | - size_t endpoint_pieces_len = (*it).first.get_url_pieces().size(); |
694 | | - size_t endpoint_tot_len = (*it).first.get_url_complete().size(); |
695 | | - if (!found || endpoint_pieces_len > len || (endpoint_pieces_len == len && endpoint_tot_len > tot_len)) { |
696 | | - if ((*it).first.match(endpoint)) { |
697 | | - found = true; |
698 | | - len = endpoint_pieces_len; |
699 | | - tot_len = endpoint_tot_len; |
700 | | - found_endpoint = it; |
| 682 | + { |
| 683 | + std::shared_lock registered_resources_lock(registered_resources_mutex); |
| 684 | + if (!single_resource) { |
| 685 | + const char* st_url = mr->standardized_url->c_str(); |
| 686 | + fe = registered_resources_str.find(st_url); |
| 687 | + if (fe == registered_resources_str.end()) { |
| 688 | + if (regex_checking) { |
| 689 | + map<details::http_endpoint, http_resource*>::iterator found_endpoint; |
| 690 | + |
| 691 | + details::http_endpoint endpoint(st_url, false, false, false); |
| 692 | + |
| 693 | + map<details::http_endpoint, http_resource*>::iterator it; |
| 694 | + |
| 695 | + size_t len = 0; |
| 696 | + size_t tot_len = 0; |
| 697 | + for (it = registered_resources.begin(); it != registered_resources.end(); ++it) { |
| 698 | + size_t endpoint_pieces_len = (*it).first.get_url_pieces().size(); |
| 699 | + size_t endpoint_tot_len = (*it).first.get_url_complete().size(); |
| 700 | + if (!found || endpoint_pieces_len > len || (endpoint_pieces_len == len && endpoint_tot_len > tot_len)) { |
| 701 | + if ((*it).first.match(endpoint)) { |
| 702 | + found = true; |
| 703 | + len = endpoint_pieces_len; |
| 704 | + tot_len = endpoint_tot_len; |
| 705 | + found_endpoint = it; |
| 706 | + } |
701 | 707 | } |
702 | 708 | } |
703 | | - } |
704 | 709 |
|
705 | | - if (found) { |
706 | | - vector<string> url_pars = found_endpoint->first.get_url_pars(); |
| 710 | + if (found) { |
| 711 | + vector<string> url_pars = found_endpoint->first.get_url_pars(); |
707 | 712 |
|
708 | | - vector<string> url_pieces = endpoint.get_url_pieces(); |
709 | | - vector<int> chunks = found_endpoint->first.get_chunk_positions(); |
710 | | - for (unsigned int i = 0; i < url_pars.size(); i++) { |
711 | | - mr->dhr->set_arg(url_pars[i], url_pieces[chunks[i]]); |
712 | | - } |
| 713 | + vector<string> url_pieces = endpoint.get_url_pieces(); |
| 714 | + vector<int> chunks = found_endpoint->first.get_chunk_positions(); |
| 715 | + for (unsigned int i = 0; i < url_pars.size(); i++) { |
| 716 | + mr->dhr->set_arg(url_pars[i], url_pieces[chunks[i]]); |
| 717 | + } |
713 | 718 |
|
714 | | - hrm = found_endpoint->second; |
| 719 | + hrm = found_endpoint->second; |
| 720 | + } |
715 | 721 | } |
| 722 | + } else { |
| 723 | + hrm = fe->second; |
| 724 | + found = true; |
716 | 725 | } |
717 | 726 | } else { |
718 | | - hrm = fe->second; |
| 727 | + hrm = registered_resources.begin()->second; |
719 | 728 | found = true; |
720 | 729 | } |
721 | | - } else { |
722 | | - hrm = registered_resources.begin()->second; |
723 | | - found = true; |
724 | 730 | } |
725 | 731 |
|
726 | 732 | if (found) { |
|
0 commit comments