aboutsummaryrefslogtreecommitdiffstats
path: root/include/http_response.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2014-08-07 01:18:33 +0900
committeripknHama <ipknhama@gmail.com>2014-08-07 01:18:33 +0900
commit031615ac866cc3c8f1900dd4b4aae2106ad31230 (patch)
treeb8b7206ffbd2043368580ec269c97436929fe452 /include/http_response.h
parenta0c93f5b84cc11b30bc6320ac26127832ef8bf7a (diff)
downloadcrow-031615ac866cc3c8f1900dd4b4aae2106ad31230.tar.gz
crow-031615ac866cc3c8f1900dd4b4aae2106ad31230.zip
source resturcturing + CMake
Diffstat (limited to 'include/http_response.h')
-rw-r--r--include/http_response.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/include/http_response.h b/include/http_response.h
new file mode 100644
index 0000000..ae92543
--- /dev/null
+++ b/include/http_response.h
@@ -0,0 +1,86 @@
+#pragma once
+#include <string>
+#include <unordered_map>
+#include "json.h"
+
+namespace crow
+{
+ template <typename T>
+ class Connection;
+ struct response
+ {
+ template <typename T>
+ friend class crow::Connection;
+
+ std::string body;
+ json::wvalue json_value;
+ int code{200};
+ std::unordered_map<std::string, std::string> headers;
+
+ response() {}
+ explicit response(int code) : code(code) {}
+ response(std::string body) : body(std::move(body)) {}
+ response(json::wvalue&& json_value) : json_value(std::move(json_value)) {}
+ response(const json::wvalue& json_value) : body(json::dump(json_value)) {}
+ response(int code, std::string body) : body(std::move(body)), code(code) {}
+
+ response(response&& r)
+ {
+ *this = std::move(r);
+ }
+
+ response& operator = (const response& r) = delete;
+
+ response& operator = (response&& r)
+ {
+ body = std::move(r.body);
+ json_value = std::move(r.json_value);
+ code = r.code;
+ headers = std::move(r.headers);
+ completed_ = r.completed_;
+ return *this;
+ }
+
+ void clear()
+ {
+ body.clear();
+ json_value.clear();
+ code = 200;
+ headers.clear();
+ completed_ = false;
+ }
+
+ void write(const std::string& body_part)
+ {
+ body += body_part;
+ }
+
+ void end()
+ {
+ if (!completed_)
+ {
+ completed_ = true;
+ if (complete_request_handler_)
+ {
+ complete_request_handler_();
+ }
+ }
+ }
+
+ void end(const std::string& body_part)
+ {
+ body += body_part;
+ end();
+ }
+
+ bool is_alive()
+ {
+ return is_alive_helper_ && is_alive_helper_();
+ }
+
+ private:
+ bool completed_{};
+ std::function<void()> complete_request_handler_;
+ std::function<bool()> is_alive_helper_;
+ };
+}