Skip to content

Commit

Permalink
refactor websocket_wrapper.go with copyHeader func (#362)
Browse files Browse the repository at this point in the history
This commit makes websocket_wrapper.go file consistent with the changes
in grpc_web_response.go file, PR #359.
  • Loading branch information
danilvpetrov authored and johanbrandhorst committed Feb 21, 2019
1 parent 5d060c9 commit f5a2e82
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions go/grpcweb/websocket_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ func (w *webSocketResponseWriter) writeHeaderFrame(headers http.Header) {
}

func (w *webSocketResponseWriter) copyFlushedHeaders() {
for k, vv := range w.headers {
// Skip the pre-annoucement of Trailer headers. Don't add them to the response headers.
if strings.ToLower(k) == "trailer" {
continue
}
for _, v := range vv {
w.flushedHeaders.Add(k, v)
}
}
copyHeader(
w.flushedHeaders, w.headers,
skipKeys("trailer"),
keyCase(http.CanonicalHeaderKey),
)
}

func (w *webSocketResponseWriter) WriteHeader(code int) {
Expand All @@ -73,25 +69,17 @@ func (w *webSocketResponseWriter) WriteHeader(code int) {
}

func (w *webSocketResponseWriter) extractTrailerHeaders() http.Header {
trailerHeaders := make(http.Header)
for k, vv := range w.headers {
// Skip the pre-annoucement of Trailer headers. Don't add them to the response headers.
if strings.ToLower(k) == "trailer" {
continue
}
// Skip existing headers that were already sent.
if _, exists := w.flushedHeaders[k]; exists {
continue
}
// Skip the Trailer prefix
if strings.HasPrefix(k, http2.TrailerPrefix) {
k = k[len(http2.TrailerPrefix):]
}
for _, v := range vv {
trailerHeaders.Add(k, v)
}
}
return trailerHeaders
th := make(http.Header)
copyHeader(
th, w.headers,
skipKeys(append([]string{"trailer"}, headerKeys(w.flushedHeaders)...)...),
replaceInKeys(http2.TrailerPrefix, ""),
// gRPC-Web spec says that must use lower-case header/trailer names.
// See "HTTP wire protocols" section in
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md#protocol-differences-vs-grpc-over-http2
keyCase(strings.ToLower),
)
return th
}

func (w *webSocketResponseWriter) FlushTrailers() {
Expand Down

0 comments on commit f5a2e82

Please sign in to comment.