Package org.microproxy.contentviews
Class Grpc
java.lang.Object
org.microproxy.contentviews.Grpc
gRPC's message framing: a body is a sequence of messages, each a compressed flag (one byte), a
length (four bytes, big-endian) and that many bytes of payload, compressed with the coding the
grpc-encoding header names when the flag is set.
List<byte[]> messages = Grpc.messages(body, request.headers().get("grpc-encoding"));
ProtoMessage first = Protobuf.decode(messages.getFirst());
byte[] rewritten = Grpc.join(List.of(Protobuf.encode(changed)));
Also here: the status codes, and google.rpc.Status from the
grpc-status-details-bin trailer.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne entry of a status's details: agoogle.protobuf.Any.static final recordOne framed message, as it is on the wire.static final recordAgoogle.rpc.Status, as servers send it ingrpc-status-details-bin. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe largest a message may grow to when it is decompressed. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcodeName(int code) The name of a status code.static byte[]Compresses one message's payload.static byte[]decompress(byte[] payload, String encoding, int maxBytes) Decompresses one message's payload.static byte[]Frames messages without compressing them.static byte[]Frames messages, compressing each withencoding.static StringDecodesgrpc-message, which is percent-encoded UTF-8.static List<byte[]> Splits a body into its messages, decompressing those that are compressed.static List<Grpc.Frame> split(byte[] body) Splits a body into its framed messages.static Grpc.Statusstatus(byte[] status) Decodes a serializedgoogle.rpc.Status.static Grpc.StatusstatusFromTrailer(String value) Decodes the value of agrpc-status-details-binfield: base64 (padded or not) of agoogle.rpc.Status.
-
Field Details
-
DEFAULT_MAX_MESSAGE_BYTES
public static final int DEFAULT_MAX_MESSAGE_BYTESThe largest a message may grow to when it is decompressed.- See Also:
-
-
Method Details
-
split
Splits a body into its framed messages.- Parameters:
body- a whole gRPC request or response body- Returns:
- the frames, in order
- Throws:
DecodeException- if a flag is neither 0 nor 1, or the body ends inside a frame
-
messages
Splits a body into its messages, decompressing those that are compressed.- Parameters:
body- a whole gRPC request or response bodyencoding- the message'sgrpc-encodingheader, ornullfor none- Returns:
- the messages' payloads, uncompressed
- Throws:
DecodeException- if the body is not framed correctly, or a message cannot be decompressed
-
join
Frames messages without compressing them.- Parameters:
messages- the payloads- Returns:
- the body
-
join
Frames messages, compressing each withencoding.- Parameters:
messages- the payloads, uncompressedencoding-gzipordeflateto compress,identityornullnot to- Returns:
- the body
- Throws:
IllegalArgumentException- if the proxy cannot compress withencoding
-
compress
Compresses one message's payload.- Parameters:
payload- the payloadencoding-gzip,deflateorbr- Returns:
- the compressed payload
- Throws:
IllegalArgumentException- if the proxy cannot compress withencoding
-
decompress
public static byte[] decompress(byte[] payload, String encoding, int maxBytes) throws DecodeException Decompresses one message's payload.- Parameters:
payload- the compressed payloadencoding- thegrpc-encoding:gzip,deflate,br,identity, andzstdwith the zstd-decoder modulemaxBytes- the largest the result may be- Returns:
- the payload, uncompressed
- Throws:
DecodeException- if the encoding is missing or unsupported, or the data corrupt or too large
-
codeName
The name of a status code.- Parameters:
code- thegrpc-statusvalue- Returns:
- its name, such as
INVALID_ARGUMENT, ornullfor an unknown code
-
message
Decodesgrpc-message, which is percent-encoded UTF-8.- Parameters:
value- the field's value- Returns:
- the message
-
status
Decodes a serializedgoogle.rpc.Status.- Parameters:
status- the serialized message- Returns:
- the status
- Throws:
DecodeException- if it is not a status message
-
statusFromTrailer
Decodes the value of agrpc-status-details-binfield: base64 (padded or not) of agoogle.rpc.Status.- Parameters:
value- the field's value- Returns:
- the status
- Throws:
DecodeException- if the value is not base64 of a status message
-