-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix webserver thread safety #330
base: master
Are you sure you want to change the base?
Changes from 4 commits
2ff6e31
ba2234d
d4a89d8
2f7b4ff
145e9aa
589ff8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
#include <iosfwd> | ||
#include <cstring> | ||
#include <memory> | ||
#include <mutex> | ||
#include <stdexcept> | ||
#include <utility> | ||
#include <vector> | ||
|
@@ -192,6 +193,7 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr | |
|
||
details::http_endpoint idx(resource, family, true, regex_checking); | ||
|
||
std::unique_lock registered_resources_lock(registered_resources_mutex); | ||
pair<map<details::http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(map<details::http_endpoint, http_resource*>::value_type(idx, hrm)); | ||
|
||
if (!family && result.second) { | ||
|
@@ -361,12 +363,14 @@ bool webserver::stop() { | |
void webserver::unregister_resource(const string& resource) { | ||
// family does not matter - it just checks the url_normalized anyhow | ||
details::http_endpoint he(resource, false, true, regex_checking); | ||
std::unique_lock registered_resources_lock(registered_resources_mutex); | ||
registered_resources.erase(he); | ||
registered_resources.erase(he.get_url_complete()); | ||
registered_resources_str.erase(he.get_url_complete()); | ||
} | ||
|
||
void webserver::ban_ip(const string& ip) { | ||
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex); | ||
ip_representation t_ip(ip); | ||
set<ip_representation>::iterator it = bans.find(t_ip); | ||
if (it != bans.end() && (t_ip.weight() < (*it).weight())) { | ||
|
@@ -378,6 +382,7 @@ void webserver::ban_ip(const string& ip) { | |
} | ||
|
||
void webserver::allow_ip(const string& ip) { | ||
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex); | ||
ip_representation t_ip(ip); | ||
set<ip_representation>::iterator it = allowances.find(t_ip); | ||
if (it != allowances.end() && (t_ip.weight() < (*it).weight())) { | ||
|
@@ -389,10 +394,12 @@ void webserver::allow_ip(const string& ip) { | |
} | ||
|
||
void webserver::unban_ip(const string& ip) { | ||
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex); | ||
bans.erase(ip_representation(ip)); | ||
} | ||
|
||
void webserver::disallow_ip(const string& ip) { | ||
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex); | ||
allowances.erase(ip_representation(ip)); | ||
} | ||
|
||
|
@@ -402,6 +409,7 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add | |
|
||
if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES; | ||
|
||
std::shared_lock bans_and_allowances_lock((static_cast<webserver*>(cls))->bans_and_allowances_mutex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can split the if statement here so that we always check only one of the two based on the chosen policy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both ACCEPT and REJECT cases, we access I added a commit simplifying the code, with no intended functional change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MINOR] I think the cleanest way would be to have two private methods (is_allowed and is_banned) that use the locks internally and just call into those methods from within here. That should localize the locks to the maximum extent possible. |
||
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) && | ||
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) && | ||
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) || | ||
|
@@ -626,51 +634,54 @@ MHD_Result webserver::finalize_answer(MHD_Connection* connection, struct details | |
|
||
bool found = false; | ||
struct MHD_Response* raw_response; | ||
if (!single_resource) { | ||
const char* st_url = mr->standardized_url->c_str(); | ||
fe = registered_resources_str.find(st_url); | ||
if (fe == registered_resources_str.end()) { | ||
if (regex_checking) { | ||
map<details::http_endpoint, http_resource*>::iterator found_endpoint; | ||
|
||
details::http_endpoint endpoint(st_url, false, false, false); | ||
|
||
map<details::http_endpoint, http_resource*>::iterator it; | ||
|
||
size_t len = 0; | ||
size_t tot_len = 0; | ||
for (it = registered_resources.begin(); it != registered_resources.end(); ++it) { | ||
size_t endpoint_pieces_len = (*it).first.get_url_pieces().size(); | ||
size_t endpoint_tot_len = (*it).first.get_url_complete().size(); | ||
if (!found || endpoint_pieces_len > len || (endpoint_pieces_len == len && endpoint_tot_len > tot_len)) { | ||
if ((*it).first.match(endpoint)) { | ||
found = true; | ||
len = endpoint_pieces_len; | ||
tot_len = endpoint_tot_len; | ||
found_endpoint = it; | ||
{ | ||
std::shared_lock registered_resources_lock(registered_resources_mutex); | ||
if (!single_resource) { | ||
const char* st_url = mr->standardized_url->c_str(); | ||
fe = registered_resources_str.find(st_url); | ||
if (fe == registered_resources_str.end()) { | ||
if (regex_checking) { | ||
map<details::http_endpoint, http_resource*>::iterator found_endpoint; | ||
|
||
details::http_endpoint endpoint(st_url, false, false, false); | ||
|
||
map<details::http_endpoint, http_resource*>::iterator it; | ||
|
||
size_t len = 0; | ||
size_t tot_len = 0; | ||
for (it = registered_resources.begin(); it != registered_resources.end(); ++it) { | ||
size_t endpoint_pieces_len = (*it).first.get_url_pieces().size(); | ||
size_t endpoint_tot_len = (*it).first.get_url_complete().size(); | ||
if (!found || endpoint_pieces_len > len || (endpoint_pieces_len == len && endpoint_tot_len > tot_len)) { | ||
if ((*it).first.match(endpoint)) { | ||
found = true; | ||
len = endpoint_pieces_len; | ||
tot_len = endpoint_tot_len; | ||
found_endpoint = it; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (found) { | ||
vector<string> url_pars = found_endpoint->first.get_url_pars(); | ||
if (found) { | ||
vector<string> url_pars = found_endpoint->first.get_url_pars(); | ||
|
||
vector<string> url_pieces = endpoint.get_url_pieces(); | ||
vector<int> chunks = found_endpoint->first.get_chunk_positions(); | ||
for (unsigned int i = 0; i < url_pars.size(); i++) { | ||
mr->dhr->set_arg(url_pars[i], url_pieces[chunks[i]]); | ||
} | ||
vector<string> url_pieces = endpoint.get_url_pieces(); | ||
vector<int> chunks = found_endpoint->first.get_chunk_positions(); | ||
for (unsigned int i = 0; i < url_pars.size(); i++) { | ||
mr->dhr->set_arg(url_pars[i], url_pieces[chunks[i]]); | ||
} | ||
|
||
hrm = found_endpoint->second; | ||
hrm = found_endpoint->second; | ||
} | ||
} | ||
} else { | ||
hrm = fe->second; | ||
found = true; | ||
} | ||
} else { | ||
hrm = fe->second; | ||
hrm = registered_resources.begin()->second; | ||
found = true; | ||
} | ||
} else { | ||
hrm = registered_resources.begin()->second; | ||
found = true; | ||
} | ||
|
||
if (found) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth splitting this into two, given the writing use-cases are isolated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done