diff options
Diffstat (limited to 'src/Uri.cpp')
-rw-r--r-- | src/Uri.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/Uri.cpp b/src/Uri.cpp index c028443..78f3b4d 100644 --- a/src/Uri.cpp +++ b/src/Uri.cpp @@ -6,6 +6,7 @@ * © 2018 by Richard Walters */ +#include <inttypes.h> #include <string> #include <Uri/Uri.hpp> #include <vector> @@ -26,6 +27,17 @@ namespace Uri { std::string host; /** + * This flag indicates whether or not the + * URI includes a port number. + */ + bool hasPort = false; + + /** + * This is the port number element of the URI. + */ + uint16_t port = 0; + + /** * This is the "path" element of the URI, * as a sequence of segments. */ @@ -46,9 +58,33 @@ namespace Uri { auto rest = uriString.substr(schemeEnd + 1); // Next parse the host. + impl_->hasPort = false; if (rest.substr(0, 2) == "//") { const auto authorityEnd = rest.find('/', 2); - impl_->host = rest.substr(2, authorityEnd - 2); + const auto portDelimiter = rest.find(':'); + if (portDelimiter == std::string::npos) { + impl_->host = rest.substr(2, authorityEnd - 2); + } else { + impl_->host = rest.substr(2, portDelimiter - 2); + uint32_t newPort = 0; + for (auto c: rest.substr(portDelimiter + 1, authorityEnd - portDelimiter - 1)) { + if ( + (c < '0') + || (c > '9') + ) { + return false; + } + newPort *= 10; + newPort += (uint16_t)(c - '0'); + if ( + (newPort & ~((1 << 16) - 1)) != 0 + ) { + return false; + } + } + impl_->port = (uint16_t)newPort; + impl_->hasPort = true; + } rest = rest.substr(authorityEnd); } else { impl_->host.clear(); @@ -90,4 +126,12 @@ namespace Uri { return impl_->path; } + bool Uri::HasPort() const { + return impl_->hasPort; + } + + uint16_t Uri::GetPort() const { + return impl_->port; + } + } |