added URISyntaxException; better error handling in URIStreamOpener

This commit is contained in:
Günter Obiltschnig 2016-09-29 17:41:37 +02:00 committed by Guenter Obiltschnig
parent 61e0561182
commit 5d8faa371e
4 changed files with 44 additions and 12 deletions

View File

@ -255,6 +255,8 @@ POCO_DECLARE_EXCEPTION(Foundation_API, OpenFileException, FileException)
POCO_DECLARE_EXCEPTION(Foundation_API, WriteFileException, FileException)
POCO_DECLARE_EXCEPTION(Foundation_API, ReadFileException, FileException)
POCO_DECLARE_EXCEPTION(Foundation_API, UnknownURISchemeException, RuntimeException)
POCO_DECLARE_EXCEPTION(Foundation_API, TooManyURIRedirectsException, RuntimeException)
POCO_DECLARE_EXCEPTION(Foundation_API, URISyntaxException, SyntaxException)
POCO_DECLARE_EXCEPTION(Foundation_API, ApplicationException, Exception)
POCO_DECLARE_EXCEPTION(Foundation_API, BadCastException, RuntimeException)

View File

@ -179,7 +179,8 @@ POCO_IMPLEMENT_EXCEPTION(OpenFileException, FileException, "Cannot open file")
POCO_IMPLEMENT_EXCEPTION(WriteFileException, FileException, "Cannot write file")
POCO_IMPLEMENT_EXCEPTION(ReadFileException, FileException, "Cannot read file")
POCO_IMPLEMENT_EXCEPTION(UnknownURISchemeException, RuntimeException, "Unknown URI scheme")
POCO_IMPLEMENT_EXCEPTION(TooManyURIRedirectsException, RuntimeException, "Too many URI redirects")
POCO_IMPLEMENT_EXCEPTION(URISyntaxException, SyntaxException, "Bad URI syntax")
POCO_IMPLEMENT_EXCEPTION(ApplicationException, Exception, "Application exception")
POCO_IMPLEMENT_EXCEPTION(BadCastException, RuntimeException, "Bad cast exception")

View File

@ -660,9 +660,9 @@ void URI::decode(const std::string& str, std::string& decodedStr, bool plusAsSpa
if (inQuery && plusAsSpace && c == '+') c = ' ';
else if (c == '%')
{
if (it == end) throw SyntaxException("URI encoding: no hex digit following percent sign", str);
if (it == end) throw URISyntaxException("URI encoding: no hex digit following percent sign", str);
char hi = *it++;
if (it == end) throw SyntaxException("URI encoding: two hex digits must follow percent sign", str);
if (it == end) throw URISyntaxException("URI encoding: two hex digits must follow percent sign", str);
char lo = *it++;
if (hi >= '0' && hi <= '9')
c = hi - '0';
@ -670,7 +670,7 @@ void URI::decode(const std::string& str, std::string& decodedStr, bool plusAsSpa
c = hi - 'A' + 10;
else if (hi >= 'a' && hi <= 'f')
c = hi - 'a' + 10;
else throw SyntaxException("URI encoding: not a hex digit");
else throw URISyntaxException("URI encoding: not a hex digit");
c *= 16;
if (lo >= '0' && lo <= '9')
c += lo - '0';
@ -678,7 +678,7 @@ void URI::decode(const std::string& str, std::string& decodedStr, bool plusAsSpa
c += lo - 'A' + 10;
else if (lo >= 'a' && lo <= 'f')
c += lo - 'a' + 10;
else throw SyntaxException("URI encoding: not a hex digit");
else throw URISyntaxException("URI encoding: not a hex digit");
}
decodedStr += c;
}
@ -732,7 +732,7 @@ void URI::parse(const std::string& uri)
if (it != end && *it == ':')
{
++it;
if (it == end) throw SyntaxException("URI scheme must be followed by authority or path", uri);
if (it == end) throw URISyntaxException("URI scheme must be followed by authority or path", uri);
setScheme(scheme);
if (*it == '/')
{
@ -786,7 +786,7 @@ void URI::parseHostAndPort(std::string::const_iterator& it, const std::string::c
// IPv6 address
++it;
while (it != end && *it != ']') host += *it++;
if (it == end) throw SyntaxException("unterminated IPv6 address");
if (it == end) throw URISyntaxException("unterminated IPv6 address");
++it;
}
else
@ -804,7 +804,7 @@ void URI::parseHostAndPort(std::string::const_iterator& it, const std::string::c
if (NumberParser::tryParse(port, nport) && nport > 0 && nport < 65536)
_port = (unsigned short) nport;
else
throw SyntaxException("bad or invalid port number", port);
throw URISyntaxException("bad or invalid port number", port);
}
else _port = getWellKnownPort();
}

View File

@ -62,13 +62,34 @@ std::istream* URIStreamOpener::open(const std::string& pathOrURI) const
std::string scheme(uri.getScheme());
FactoryMap::const_iterator it = _map.find(scheme);
if (it != _map.end())
{
return openURI(scheme, uri);
}
else
{
Path path;
if (path.tryParse(pathOrURI, Path::PATH_GUESS))
return openFile(path);
else
throw UnknownURISchemeException(pathOrURI);
}
}
catch (Exception&)
catch (UnknownURISchemeException&)
{
throw;
}
catch (TooManyURIRedirectsException&)
{
throw;
}
catch (URISyntaxException&)
{
Path path;
if (path.tryParse(pathOrURI, Path::PATH_GUESS))
return openFile(path);
else
throw;
}
Path path(pathOrURI, Path::PATH_GUESS);
return openFile(path);
}
@ -87,6 +108,14 @@ std::istream* URIStreamOpener::open(const std::string& basePathOrURI, const std:
return openURI(scheme, uri);
}
}
catch (UnknownURISchemeException&)
{
throw;
}
catch (TooManyURIRedirectsException&)
{
throw;
}
catch (Exception&)
{
}
@ -176,7 +205,7 @@ std::istream* URIStreamOpener::openURI(const std::string& scheme, const URI& uri
++redirects;
}
}
throw IOException("Too many redirects while opening URI", uri.toString());
throw TooManyURIRedirectsException(uri.toString());
}