Explorar o código

2009-11-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added util::parseUIntNoThrow(). Use it in Request::parseUrl().
	* src/Request.cc
	* src/util.cc
	* src/util.h
	* test/RequestTest.cc
Tatsuhiro Tsujikawa %!s(int64=16) %!d(string=hai) anos
pai
achega
9cdf102850
Modificáronse 4 ficheiros con 36 adicións e 6 borrados
  1. 8 0
      ChangeLog
  2. 4 6
      src/Request.cc
  3. 22 0
      src/util.cc
  4. 2 0
      src/util.h

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2009-11-13  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	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  <t-tujikawa@users.sourceforge.net>
 
 	Rewritten util::uitos()

+ 4 - 6
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;
     }
   }

+ 22 - 0
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);

+ 2 - 0
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);