Skip to content

Commit

Permalink
Check flusher interface before calling Flush (#479)
Browse files Browse the repository at this point in the history
* check before flushing

* check before flushing

* code review
  • Loading branch information
mangas authored and johanbrandhorst committed Aug 5, 2019
1 parent cc5469c commit cf7d15a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions go/grpcweb/grpc_web_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +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.
w.wrapped.(http.Flusher).Flush()
f.Flush()
}
}

Expand Down Expand Up @@ -90,7 +95,7 @@ func (w *grpcWebResponse) finishRequest(req *http.Request) {
w.copyTrailersToPayload()
} else {
w.WriteHeader(http.StatusOK)
w.wrapped.(http.Flusher).Flush()
w.Flush()
}
}

Expand All @@ -102,7 +107,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 {
Expand Down Expand Up @@ -157,5 +162,5 @@ func (w *base64ResponseWriter) Flush() {
grpclog.Errorf("ignoring error Flushing base64 encoder: %v", err)
}
w.newEncoder()
w.wrapped.(http.Flusher).Flush()
w.Flush()
}

0 comments on commit cf7d15a

Please sign in to comment.