diff options
| author | zousar <[email protected]> | 2021-12-01 09:45:52 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-01 09:45:52 -0700 |
| commit | de2d39172c890448e6f9f3c9d0294a4b079ad025 (patch) | |
| tree | 763043826a15acb9140b5e6ca724bc4f5827e5d3 /zenhttp/httpasio.cpp | |
| parent | Merge remote-tracking branch 'origin/ridgers-pr' into main (diff) | |
| parent | Address review feedback/comments. (diff) | |
| download | zen-de2d39172c890448e6f9f3c9d0294a4b079ad025.tar.xz zen-de2d39172c890448e6f9f3c9d0294a4b079ad025.zip | |
Merge pull request #31 from EpicGames/non-elevated-asio
Handle double slashes in URL path by normalizing them away like http.…
Diffstat (limited to 'zenhttp/httpasio.cpp')
| -rw-r--r-- | zenhttp/httpasio.cpp | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/zenhttp/httpasio.cpp b/zenhttp/httpasio.cpp index b9db108db..9a23a25ef 100644 --- a/zenhttp/httpasio.cpp +++ b/zenhttp/httpasio.cpp @@ -122,7 +122,7 @@ struct HttpRequest HttpVerb RequestVerb() const { return m_RequestVerb; } bool IsKeepAlive() const { return m_KeepAlive; } - std::string_view Url() const { return std::string_view(m_Url, m_UrlLength); } + std::string_view Url() const { return m_NormalizedUrl.empty() ? std::string_view(m_Url, m_UrlLength) : m_NormalizedUrl; } std::string_view QueryString() const { return std::string_view(m_QueryString, m_QueryLength); } IoBuffer Body() { return m_BodyBuffer; } @@ -179,6 +179,7 @@ private: uint64_t m_BodyPosition = 0; http_parser m_Parser; char m_HeaderBuffer[1024]; + std::string m_NormalizedUrl; void AppendInputBytes(const char* Data, size_t Bytes); void AppendCurrentHeader(); @@ -750,6 +751,37 @@ HttpRequest::TerminateConnection() m_Connection.TerminateConnection(); } +static void +NormalizeUrlPath(const char* Url, size_t UrlLength, std::string& NormalizedUrl) +{ + bool LastCharWasSeparator = false; + for (std::string_view::size_type UrlIndex = 0; UrlIndex < UrlLength; ++UrlIndex) + { + const char UrlChar = Url[UrlIndex]; + const bool IsSeparator = (UrlChar == '/'); + + if (IsSeparator && LastCharWasSeparator) + { + if (NormalizedUrl.empty()) + { + NormalizedUrl.reserve(UrlLength); + NormalizedUrl.append(Url, UrlIndex); + } + + if (!LastCharWasSeparator) + { + NormalizedUrl.push_back('/'); + } + } + else if (!NormalizedUrl.empty()) + { + NormalizedUrl.push_back(UrlChar); + } + + LastCharWasSeparator = IsSeparator; + } +} + int HttpRequest::OnHeadersComplete() { @@ -816,6 +848,9 @@ HttpRequest::OnHeadersComplete() m_QueryLength = Url.size() - QuerySplit - 1; } + NormalizeUrlPath(m_Url, m_UrlLength, m_NormalizedUrl); + + return 0; } @@ -856,6 +891,7 @@ HttpRequest::ResetState() m_BodyBuffer = {}; m_BodyPosition = 0; m_Headers.clear(); + m_NormalizedUrl.clear(); } int |