Skip to content

Commit 1a57e2c

Browse files
committed
standardize_url doesn't remove the root / now
1 parent 4399d38 commit 1a57e2c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/http_utils.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,11 @@ void http_utils::standardize_url(const std::string& url, std::string& result)
224224
{
225225
std::string n_url;
226226
string_utilities::regex_replace(url, "(\\/)+", "/", n_url);
227-
if(n_url[n_url.size() - 1] == '/')
227+
std::string::size_type n_url_length = n_url.length();
228+
229+
if (n_url_length > 1 && n_url[n_url_length - 1] == '/')
228230
{
229-
result = n_url.substr(0, n_url.size() -1);
231+
result = n_url.substr(0, n_url_length - 1);
230232
}
231233
else
232234
{
@@ -545,7 +547,7 @@ char* load_file (const char *filename)
545547
void dump_header_map(std::ostream &os, const std::string &prefix,
546548
const std::map<std::string,std::string,header_comparator> &map)
547549
{
548-
std::map<std::string,std::string,header_comparator>::const_iterator it = map.begin();
550+
std::map<std::string,std::string,header_comparator>::const_iterator it = map.begin();
549551
std::map<std::string,std::string,header_comparator>::const_iterator end = map.end();
550552

551553
if (map.size()) {
@@ -560,7 +562,7 @@ void dump_header_map(std::ostream &os, const std::string &prefix,
560562
void dump_arg_map(std::ostream &os, const std::string &prefix,
561563
const std::map<std::string,std::string,arg_comparator> &map)
562564
{
563-
std::map<std::string,std::string,arg_comparator>::const_iterator it = map.begin();
565+
std::map<std::string,std::string,arg_comparator>::const_iterator it = map.begin();
564566
std::map<std::string,std::string,arg_comparator>::const_iterator end = map.end();
565567

566568
if (map.size()) {
@@ -571,7 +573,7 @@ void dump_arg_map(std::ostream &os, const std::string &prefix,
571573
os << "]" << std::endl;
572574
}
573575
}
574-
575-
576+
577+
576578
};
577579
};

test/unit/http_utils_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, unescape)
4747
LT_CHECK_EQ(expected_size, 3);
4848
LT_END_AUTO_TEST(unescape)
4949

50+
LT_BEGIN_AUTO_TEST(http_utils_suite, standardize_url)
51+
string url = "/", result;
52+
http::http_utils::standardize_url(url, result);
53+
LT_CHECK_EQ(result, "/");
54+
55+
url = "/abc/", result = "";
56+
http::http_utils::standardize_url(url, result);
57+
LT_CHECK_EQ(result, "/abc");
58+
59+
url = "/abc", result = "";
60+
http::http_utils::standardize_url(url, result);
61+
LT_CHECK_EQ(result, "/abc");
62+
63+
url = "/abc/pqr/", result = "";
64+
http::http_utils::standardize_url(url, result);
65+
LT_CHECK_EQ(result, "/abc/pqr");
66+
67+
url = "/abc/pqr", result = "";
68+
http::http_utils::standardize_url(url, result);
69+
LT_CHECK_EQ(result, "/abc/pqr");
70+
LT_END_AUTO_TEST(standardize_url)
71+
5072
LT_BEGIN_AUTO_TEST_ENV()
5173
AUTORUN_TESTS()
5274
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)