-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.cpp
More file actions
52 lines (50 loc) · 1.85 KB
/
response.cpp
File metadata and controls
52 lines (50 loc) · 1.85 KB
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
#include "./response.h"
// templates instantiation
// objects
template class qb::http::TResponse<std::string>;
template class qb::http::TResponse<std::string_view>;
namespace qb::allocator {
/**
* @brief Serialize an HTTP Response into a byte stream
* @param r HTTP Response to serialize
* @return Reference to this pipe
*
* Formats an HTTP response into a properly formatted response string
* including status line, headers, and body.
*
* The format follows the HTTP/1.1 specification with:
* - Status line: HTTP/VERSION STATUS_CODE STATUS_TEXT
* - Headers: HEADER: VALUE
* - Empty line separator
* - Response body (if present)
*
* This method also handles automatic compression of the body
* if Content-Encoding header is present.
*/
template<>
pipe<char> &
pipe<char>::put<qb::http::Response>(const qb::http::Response &r) {
// HTTP Status Line
*this << "HTTP/" << r.major_version << "." << r.minor_version << qb::http::sep
<< r.status().code() << qb::http::sep
<< std::to_string(r.status())
<< qb::http::endl;
// HTTP Headers
for (const auto &it: r.headers()) {
for (const auto &value: it.second)
*this << it.first << ": " << value << qb::http::endl;
}
// Body
const auto length = r.body().size();
const auto is_chunked = r.header("Transfer-Encoding").find("chunked") != std::string::npos;
if (length && !is_chunked) {
if (!r.has_header("Content-Length")) {
*this << "content-length: " << length << qb::http::endl;
}
*this << qb::http::endl
<< r.body().raw();
} else
*this << qb::http::endl;
return *this;
}
} // namespace qb::allocator