Skip to content

Commit

Permalink
rgw: swith std::string to boost::string_ref to handle
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Wenjun <jingwenjun@cmss.chinamobile.com>
  • Loading branch information
Jing-Scott committed Jan 25, 2017
1 parent 95cd023 commit 8a3c09d
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions src/rgw/rgw_acl.h
Expand Up @@ -8,6 +8,9 @@
#include <string>
#include <include/types.h>

#include <boost/optional.hpp>
#include <boost/utility/string_ref.hpp>

#include "common/debug.h"

#include "rgw_basic_types.h"
Expand Down Expand Up @@ -214,57 +217,30 @@ struct ACLReferer {
perm(perm) {
}

bool is_match(std::string http_referer) const {
std::string http_host;
if (!get_http_host(http_referer, http_host))
bool is_match(boost::string_ref http_referer) const {
boost::optional<boost::string_ref> http_host = get_http_host(http_referer);
if ((*http_host).empty()) {
return false;
}

if (http_host == url_spec) {
boost::string_ref host_spec(url_spec);
if (*http_host == host_spec) {
return true;
}

if (http_host.length() < url_spec.length()) {
if ((*http_host).length() < host_spec.length()) {
return false;
}

if ('.' == url_spec[0]) {
if ('.' == host_spec[0]) {
/* Wildcard support: a referer matches the spec when its last char are
* perfectly equal to spec. */
return !http_host.compare(http_host.length() - url_spec.length(),
url_spec.length(), url_spec);
return (*http_host).ends_with(host_spec);
}

return false;
}

bool get_http_host(std::string url, std::string& http_host) const {
int pos = url.find("://");
if (pos < 0 || pos == (int)url.size() - 3)
return false;

string scheme = url.substr(0, pos);
if (scheme.empty())
return false;

string url_sub = url.substr(pos + 3);
pos = url_sub.find('@');
if (pos == (int)url_sub.size())
return false;

url_sub = url_sub.substr(pos + 1);
pos = url_sub.find_first_of("/:");
if (pos < 0) {
//no port or path exists
http_host = url_sub;
return true;
}
http_host = url_sub.substr(0, pos);
if (!http_host.empty())
return true;

return false;
}

void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(url_spec, bl);
Expand All @@ -278,6 +254,26 @@ struct ACLReferer {
DECODE_FINISH(bl);
}
void dump(Formatter *f) const;

private:
boost::optional<boost::string_ref> get_http_host(const boost::string_ref url) const {
size_t pos = url.find("://");
if (pos == boost::string_ref::npos || url.starts_with("://") ||
url.ends_with("://") || url.ends_with('@')) {
return boost::string_ref(nullptr, 0);
}
boost::string_ref url_sub = url.substr(pos + strlen("://"));
pos = url_sub.find('@');
if (pos != boost::string_ref::npos) {
url_sub = url_sub.substr(pos + 1);
}
pos = url_sub.find_first_of("/:");
if (pos == boost::string_ref::npos) {
/* no port or path exists */
return url_sub;
}
return url_sub.substr(0, pos);
}
};
WRITE_CLASS_ENCODER(ACLReferer)

Expand Down

0 comments on commit 8a3c09d

Please sign in to comment.