userver: userver/ugrpc/server/metadata_utils.hpp Source File
Loading...
Searching...
No Matches
metadata_utils.hpp
1#pragma once
2
3/// @file userver/ugrpc/server/metadta_utils.hpp
4/// @brief Utilities to work with the request metadata
5
6#include <map>
7#include <string_view>
8#include <utility>
9
10#include <grpcpp/support/string_ref.h>
11#include <boost/range/adaptor/map.hpp>
12#include <boost/range/adaptor/transformed.hpp>
13
14#include <userver/ugrpc/server/rpc.hpp>
15#include <userver/utils/assert.hpp>
16#include <userver/utils/text.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace ugrpc::server {
21
22/// @brief Returns an std::input_range containing std::string_view
23/// which are non-owning references to the values of the metadata field.
24/// The references must not outlive the call object to avoid undefined behavior.
25inline auto GetRepeatedMetadata(ugrpc::server::CallAnyBase& call, std::string_view field_name) {
26 UASSERT(field_name == utils::text::ToLower(field_name));
27 const auto& metadata = call.GetContext().client_metadata();
28 auto [it_begin, it_end] = metadata.equal_range(grpc::string_ref(field_name.data(), field_name.length()));
29
30 using Metadata = std::multimap<grpc::string_ref, grpc::string_ref>;
31 return boost::iterator_range<Metadata::const_iterator>(it_begin, it_end) |
32 boost::adaptors::transformed([](const std::pair<const grpc::string_ref, grpc::string_ref>& entry) {
33 return std::string_view(entry.second.begin(), entry.second.length());
34 });
35}
36
37} // namespace ugrpc::server
38
39USERVER_NAMESPACE_END