Moved joinUri from util namespace to uri namespace.

pull/1/head
Tatsuhiro Tsujikawa 2011-05-06 23:20:43 +09:00
parent 11d7e4fa46
commit e7d7233d54
6 changed files with 108 additions and 95 deletions

View File

@ -295,6 +295,48 @@ std::string construct(const UriStruct& us)
return res;
}
std::string joinUri(const std::string& baseUri, const std::string& uri)
{
UriStruct us;
if(parse(us, uri)) {
return uri;
} else {
UriStruct bus;
if(!parse(bus, baseUri)) {
return uri;
}
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 res = construct(bus);
res += util::joinPath(parts.begin(), parts.end());
if((path.empty() || util::endsWith(path, "/")) &&
!util::endsWith(res, "/")) {
res += "/";
}
res += std::string(end, qend);
return res;
}
}
} // namespace uri
} // namespace aria2

View File

@ -72,6 +72,8 @@ bool parse(UriStruct& result, const std::string& uri);
std::string construct(const UriStruct& us);
std::string joinUri(const std::string& baseUri, const std::string& uri);
} // namespace uri
} // namespace aria2

View File

@ -81,7 +81,6 @@
#include "Logger.h"
#include "Option.h"
#include "DownloadContext.h"
#include "uri.h"
#ifdef ENABLE_MESSAGE_DIGEST
# include "MessageDigest.h"
@ -1584,48 +1583,6 @@ std::string safeStrerror(int errNum)
return makeString(strerror(errNum));
}
std::string joinUri(const std::string& baseUri, const std::string& uri)
{
uri::UriStruct us;
if(uri::parse(us, uri)) {
return uri;
} else {
uri::UriStruct bus;
if(!uri::parse(bus, baseUri)) {
return uri;
}
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 res = uri::construct(bus);
res += util::joinPath(parts.begin(), parts.end());
if((path.empty() || util::endsWith(path, "/")) &&
!util::endsWith(res, "/")) {
res += "/";
}
res += std::string(end, qend);
return res;
}
}
} // namespace util
} // namespace aria2

View File

@ -324,8 +324,6 @@ std::string joinPath(InputIterator first, InputIterator last)
return strjoin(elements.begin(), elements.end(), "/");
}
std::string joinUri(const std::string& baseUri, const std::string& uri);
// Parses INDEX=PATH format string. INDEX must be an unsigned
// integer.
std::map<size_t, std::string>::value_type

View File

@ -35,6 +35,7 @@ class UriTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testInnerLink);
CPPUNIT_TEST(testConstruct);
CPPUNIT_TEST(testSwap);
CPPUNIT_TEST(testJoinUri);
CPPUNIT_TEST_SUITE_END();
public:
@ -64,6 +65,7 @@ public:
void testInnerLink();
void testConstruct();
void testSwap();
void testJoinUri();
};
@ -485,6 +487,68 @@ void UriTest::testSwap()
construct(us2));
}
void UriTest::testJoinUri()
{
CPPUNIT_ASSERT_EQUAL(std::string("http://host/dir/file"),
joinUri("http://base/d/f",
"http://host/dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/dir/file"),
joinUri("http://base/d/f",
"/dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/dir/file"),
joinUri("http://base/d/f",
"dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/"),
joinUri("http://base/d/f",
""));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/dir/file?q=k"),
joinUri("http://base/d/f",
"dir/file?q=k"));
CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
joinUri("baduri", "dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/b/d/file"),
joinUri("http://base/a/b/c/x",
"../d/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/b/file"),
joinUri("http://base/c/x",
"../../a/b/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/"),
joinUri("http://base/c/x",
"../.."));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/"),
joinUri("http://base/c/x",
".."));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/a/file"),
joinUri("http://base/b/c/x",
"/a/x/../file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file"),
joinUri("http://base/f/?q=k",
"/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file?q=/"),
joinUri("http://base/",
"/file?q=/"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file?q=v"),
joinUri("http://base/",
"/file?q=v#a?q=x"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/file"),
joinUri("http://base/",
"/file#a?q=x"));
}
} // namespace uri
} // namespace aria2

View File

@ -71,7 +71,6 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testGetCidrPrefix);
CPPUNIT_TEST(testInSameCidrBlock);
CPPUNIT_TEST(testIsUtf8String);
CPPUNIT_TEST(testJoinUri);
CPPUNIT_TEST_SUITE_END();
private:
@ -131,7 +130,6 @@ public:
void testGetCidrPrefix();
void testInSameCidrBlock();
void testIsUtf8String();
void testJoinUri();
};
@ -1249,52 +1247,4 @@ void UtilTest::testIsUtf8String()
CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
}
void UtilTest::testJoinUri()
{
CPPUNIT_ASSERT_EQUAL(std::string("http://host/dir/file"),
util::joinUri("http://base/d/f",
"http://host/dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/dir/file"),
util::joinUri("http://base/d/f",
"/dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/dir/file"),
util::joinUri("http://base/d/f",
"dir/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/"),
util::joinUri("http://base/d/f",
""));
CPPUNIT_ASSERT_EQUAL(std::string("http://base/d/dir/file?q=k"),
util::joinUri("http://base/d/f",
"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