Skip to content

Use response headers x-grpc-request-type and x-grpc-gateway-body to etermine when to close the request writer #42

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions wsproxy/websocket_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func defaultHeaderForwarder(header string) bool {
// The cookie name is specified by the TokenCookieName value.
//
// example:
// Sec-Websocket-Protocol: Bearer, foobar
//
// Sec-Websocket-Protocol: Bearer, foobar
//
// is converted to:
// Authorization: Bearer foobar
//
// Authorization: Bearer foobar
//
// Method can be overwritten with the MethodOverrideParam get parameter in the requested URL
func WebsocketProxy(h http.Handler, opts ...Option) http.Handler {
Expand Down Expand Up @@ -166,6 +169,9 @@ func isClosedConnError(err error) bool {

func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
var responseHeader http.Header
var grpcMethodType string
var grpcGatewayBody string

// If Sec-WebSocket-Protocol starts with "Bearer", respond in kind.
// TODO(tmc): consider customizability/extension point here.
if strings.HasPrefix(r.Header.Get("Sec-WebSocket-Protocol"), "Bearer") {
Expand All @@ -192,6 +198,8 @@ func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
if swsp := r.Header.Get("Sec-WebSocket-Protocol"); swsp != "" {
request.Header.Set("Authorization", transformSubProtocolHeader(swsp))
}
grpcMethodType = r.Header.Get("x-grpc-method-type")
grpcGatewayBody = r.Header.Get("x-grpc-gateway-body")
for header := range r.Header {
if p.headerForwarder(header) {
request.Header.Set(header, r.Header.Get(header))
Expand Down Expand Up @@ -233,6 +241,9 @@ func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
defer func() {
cancelFn()
}()
if grpcGatewayBody == "false" {
requestBodyW.Close()
}
for {
select {
case <-ctx.Done():
Expand All @@ -259,6 +270,9 @@ func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
p.logger.Warnln("[read] error writing message to upstream http server:", err)
return
}
if grpcMethodType == "Unary" || grpcMethodType == "ServerStreaming" {
requestBodyW.Close()
}
}
}()
// ping write loop
Expand Down Expand Up @@ -303,6 +317,13 @@ func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
p.logger.Warnln("[write] error writing websocket message:", err)
return
}
if grpcMethodType == "Unary" || grpcMethodType == "ClientStreaming" {
// Close WebSocket
if err = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil {
p.logger.Warnln("[write] error writing websocket close message:", err)
return
}
}
}
if err := scanner.Err(); err != nil {
p.logger.Warnln("scanner err:", err)
Expand Down Expand Up @@ -338,12 +359,15 @@ func transformSubProtocolHeader(header string) string {
func (w *inMemoryResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func (w *inMemoryResponseWriter) Header() http.Header {
return w.header
}

func (w *inMemoryResponseWriter) WriteHeader(code int) {
w.code = code
}

func (w *inMemoryResponseWriter) CloseNotify() <-chan bool {
return w.closed
}
Expand Down