diff --git a/ChangeLog b/ChangeLog index 19261205..20fde92c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-11-13 Tatsuhiro Tsujikawa + + Added util::parseUIntNoThrow(). Use it in Request::parseUrl(). + * src/Request.cc + * src/util.cc + * src/util.h + * test/RequestTest.cc + 2009-11-13 Tatsuhiro Tsujikawa Rewritten util::uitos() diff --git a/src/Request.cc b/src/Request.cc index 2fbeea2e..8dd4dc14 100644 --- a/src/Request.cc +++ b/src/Request.cc @@ -38,7 +38,6 @@ #include "util.h" #include "FeatureConfig.h" -#include "RecoverableException.h" #include "StringFormat.h" #include "A2STR.h" #include "a2functional.h" @@ -252,14 +251,13 @@ bool Request::parseUrl(const std::string& url) { // its protocol.. _port = defPort; } else { - try { - unsigned int tempPort = - util::parseUInt(std::string(portFirst, authorityLast)); + uint32_t tempPort; + if(util::parseUIntNoThrow(tempPort, std::string(portFirst, authorityLast))){ if(65535 < tempPort) { return false; } - _port = tempPort; - } catch(RecoverableException& e) { + _port = tempPort; + } else { return false; } } diff --git a/src/util.cc b/src/util.cc index 6652f2bc..59e4bc52 100644 --- a/src/util.cc +++ b/src/util.cc @@ -395,6 +395,28 @@ uint32_t parseUInt(const std::string& s, int base) return v; } +bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base) +{ + std::string trimed = trim(s); + if(trimed.empty()) { + return false; + } + // We don't allow negative number. + if(trimed[0] == '-') { + return false; + } + char* stop; + errno = 0; + unsigned long int v = strtoul(trimed.c_str(), &stop, base); + if(*stop != '\0') { + return false; + } else if(((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT32_MAX)) { + return false; + } + result = v; + return true; +} + int64_t parseLLInt(const std::string& s, int32_t base) { std::string trimed = trim(s); diff --git a/src/util.h b/src/util.h index b60b246e..c4948ce9 100644 --- a/src/util.h +++ b/src/util.h @@ -180,6 +180,8 @@ int32_t parseInt(const std::string& s, int32_t base = 10); uint32_t parseUInt(const std::string& s, int base = 10); +bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base = 10); + int64_t parseLLInt(const std::string& s, int32_t base = 10); uint64_t parseULLInt(const std::string& s, int base = 10);