diff options
author | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 23:34:11 -0700 |
---|---|---|
committer | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 23:34:11 -0700 |
commit | 6f389efe5808c83f86a4c71861c50e6e941fc5bb (patch) | |
tree | a86128ae9660ee7b81cebfb0adf244a8473a3bb7 /src | |
parent | f126a79dea5f139449754ecdf7bc94132eeea569 (diff) |
Refactoring
Extract method ParseAuthority
Diffstat (limited to 'src')
-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(); |