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

Allow clients to set Host in headers #522

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions connect_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,33 @@ func TestHeaderBasic(t *testing.T) {
assert.Equal(t, response.Header().Get(key), hval)
}

func TestHeaderHost(t *testing.T) {
t.Parallel()
const (
key = "Host"
cval = "buf.build"
)

pingServer := &pluggablePingServer{
ping: func(_ context.Context, request *connect.Request[pingv1.PingRequest]) (*connect.Response[pingv1.PingResponse], error) {
assert.Equal(t, request.Header().Get(key), cval)
response := connect.NewResponse(&pingv1.PingResponse{})
return response, nil
},
}
mux := http.NewServeMux()
mux.Handle(pingv1connect.NewPingServiceHandler(pingServer))
server := httptest.NewServer(mux)
defer server.Close()

client := pingv1connect.NewPingServiceClient(server.Client(), server.URL)
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
request := connect.NewRequest(&pingv1.PingRequest{})
request.Header().Set(key, cval)
response, err := client.Ping(context.Background(), request)
assert.Nil(t, err)
assert.Equal(t, response.Header().Get(key), "")
}

func TestTimeoutParsing(t *testing.T) {
t.Parallel()
const timeout = 10 * time.Minute
Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func NewBidiStreamHandler[Req, Res any](

// ServeHTTP implements [http.Handler].
func (h *Handler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
// We don't need to defer functions to close the request body or read to
// We don't need to defer functions to close the request body or read to
// EOF: the stream we construct later on already does that, and we only
// return early when dealing with misbehaving clients. In those cases, it's
// okay if we can't re-use the connection.
Expand Down
6 changes: 6 additions & 0 deletions protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func (h *connectHandler) NewConn(
contentEncoding = getHeaderCanonical(request.Header, connectStreamingHeaderCompression)
acceptEncoding = getHeaderCanonical(request.Header, connectStreamingHeaderAcceptCompression)
}
if host := request.Host; len(host) > 0 {
request.Header["Host"] = []string{host}
}
requestCompression, responseCompression, failed := negotiateCompression(
h.CompressionPools,
contentEncoding,
Expand Down Expand Up @@ -367,6 +370,9 @@ func (c *connectClient) NewConn(
}
}
duplexCall := newDuplexHTTPCall(ctx, c.HTTPClient, c.URL, spec, header)
if hosts := header["Host"]; len(hosts) > 0 {
duplexCall.request.Host = hosts[0]
}
var conn streamingClientConn
if spec.StreamType == StreamTypeUnary {
unaryConn := &connectUnaryClientConn{
Expand Down