Files
spark/src/lib_http/http/http_headers.cpp
T

45 lines
951 B
C++

#include "http_headers.h"
#include "system/string_utils.h"
#include <string>
namespace bts::http {
using bts::system::string_utils::split;
HttpHeaders::HttpHeaders(const std::string &str) {
auto item_strs = split(str, "\n");
for (const auto &item_str : item_strs) {
auto kv = split(item_str, ":");
if (kv.empty()) {
try_emplace("", "");
continue;
}
if (kv.size() == 1) {
try_emplace(kv[0], "");
continue;
}
try_emplace(kv[0], kv[1]);
}
}
std::string HttpHeaders::to_string() const {
auto it = begin();
if (it == end()) {
return "";
}
std::string rv{it->first + ": " + it->second};
while (++it != end()) {
rv.append("\n" + it->first + ": " + it->second);
}
return rv;
}
std::ostream &operator<<(
std::ostream &out, const HttpHeaders &headers) noexcept {
out << headers.to_string();
return out;
}
} // namespace bts::http