-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.cpp
More file actions
53 lines (51 loc) · 1.89 KB
/
request.cpp
File metadata and controls
53 lines (51 loc) · 1.89 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
53
#include "./request.h"
// templates instantiation
// objects
template class qb::http::TRequest<std::string>;
template class qb::http::TRequest<std::string_view>;
namespace qb::allocator {
/**
* @brief Serialize an HTTP Request into a byte stream
* @param r HTTP Request to serialize
* @return Reference to this pipe
*
* Formats an HTTP request into a properly formatted request string
* including request line, headers, and body.
*
* The format follows the HTTP/1.1 specification with:
* - Request line: METHOD PATH HTTP/VERSION
* - Headers: HEADER: VALUE
* - Empty line separator
* - Request body (if present)
*/
template<>
pipe<char> &
pipe<char>::put<qb::http::Request>(const qb::http::Request &r) {
// HTTP Status Line
*this << ::http_method_name(r.method()) << qb::http::sep
<< r.uri().path();
if (r.uri().encoded_queries().size())
*this << "?" << r.uri().encoded_queries();
if (r.uri().fragment().size())
*this << "#" << r.uri().fragment();
*this << qb::http::sep << "HTTP/" << r.major_version << "." << r.minor_version
<< 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