diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-25 02:14:37 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-25 02:14:37 +0200 |
| commit | 98727c1173a5865143b70bd68218b95a030824b1 (patch) | |
| tree | 5714eaf8184a8926d4a063682b4a4119c706778a /src/http.cc | |
| parent | 85adf26a787b33d69d07df0dc786516fed800e21 (diff) | |
Add supports for specifying headers in Http::response
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; } |
