diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 93f8c917..14cb0395 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -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); diff --git a/src/asio_common.cc b/src/asio_common.cc index 0319169e..bbcb7e6a 100644 --- a/src/asio_common.cc +++ b/src/asio_common.cc @@ -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); } diff --git a/src/asio_common.h b/src/asio_common.h index b2384649..7eacfa51 100644 --- a/src/asio_common.h +++ b/src/asio_common.h @@ -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); } diff --git a/src/http2.h b/src/http2.h index dedee285..7fa42b37 100644 --- a/src/http2.h +++ b/src/http2.h @@ -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; diff --git a/src/nghttp.cc b/src/nghttp.cc index 057eac01..2a392f67 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -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; diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index b379b7ac..c6b88721 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -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); } } diff --git a/src/util.cc b/src/util.cc index 5f0a7276..8dd84b1e 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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) != @@ -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 += '%'; @@ -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(target.c_str()), - target.size()); +std::string percent_encode(const std::string &target) { + return percent_encode(reinterpret_cast(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; } diff --git a/src/util.h b/src/util.h index 8eb88cdd..b2e62171 100644 --- a/src/util.h +++ b/src/util.h @@ -64,104 +64,6 @@ constexpr const char NGHTTP2_H1_1[] = "http/1.1"; namespace util { -extern const char DEFAULT_STRIP_CHARSET[]; - -template -std::pair -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 -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 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 -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 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 -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 -std::string joinPath(InputIterator first, InputIterator last) { - std::vector 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'); } @@ -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); @@ -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 -std::string percentDecode(InputIt first, InputIt last) { +std::string percent_decode(InputIt first, InputIt last) { std::string result; for (; first != last; ++first) { if (*first == '%') { diff --git a/src/util_test.cc b/src/util_test.cc index da2485a9..77542017 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -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))); } }