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

Provide the Host value to handlers #513

Closed
ymmt2005 opened this issue May 29, 2023 · 3 comments · Fixed by #522
Closed

Provide the Host value to handlers #513

ymmt2005 opened this issue May 29, 2023 · 3 comments · Fixed by #522
Labels
enhancement New feature or request

Comments

@ymmt2005
Copy link

ymmt2005 commented May 29, 2023

I wanted to use the Host field value of the http.Request in my application.
Since Go does not set Host header in http.Header, connect-go handlers cannot
access this information.

ref: https://cs.opensource.google/go/go/+/refs/tags/go1.20.4:src/net/http/request.go;l=157-158

Describe the solution you'd like
It would be great if connect.Request provided the Host field value in the http.Request.
For example,

func (MyHandler) DoSomething(ctx context.Context, req *connect.Request[pb.Request]) (*connect.Response[pb.Response], error) {
    fmt.Println(req.Host())
}

Describe alternatives you've considered
My current workaround is to wrap the HTTP handler returned by NewXXX generated function like:

	mux := http.NewServeMux()
	p, h := myserviceconnect.NewMyServiceHandler(handler)
	h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.Header.Set("Host", r.Host)
		h.ServeHTTP(w, r)
	})
	mux.Handle(p, h2)

This way, I can access the Host value from handlers with req.Header().Get("Host").

@ymmt2005 ymmt2005 added the enhancement New feature or request label May 29, 2023
@jhump
Copy link
Member

jhump commented Jun 14, 2023

It looks like the reverse flow is also problematic: the Go "net/http" client ignores a header key of "Host", instead requiring the caller to set the Host field of the *http.Request. But Connect clients don't have access to set this field. So in addition to currying the request.Host field into headers for server handlers, we should do the reverse (set the request.Host field from headers, if set by client or interceptor) on the client side.

emcfarlane added a commit that referenced this issue Jun 14, 2023
emcfarlane added a commit that referenced this issue Jun 16, 2023
Allows Host to set the request.Host on the client and promotes
request.Host back to a Header in the handler.

Fixes #513
@emcfarlane
Copy link
Contributor

@ymmt2005 fixed in a similar way to the workaround. Host is available in the request header.

@ymmt2005
Copy link
Author

@emcfarlane Thank you so much!

akshayjshah pushed a commit that referenced this issue Jul 26, 2023
Allows Host to set the request.Host on the client and promotes
request.Host back to a Header in the handler.

Fixes #513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants