18 const auto s = body.dump();
20 std::vector<uint8_t> data(s.begin(), s.end());
24 ccf::http::headers::CONTENT_TYPE,
25 ccf::http::headervalues::contenttype::JSON);
26 response.set_body(&data);
28 return response.build_response();
31 inline std::vector<uint8_t>
error(
32 http_status status,
const std::string& code, std::string&& msg)
34 return error({status, code, std::move(msg)});
43 std::string whole_path = {};
44 std::string path = {};
45 std::string query = {};
46 std::string fragment = {};
50 std::vector<uint8_t> request_body = {};
52 std::shared_ptr<ccf::http::HTTPResponder> responder =
nullptr;
54 std::vector<uint8_t> serialised_request = {};
58 std::vector<uint8_t> response_body = {};
61 bool serialised =
false;
63 std::optional<bool> explicit_apply_writes = std::nullopt;
69 const auto request_prefix = fmt::format(
75 ::http::get_header_string(request_headers));
77 serialised_request.resize(request_prefix.size() + request_body.size());
79 serialised_request.data(),
80 request_prefix.data(),
81 request_prefix.size());
82 if (!request_body.empty())
85 serialised_request.data() + request_prefix.size(),
96 std::shared_ptr<ccf::SessionContext> s,
99 const std::string_view& url_,
101 const std::vector<uint8_t>& body_,
102 const std::shared_ptr<ccf::http::HTTPResponder>& responder_ =
nullptr,
103 const std::vector<uint8_t>& raw_request_ = {}) :
107 request_headers(headers_),
109 responder(responder_),
110 serialised_request(raw_request_)
115 query = url_decode(query_);
116 fragment = url_decode(fragment_);
118 if (!serialised_request.empty())
126 return response_headers;
131 return response_trailers;
136 return response_body;
141 return response_status;
177 return serialised_request;
192 return request_headers;
196 const std::string_view& name)
const override
198 const auto it = request_headers.find(name);
199 if (it != request_headers.end())
220 response_body = body;
225 response_body = std::move(body);
230 response_body = std::vector<uint8_t>(body.begin(), body.end());
235 return response_body;
245 return response_status;
249 const std::string_view& name,
const std::string_view& value)
override
251 response_headers[std::string(name)] = value;
256 response_headers.clear();
260 const std::string_view& name,
const std::string_view& value)
override
262 response_trailers[std::string(name)] = value;
267 explicit_apply_writes = apply;
272 if (explicit_apply_writes.has_value())
274 return explicit_apply_writes.value();
283 response_headers.clear();
284 response_body.clear();
285 response_status = HTTP_STATUS_OK;
286 explicit_apply_writes.reset();
293 for (
const auto& [k, v] : response_headers)
295 http_response.set_header(k, v);
298 http_response.set_body(&response_body);
299 return http_response.build_response();
303 inline static std::optional<std::string> extract_actor(HttpRpcContext& ctx)
305 const auto path = ctx.get_method();
306 const auto first_slash = path.find_first_of(
'/');
307 const auto second_slash = path.find_first_of(
'/', first_slash + 1);
309 if (first_slash != 0 || second_slash == std::string::npos)
314 auto actor = path.substr(first_slash + 1, second_slash - first_slash - 1);
315 auto remaining_path = path.substr(second_slash);
322 if (actor ==
".well-known")
324 const auto third_slash = path.find_first_of(
'/', second_slash + 1);
325 if (third_slash == std::string::npos)
330 actor = path.substr(first_slash + 1, third_slash - first_slash - 1);
331 remaining_path = path.substr(third_slash);
334 if (actor.empty() || remaining_path.empty())
342 ctx.set_method(remaining_path);
347 inline static std::shared_ptr<ccf::RpcHandler> fetch_rpc_handler(
348 std::shared_ptr<http::HttpRpcContext>& ctx,
349 std::shared_ptr<ccf::RPCMap>& rpc_map)
351 const auto actor_opt = http::extract_actor(*ctx);
352 std::optional<std::shared_ptr<ccf::RpcHandler>> search;
355 if (actor_opt.has_value())
357 const auto& actor_s = actor_opt.value();
358 actor = rpc_map->resolve(actor_s);
359 search = rpc_map->find(actor);
369 return search.value();
376 std::shared_ptr<ccf::SessionContext> s,
const std::vector<uint8_t>& packed)
380 parser.
execute(packed.data(), packed.size());
384 throw std::logic_error(fmt::format(
385 "Expected packed to contain a single complete HTTP message. Actually "
386 "parsed {} messages",
390 const auto& msg = processor.
received.front();
392 return std::make_shared<::http::HttpRpcContext>(
404 std::shared_ptr<ccf::SessionContext> s,
405 const std::vector<uint8_t>& packed,
408 switch (frame_format)
415 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
Definition http_rpc_context.h:38
std::vector< uint8_t > & get_response_body()
Definition http_rpc_context.h:134
virtual void set_apply_writes(bool apply) override
Definition http_rpc_context.h:265
virtual void set_response_body(std::vector< uint8_t > &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:223
void set_method(const std::string_view &p)
Definition http_rpc_context.h:185
virtual std::string get_request_path() const override
Definition http_rpc_context.h:169
virtual const std::vector< uint8_t > & get_response_body() const override
Definition http_rpc_context.h:233
virtual const std::vector< uint8_t > & get_request_body() const override
Definition http_rpc_context.h:154
virtual void set_tx_id(const ccf::TxID &tx_id) override
Definition http_rpc_context.h:149
ccf::http::HeaderMap get_response_trailers() const
Definition http_rpc_context.h:129
virtual void set_response_body(std::string &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:228
virtual const std::string & get_request_query() const override
Definition http_rpc_context.h:159
virtual ccf::FrameFormat frame_format() const override
Definition http_rpc_context.h:144
virtual void set_response_trailer(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:259
virtual std::optional< std::string > get_request_header(const std::string_view &name) const override
Definition http_rpc_context.h:195
virtual void clear_response_headers() override
Definition http_rpc_context.h:254
http_status get_response_http_status() const
Definition http_rpc_context.h:139
virtual std::vector< uint8_t > serialise_response() const override
Definition http_rpc_context.h:289
virtual const ccf::http::HeaderMap & get_request_headers() const override
Returns map of all headers found in the request.
Definition http_rpc_context.h:190
HttpRpcContext(std::shared_ptr< ccf::SessionContext > s, ccf::HttpVersion http_version, llhttp_method verb_, const std::string_view &url_, const ccf::http::HeaderMap &headers_, const std::vector< uint8_t > &body_, const std::shared_ptr< ccf::http::HTTPResponder > &responder_=nullptr, const std::vector< uint8_t > &raw_request_={})
Definition http_rpc_context.h:95
virtual std::string get_method() const override
Definition http_rpc_context.h:180
ccf::http::HeaderMap get_response_headers() const
Definition http_rpc_context.h:124
virtual const std::string & get_request_url() const override
Definition http_rpc_context.h:207
virtual const std::vector< uint8_t > & get_serialised_request() override
Definition http_rpc_context.h:174
virtual bool should_apply_writes() const override
Definition http_rpc_context.h:270
virtual int get_response_status() const override
Definition http_rpc_context.h:243
virtual 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:218
virtual std::shared_ptr< ccf::http::HTTPResponder > get_responder() const override
Definition http_rpc_context.h:212
virtual void set_response_status(int status) override
Sets initial status code summarising result of RPC.
Definition http_rpc_context.h:238
virtual void set_response_header(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:248
virtual void reset_response() override
Definition http_rpc_context.h:281
virtual const ccf::RESTVerb & get_request_verb() const override
Definition http_rpc_context.h:164
void execute(const uint8_t *data, size_t size)
Definition http_parser.h:221
Definition http_parser.h:394
Definition http_builder.h:188
llhttp_status http_status
Definition http_status.h:7
std::map< std::string, std::string, std::less<> > HeaderMap
Definition http_header_map.h:10
Definition app_interface.h:15
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:403
bool is_valid_actor(const std::string &actor)
Definition actors.h:20
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:375
Definition error_reporter.h:6
bool status_success(http_status status)
Definition http_parser.h:71
std::vector< uint8_t > error(ccf::ErrorDetails &&error)
Definition http_rpc_context.h:14
auto split_url_path(const std::string_view &url)
Definition http_parser.h:23
Definition odata_error.h:56
Definition odata_error.h:48
Definition odata_error.h:37
std::string to_str() const
Definition tx_id.h:48
Definition http_parser.h:77
std::queue< Request > received
Definition http_parser.h:87