Skip to content

Commit 449adce

Browse files
authored
server: add method to expose authority seen by server (#29768)
* server: add method to expose authority seen by server * Automated change: Fix sanity tests Co-authored-by: markdroth <markdroth@users.noreply.github.com>
1 parent 831ce7d commit 449adce

9 files changed

Lines changed: 152 additions & 75 deletions

File tree

include/grpcpp/impl/codegen/server_context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ class ServerContextBase {
297297
return call_metric_recorder_;
298298
}
299299

300+
/// EXPERIMENTAL API
301+
/// Returns the call's authority.
302+
grpc::string_ref ExperimentalGetAuthority() const;
303+
300304
protected:
301305
/// Async only. Has to be called before the rpc starts.
302306
/// Returns the tag in completion queue when the rpc finishes.

src/core/lib/surface/call.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Call : public CppImplOf<Call, grpc_call> {
104104
bool is_notify_tag_closure) = 0;
105105
virtual bool failed_before_recv_message() const = 0;
106106
virtual bool is_trailers_only() const = 0;
107+
virtual absl::string_view GetServerAuthority() const = 0;
107108
virtual void ExternalRef() = 0;
108109
virtual void ExternalUnref() = 0;
109110
virtual void InternalRef(const char* reason) = 0;
@@ -234,6 +235,13 @@ class FilterStackCall final : public Call {
234235
return call_failed_before_recv_message_;
235236
}
236237

238+
absl::string_view GetServerAuthority() const override {
239+
const Slice* authority_metadata =
240+
recv_initial_metadata_.get_pointer(HttpAuthorityMetadata());
241+
if (authority_metadata == nullptr) return "";
242+
return authority_metadata->as_string_view();
243+
}
244+
237245
grpc_compression_algorithm test_only_compression_algorithm() override {
238246
return incoming_compression_algorithm_;
239247
}
@@ -1942,6 +1950,10 @@ int grpc_call_failed_before_recv_message(const grpc_call* c) {
19421950
return grpc_core::Call::FromC(c)->failed_before_recv_message();
19431951
}
19441952

1953+
absl::string_view grpc_call_server_authority(const grpc_call* call) {
1954+
return grpc_core::Call::FromC(call)->GetServerAuthority();
1955+
}
1956+
19451957
const char* grpc_call_error_to_string(grpc_call_error error) {
19461958
switch (error) {
19471959
case GRPC_CALL_ERROR:

src/core/lib/surface/call.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stddef.h>
2525
#include <stdint.h>
2626

27+
#include "absl/strings/string_view.h"
2728
#include "absl/types/optional.h"
2829

2930
#include <grpc/impl/codegen/compression_types.h>
@@ -125,6 +126,9 @@ grpc_compression_algorithm grpc_call_compression_for_level(
125126
Move to surface API if requested by other languages. */
126127
bool grpc_call_is_trailers_only(const grpc_call* call);
127128

129+
// Returns the authority for the call, as seen on the server side.
130+
absl::string_view grpc_call_server_authority(const grpc_call* call);
131+
128132
extern grpc_core::TraceFlag grpc_call_error_trace;
129133
extern grpc_core::TraceFlag grpc_compression_trace;
130134

src/core/lib/surface/server.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,9 @@ void Server::CallData::RecvInitialMetadataReady(void* arg,
13741374
CallData* calld = static_cast<CallData*>(elem->call_data);
13751375
if (error == GRPC_ERROR_NONE) {
13761376
calld->path_ = calld->recv_initial_metadata_->Take(HttpPathMetadata());
1377-
calld->host_ = calld->recv_initial_metadata_->Take(HttpAuthorityMetadata());
1377+
auto* host =
1378+
calld->recv_initial_metadata_->get_pointer(HttpAuthorityMetadata());
1379+
if (host != nullptr) calld->host_.emplace(host->Ref());
13781380
} else {
13791381
(void)GRPC_ERROR_REF(error);
13801382
}

src/cpp/server/server_context.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <utility>
2828
#include <vector>
2929

30+
#include "absl/strings/string_view.h"
31+
3032
#include <grpc/compression.h>
3133
#include <grpc/grpc.h>
3234
#include <grpc/impl/codegen/compression_types.h>
@@ -52,6 +54,7 @@
5254
#include <grpcpp/support/interceptor.h>
5355
#include <grpcpp/support/server_callback.h>
5456
#include <grpcpp/support/server_interceptor.h>
57+
#include <grpcpp/support/string_ref.h>
5558

5659
#include "src/core/lib/gprpp/ref_counted.h"
5760
#include "src/core/lib/gprpp/sync.h"
@@ -407,4 +410,9 @@ void ServerContextBase::CreateCallMetricRecorder() {
407410
call_metric_recorder_ = arena->New<experimental::CallMetricRecorder>(arena);
408411
}
409412

413+
grpc::string_ref ServerContextBase::ExperimentalGetAuthority() const {
414+
absl::string_view authority = grpc_call_server_authority(call_.call);
415+
return grpc::string_ref(authority.data(), authority.size());
416+
}
417+
410418
} // namespace grpc

src/proto/grpc/testing/echo_messages.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ message RequestParams {
5454
bool echo_metadata_initially = 17;
5555
bool server_notify_client_when_started = 18;
5656
xds.data.orca.v3.OrcaLoadReport backend_metrics = 19;
57+
bool echo_host_from_authority_header = 20;
5758
}
5859

5960
message EchoRequest {

0 commit comments

Comments
 (0)