Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a CallOption for modifying the :authority header #5787

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
headerFields = append(headerFields, hpack.HeaderField{Name: ":method", Value: "POST"})
headerFields = append(headerFields, hpack.HeaderField{Name: ":scheme", Value: t.scheme})
headerFields = append(headerFields, hpack.HeaderField{Name: ":path", Value: callHdr.Method})
headerFields = append(headerFields, hpack.HeaderField{Name: ":authority", Value: callHdr.Host})
headerFields = append(headerFields, hpack.HeaderField{Name: ":authority", Value: callHdr.Authority})
headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: grpcutil.ContentType(callHdr.ContentSubtype)})
headerFields = append(headerFields, hpack.HeaderField{Name: "user-agent", Value: t.userAgent})
headerFields = append(headerFields, hpack.HeaderField{Name: "te", Value: "trailers"})
Expand Down
3 changes: 3 additions & 0 deletions internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ type CallHdr struct {
// Host specifies the peer's host.
Host string

// Authority specifies the :authority header.
Authority string

// Method specifies the operation to perform.
Method string

Expand Down
24 changes: 24 additions & 0 deletions rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type callInfo struct {
contentSubtype string
codec baseCodec
maxRetryRPCBufferSize int
authorityModifier func(ctx context.Context, authority string) string
}

func defaultCallInfo() *callInfo {
Expand Down Expand Up @@ -233,6 +234,29 @@ func (o TrailerCallOption) after(c *callInfo, attempt *csAttempt) {
*o.TrailerAddr = attempt.s.Trailer()
}

// Authority returns a CallOption that mofifies the :authority header for a RPC.
func Authority(modifier func(ctx context.Context, authority string) string) CallOption {
Copy link
Author

@110y 110y Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fully confident about the signature of this modifier function (this is a user-facing API), but at least, I think it's worth making it have context.Context as its argument since we can use values inside the Context (e.g. metadata) for :authority modification as I described in the description of this PR.

return AuthorityCallOption{
modifier: modifier,
}
}

// AuthorityCallOption is a CallOption for modifying the :authority header.
//
// # Experimental
//
// Notice: This type is EXPERIMENTAL and may be changed or removed in a
// later release.
type AuthorityCallOption struct {
modifier func(ctx context.Context, authority string) string
}

func (o AuthorityCallOption) before(c *callInfo) error {
c.authorityModifier = o.modifier
return nil
}
func (o AuthorityCallOption) after(c *callInfo, attempt *csAttempt) {}

// Peer returns a CallOption that retrieves peer information for a unary RPC.
// The peer field will be populated *after* the RPC completes.
func Peer(p *peer.Peer) CallOption {
Expand Down
13 changes: 12 additions & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,14 @@ func newClientStreamWithParams(ctx context.Context, desc *StreamDesc, cc *Client
return nil, err
}

authority := cc.authority
if c.authorityModifier != nil {
authority = c.authorityModifier(ctx, authority)
}

callHdr := &transport.CallHdr{
Host: cc.authority,
Authority: authority,
Method: method,
ContentSubtype: c.contentSubtype,
DoneFunc: doneFunc,
Expand Down Expand Up @@ -762,7 +768,6 @@ func (cs *clientStream) Header() (metadata.MD, error) {
}
return toRPCErr(err)
}, cs.commitAttemptLocked)

if err != nil {
cs.finish(err)
return nil, err
Expand Down Expand Up @@ -1181,8 +1186,14 @@ func newNonRetryClientStream(ctx context.Context, desc *StreamDesc, method strin
return nil, err
}

authority := ac.cc.authority
if c.authorityModifier != nil {
authority = c.authorityModifier(ctx, authority)
}

callHdr := &transport.CallHdr{
Host: ac.cc.authority,
Authority: authority,
Method: method,
ContentSubtype: c.contentSubtype,
}
Expand Down