diff options
author | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 21:20:37 -0700 |
---|---|---|
committer | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 21:20:37 -0700 |
commit | a43820d0b4014878e4bbfede6acde25f5830faa7 (patch) | |
tree | 58892b88fe5927b04b060af1501741c4766b7bee /src | |
parent | 58e2beb7717cf724ae37e03f2e5bf3afbfc23a35 (diff) |
Add support for port and hasPort elements
Diffstat (limited to 'src')
-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; + } + } |