stuff-from-scratch/src/core/http/HttpHeader.cpp

98 lines
1.7 KiB
C++
Raw Normal View History

2022-05-18 07:42:44 +00:00
#include "HttpHeader.h"
#include "StringUtils.h"
2023-01-10 09:28:15 +00:00
HttpHeader::HttpHeader()
: mContentType("text / html"),
mHttpVersion("1.1")
{
}
std::string HttpHeader::getContentType() const
{
return mContentType;
}
std::string HttpHeader::getHttpVersion() const
{
return mHttpVersion;
}
2022-05-18 07:42:44 +00:00
void HttpHeader::parse(const std::vector<std::string >& message)
{
2022-12-04 18:13:32 +00:00
std::string tag;
std::string value;
bool foundDelimiter{false};
for (const auto& line : message)
{
for(std::size_t idx = 0; idx< line.size(); idx++)
{
const auto c = line[idx];
if (c == StringUtils::COLON)
{
foundDelimiter = true;
}
else if(foundDelimiter)
{
value.push_back(c);
}
else
{
tag.push_back(c);
}
}
}
2022-05-18 07:42:44 +00:00
2022-12-04 18:13:32 +00:00
if (tag.empty() || value.empty())
{
return;
}
2022-05-18 07:42:44 +00:00
2022-12-04 18:13:32 +00:00
if (tag == "Host")
{
mHost = value;
}
else if (tag == "User-Agent")
{
mUserAgent = value;
}
else if (tag == "Accept")
{
mAccept = value;
}
else if (tag == "Accept-Language")
{
mAcceptLanguage = value;
}
else if (tag == "Accept-Encoding")
{
mAcceptEncoding = value;
}
else if (tag == "Connection")
{
mConnection = value;
}
else if (tag == "Referer")
{
mReferer = value;
}
else if (tag == "Sec-Fetch-Dest")
{
mSecFetchDest = value;
}
else if (tag == "Sec-Fetch-Mode")
{
mSecFetchMode = value;
}
else if (tag == "Sec-Fetch-Site")
{
mSecFetchSite = value;
}
else
{
mOtherFields[tag] = value;
}
2022-05-18 07:42:44 +00:00
}