Electroneum
http_response.h
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <unordered_map>
4 #include "json.h"
5 #include "http_request.h"
6 #include "ci_map.h"
7 
8 namespace crow
9 {
10  template <typename Adaptor, typename Handler, typename ... Middlewares>
11  class Connection;
12  struct response
13  {
14  template <typename Adaptor, typename Handler, typename ... Middlewares>
15  friend class crow::Connection;
16 
17  int code{200};
18  std::string body;
20 
21  // `headers' stores HTTP headers.
23 
24  void set_header(std::string key, std::string value)
25  {
26  headers.erase(key);
27  headers.emplace(std::move(key), std::move(value));
28  }
29  void add_header(std::string key, std::string value)
30  {
31  headers.emplace(std::move(key), std::move(value));
32  }
33 
34  const std::string& get_header_value(const std::string& key)
35  {
36  return crow::get_header_value(headers, key);
37  }
38 
39 
40  response() {}
41  explicit response(int code) : code(code) {}
42  response(std::string body) : body(std::move(body)) {}
44  {
45  json_mode();
46  }
47  response(int code, std::string body) : code(code), body(std::move(body)) {}
49  {
50  json_mode();
51  }
53  {
54  json_mode();
55  }
56 
58  {
59  *this = std::move(r);
60  }
61 
62  response& operator = (const response& r) = delete;
63 
64  response& operator = (response&& r) noexcept
65  {
66  body = std::move(r.body);
67  json_value = std::move(r.json_value);
68  code = r.code;
69  headers = std::move(r.headers);
70  completed_ = r.completed_;
71  return *this;
72  }
73 
74  bool is_completed() const noexcept
75  {
76  return completed_;
77  }
78 
79  void clear()
80  {
81  body.clear();
82  json_value.clear();
83  code = 200;
84  headers.clear();
85  completed_ = false;
86  }
87 
88  void write(const std::string& body_part)
89  {
90  body += body_part;
91  }
92 
93  void end()
94  {
95  if (!completed_)
96  {
97  completed_ = true;
98 
100  {
102  }
103  }
104  }
105 
106  void end(const std::string& body_part)
107  {
108  body += body_part;
109  end();
110  }
111 
112  bool is_alive()
113  {
115  }
116 
117  private:
118  bool completed_{};
119  std::function<void()> complete_request_handler_;
120  std::function<bool()> is_alive_helper_;
121 
122  //In case of a JSON object, set the Content-Type header
123  void json_mode()
124  {
125  set_header("Content-Type", "application/json");
126  }
127  };
128 }
Definition: http_response.h:12
response & operator=(const response &r)=delete
response(response &&r)
Definition: http_response.h:57
void end(const std::string &body_part)
Definition: http_response.h:106
std::function< bool()> is_alive_helper_
Definition: http_response.h:120
response(int code)
Definition: http_response.h:41
bool is_alive()
Definition: http_response.h:112
ci_map headers
Definition: http_response.h:22
std::string dump(const wvalue &v)
Definition: json.h:1426
Definition: block_queue.cpp:41
bool completed_
Definition: http_response.h:118
response(const json::wvalue &json_value)
Definition: http_response.h:48
bool is_completed() const noexcept
Definition: http_response.h:74
int code
Definition: http_response.h:17
const std::string & get_header_value(const std::string &key)
Definition: http_response.h:34
Definition: json.h:1086
Definition: http_connection.h:182
const std::string & get_header_value(const T &headers, const std::string &key)
Definition: http_request.h:11
response(int code, std::string body)
Definition: http_response.h:47
void clear()
Definition: json.h:1148
std::function< void()> complete_request_handler_
Definition: http_response.h:119
response()
Definition: http_response.h:40
response(json::wvalue &&json_value)
Definition: http_response.h:43
void write(const std::string &body_part)
Definition: http_response.h:88
std::unordered_multimap< std::string, std::string, ci_hash, ci_key_eq > ci_map
Definition: ci_map.h:33
json::wvalue json_value
Definition: http_response.h:19
Definition: ci_map.h:7
void clear()
Definition: http_response.h:79
std::string body
Definition: http_response.h:18
basic_json<> json
default JSON class
Definition: json.hpp:14369
response(int code, const json::wvalue &json_value)
Definition: http_response.h:52
void end()
Definition: http_response.h:93
void add_header(std::string key, std::string value)
Definition: http_response.h:29
void json_mode()
Definition: http_response.h:123
void set_header(std::string key, std::string value)
Definition: http_response.h:24
response(std::string body)
Definition: http_response.h:42