Fixed bug that some information such as port number is lost if

redirect URI contains only path component.
pull/2/head
Tatsuhiro Tsujikawa 2011-11-05 13:16:45 +09:00
parent 0b515d7204
commit 5e2a8b2d4c
2 changed files with 18 additions and 10 deletions

View File

@ -111,14 +111,17 @@ bool Request::redirectUri(const std::string& uri) {
if(uri.find("://") == std::string::npos) {
// rfc2616 requires absolute URI should be provided by Location header
// field, but some servers don't obey this rule.
uri::UriStruct rus(us_);
rus.query.clear();
rus.file.clear();
size_t offset = 0;
if(uri[0] == '/') {
// abosulute path
redirectedUri = strconcat(us_.protocol, "://", us_.host, uri);
} else {
// relative path
redirectedUri = strconcat(us_.protocol, "://", us_.host, us_.dir,
"/", uri);
rus.dir.clear();
offset = 1;
}
redirectedUri = uri::construct(rus);
redirectedUri.append(uri.begin()+offset, uri.end());
} else {
redirectedUri = uri;
}

View File

@ -98,8 +98,13 @@ void RequestTest::testRedirectUri()
req.supportsPersistentConnection(false);
req.setUri("http://aria.rednoah.com:8080/aria2/index.html");
bool v2 = req.redirectUri("http://aria.rednoah.co.jp/");
CPPUNIT_ASSERT(v2);
// See port number is preserved.
CPPUNIT_ASSERT(req.redirectUri("/foo"));
CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com:8080/foo"),
req.getCurrentUri());
CPPUNIT_ASSERT_EQUAL((unsigned int)1, req.getRedirectCount());
CPPUNIT_ASSERT(req.redirectUri("http://aria.rednoah.co.jp/"));
// persistent connection flag is set to be true after redirection
CPPUNIT_ASSERT(req.supportsPersistentConnection());
// uri must be the same
@ -118,20 +123,20 @@ void RequestTest::testRedirectUri()
CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile());
CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery());
// See redirect count is incremented.
CPPUNIT_ASSERT_EQUAL((unsigned int)1, req.getRedirectCount());
CPPUNIT_ASSERT_EQUAL((unsigned int)2, req.getRedirectCount());
// Give abosulute path
CPPUNIT_ASSERT(req.redirectUri("/abspath/to/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.co.jp/abspath/to/file"),
req.getCurrentUri());
CPPUNIT_ASSERT_EQUAL((unsigned int)2, req.getRedirectCount());
CPPUNIT_ASSERT_EQUAL((unsigned int)3, req.getRedirectCount());
// Give relative path
CPPUNIT_ASSERT(req.redirectUri("relativepath/to/file"));
CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.co.jp/abspath/to/"
"relativepath/to/file"),
req.getCurrentUri());
CPPUNIT_ASSERT_EQUAL((unsigned int)3, req.getRedirectCount());
CPPUNIT_ASSERT_EQUAL((unsigned int)4, req.getRedirectCount());
}
void RequestTest::testRedirectUri2()