Skip to content

Commit

Permalink
Improvements to gRPC server-side support (#8765)
Browse files Browse the repository at this point in the history
* Cleanup of TODOs in GrpcProtocolHandler. New utility class for converting headers.

* New implementation of GrpcHeadersUtil without using any internal APIs.

* Initial support for compression/decompression of gRPC data.

* Adds support for codec negotiation and new testing showing gZIP compression support in gRPC calls.

* Uses a client interceptor to verify that response is properly encoded using GZIP.

* Fixes import problems and re-arranges private fields.
  • Loading branch information
spericas authored May 28, 2024
1 parent 19edc8b commit 2525b39
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.webserver.grpc;

import java.util.Base64;

import io.helidon.http.HeaderNames;
import io.helidon.http.WritableHeaders;
import io.helidon.http.http2.Http2Headers;

import io.grpc.Metadata;

import static java.nio.charset.StandardCharsets.US_ASCII;

class GrpcHeadersUtil {

private GrpcHeadersUtil() {
}

static void updateHeaders(WritableHeaders<?> writable, Metadata headers) {
Base64.Encoder encoder = Base64.getEncoder();
headers.keys().forEach(name -> {
if (name.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
byte[] binary = headers.get(Metadata.Key.of(name, Metadata.BINARY_BYTE_MARSHALLER));
writable.add(HeaderNames.create(name, new String(encoder.encode(binary), US_ASCII)));
} else {
String ascii = headers.get(Metadata.Key.of(name, Metadata.ASCII_STRING_MARSHALLER));
writable.add(HeaderNames.create(name, ascii));
}
});
}

static Metadata toMetadata(Http2Headers headers) {
Base64.Decoder decoder = Base64.getDecoder();
Metadata metadata = new Metadata();
headers.httpHeaders().forEach(header -> {
String name = header.name();
if (name.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
metadata.put(Metadata.Key.of(name, Metadata.BINARY_BYTE_MARSHALLER),
decoder.decode(name.getBytes(US_ASCII)));
} else {
metadata.put(Metadata.Key.of(name, Metadata.ASCII_STRING_MARSHALLER), name);
}
});
return metadata;
}
}
Loading

0 comments on commit 2525b39

Please sign in to comment.