From fc4d38d236a883cea649cccfbe3fd0d6f3d06f48 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 18 Oct 2011 00:40:43 +0900 Subject: [PATCH] Rewritten HttpProxyOptionHandler using uri::parse(). Removed test case where username is empty and resulted URI has empty username in URI. --- src/OptionHandlerImpl.cc | 32 ++++++++++---------------------- test/OptionHandlerTest.cc | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/OptionHandlerImpl.cc b/src/OptionHandlerImpl.cc index e0b9d33a..551bd29d 100644 --- a/src/OptionHandlerImpl.cc +++ b/src/OptionHandlerImpl.cc @@ -56,6 +56,7 @@ #include "FileEntry.h" #include "a2io.h" #include "LogFactory.h" +#include "uri.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigest.h" #endif // ENABLE_MESSAGE_DIGEST @@ -674,36 +675,23 @@ void HttpProxyOptionHandler::parseArg(Option& option, const std::string& optarg) uri = "http://"; uri += optarg; } - if(!req.setUri(uri)) { + uri::UriStruct us; + if(!uri::parse(us, uri)) { throw DL_ABORT_EX(_("unrecognized proxy format")); } - uri = "http://"; - if(req.getUsername().empty()) { + us.protocol = "http"; + if(us.username.empty()) { if(option.defined(proxyUserPref_)) { - uri += util::percentEncode(option.get(proxyUserPref_)); + us.username = option.get(proxyUserPref_); } - } else { - uri += util::percentEncode(req.getUsername()); } - if(!req.hasPassword()) { + if(!us.hasPassword) { if(option.defined(proxyPasswdPref_)) { - uri += A2STR::COLON_C; - uri += util::percentEncode(option.get(proxyPasswdPref_)); + us.password = option.get(proxyPasswdPref_); + us.hasPassword = true; } - } else { - uri += A2STR::COLON_C; - uri += util::percentEncode(req.getPassword()); } - if(uri.size() > 7) { - uri += "@"; - } - if(req.isIPv6LiteralAddress()) { - strappend(uri, "[", req.getHost(), "]"); - } else { - uri += req.getHost(); - } - strappend(uri, A2STR::COLON_C, util::uitos(req.getPort())); - option.put(optName_, uri); + option.put(optName_, uri::construct(us)); } } diff --git a/test/OptionHandlerTest.cc b/test/OptionHandlerTest.cc index 0c8fc197..671192af 100644 --- a/test/OptionHandlerTest.cc +++ b/test/OptionHandlerTest.cc @@ -313,11 +313,11 @@ void OptionHandlerTest::testHttpProxyOptionHandler() CPPUNIT_ASSERT(!handler.canHandle("foobar")); Option option; handler.parse(option, "proxy:65535"); - CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:65535"), + CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:65535/"), option.get(PREF_HTTP_PROXY)); handler.parse(option, "http://proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:8080"), + CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:8080/"), option.get(PREF_HTTP_PROXY)); handler.parse(option, ""); @@ -332,32 +332,31 @@ void OptionHandlerTest::testHttpProxyOptionHandler() handler.createPossibleValuesString()); handler.parse(option, "http://user%40:passwd%40@proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://user%40:passwd%40@proxy:8080"), + CPPUNIT_ASSERT_EQUAL(std::string("http://user%40:passwd%40@proxy:8080/"), option.get(PREF_HTTP_PROXY)); option.put(PREF_HTTP_PROXY_USER, "proxy@user"); handler.parse(option, "http://proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user@proxy:8080"), + CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user@proxy:8080/"), option.get(PREF_HTTP_PROXY)); option.put(PREF_HTTP_PROXY_PASSWD, "proxy@passwd"); handler.parse(option, "http://proxy:8080"); CPPUNIT_ASSERT_EQUAL - (std::string("http://proxy%40user:proxy%40passwd@proxy:8080"), + (std::string("http://proxy%40user:proxy%40passwd@proxy:8080/"), option.get(PREF_HTTP_PROXY)); handler.parse(option, "http://user:passwd@proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://user:passwd@proxy:8080"), - option.get(PREF_HTTP_PROXY)); - - option.put(PREF_HTTP_PROXY_USER, ""); - handler.parse(option, "http://proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://:proxy%40passwd@proxy:8080"), + CPPUNIT_ASSERT_EQUAL(std::string("http://user:passwd@proxy:8080/"), option.get(PREF_HTTP_PROXY)); option.put(PREF_HTTP_PROXY_PASSWD, ""); handler.parse(option, "http://proxy:8080"); - CPPUNIT_ASSERT_EQUAL(std::string("http://:@proxy:8080"), + CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user:@proxy:8080/"), + option.get(PREF_HTTP_PROXY)); + + handler.parse(option, "http://[::1]:8080"); + CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user:@[::1]:8080/"), option.get(PREF_HTTP_PROXY)); }