diff options
| author | Zousar Shaker <[email protected]> | 2021-12-01 09:45:08 -0700 |
|---|---|---|
| committer | Zousar Shaker <[email protected]> | 2021-12-01 09:45:08 -0700 |
| commit | 128337912716469cbc452bd97c417504d978f18a (patch) | |
| tree | 9fd2d8f7629e3e293ea51524a13130740aa21a49 /zenhttp/httpasio.cpp | |
| parent | Handle double slashes in URL path by normalizing them away like http.sys. (diff) | |
| download | zen-128337912716469cbc452bd97c417504d978f18a.tar.xz zen-128337912716469cbc452bd97c417504d978f18a.zip | |
Address review feedback/comments.
Diffstat (limited to 'zenhttp/httpasio.cpp')
| -rw-r--r-- | zenhttp/httpasio.cpp | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/zenhttp/httpasio.cpp b/zenhttp/httpasio.cpp index ecca05e08..9a23a25ef 100644 --- a/zenhttp/httpasio.cpp +++ b/zenhttp/httpasio.cpp @@ -751,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() { @@ -817,32 +848,7 @@ HttpRequest::OnHeadersComplete() m_QueryLength = Url.size() - QuerySplit - 1; } - bool bLastCharWasSeparator = false; - for (std::string_view::size_type UrlIndex = 0; UrlIndex < m_UrlLength; ++UrlIndex) - { - char UrlChar = m_Url[UrlIndex]; - bool bIsSeparator = (UrlChar == '\\') || (UrlChar == '/'); - - if ((UrlChar == '\\') || (bIsSeparator && bLastCharWasSeparator)) - { - if (m_NormalizedUrl.empty()) - { - m_NormalizedUrl.reserve(m_UrlLength); - m_NormalizedUrl.append(m_Url, UrlIndex); - } - - if (!bLastCharWasSeparator) - { - m_NormalizedUrl.push_back('/'); - } - } - else if (!m_NormalizedUrl.empty()) - { - m_NormalizedUrl.push_back(UrlChar); - } - - bLastCharWasSeparator = bIsSeparator; - } + NormalizeUrlPath(m_Url, m_UrlLength, m_NormalizedUrl); return 0; |