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

Remove connection-specific headers from HTTP/2 message. #7

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
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
51 changes: 40 additions & 11 deletions pkg/service/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,71 @@ import (

const (
InvalidGrpcMethodName = jErrors.ConstError("gRPC method name is invalid")

headerAccept = "accept"
headerContentType = "content-type"
headerContentLength = "content-length"

// connection specific headers http1.0/1.1
headerConnection = "connection"
headerProxyConnection = "proxy-connection"
headerKeepAlive = "keep-alive"
headerTransferEncoding = "transfer-encoding"
headerUpgrade = "upgrade"
)

func isConnectionSpecificHeader(name string) bool {
switch name {
case headerConnection, headerProxyConnection, headerKeepAlive, headerTransferEncoding, headerUpgrade:
return true
}
return false
}

func GetRPCRequestContext(request *http.Request) context.Context {
grpcMetadata := metadata.Pairs()

for name, values := range request.Header {
name = strings.ToLower(name)

// in case the client sends a content-length header it will be removed before proceeding
if name == "content-length" {
if name == headerContentLength {
continue
}
// RFC 9113 8.2.2.: endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields
if request.ProtoMajor > 1 && isConnectionSpecificHeader(name) {
continue
}
grpcMetadata.Append(name, values...)
}

grpcMetadata.Set("accept", "application/protobuf")
grpcMetadata.Set("content-type", "application/protobuf")
grpcMetadata.Set(headerAccept, "application/protobuf")
grpcMetadata.Set(headerContentType, "application/protobuf")

return metadata.NewOutgoingContext(request.Context(), grpcMetadata)
}

func SetRESTHeaders(headers http.Header, gRPCheader metadata.MD, gRPCTrailer metadata.MD) {
func setHeader(headers http.Header, protoMajor int, name string, values []string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Move this function to the bottom of the file.

It is not required in Go to define a function before this function is used. Best practice for more readable code is to define exported functions first and then un-exported functions. Files are meant to be read from top to bottom, not the opposite way. Therefore the more important functions (exproted) should be defined before less important functions.

// RFC 9113 8.2.2.: endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields
if protoMajor > 1 && isConnectionSpecificHeader(name) {
return
}
for _, value := range values {
headers.Add(name, value)
}
}

func SetRESTHeaders(protoMajor int, headers http.Header, gRPCheader metadata.MD, gRPCTrailer metadata.MD) {
// set headers
for name, values := range gRPCheader {
for _, value := range values {
headers.Add(name, value)
}
setHeader(headers, protoMajor, name, values)
}
// append trailers as headers
for name, values := range gRPCTrailer {
for _, value := range values {
headers.Add(name, value)
}
setHeader(headers, protoMajor, name, values)
}

headers.Set("content-type", "application/json")
headers.Set(headerContentType, "application/json")
}

func GetRPCResponse(responseDesc protoreflect.MessageDescriptor) *dynamicpb.Message {
Expand Down
20 changes: 16 additions & 4 deletions pkg/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,28 @@ func createRoutingEndpoint(rc *Context, logger Logger) func(w http.ResponseWrite
)
if err != nil {
if e, ok := status.FromError(err); ok {
transformer.SetRESTHeaders(w.Header(), header, trailer)
transformer.SetRESTHeaders(r.ProtoMajor, w.Header(), header, trailer)
w.WriteHeader(transformer.GetHTTPStatusCode(e.Code()))
}
logger.ErrorContext(r.Context(), jErrors.Details(jErrors.Trace(err)))
return
}

transformer.SetRESTHeaders(w.Header(), header, trailer)
fmt.Fprint(w, protojson.Format(rpcResponse))
w.WriteHeader(http.StatusOK)
transformer.SetRESTHeaders(r.ProtoMajor, w.Header(), header, trailer)

response, err := protojson.Marshal(rpcResponse)
if err != nil {
logger.ErrorContext(r.Context(), jErrors.Details(jErrors.Trace(err)))
w.WriteHeader(http.StatusInternalServerError)
return
}

_, err = w.Write(response)
if err != nil {
logger.ErrorContext(r.Context(), jErrors.Details(jErrors.Trace(err)))
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}

Expand Down
Loading