Class Grpc

java.lang.Object
org.microproxy.contentviews.Grpc

public final class Grpc extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static final record 
    One entry of a status's details: a google.protobuf.Any.
    static final record 
    One framed message, as it is on the wire.
    static final record 
    A google.rpc.Status, as servers send it in grpc-status-details-bin.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The largest a message may grow to when it is decompressed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    codeName(int code)
    The name of a status code.
    static byte[]
    compress(byte[] payload, String encoding)
    Compresses one message's payload.
    static byte[]
    decompress(byte[] payload, String encoding, int maxBytes)
    Decompresses one message's payload.
    static byte[]
    join(List<byte[]> messages)
    Frames messages without compressing them.
    static byte[]
    join(List<byte[]> messages, String encoding)
    Frames messages, compressing each with encoding.
    static String
    message(String value)
    Decodes grpc-message, which is percent-encoded UTF-8.
    static List<byte[]>
    messages(byte[] body, String encoding)
    Splits a body into its messages, decompressing those that are compressed.
    split(byte[] body)
    Splits a body into its framed messages.
    status(byte[] status)
    Decodes a serialized google.rpc.Status.
    Decodes the value of a grpc-status-details-bin field: base64 (padded or not) of a google.rpc.Status.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_MAX_MESSAGE_BYTES

      public static final int DEFAULT_MAX_MESSAGE_BYTES
      The largest a message may grow to when it is decompressed.
      See Also:
  • Method Details

    • split

      public static List<Grpc.Frame> split(byte[] body) throws DecodeException
      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

      public static List<byte[]> messages(byte[] body, String encoding) throws DecodeException
      Splits a body into its messages, decompressing those that are compressed.
      Parameters:
      body - a whole gRPC request or response body
      encoding - the message's grpc-encoding header, or null for none
      Returns:
      the messages' payloads, uncompressed
      Throws:
      DecodeException - if the body is not framed correctly, or a message cannot be decompressed
    • join

      public static byte[] join(List<byte[]> messages)
      Frames messages without compressing them.
      Parameters:
      messages - the payloads
      Returns:
      the body
    • join

      public static byte[] join(List<byte[]> messages, String encoding)
      Frames messages, compressing each with encoding.
      Parameters:
      messages - the payloads, uncompressed
      encoding - gzip or deflate to compress, identity or null not to
      Returns:
      the body
      Throws:
      IllegalArgumentException - if the proxy cannot compress with encoding
    • compress

      public static byte[] compress(byte[] payload, String encoding)
      Compresses one message's payload.
      Parameters:
      payload - the payload
      encoding - gzip, deflate or br
      Returns:
      the compressed payload
      Throws:
      IllegalArgumentException - if the proxy cannot compress with encoding
    • decompress

      public static byte[] decompress(byte[] payload, String encoding, int maxBytes) throws DecodeException
      Decompresses one message's payload.
      Parameters:
      payload - the compressed payload
      encoding - the grpc-encoding: gzip, deflate, br, identity, and zstd with the zstd-decoder module
      maxBytes - 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

      public static String codeName(int code)
      The name of a status code.
      Parameters:
      code - the grpc-status value
      Returns:
      its name, such as INVALID_ARGUMENT, or null for an unknown code
    • message

      public static String message(String value)
      Decodes grpc-message, which is percent-encoded UTF-8.
      Parameters:
      value - the field's value
      Returns:
      the message
    • status

      public static Grpc.Status status(byte[] status) throws DecodeException
      Decodes a serialized google.rpc.Status.
      Parameters:
      status - the serialized message
      Returns:
      the status
      Throws:
      DecodeException - if it is not a status message
    • statusFromTrailer

      public static Grpc.Status statusFromTrailer(String value) throws DecodeException
      Decodes the value of a grpc-status-details-bin field: base64 (padded or not) of a google.rpc.Status.
      Parameters:
      value - the field's value
      Returns:
      the status
      Throws:
      DecodeException - if the value is not base64 of a status message