diff options
Diffstat (limited to 'src/Uri.cpp')
-rw-r--r-- | src/Uri.cpp | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/src/Uri.cpp b/src/Uri.cpp index 1768849..8bc2f4a 100644 --- a/src/Uri.cpp +++ b/src/Uri.cpp @@ -142,6 +142,46 @@ namespace Uri { } return true; } + + /** + * This method parses the elements that make up the authority + * composite part of the URI, by parsing it from the given string. + * + * @param[in] authorityString + * This is the string containing the whole authority part + * of the URI. + * + * @return + * An indication if the path was parsed correctly or not + * is returned. + */ + bool ParseAuthority(const std::string& authorityString) { + // Next, check if there is a UserInfo, and if so, extract it. + const auto userInfoDelimiter = authorityString.find('@'); + std::string hostPortString; + if (userInfoDelimiter == std::string::npos) { + userInfo.clear(); + hostPortString = authorityString; + } else { + userInfo = authorityString.substr(0, userInfoDelimiter); + hostPortString = authorityString.substr(userInfoDelimiter + 1); + } + + // Next, parsing host and port from authority and path. + const auto portDelimiter = hostPortString.find(':'); + if (portDelimiter == std::string::npos) { + host = hostPortString; + hasPort = false; + } else { + host = hostPortString.substr(0, portDelimiter); + const auto portString = hostPortString.substr(portDelimiter + 1); + if (!ParseUint16(portString, port)) { + return false; + } + hasPort = true; + } + return true; + } }; Uri::~Uri() = default; @@ -180,29 +220,9 @@ namespace Uri { pathString = authorityAndPathString.substr(authorityEnd); auto authorityString = authorityAndPathString.substr(0, authorityEnd); - // Next, check if there is a UserInfo, and if so, extract it. - const auto userInfoDelimiter = authorityString.find('@'); - std::string hostPortString; - if (userInfoDelimiter == std::string::npos) { - impl_->userInfo.clear(); - hostPortString = authorityString; - } else { - impl_->userInfo = authorityString.substr(0, userInfoDelimiter); - hostPortString = authorityString.substr(userInfoDelimiter + 1); - } - - // Next, parsing host and port from authority and path. - const auto portDelimiter = hostPortString.find(':'); - if (portDelimiter == std::string::npos) { - impl_->host = hostPortString; - impl_->hasPort = false; - } else { - impl_->host = hostPortString.substr(0, portDelimiter); - const auto portString = hostPortString.substr(portDelimiter + 1); - if (!ParseUint16(portString, impl_->port)) { - return false; - } - impl_->hasPort = true; + // Parse the elements inside the authority string. + if (!impl_->ParseAuthority(authorityString)) { + return false; } } else { impl_->userInfo.clear(); |