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(
34 return error({status, code, std::move(msg)});
43 std::string whole_path;
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(
std::move(headers_)),
109 responder(responder_),
110 serialised_request(raw_request_)
116 query = url_decode(query_);
117 fragment = url_decode(fragment_);
119 if (!serialised_request.empty())
128 return response_headers;
133 return response_trailers;
138 return response_status;
169 return serialised_request;
185 return request_headers;
189 const std::string_view& name)
const override
191 const auto it = request_headers.find(name);
192 if (it != request_headers.end())
211 template <
typename T>
215 if (verb != HTTP_HEAD)
217 if constexpr (std::is_same_v<T, std::string>)
219 response_body = std::vector<uint8_t>(body.begin(), body.end());
223 response_body = std::forward<T>(body);
245 return response_body;
250 return std::move(response_body);
260 return response_status;
264 const std::string_view& name,
const std::string_view& value)
override
266 response_headers[std::string(name)] = value;
271 response_headers.clear();
275 const std::string_view& name,
const std::string_view& value)
override
277 response_trailers[std::string(name)] = value;
282 explicit_apply_writes = apply;
287 if (explicit_apply_writes.has_value())
289 return explicit_apply_writes.value();
298 response_headers.clear();
299 response_body.clear();
300 response_status = HTTP_STATUS_OK;
301 explicit_apply_writes.reset();
308 for (
const auto& [k, v] : response_headers)
310 http_response.set_header(k, v);
313 http_response.set_body(&response_body);
314 return http_response.build_response();
318 inline static std::optional<std::string> extract_actor(HttpRpcContext& ctx)
320 const auto path = ctx.get_method();
321 const auto first_slash = path.find_first_of(
'/');
322 const auto second_slash = path.find_first_of(
'/', first_slash + 1);
324 if (first_slash != 0 || second_slash == std::string::npos)
329 auto actor = path.substr(first_slash + 1, second_slash - first_slash - 1);
330 auto remaining_path = path.substr(second_slash);
332 if (actor.empty() || remaining_path.empty())
340 ctx.set_method(remaining_path);
345 inline static std::shared_ptr<ccf::RpcHandler> fetch_rpc_handler(
346 std::shared_ptr<http::HttpRpcContext>& ctx,
347 std::shared_ptr<ccf::RPCMap>& rpc_map)
349 const auto actor_opt = http::extract_actor(*ctx);
350 std::optional<std::shared_ptr<ccf::RpcHandler>> search;
353 if (actor_opt.has_value())
355 const auto& actor_s = actor_opt.value();
356 actor = rpc_map->resolve(actor_s);
357 search = rpc_map->find(actor);
375 std::shared_ptr<ccf::SessionContext> s,
const std::vector<uint8_t>& packed)
379 parser.
execute(packed.data(), packed.size());
383 throw std::logic_error(fmt::format(
384 "Expected packed to contain a single complete HTTP message. Actually "
385 "parsed {} messages",
389 const auto& msg = processor.
received.front();
391 return std::make_shared<::http::HttpRpcContext>(
403 std::shared_ptr<ccf::SessionContext> s,
404 const std::vector<uint8_t>& packed,
407 switch (frame_format)
414 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:21
RpcContextImpl(const std::shared_ptr< SessionContext > &s, HttpVersion v=HttpVersion::HTTP1)
Definition rpc_context_impl.h:29
HttpVersion http_version
Definition rpc_context_impl.h:24
Definition http_rpc_context.h:38
ccf::FrameFormat frame_format() const override
Definition http_rpc_context.h:141
void set_apply_writes(bool apply) override
Definition http_rpc_context.h:280
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::shared_ptr< ccf::http::HTTPResponder > &responder_=nullptr, const std::vector< uint8_t > &raw_request_={})
Definition http_rpc_context.h:95
void reset_response() override
Definition http_rpc_context.h:296
int get_response_status() const override
Definition http_rpc_context.h:258
void set_method(const std::string_view &p)
Definition http_rpc_context.h:177
std::optional< std::string > get_request_header(const std::string_view &name) const override
Definition http_rpc_context.h:188
bool should_apply_writes() const override
Definition http_rpc_context.h:285
void set_response_header(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:263
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:228
const ccf::RESTVerb & get_request_verb() const override
Definition http_rpc_context.h:156
std::vector< uint8_t > serialise_response() const override
Definition http_rpc_context.h:304
ccf::http::HeaderMap get_response_trailers() const
Definition http_rpc_context.h:131
void set_response_body(std::vector< uint8_t > &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:233
void _set_response_body(T &&body)
Definition http_rpc_context.h:212
const std::vector< uint8_t > & get_request_body() const override
Definition http_rpc_context.h:146
const std::vector< uint8_t > & get_serialised_request() override
Definition http_rpc_context.h:166
const std::string & get_request_query() const override
Definition http_rpc_context.h:151
std::string get_method() const override
Definition http_rpc_context.h:172
const ccf::http::HeaderMap & get_request_headers() const override
Returns map of all headers found in the request.
Definition http_rpc_context.h:182
void set_response_trailer(const std::string_view &name, const std::string_view &value) override
Definition http_rpc_context.h:274
ccf::http::HeaderMap get_response_headers() const
Definition http_rpc_context.h:126
const std::string & get_request_url() const override
Definition http_rpc_context.h:200
void set_response_body(std::string &&body) override
Sets the main body or payload of the response.
Definition http_rpc_context.h:238
const std::vector< uint8_t > & get_response_body() const override
Definition http_rpc_context.h:243
std::vector< uint8_t > && take_response_body() override
Definition http_rpc_context.h:248
std::string get_request_path() const override
Definition http_rpc_context.h:161
ccf::http_status get_response_http_status() const
Definition http_rpc_context.h:136
void set_response_status(int status) override
Sets initial status code summarising result of RPC.
Definition http_rpc_context.h:253
void clear_response_headers() override
Definition http_rpc_context.h:269
std::shared_ptr< ccf::http::HTTPResponder > get_responder() const override
Definition http_rpc_context.h:205
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:14
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:402
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:11
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:374
Definition error_reporter.h:6
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
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