From 98727c1173a5865143b70bd68218b95a030824b1 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 25 Jun 2015 02:14:37 +0200 Subject: Add supports for specifying headers in Http::response --- src/http.cc | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/http.cc') 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 #include #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 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& 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; } -- cgit v1.2.3-70-g09d2