From a9dfb6b3ac5f1fc75e41f1fdca99793f38b30043 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Thu, 13 Jun 2019 13:15:06 +0100 Subject: [PATCH 1/3] check before flushing --- go/grpcweb/grpc_web_response.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/go/grpcweb/grpc_web_response.go b/go/grpcweb/grpc_web_response.go index 2fdf7f04..c0fefdd2 100644 --- a/go/grpcweb/grpc_web_response.go +++ b/go/grpcweb/grpc_web_response.go @@ -62,7 +62,12 @@ func (w *grpcWebResponse) Flush() { if w.wroteHeaders || w.wroteBody { // Work around the fact that WriteHeader and a call to Flush would have caused a 200 response. // This is the case when there is no payload. - w.wrapped.(http.Flusher).Flush() + switch w.wrapped.(type) { + case http.Flusher: + w.wrapped.(http.Flusher).Flush() + default: + + } } } From cec2daf74e7c3411ea7d05d786e54166c2e501df Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Thu, 13 Jun 2019 13:58:02 +0100 Subject: [PATCH 2/3] check before flushing --- go/grpcweb/grpc_web_response.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/go/grpcweb/grpc_web_response.go b/go/grpcweb/grpc_web_response.go index c0fefdd2..f7eaa1cf 100644 --- a/go/grpcweb/grpc_web_response.go +++ b/go/grpcweb/grpc_web_response.go @@ -62,11 +62,10 @@ func (w *grpcWebResponse) Flush() { if w.wroteHeaders || w.wroteBody { // Work around the fact that WriteHeader and a call to Flush would have caused a 200 response. // This is the case when there is no payload. - switch w.wrapped.(type) { - case http.Flusher: - w.wrapped.(http.Flusher).Flush() - default: + f, ok := w.wrapped.(http.Flusher) + if ok { + f.Flush() } } } @@ -97,7 +96,7 @@ func (w *grpcWebResponse) finishRequest(req *http.Request) { w.copyTrailersToPayload() } else { w.WriteHeader(http.StatusOK) - w.wrapped.(http.Flusher).Flush() + w.Flush() } } @@ -109,7 +108,7 @@ func (w *grpcWebResponse) copyTrailersToPayload() { binary.BigEndian.PutUint32(trailerGrpcDataHeader[1:5], uint32(trailerBuffer.Len())) w.wrapped.Write(trailerGrpcDataHeader) w.wrapped.Write(trailerBuffer.Bytes()) - w.wrapped.(http.Flusher).Flush() + w.Flush() } func extractTrailingHeaders(src http.Header, flushed http.Header) http.Header { @@ -164,7 +163,7 @@ func (w *base64ResponseWriter) Flush() { grpclog.Errorf("ignoring error Flushing base64 encoder: %v", err) } w.newEncoder() - w.wrapped.(http.Flusher).Flush() + w.Flush() } func (w *base64ResponseWriter) CloseNotify() <-chan bool { From a3e69064f57ec0f8b015a31ae692f6cfb25b21ec Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Thu, 13 Jun 2019 14:25:04 +0100 Subject: [PATCH 3/3] code review --- go/grpcweb/grpc_web_response.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/go/grpcweb/grpc_web_response.go b/go/grpcweb/grpc_web_response.go index f7eaa1cf..07513d5b 100644 --- a/go/grpcweb/grpc_web_response.go +++ b/go/grpcweb/grpc_web_response.go @@ -59,14 +59,15 @@ func (w *grpcWebResponse) WriteHeader(code int) { } func (w *grpcWebResponse) Flush() { + f, ok := w.wrapped.(http.Flusher) + if !ok { + return + } + if w.wroteHeaders || w.wroteBody { // Work around the fact that WriteHeader and a call to Flush would have caused a 200 response. // This is the case when there is no payload. - - f, ok := w.wrapped.(http.Flusher) - if ok { - f.Flush() - } + f.Flush() } }