userver: userver/ugrpc/server/metadata_utils.hpp Source File
Loading...
Searching...
No Matches
metadata_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/metadata_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/impl/to_string.hpp>
15#include <userver/ugrpc/server/call_context.hpp>
16#include <userver/utils/assert.hpp>
17#include <userver/utils/text.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace ugrpc::server {
22
23/// @brief Returns an std::input_range containing std::string_view
24/// which are non-owning references to the values of the metadata field.
25/// The references must not outlive the call object to avoid undefined behavior.
26inline auto GetRepeatedMetadata(const CallContextBase& context, std::string_view field_name) {
27 UASSERT(field_name == utils::text::ToLower(field_name));
28 const auto& metadata = context.GetServerContext().client_metadata();
29 auto [it_begin, it_end] = metadata.equal_range(ugrpc::impl::ToGrpcStringRef(field_name));
30
31 using Metadata = std::multimap<grpc::string_ref, grpc::string_ref>;
32 return boost::iterator_range<Metadata::const_iterator>(it_begin, it_end) |
33 boost::adaptors::transformed([](const std::pair<const grpc::string_ref, grpc::string_ref>& entry) {
34 return ugrpc::impl::ToStringView(entry.second);
35 });
36}
37
38} // namespace ugrpc::server
39
40USERVER_NAMESPACE_END