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

refactor websocket_wrapper.go with copyHeader func #362

Merged
Merged
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
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