CCF
Loading...
Searching...
No Matches
rpc_context.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/frame_format.h"
7#include "ccf/http_consts.h"
10#include "ccf/odata_error.h"
11#include "ccf/redirect.h"
12#include "ccf/rest_verb.h"
14#include "ccf/tx_id.h"
15
16#include <vector>
17
18namespace ccf
19{
20 static constexpr size_t InvalidSessionId = std::numeric_limits<size_t>::max();
21 using ListenInterfaceID = std::string;
22
24 {
25 size_t client_session_id = InvalidSessionId;
26
27 // Contains DER encoding of original caller
28 std::vector<uint8_t> caller_cert = {};
29 bool is_forwarding = false;
30
31 // Only set for RPC sessions (i.e. non-forwarded and non-internal)
32 std::optional<ListenInterfaceID> interface_id = std::nullopt;
33
34 // Only set in the case of a forwarded RPC
35 bool is_forwarded = false;
36
37 // All requests on this session must occur within the same view. If the view
38 // changes, the next request will receive an error response and the session
39 // will be closed.
40 std::optional<ccf::View> active_view = std::nullopt;
41
43 size_t client_session_id_,
44 const std::vector<uint8_t>& caller_cert_,
45 const std::optional<ListenInterfaceID>& interface_id_ = std::nullopt) :
46 client_session_id(client_session_id_),
47 caller_cert(caller_cert_),
48 interface_id(interface_id_)
49 {}
50
51 virtual ~SessionContext() = default;
52 };
53
54 using PathParams = std::map<std::string, std::string, std::less<>>;
55
58 {
59 public:
60 virtual ~RpcContext() = default;
61
66
70 virtual std::shared_ptr<SessionContext> get_session_context() const = 0;
71
72 // Set user data that will be available in the post-local-commit handler.
73 // This is useful to avoid the serialisation/deserialisation cost.
74 virtual void set_user_data(std::shared_ptr<void> data) = 0;
75 // Get the user data that was previously set.
76 virtual void* get_user_data() const = 0;
77
78 virtual const std::vector<uint8_t>& get_request_body() const = 0;
79 virtual const std::string& get_request_query() const = 0;
80 virtual const ccf::RESTVerb& get_request_verb() const = 0;
81 virtual std::string get_request_path() const = 0;
82 virtual std::string get_method() const = 0;
83 virtual std::shared_ptr<ccf::http::HTTPResponder> get_responder() const = 0;
84
89 virtual const PathParams& get_request_path_params() = 0;
90
96
98 virtual const http::HeaderMap& get_request_headers() const = 0;
99
102 virtual std::optional<std::string> get_request_header(
103 const std::string_view& name) const = 0;
104
107 virtual const std::string& get_request_url() const = 0;
108
111 virtual ccf::FrameFormat frame_format() const = 0;
113
118
120 virtual void set_response_body(const std::vector<uint8_t>& body) = 0;
122 virtual void set_response_body(std::vector<uint8_t>&& body) = 0;
124 virtual void set_response_body(std::string&& body) = 0;
125 virtual const std::vector<uint8_t>& get_response_body() const = 0;
126
128 virtual void set_response_status(int status) = 0;
129 virtual int get_response_status() const = 0;
130
132 const std::string_view& name, const std::string_view& value) = 0;
133 virtual void set_response_header(const std::string_view& name, size_t n)
134 {
135 set_response_header(name, std::to_string(n));
136 }
138 {
139 set_response_header(kv.first, kv.second);
140 }
141 virtual void clear_response_headers() = 0;
142
144 const std::string_view& name, const std::string_view& value) = 0;
145 virtual void set_response_trailer(const std::string_view& name, size_t n)
146 {
147 set_response_trailer(name, std::to_string(n));
148 }
150 {
151 set_response_trailer(kv.first, kv.second);
152 }
153
154 virtual void set_response_json(
155 const nlohmann::json& body, http_status status) = 0;
156
158 virtual void set_error(
159 http_status status,
160 const std::string& code,
161 std::string&& msg,
162 const std::vector<nlohmann::json>& details = {}) = 0;
163
165 virtual void set_error(ccf::ErrorDetails&& error) = 0;
166
168
172
178 virtual void set_apply_writes(bool apply) = 0;
179
188 };
189}
Definition rest_verb.h:45
Describes the currently executing RPC.
Definition rpc_context.h:58
virtual ~RpcContext()=default
Definition sha256_hash.h:16
virtual void * get_user_data() const =0
virtual std::string get_request_path() const =0
virtual const PathParams & get_decoded_request_path_params()=0
virtual const http::HeaderMap & get_request_headers() const =0
Returns map of all headers found in the request.
virtual const std::string & get_request_url() const =0
virtual ccf::FrameFormat frame_format() const =0
virtual const ccf::RESTVerb & get_request_verb() const =0
virtual const PathParams & get_request_path_params()=0
virtual std::shared_ptr< SessionContext > get_session_context() const =0
virtual std::optional< std::string > get_request_header(const std::string_view &name) const =0
virtual void set_user_data(std::shared_ptr< void > data)=0
virtual std::string get_method() const =0
virtual std::shared_ptr< ccf::http::HTTPResponder > get_responder() const =0
virtual const std::vector< uint8_t > & get_request_body() const =0
virtual const std::string & get_request_query() const =0
virtual int get_response_status() const =0
virtual void set_error(http_status status, const std::string &code, std::string &&msg, const std::vector< nlohmann::json > &details={})=0
Construct error response, formatted as JSON OData.
virtual void set_response_trailer(const std::string_view &name, const std::string_view &value)=0
virtual void clear_response_headers()=0
virtual void set_response_body(std::vector< uint8_t > &&body)=0
Sets the main body or payload of the response.
virtual void set_response_status(int status)=0
Sets initial status code summarising result of RPC.
virtual void set_response_trailer(const std::string_view &name, size_t n)
Definition rpc_context.h:145
virtual void set_response_trailer(const http::HeaderKeyValue &kv)
Definition rpc_context.h:149
virtual void set_response_header(const std::string_view &name, const std::string_view &value)=0
virtual const std::vector< uint8_t > & get_response_body() const =0
virtual void set_response_body(const std::vector< uint8_t > &body)=0
Sets the main body or payload of the response.
virtual void set_response_body(std::string &&body)=0
Sets the main body or payload of the response.
virtual void set_response_json(const nlohmann::json &body, http_status status)=0
virtual void set_error(ccf::ErrorDetails &&error)=0
Construct error response, formatted as JSON OData.
virtual void set_response_header(const std::string_view &name, size_t n)
Definition rpc_context.h:133
virtual void set_response_header(const http::HeaderKeyValue &kv)
Definition rpc_context.h:137
virtual void set_apply_writes(bool apply)=0
virtual void set_claims_digest(ccf::ClaimsDigest::Digest &&digest)=0
std::map< std::string, std::string, std::less<> > HeaderMap
Definition http_header_map.h:10
HeaderMap::value_type HeaderKeyValue
Definition http_header_map.h:11
Definition app_interface.h:14
@ error
Definition tls_session.h:24
std::map< std::string, std::string, std::less<> > PathParams
Definition rpc_context.h:54
std::string ListenInterfaceID
Definition rpc_context.h:21
llhttp_status http_status
Definition http_status.h:9
FrameFormat
Definition frame_format.h:8
Definition odata_error.h:58
Definition rpc_context.h:24
std::optional< ccf::View > active_view
Definition rpc_context.h:40
size_t client_session_id
Definition rpc_context.h:25
virtual ~SessionContext()=default
SessionContext(size_t client_session_id_, const std::vector< uint8_t > &caller_cert_, const std::optional< ListenInterfaceID > &interface_id_=std::nullopt)
Definition rpc_context.h:42
bool is_forwarded
Definition rpc_context.h:35
bool is_forwarding
Definition rpc_context.h:29
std::optional< ListenInterfaceID > interface_id
Definition rpc_context.h:32
std::vector< uint8_t > caller_cert
Definition rpc_context.h:28