summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/http.cc24
-rw-r--r--src/http.hh4
2 files changed, 26 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;
}
diff --git a/src/http.hh b/src/http.hh
index 58bfb92..c1e9e3b 100644
--- a/src/http.hh
+++ b/src/http.hh
@@ -1,6 +1,7 @@
#ifndef HTTP_HH
#define HTTP_HH
+#include <map>
#include <string>
namespace stuff {
@@ -8,6 +9,9 @@ namespace stuff {
class Http {
public:
static void response(unsigned int status, const std::string& content);
+ static void response(unsigned int status,
+ const std::map<std::string,std::string>& headers,
+ const std::string& content);
private:
Http() = delete;