Rewritten HttpProxyOptionHandler using uri::parse().

Removed test case where username is empty and resulted URI has empty
username in URI.
pull/2/head
Tatsuhiro Tsujikawa 2011-10-18 00:40:43 +09:00
parent 065fb3a6b8
commit fc4d38d236
2 changed files with 21 additions and 34 deletions

View File

@ -56,6 +56,7 @@
#include "FileEntry.h" #include "FileEntry.h"
#include "a2io.h" #include "a2io.h"
#include "LogFactory.h" #include "LogFactory.h"
#include "uri.h"
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
# include "MessageDigest.h" # include "MessageDigest.h"
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
@ -674,36 +675,23 @@ void HttpProxyOptionHandler::parseArg(Option& option, const std::string& optarg)
uri = "http://"; uri = "http://";
uri += optarg; uri += optarg;
} }
if(!req.setUri(uri)) { uri::UriStruct us;
if(!uri::parse(us, uri)) {
throw DL_ABORT_EX(_("unrecognized proxy format")); throw DL_ABORT_EX(_("unrecognized proxy format"));
} }
uri = "http://"; us.protocol = "http";
if(req.getUsername().empty()) { if(us.username.empty()) {
if(option.defined(proxyUserPref_)) { 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_)) { if(option.defined(proxyPasswdPref_)) {
uri += A2STR::COLON_C; us.password = option.get(proxyPasswdPref_);
uri += util::percentEncode(option.get(proxyPasswdPref_)); us.hasPassword = true;
} }
} else {
uri += A2STR::COLON_C;
uri += util::percentEncode(req.getPassword());
} }
if(uri.size() > 7) { option.put(optName_, uri::construct(us));
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);
} }
} }

View File

@ -313,11 +313,11 @@ void OptionHandlerTest::testHttpProxyOptionHandler()
CPPUNIT_ASSERT(!handler.canHandle("foobar")); CPPUNIT_ASSERT(!handler.canHandle("foobar"));
Option option; Option option;
handler.parse(option, "proxy:65535"); 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)); option.get(PREF_HTTP_PROXY));
handler.parse(option, "http://proxy:8080"); 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)); option.get(PREF_HTTP_PROXY));
handler.parse(option, ""); handler.parse(option, "");
@ -332,32 +332,31 @@ void OptionHandlerTest::testHttpProxyOptionHandler()
handler.createPossibleValuesString()); handler.createPossibleValuesString());
handler.parse(option, "http://user%40:passwd%40@proxy:8080"); 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.get(PREF_HTTP_PROXY));
option.put(PREF_HTTP_PROXY_USER, "proxy@user"); option.put(PREF_HTTP_PROXY_USER, "proxy@user");
handler.parse(option, "http://proxy:8080"); 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.get(PREF_HTTP_PROXY));
option.put(PREF_HTTP_PROXY_PASSWD, "proxy@passwd"); option.put(PREF_HTTP_PROXY_PASSWD, "proxy@passwd");
handler.parse(option, "http://proxy:8080"); handler.parse(option, "http://proxy:8080");
CPPUNIT_ASSERT_EQUAL 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)); option.get(PREF_HTTP_PROXY));
handler.parse(option, "http://user:passwd@proxy:8080"); handler.parse(option, "http://user:passwd@proxy:8080");
CPPUNIT_ASSERT_EQUAL(std::string("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"),
option.get(PREF_HTTP_PROXY)); option.get(PREF_HTTP_PROXY));
option.put(PREF_HTTP_PROXY_PASSWD, ""); option.put(PREF_HTTP_PROXY_PASSWD, "");
handler.parse(option, "http://proxy:8080"); 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)); option.get(PREF_HTTP_PROXY));
} }