-
-
Notifications
You must be signed in to change notification settings - Fork 347
/
http_response.h
282 lines (244 loc) · 8.44 KB
/
http_response.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once
#include <string>
#include <unordered_map>
#include <ios>
#include <fstream>
#include <sstream>
// S_ISREG is not defined for windows
// This defines it like suggested in https://stackoverflow.com/a/62371749
#if defined(_MSC_VER)
#define _CRT_INTERNAL_NONSTDC_NAMES 1
#endif
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
#endif
#include "crow/http_request.h"
#include "crow/ci_map.h"
#include "crow/socket_adaptors.h"
#include "crow/logging.h"
#include "crow/mime_types.h"
#include "crow/returnable.h"
namespace crow
{
template<typename Adaptor, typename Handler, typename... Middlewares>
class Connection;
class Router;
/// HTTP response
struct response
{
template<typename Adaptor, typename Handler, typename... Middlewares>
friend class crow::Connection;
friend class Router;
int code{200}; ///< The Status code for the response.
std::string body; ///< The actual payload containing the response data.
ci_map headers; ///< HTTP headers.
#ifdef CROW_ENABLE_COMPRESSION
bool compressed = true; ///< If compression is enabled and this is false, the individual response will not be compressed.
#endif
bool skip_body = false; ///< Whether this is a response to a HEAD request.
bool manual_length_header = false; ///< Whether Crow should automatically add a "Content-Length" header.
/// Set the value of an existing header in the response.
void set_header(std::string key, std::string value)
{
headers.erase(key);
headers.emplace(std::move(key), std::move(value));
}
/// Add a new header to the response.
void add_header(std::string key, std::string value)
{
headers.emplace(std::move(key), std::move(value));
}
const std::string& get_header_value(const std::string& key)
{
return crow::get_header_value(headers, key);
}
// clang-format off
response() {}
explicit response(int code) : code(code) {}
response(std::string body) : body(std::move(body)) {}
response(int code, std::string body) : code(code), body(std::move(body)) {}
// clang-format on
response(returnable&& value)
{
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(returnable& value)
{
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(int code, returnable& value):
code(code)
{
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(int code, returnable&& value):
code(code), body(std::move(value.dump()))
{
set_header("Content-Type", std::move(value.content_type));
}
response(response&& r)
{
*this = std::move(r);
}
response(std::string contentType, std::string body):
body(std::move(body))
{
set_header("Content-Type", mime_types.at(contentType));
}
response(int code, std::string contentType, std::string body):
code(code), body(std::move(body))
{
set_header("Content-Type", mime_types.at(contentType));
}
response& operator=(const response& r) = delete;
response& operator=(response&& r) noexcept
{
body = std::move(r.body);
code = r.code;
headers = std::move(r.headers);
completed_ = r.completed_;
file_info = std::move(r.file_info);
return *this;
}
/// Check if the response has completed (whether response.end() has been called)
bool is_completed() const noexcept
{
return completed_;
}
void clear()
{
body.clear();
code = 200;
headers.clear();
completed_ = false;
file_info = static_file_info{};
}
/// Return a "Temporary Redirect" response.
///
/// Location can either be a route or a full URL.
void redirect(const std::string& location)
{
code = 307;
set_header("Location", location);
}
/// Return a "Permanent Redirect" response.
///
/// Location can either be a route or a full URL.
void redirect_perm(const std::string& location)
{
code = 308;
set_header("Location", location);
}
/// Return a "Found (Moved Temporarily)" response.
///
/// Location can either be a route or a full URL.
void moved(const std::string& location)
{
code = 302;
set_header("Location", location);
}
/// Return a "Moved Permanently" response.
///
/// Location can either be a route or a full URL.
void moved_perm(const std::string& location)
{
code = 301;
set_header("Location", location);
}
void write(const std::string& body_part)
{
body += body_part;
}
/// Set the response completion flag and call the handler (to send the response).
void end()
{
if (!completed_)
{
completed_ = true;
if (skip_body)
{
set_header("Content-Length", std::to_string(body.size()));
body = "";
manual_length_header = true;
}
if (complete_request_handler_)
{
complete_request_handler_();
}
}
}
/// Same as end() except it adds a body part right before ending.
void end(const std::string& body_part)
{
body += body_part;
end();
}
/// Check if the connection is still alive (usually by checking the socket status).
bool is_alive()
{
return is_alive_helper_ && is_alive_helper_();
}
/// Check whether the response has a static file defined.
bool is_static_type()
{
return file_info.path.size();
}
/// This constains metadata (coming from the `stat` command) related to any static files associated with this response.
///
/// Either a static file or a string body can be returned as 1 response.
struct static_file_info
{
std::string path = "";
struct stat statbuf;
int statResult;
};
/// Return a static file as the response body
void set_static_file_info(std::string path)
{
utility::sanitize_filename(path);
set_static_file_info_unsafe(path);
}
/// Return a static file as the response body without sanitizing the path (use set_static_file_info instead)
void set_static_file_info_unsafe(std::string path)
{
file_info.path = path;
file_info.statResult = stat(file_info.path.c_str(), &file_info.statbuf);
#ifdef CROW_ENABLE_COMPRESSION
compressed = false;
#endif
if (file_info.statResult == 0 && S_ISREG(file_info.statbuf.st_mode))
{
std::size_t last_dot = path.find_last_of(".");
std::string extension = path.substr(last_dot + 1);
code = 200;
this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size));
if (!extension.empty())
{
const auto mimeType = mime_types.find(extension);
if (mimeType != mime_types.end())
{
this->add_header("Content-Type", mimeType->second);
}
else
{
this->add_header("Content-Type", "text/plain");
}
}
}
else
{
code = 404;
file_info.path.clear();
}
}
private:
bool completed_{};
std::function<void()> complete_request_handler_;
std::function<bool()> is_alive_helper_;
static_file_info file_info;
};
} // namespace crow