Skip to content

Commit

Permalink
Add a GetHostNameWithPort() function to HTTPHeaders.
Browse files Browse the repository at this point in the history
Make HTTPHeaders::WriteTo a const function, as it does not need to change
the headers in any way.
  • Loading branch information
qris committed Nov 28, 2015
1 parent 2e87572 commit 2931517
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
28 changes: 18 additions & 10 deletions lib/httpserver/HTTPHeaders.cpp
Expand Up @@ -180,7 +180,7 @@ void HTTPHeaders::AddHeader(const std::string& rName, const std::string& value)
// Created: 2015-08-22
//
// --------------------------------------------------------------------------
void HTTPHeaders::WriteTo(IOStream& rOutput, int Timeout)
void HTTPHeaders::WriteTo(IOStream& rOutput, int Timeout) const
{
std::ostringstream oss;

Expand All @@ -196,14 +196,7 @@ void HTTPHeaders::WriteTo(IOStream& rOutput, int Timeout)

if (mHostName != "")
{
if (mHostPort != 80)
{
oss << "Host: " << mHostName << ":" << mHostPort << "\r\n";
}
else
{
oss << "Host: " << mHostName << "\r\n";
}
oss << "Host: " << GetHostNameWithPort() << "\r\n";
}

if (mKeepAlive)
Expand All @@ -215,7 +208,7 @@ void HTTPHeaders::WriteTo(IOStream& rOutput, int Timeout)
oss << "Connection: close\r\n";
}

for (std::vector<Header>::iterator i = mExtraHeaders.begin();
for (std::vector<Header>::const_iterator i = mExtraHeaders.begin();
i != mExtraHeaders.end(); i++)
{
oss << i->first << ": " << i->second << "\r\n";
Expand All @@ -224,3 +217,18 @@ void HTTPHeaders::WriteTo(IOStream& rOutput, int Timeout)
rOutput.Write(oss.str(), Timeout);
}

std::string HTTPHeaders::GetHostNameWithPort() const
{

if (mHostPort != 80)
{
std::ostringstream oss;
oss << mHostName << ":" << mHostPort;
return oss.str();
}
else
{
return mHostName;
}
}

3 changes: 2 additions & 1 deletion lib/httpserver/HTTPHeaders.h
Expand Up @@ -40,7 +40,7 @@ class HTTPHeaders
void ReadFromStream(IOStreamGetLine &rGetLine, int Timeout);
void ParseHeaderLine(const std::string& line);
void AddHeader(const std::string& name, const std::string& value);
void WriteTo(IOStream& rOutput, int Timeout);
void WriteTo(IOStream& rOutput, int Timeout) const;
typedef std::pair<std::string, std::string> Header;
bool GetHeader(const std::string& name, std::string* pValueOut) const
{
Expand Down Expand Up @@ -89,6 +89,7 @@ class HTTPHeaders
int64_t GetContentLength() const { return mContentLength; }
const std::string &GetHostName() const {return mHostName;}
const int GetHostPort() const {return mHostPort;}
std::string GetHostNameWithPort() const;
void SetHostName(const std::string& rHostName)
{
AddHeader("host", rHostName);
Expand Down
2 changes: 1 addition & 1 deletion test/httpserver/testhttpserver.cpp
Expand Up @@ -221,7 +221,7 @@ int test(int argc, const char *argv[])
// be unknown in the received stream too (certainly before it has all
// been read!)
TEST_EQUAL(-1, request2.GetContentLength());
HTTPHeaders& headers(request2.GetHeaders());
const HTTPHeaders& headers(request2.GetHeaders());
TEST_EQUAL("Wed, 01 Mar 2006 12:00:00 GMT",
headers.GetHeaderValue("Date"));
TEST_EQUAL("AWS 0PN5J17HBGZHT7JJ3X82:XtMYZf0hdOo4TdPYQknZk0Lz7rw=",
Expand Down

0 comments on commit 2931517

Please sign in to comment.