When adding header data to the response. We accept what the user is telling us and do no conversion of the data.
But: The Response object (and its stream) is going to handle the encoding/ transport. So we filter out content-length:
and transport-encoding
headers from outgoing requests as this will be added manually by the Response
object.
HeaderResponse.h
#ifndef THORSANVIL_NISSE_NISSEHTTP_HEADER_RESPONSE_H
#define THORSANVIL_NISSE_NISSEHTTP_HEADER_RESPONSE_H
#include "NisseHTTPConfig.h"
#include <string_view>
#include <string>
#include <map>
#include <iostream>
namespace ThorsAnvil::Nisse::NisseHTTP
{
inline bool ichar_equals(char a, char b)
{
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
}
class HeaderResponse
{
std::map<std::string, std::string> headers;
public:
bool empty() const;
void add(std::string_view header, std::string_view value);
friend std::ostream& operator<<(std::ostream& stream, HeaderResponse const& headersBlock)
{
for (auto header: headersBlock.headers)
{
if (std::ranges::equal(std::string_view(header.first), std::string_view("content-length"), ichar_equals)) {
continue;
}
if (std::ranges::equal(std::string_view(header.first), std::string_view("transfer-encoding"), ichar_equals)) {
continue;
}
stream << header.first << ": " << header.second << "\r\n";
}
return stream;
}
};
}
#endif
HeaderResponse.cpp
#include "HeaderResponse.h"
using namespace ThorsAnvil::Nisse::NisseHTTP;
bool HeaderResponse::empty() const
{
return headers.empty();
}
void HeaderResponse::add(std::string_view header, std::string_view value)
{
headers.emplace(header, value);
}