Skip to content

Commit

Permalink
util: Remove unused functions; rename regacy camel-case function names
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Nov 27, 2015
1 parent c0858d8 commit 1ba28be
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/HttpServer.cc
Expand Up @@ -1174,7 +1174,7 @@ void prepare_response(Stream *stream, Http2Handler *hd,

auto sessions = hd->get_sessions();

url = util::percentDecode(std::begin(url), std::end(url));
url = util::percent_decode(std::begin(url), std::end(url));
if (!util::check_path(url)) {
if (stream->file_ent) {
sessions->release_fd(stream->file_ent);
Expand Down
2 changes: 1 addition & 1 deletion src/asio_common.cc
Expand Up @@ -134,7 +134,7 @@ generator_cb file_generator_from_fd(int fd) {
bool check_path(const std::string &path) { return util::check_path(path); }

std::string percent_decode(const std::string &s) {
return util::percentDecode(std::begin(s), std::end(s));
return util::percent_decode(std::begin(s), std::end(s));
}

std::string http_date(int64_t t) { return util::http_date(t); }
Expand Down
2 changes: 1 addition & 1 deletion src/asio_common.h
Expand Up @@ -55,7 +55,7 @@ void split_path(uri_ref &dst, InputIt first, InputIt last) {
} else {
query_first = path_last + 1;
}
dst.path = util::percentDecode(first, path_last);
dst.path = util::percent_decode(first, path_last);
dst.raw_path.assign(first, path_last);
dst.raw_query.assign(query_first, last);
}
Expand Down
2 changes: 1 addition & 1 deletion src/http2.h
Expand Up @@ -343,7 +343,7 @@ std::string normalize_path(InputIt first, InputIt last) {
if (util::isHexDigit(*(first + 1)) && util::isHexDigit(*(first + 2))) {
auto c = (util::hex_to_uint(*(first + 1)) << 4) +
util::hex_to_uint(*(first + 2));
if (util::inRFC3986UnreservedChars(c)) {
if (util::in_rfc3986_unreserved_chars(c)) {
result += c;
first += 3;
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/nghttp.cc
Expand Up @@ -199,7 +199,7 @@ std::string decode_host(std::string host) {
auto zone_id_src = (*(zone_start + 1) == '2' && *(zone_start + 2) == '5')
? zone_start + 3
: zone_start + 1;
auto zone_id = util::percentDecode(zone_id_src, std::end(host));
auto zone_id = util::percent_decode(zone_id_src, std::end(host));
host.erase(zone_start + 1, std::end(host));
host += zone_id;
return host;
Expand Down
2 changes: 1 addition & 1 deletion src/shrpx_config.cc
Expand Up @@ -1664,7 +1664,7 @@ int parse_config(const char *opt, const char *optarg,
// Surprisingly, u.field_set & UF_USERINFO is nonzero even if
// userinfo component is empty string.
if (!val.empty()) {
val = util::percentDecode(val.begin(), val.end());
val = util::percent_decode(std::begin(val), std::end(val));
mod_config()->downstream_http_proxy_userinfo = strcopy(val);
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/util.cc
Expand Up @@ -63,11 +63,9 @@ namespace nghttp2 {

namespace util {

const char DEFAULT_STRIP_CHARSET[] = "\r\n\t ";

const char UPPER_XDIGITS[] = "0123456789ABCDEF";

bool inRFC3986UnreservedChars(const char c) {
bool in_rfc3986_unreserved_chars(const char c) {
static constexpr const char unreserved[] = {'-', '.', '_', '~'};
return isAlpha(c) || isDigit(c) ||
std::find(std::begin(unreserved), std::end(unreserved), c) !=
Expand All @@ -81,12 +79,12 @@ bool in_rfc3986_sub_delims(const char c) {
std::end(sub_delims);
}

std::string percentEncode(const unsigned char *target, size_t len) {
std::string percent_encode(const unsigned char *target, size_t len) {
std::string dest;
for (size_t i = 0; i < len; ++i) {
unsigned char c = target[i];

if (inRFC3986UnreservedChars(c)) {
if (in_rfc3986_unreserved_chars(c)) {
dest += c;
} else {
dest += '%';
Expand All @@ -97,15 +95,16 @@ std::string percentEncode(const unsigned char *target, size_t len) {
return dest;
}

std::string percentEncode(const std::string &target) {
return percentEncode(reinterpret_cast<const unsigned char *>(target.c_str()),
target.size());
std::string percent_encode(const std::string &target) {
return percent_encode(reinterpret_cast<const unsigned char *>(target.c_str()),
target.size());
}

std::string percent_encode_path(const std::string &s) {
std::string dest;
for (auto c : s) {
if (inRFC3986UnreservedChars(c) || in_rfc3986_sub_delims(c) || c == '/') {
if (in_rfc3986_unreserved_chars(c) || in_rfc3986_sub_delims(c) ||
c == '/') {
dest += c;
continue;
}
Expand Down
106 changes: 4 additions & 102 deletions src/util.h
Expand Up @@ -64,104 +64,6 @@ constexpr const char NGHTTP2_H1_1[] = "http/1.1";

namespace util {

extern const char DEFAULT_STRIP_CHARSET[];

template <typename InputIterator>
std::pair<InputIterator, InputIterator>
stripIter(InputIterator first, InputIterator last,
const char *chars = DEFAULT_STRIP_CHARSET) {
for (; first != last && strchr(chars, *first) != 0; ++first)
;
if (first == last) {
return std::make_pair(first, last);
}
InputIterator left = last - 1;
for (; left != first && strchr(chars, *left) != 0; --left)
;
return std::make_pair(first, left + 1);
}

template <typename InputIterator, typename OutputIterator>
OutputIterator splitIter(InputIterator first, InputIterator last,
OutputIterator out, char delim, bool doStrip = false,
bool allowEmpty = false) {
for (InputIterator i = first; i != last;) {
InputIterator j = std::find(i, last, delim);
std::pair<InputIterator, InputIterator> p(i, j);
if (doStrip) {
p = stripIter(i, j);
}
if (allowEmpty || p.first != p.second) {
*out++ = p;
}
i = j;
if (j != last) {
++i;
}
}
if (allowEmpty && (first == last || *(last - 1) == delim)) {
*out++ = std::make_pair(last, last);
}
return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator split(InputIterator first, InputIterator last,
OutputIterator out, char delim, bool doStrip = false,
bool allowEmpty = false) {
for (InputIterator i = first; i != last;) {
InputIterator j = std::find(i, last, delim);
std::pair<InputIterator, InputIterator> p(i, j);
if (doStrip) {
p = stripIter(i, j);
}
if (allowEmpty || p.first != p.second) {
*out++ = std::string(p.first, p.second);
}
i = j;
if (j != last) {
++i;
}
}
if (allowEmpty && (first == last || *(last - 1) == delim)) {
*out++ = std::string(last, last);
}
return out;
}

template <typename InputIterator, typename DelimiterType>
std::string strjoin(InputIterator first, InputIterator last,
const DelimiterType &delim) {
std::string result;
if (first == last) {
return result;
}
InputIterator beforeLast = last - 1;
for (; first != beforeLast; ++first) {
result += *first;
result += delim;
}
result += *beforeLast;
return result;
}

template <typename InputIterator>
std::string joinPath(InputIterator first, InputIterator last) {
std::vector<std::string> elements;
for (; first != last; ++first) {
if (*first == "..") {
if (!elements.empty()) {
elements.pop_back();
}
} else if (*first == ".") {
// do nothing
} else {
elements.push_back(*first);
}
}
return strjoin(elements.begin(), elements.end(), "/");
}

inline bool isAlpha(const char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
Expand All @@ -172,7 +74,7 @@ inline bool isHexDigit(const char c) {
return isDigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}

bool inRFC3986UnreservedChars(const char c);
bool in_rfc3986_unreserved_chars(const char c);

bool in_rfc3986_sub_delims(const char c);

Expand All @@ -185,15 +87,15 @@ bool in_attr_char(char c);
// if isHexDigit(c) is false.
uint32_t hex_to_uint(char c);

std::string percentEncode(const unsigned char *target, size_t len);
std::string percent_encode(const unsigned char *target, size_t len);

std::string percentEncode(const std::string &target);
std::string percent_encode(const std::string &target);

// percent-encode path component of URI |s|.
std::string percent_encode_path(const std::string &s);

template <typename InputIt>
std::string percentDecode(InputIt first, InputIt last) {
std::string percent_decode(InputIt first, InputIt last) {
std::string result;
for (; first != last; ++first) {
if (*first == '%') {
Expand Down
6 changes: 3 additions & 3 deletions src/util_test.cc
Expand Up @@ -147,15 +147,15 @@ void test_util_percent_encode_path(void) {
void test_util_percent_decode(void) {
{
std::string s = "%66%6F%6f%62%61%72";
CU_ASSERT("foobar" == util::percentDecode(std::begin(s), std::end(s)));
CU_ASSERT("foobar" == util::percent_decode(std::begin(s), std::end(s)));
}
{
std::string s = "%66%6";
CU_ASSERT("f%6" == util::percentDecode(std::begin(s), std::end(s)));
CU_ASSERT("f%6" == util::percent_decode(std::begin(s), std::end(s)));
}
{
std::string s = "%66%";
CU_ASSERT("f%" == util::percentDecode(std::begin(s), std::end(s)));
CU_ASSERT("f%" == util::percent_decode(std::begin(s), std::end(s)));
}
}

Expand Down

0 comments on commit 1ba28be

Please sign in to comment.