17 const auto s = body.dump();
19 std::vector<uint8_t> data(s.begin(), s.end());
23 ccf::http::headers::CONTENT_TYPE,
24 ccf::http::headervalues::contenttype::JSON);
25 response.set_body(&data);
27 return response.build_response();
30 inline std::vector<uint8_t>
error(
33 return error({status, code, std::move(msg)});
42 std::string whole_path;
49 std::vector<uint8_t> request_body;
51 std::vector<uint8_t> serialised_request;
55 std::vector<uint8_t> response_body;
58 bool serialised =
false;
60 std::optional<bool> explicit_apply_writes = std::nullopt;
66 const auto request_prefix = fmt::format(
72 ::http::get_header_string(request_headers));
74 serialised_request.resize(request_prefix.size() + request_body.size());
76 serialised_request.data(),
77 request_prefix.data(),
78 request_prefix.size());
79 if (!request_body.empty())
82 serialised_request.data() + request_prefix.size(),
93 std::shared_ptr<ccf::SessionContext> s,
96 const std::string_view& url_,
98 const std::vector<uint8_t>& body_,
99 const std::vector<uint8_t>& raw_request_ = {}) :
103 request_headers(
std::move(headers_)),
105 serialised_request(raw_request_)
111 query = url_decode(query_);
112 fragment = url_decode(fragment_);
114 if (!serialised_request.empty())
123 return response_headers;
128 return response_trailers;
133 return response_status;
164 return serialised_request;
180 return request_headers;
184 const std::string_view& name)
const override
186 const auto it = request_headers.find(name);
187 if (it != request_headers.end())
200 template <
typename T>
204 if (verb != HTTP_HEAD)
206 if constexpr (std::is_same_v<T, std::string>)
208 response_body = std::vector<uint8_t>(body.begin(), body.end());
212 response_body = std::forward<T>(body);
234 return response_body;
239 return std::move(response_body);
249 return response_status;
253 const std::string_view& name,
const std::string_view& value)
override
255 response_headers[std::string(name)] = value;
260 response_headers.clear();
264 const std::string_view& name,
const std::string_view& value)
override
266 response_trailers[std::string(name)] = value;
271 explicit_apply_writes = apply;
276 if (explicit_apply_writes.has_value())
278 return explicit_apply_writes.value();
287 response_headers.clear();
288 response_body.clear();
289 response_status = HTTP_STATUS_OK;
290 explicit_apply_writes.reset();
298 for (
const auto& [k, v] : response_headers)
300 http_response.set_header(k, v);
303 http_response.set_body(&response_body);
304 return http_response.build_response();
308 inline static std::optional<std::string> extract_actor(HttpRpcContext& ctx)
310 const auto path = ctx.get_method();
311 const auto first_slash = path.find_first_of(
'/');
312 const auto second_slash = path.find_first_of(
'/', first_slash + 1);
314 if (first_slash != 0 || second_slash == std::string::npos)
319 auto actor = path.substr(first_slash + 1, second_slash - first_slash - 1);
320 auto remaining_path = path.substr(second_slash);
322 if (actor.empty() || remaining_path.empty())
330 ctx.set_method(remaining_path);
335 inline static std::shared_ptr<ccf::RpcHandler> fetch_rpc_handler(
336 std::shared_ptr<http::HttpRpcContext>& ctx,
337 std::shared_ptr<ccf::RPCMap>& rpc_map)
339 const auto actor_opt = http::extract_actor(*ctx);
340 std::optional<std::shared_ptr<ccf::RpcHandler>> search;
343 if (actor_opt.has_value())
345 const auto& actor_s = actor_opt.value();
346 actor = rpc_map->resolve(actor_s);
347 search = rpc_map->find(actor);
365 std::shared_ptr<ccf::SessionContext> s,
const std::vector<uint8_t>& packed)
369 parser.
execute(packed.data(), packed.size());
373 throw std::logic_error(fmt::format(
374 "Expected packed to contain a single complete HTTP message. Actually "
375 "parsed {} messages",
379 const auto& msg = processor.
received.front();
381 return std::make_shared<::http::HttpRpcContext>(
392 std::shared_ptr<ccf::SessionContext> s,
393 const std::vector<uint8_t>& packed,
396 switch (frame_format)
403 throw std::logic_error(
"Unknown Frame Format");
Definition rest_verb.h:45
const char * c_str() const
Definition rest_verb.h:62
Definition rpc_context_impl.h:22
RpcContextImpl(const std::shared_ptr< SessionContext > &s, HttpVersion v=HttpVersion::HTTP1)
Definition rpc_context_impl.h:30
HttpVersion http_version
Definition rpc_context_impl.h:25
ccf::endpoints::ConsensusCommittedEndpointFunction consensus_committed_func
Definition rpc_context_impl.h:111
Definition http_rpc_context.h:37
ccf::FrameFormat frame_format() const override
Definition http_rpc_context.h:136
void set_apply_writes(bool apply) override
Definition http_rpc_context.h:269
void reset_response() override
Definition http_rpc_context.h:285
int get_response_status() const override
Definition http_rpc_context.h:247
void set_method(const std::string_view &p)
Definition http_rpc_context.h:172
std::optional< std::string > get_request_header(const std::string_view &name) const override
Definition http_rpc_context.h:183
bool should_apply_writes() const override
Definition http_rpc_context.h:274
void set_response_header(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:252
void set_response_body(const std::vector< uint8_t > &body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:217
const ccf::RESTVerb & get_request_verb() const override
Definition http_rpc_context.h:151
HttpRpcContext(std::shared_ptr< ccf::SessionContext > s, ccf::HttpVersion http_version, llhttp_method verb_, const std::string_view &url_, ccf::http::HeaderMap headers_, const std::vector< uint8_t > &body_, const std::vector< uint8_t > &raw_request_={})
Definition http_rpc_context.h:92
std::vector< uint8_t > serialise_response() const override
Definition http_rpc_context.h:294
ccf::http::HeaderMap get_response_trailers() const
Definition http_rpc_context.h:126
void set_response_body(std::vector< uint8_t > &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:222
void _set_response_body(T &&body)
Definition http_rpc_context.h:201
const std::vector< uint8_t > & get_request_body() const override
Definition http_rpc_context.h:141
const std::vector< uint8_t > & get_serialised_request() override
Definition http_rpc_context.h:161
const std::string & get_request_query() const override
Definition http_rpc_context.h:146
std::string get_method() const override
Definition http_rpc_context.h:167
const ccf::http::HeaderMap & get_request_headers() const override
Returns map of all headers found in the request.
Definition http_rpc_context.h:177
void set_response_trailer(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:263
ccf::http::HeaderMap get_response_headers() const
Definition http_rpc_context.h:121
const std::string & get_request_url() const override
Definition http_rpc_context.h:195
void set_response_body(std::string &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:227
const std::vector< uint8_t > & get_response_body() const override
Definition http_rpc_context.h:232
std::vector< uint8_t > && take_response_body() override
Definition http_rpc_context.h:237
std::string get_request_path() const override
Definition http_rpc_context.h:156
ccf::http_status get_response_http_status() const
Definition http_rpc_context.h:131
void set_response_status(int status) override
Sets initial status code summarising result of RPC.
Definition http_rpc_context.h:242
void clear_response_headers() override
Definition http_rpc_context.h:258
void execute(const uint8_t *data, size_t size)
Definition http_parser.h:232
Definition http_parser.h:406
Definition http_builder.h:200
std::map< std::string, std::string, std::less<> > HeaderMap
Definition http_header_map.h:10
Definition app_interface.h:13
std::shared_ptr<::http::HttpRpcContext > make_fwd_rpc_context(std::shared_ptr< ccf::SessionContext > s, const std::vector< uint8_t > &packed, ccf::FrameFormat frame_format)
Definition http_rpc_context.h:391
bool is_valid_actor(const std::string &actor)
Definition actors.h:19
llhttp_status http_status
Definition http_status.h:9
HttpVersion
Definition rpc_context_impl.h:12
FrameFormat
Definition frame_format.h:8
ActorsType
Definition actors.h:11
std::shared_ptr<::http::HttpRpcContext > make_rpc_context(std::shared_ptr< ccf::SessionContext > s, const std::vector< uint8_t > &packed)
Definition http_rpc_context.h:364
Definition error_reporter.h:8
std::vector< uint8_t > error(ccf::ErrorDetails &&error)
Definition http_rpc_context.h:13
auto split_url_path(const std::string_view &url)
Definition http_parser.h:23
bool status_success(ccf::http_status status)
Definition http_parser.h:73
Definition odata_error.h:58
Definition odata_error.h:50
Definition odata_error.h:37
Definition http_parser.h:79
std::queue< Request > received
Definition http_parser.h:91