Skip to content

Commit

Permalink
Handle "..", "." in util::joinUri()
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Apr 30, 2011
1 parent 55d00d0 commit 1122e55
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1594,18 +1594,35 @@ std::string joinUri(const std::string& baseUri, const std::string& uri)
if(!uri::parse(bus, baseUri)) {
return uri;
}
if(util::startsWith(uri, "/")) {
bus.dir.clear();
std::vector<std::string> parts;
if(!util::startsWith(uri, "/")) {
util::split(bus.dir, std::back_inserter(parts), "/");
}
std::string::const_iterator qend;
for(qend = uri.begin(); qend != uri.end(); ++qend) {
if(*qend == '#') {
break;
}
}
std::string::const_iterator end;
for(end = uri.begin(); end != qend; ++end) {
if(*end == '?') {
break;
}
}
std::string path(uri.begin(), end);
util::split(path, std::back_inserter(parts), "/");
bus.dir.clear();
bus.file.clear();
bus.query.clear();
std::string newUri = construct(bus);
if(util::startsWith(uri, "/")) {
newUri += uri.substr(1);
} else {
newUri += uri;
std::string res = uri::construct(bus);
res += util::joinPath(parts.begin(), parts.end());
if((path.empty() || util::endsWith(path, "/")) &&
!util::endsWith(res, "/")) {
res += "/";
}
return newUri;
res += std::string(end, qend);
return res;
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/UtilTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,33 @@ void UtilTest::testJoinUri()
"dir/file?q=k"));
CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
util::joinUri("baduri", "dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/b/d/file"),
util::joinUri("http://base/a/b/c/x",
"../d/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/b/file"),
util::joinUri("http://base/c/x",
"../../a/b/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/"),
util::joinUri("http://base/c/x",
"../.."));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/"),
util::joinUri("http://base/c/x",
".."));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/file"),
util::joinUri("http://base/b/c/x",
"/a/x/../file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file"),
util::joinUri("http://base/f/?q=k",
"/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file?q=/"),
util::joinUri("http://base/",
"/file?q=/"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file?q=v"),
util::joinUri("http://base/",
"/file?q=v#a?q=x"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file"),
util::joinUri("http://base/",
"/file#a?q=x"));
}

} // namespace aria2

0 comments on commit 1122e55

Please sign in to comment.