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 <ranges>
8#include <string_view>
9#include <utility>
10
11#include <grpcpp/support/string_ref.h>
12
13#include <userver/ugrpc/impl/to_string.hpp>
14#include <userver/ugrpc/server/call_context.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(const CallContextBase& context, std::string_view field_name) {
26 UASSERT(field_name == utils::text::ToLower(field_name));
27 const auto& metadata = context.GetServerContext().client_metadata();
28 auto [it_begin, it_end] = metadata.equal_range(ugrpc::impl::ToGrpcStringRef(field_name));
29
30 return std::ranges::subrange(it_begin, it_end) |
31 std::views::transform([](const std::pair<const grpc::string_ref, grpc::string_ref>& entry) {
32 return ugrpc::impl::ToStringView(entry.second);
33 });
34}
35
36} // namespace ugrpc::server
37
38USERVER_NAMESPACE_END