CCF
Loading...
Searching...
No Matches
rpc_context_impl.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/claims_digest.h"
6#include "ccf/rpc_context.h"
7
8namespace ccf
9{
10 enum class HttpVersion : uint8_t
11 {
12 HTTP1 = 0,
13 HTTP2
14 };
15
16 // Partial implementation of RpcContext, private to the framework (not visible
17 // to apps). Serves 2 purposes:
18 // - Default implementation of simple methods accessing member fields
19 // - Adding methods like `serialise_response()`, required by frontends
21 {
22 protected:
23 std::shared_ptr<SessionContext> session;
25
26 std::shared_ptr<void> user_data;
27
28 public:
30 const std::shared_ptr<SessionContext>& s,
32 session(s),
34 {}
35
36 [[nodiscard]] std::shared_ptr<SessionContext> get_session_context()
37 const override
38 {
39 return session;
40 }
41
42 void set_user_data(std::shared_ptr<void> data) override
43 {
44 user_data = data;
45 }
46
47 [[nodiscard]] void* get_user_data() const override
48 {
49 return user_data.get();
50 }
51
52 ccf::ClaimsDigest claims = ccf::empty_claims();
53 // NOLINTBEGIN(performance-move-const-arg)
55 {
56 claims.set(std::move(digest));
57 }
58 // NOLINTEND(performance-move-const-arg)
59
62 {
63 return path_params;
64 }
65
71
72 [[nodiscard]] HttpVersion get_http_version() const
73 {
74 return http_version;
75 }
76
78 ccf::http_status status,
79 const std::string& code,
80 std::string&& msg,
81 const std::vector<nlohmann::json>& details = {}) override
82 {
83 nlohmann::json body =
84 ccf::ODataErrorResponse{ccf::ODataError{code, std::move(msg), details}};
85 set_response_json(body, status);
86 }
87
89 {
90 nlohmann::json body = ccf::ODataErrorResponse{
91 ccf::ODataError{std::move(error.code), std::move(error.msg), {}}};
92 set_response_json(body, error.status);
93 }
94
96 const nlohmann::json& body, ccf::http_status status) override
97 {
98 // Set error_handler to replace, to avoid throwing if the error message
99 // contains non-UTF8 characters. Other args are default values
100 const auto s =
101 body.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
102 set_response_status(status);
103 set_response_body(std::vector<uint8_t>(s.begin(), s.end()));
105 ccf::http::headers::CONTENT_TYPE,
106 http::headervalues::contenttype::JSON);
107 }
108
110 bool terminate_session = false;
111
112 [[nodiscard]] virtual bool should_apply_writes() const = 0;
113 virtual void reset_response() = 0;
114 [[nodiscard]] virtual std::vector<uint8_t> serialise_response() const = 0;
115 virtual const std::vector<uint8_t>& get_serialised_request() = 0;
116 };
117}
Definition claims_digest.h:10
void set(Digest &&digest_)
Definition claims_digest.h:21
Definition rpc_context_impl.h:21
void * get_user_data() const override
Definition rpc_context_impl.h:47
virtual const std::vector< uint8_t > & get_serialised_request()=0
ccf::PathParams decoded_path_params
Definition rpc_context_impl.h:66
HttpVersion get_http_version() const
Definition rpc_context_impl.h:72
RpcContextImpl(const std::shared_ptr< SessionContext > &s, HttpVersion v=HttpVersion::HTTP1)
Definition rpc_context_impl.h:29
void set_response_json(const nlohmann::json &body, ccf::http_status status) override
Definition rpc_context_impl.h:95
bool response_is_pending
Definition rpc_context_impl.h:109
virtual std::vector< uint8_t > serialise_response() const =0
virtual bool should_apply_writes() const =0
bool terminate_session
Definition rpc_context_impl.h:110
void set_claims_digest(ccf::ClaimsDigest::Digest &&digest) override
Definition rpc_context_impl.h:54
void set_error(ccf::ErrorDetails &&error) override
Construct error response, formatted as JSON OData.
Definition rpc_context_impl.h:88
void set_error(ccf::http_status status, const std::string &code, std::string &&msg, const std::vector< nlohmann::json > &details={}) override
Construct error response, formatted as JSON OData.
Definition rpc_context_impl.h:77
const ccf::PathParams & get_request_path_params() override
Definition rpc_context_impl.h:61
std::shared_ptr< void > user_data
Definition rpc_context_impl.h:26
void set_user_data(std::shared_ptr< void > data) override
Definition rpc_context_impl.h:42
HttpVersion http_version
Definition rpc_context_impl.h:24
virtual void reset_response()=0
ccf::PathParams path_params
Definition rpc_context_impl.h:60
const ccf::PathParams & get_decoded_request_path_params() override
Definition rpc_context_impl.h:67
std::shared_ptr< SessionContext > session
Definition rpc_context_impl.h:23
std::shared_ptr< SessionContext > get_session_context() const override
Definition rpc_context_impl.h:36
ccf::ClaimsDigest claims
Definition rpc_context_impl.h:52
Describes the currently executing RPC.
Definition rpc_context.h:58
Definition sha256_hash.h:16
virtual void set_response_status(int status)=0
Sets initial status code summarising result of RPC.
virtual void set_response_header(const std::string_view &name, const std::string_view &value)=0
virtual void set_response_body(const std::vector< uint8_t > &body)=0
Sets the main body or payload of the response.
Definition app_interface.h:14
std::map< std::string, std::string, std::less<> > PathParams
Definition rpc_context.h:54
llhttp_status http_status
Definition http_status.h:9
HttpVersion
Definition rpc_context_impl.h:11
@ error
Definition tls_session.h:23
Definition odata_error.h:58
Definition odata_error.h:50
Definition odata_error.h:37