HTTP should contain port in 'Host' header (#427)

This commit is contained in:
Vol-Alex 2023-02-12 00:27:40 +01:00 committed by GitHub
parent 679ce519dd
commit 6cc21f3658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 16 deletions

View File

@ -139,8 +139,9 @@ namespace ix
std::string protocol, host, path, query; std::string protocol, host, path, query;
int port; int port;
bool isProtocolDefaultPort;
if (!UrlParser::parse(url, protocol, host, path, query, port)) if (!UrlParser::parse(url, protocol, host, path, query, port, isProtocolDefaultPort))
{ {
std::stringstream ss; std::stringstream ss;
ss << "Cannot parse url: " << url; ss << "Cannot parse url: " << url;
@ -173,7 +174,12 @@ namespace ix
// Build request string // Build request string
std::stringstream ss; std::stringstream ss;
ss << verb << " " << path << " HTTP/1.1\r\n"; ss << verb << " " << path << " HTTP/1.1\r\n";
ss << "Host: " << host << "\r\n"; ss << "Host: " << host;
if (!isProtocolDefaultPort)
{
ss << ":" << port;
}
ss << "\r\n";
#ifdef IXWEBSOCKET_USE_ZLIB #ifdef IXWEBSOCKET_USE_ZLIB
if (args->compress && !args->onChunkCallback) if (args->compress && !args->onChunkCallback)

View File

@ -333,6 +333,19 @@ namespace
return Result; return Result;
} }
int getProtocolPort(const std::string& protocol)
{
if (protocol == "ws" || protocol == "http")
{
return 80;
}
else if (protocol == "wss" || protocol == "https")
{
return 443;
}
return -1;
}
} // namespace } // namespace
namespace ix namespace ix
@ -343,6 +356,18 @@ namespace ix
std::string& path, std::string& path,
std::string& query, std::string& query,
int& port) int& port)
{
bool isProtocolDefaultPort;
return parse(url, protocol, host, path, query, port, isProtocolDefaultPort);
}
bool UrlParser::parse(const std::string& url,
std::string& protocol,
std::string& host,
std::string& path,
std::string& query,
int& port,
bool& isProtocolDefaultPort)
{ {
clParseURL res = clParseURL::ParseURL(url); clParseURL res = clParseURL::ParseURL(url);
@ -356,23 +381,12 @@ namespace ix
path = res.m_Path; path = res.m_Path;
query = res.m_Query; query = res.m_Query;
const auto protocolPort = getProtocolPort(protocol);
if (!res.GetPort(&port)) if (!res.GetPort(&port))
{ {
if (protocol == "ws" || protocol == "http") port = protocolPort;
{
port = 80;
}
else if (protocol == "wss" || protocol == "https")
{
port = 443;
}
else
{
// Invalid protocol. Should be caught by regex check
// but this missing branch trigger cpplint linter.
return false;
}
} }
isProtocolDefaultPort = port == protocolPort;
if (path.empty()) if (path.empty())
{ {

View File

@ -19,5 +19,13 @@ namespace ix
std::string& path, std::string& path,
std::string& query, std::string& query,
int& port); int& port);
static bool parse(const std::string& url,
std::string& protocol,
std::string& host,
std::string& path,
std::string& query,
int& port,
bool& isProtocolDefaultPort);
}; };
} // namespace ix } // namespace ix