10#define FMT_HEADER_ONLY
11#include <fmt/format.h>
12#include <llhttp/llhttp.h>
21 std::string header_string;
22 for (
const auto& [k, v] : headers)
24 header_string += fmt::format(
"{}: {}\r\n", k, v);
34 const uint8_t*
body =
nullptr;
48 ccf::nonstd::to_lower(k);
75 const std::vector<uint8_t>* b,
bool overwrite_content_length =
true)
79 set_body(b->data(), b->size(), overwrite_content_length);
83 set_body(
nullptr, 0, overwrite_content_length);
88 const uint8_t* b,
size_t s,
bool overwrite_content_length =
true)
94 overwrite_content_length ||
97 headers[ccf::http::headers::CONTENT_LENGTH] =
102 void set_body(
const std::string& s,
bool overwrite_content_length =
true)
104 body = (uint8_t*)s.data();
108 overwrite_content_length ||
109 headers.find(ccf::http::headers::CONTENT_LENGTH) ==
headers.end())
111 headers[ccf::http::headers::CONTENT_LENGTH] =
120 llhttp_method method;
121 std::string path =
"/";
122 std::map<std::string, std::string> query_params = {};
125 Request(
const std::string_view& p =
"/", llhttp_method m = HTTP_POST) :
144 if (p.size() > 0 && p[0] ==
'/')
150 path = fmt::format(
"/{}", p);
166 std::string formatted_query;
168 for (
const auto& it : query_params)
171 fmt::format(
"{}{}={}", (first ?
'?' :
'&'), it.first, it.second);
174 return formatted_query;
181 const auto body_view = (header_only ||
body ==
nullptr) ?
185 const auto request_string = fmt::format(
190 llhttp_method_name(method),
195 return std::vector<uint8_t>(request_string.begin(), request_string.end());
209 const auto body_view = (header_only ||
body ==
nullptr) ?
213 const auto response_string = fmt::format(
219 ccf::http_status_str(status),
223 return std::vector<uint8_t>(
224 response_string.begin(), response_string.end());
229#pragma clang diagnostic push
230#pragma clang diagnostic ignored "-Wunused-function"
233 static std::vector<uint8_t> build_header(
234 llhttp_method method,
const std::vector<uint8_t>& body)
236 Request r(
"/", method);
238 return r.build_request(
true);
241 static std::vector<uint8_t> build_request(
242 llhttp_method method,
const std::vector<uint8_t>& body)
244 Request r(
"/", method);
246 return r.build_request(
false);
250 static std::vector<uint8_t> build_delete_header(
251 const std::vector<uint8_t>& body)
253 return build_header(HTTP_DELETE, body);
256 static std::vector<uint8_t> build_delete_request(
257 const std::vector<uint8_t>& body)
259 return build_request(HTTP_DELETE, body);
263 static std::vector<uint8_t> build_get_header(
const std::vector<uint8_t>& body)
265 return build_header(HTTP_GET, body);
268 static std::vector<uint8_t> build_get_request(
269 const std::vector<uint8_t>& body)
271 return build_request(HTTP_GET, body);
275 static std::vector<uint8_t> build_post_header(
276 const std::vector<uint8_t>& body)
278 return build_header(HTTP_POST, body);
281 static std::vector<uint8_t> build_post_request(
282 const std::vector<uint8_t>& body)
284 return build_request(HTTP_POST, body);
288 static std::vector<uint8_t> build_put_header(
const std::vector<uint8_t>& body)
290 return build_header(HTTP_PUT, body);
293 static std::vector<uint8_t> build_put_request(
294 const std::vector<uint8_t>& body)
296 return build_request(HTTP_PUT, body);
298#pragma clang diagnostic pop
Definition http_builder.h:31
ccf::http::HeaderMap headers
Definition http_builder.h:33
const uint8_t * body
Definition http_builder.h:34
const ccf::http::HeaderMap & get_headers() const
Definition http_builder.h:40
void set_body(const uint8_t *b, size_t s, bool overwrite_content_length=true)
Definition http_builder.h:87
size_t get_content_length() const
Definition http_builder.h:57
void set_header(std::string k, const std::string &v)
Definition http_builder.h:45
void set_body(const std::string &s, bool overwrite_content_length=true)
Definition http_builder.h:102
void set_body(const std::vector< uint8_t > *b, bool overwrite_content_length=true)
Definition http_builder.h:74
const uint8_t * get_content_data() const
Definition http_builder.h:69
size_t body_size
Definition http_builder.h:35
void clear_headers()
Definition http_builder.h:52
Definition http_builder.h:118
std::vector< uint8_t > build_request(bool header_only=false) const
Definition http_builder.h:177
std::string get_path() const
Definition http_builder.h:154
std::string get_formatted_query() const
Definition http_builder.h:164
void set_method(llhttp_method m)
Definition http_builder.h:132
llhttp_method get_method() const
Definition http_builder.h:137
Request(const std::string_view &p="/", llhttp_method m=HTTP_POST)
Definition http_builder.h:125
void set_query_param(const std::string &k, const std::string &v)
Definition http_builder.h:159
void set_path(const std::string_view &p)
Definition http_builder.h:142
Definition http_builder.h:200
std::vector< uint8_t > build_response(bool header_only=false) const
Definition http_builder.h:207
Response(ccf::http_status s=HTTP_STATUS_OK)
Definition http_builder.h:205
std::map< std::string, std::string, std::less<> > HeaderMap
Definition http_header_map.h:10
llhttp_status http_status
Definition http_status.h:9
Definition error_reporter.h:6