diff options
Diffstat (limited to 'src/http.cc')
| -rw-r--r-- | src/http.cc | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/http.cc b/src/http.cc index 248bd6b..6ae0634 100644 --- a/src/http.cc +++ b/src/http.cc @@ -1,8 +1,10 @@ #include "common.hh" +#include <algorithm> #include <iostream> #include "http.hh" +#include "strutils.hh" namespace stuff { @@ -35,6 +37,15 @@ const char* get_status_message(unsigned int status) { // static void Http::response(unsigned int status, const std::string& content) { + std::map<std::string, std::string> headers; + headers.insert(std::make_pair("Content-Type", "text/plain; charset=utf-8")); + response(status, headers, content); +} + +// static +void Http::response(unsigned int status, + const std::map<std::string, std::string>& headers, + const std::string& content) { if (status != 200) { std::cout << "Status: " << status; const char* msg = get_status_message(status); @@ -43,8 +54,17 @@ void Http::response(unsigned int status, const std::string& content) { } std::cout << EOL; } - std::cout << "Content-Type: text/plain; charset=utf-8" << EOL; - std::cout << "Content-Length: " << content.size() << EOL; + bool had_content_length = false; + for (auto const& header : headers) { + if (!had_content_length && + ascii_tolower(header.first) == "content-length") { + had_content_length = true; + } + std::cout << header.first << ": " << header.second << EOL; + } + if (!had_content_length) { + std::cout << "Content-Length: " << content.size() << EOL; + } std::cout << EOL; std::cout << content; } |
