Class GrpcStatusMapper

java.lang.Object
org.mockserver.grpc.GrpcStatusMapper

public class GrpcStatusMapper extends Object
  • Field Details

    • GRPC_STATUS_HEADER

      public static final String GRPC_STATUS_HEADER
      See Also:
    • GRPC_MESSAGE_HEADER

      public static final String GRPC_MESSAGE_HEADER
      See Also:
    • GRPC_STATUS_NAME_HEADER

      public static final String GRPC_STATUS_NAME_HEADER
      See Also:
    • GRPC_CONTENT_TYPE

      public static final String GRPC_CONTENT_TYPE
      See Also:
    • GRPC_ENCODING_HEADER

      public static final String GRPC_ENCODING_HEADER
      Request header naming the message encoding the client used, e.g. gzip. The frame's compressed flag says only THAT a message is compressed; this says how.
      See Also:
    • GRPC_ACCEPT_ENCODING_HEADER

      public static final String GRPC_ACCEPT_ENCODING_HEADER
      Response header advertising the encodings the server can decode, so a client whose preferred encoding is unsupported knows what to retry with.
      See Also:
  • Constructor Details

    • GrpcStatusMapper

      public GrpcStatusMapper()
  • Method Details

    • fromCode

      public static GrpcStatusMapper.GrpcStatusCode fromCode(int code)
    • fromName

      public static GrpcStatusMapper.GrpcStatusCode fromName(String name)
    • fromHttpStatus

      public static GrpcStatusMapper.GrpcStatusCode fromHttpStatus(int httpStatus)
    • fromHttpTransportStatus

      public static GrpcStatusMapper.GrpcStatusCode fromHttpTransportStatus(int httpStatus)
      Maps an HTTP response status onto the gRPC status a client should observe, per the "HTTP-Status → Status code" table in the gRPC-over-HTTP/2 protocol specification.

      This is deliberately not fromHttpStatus(int). That method inverts the gRPC → HTTP mapping carried on GrpcStatusMapper.GrpcStatusCode (so 404 → NOT_FOUND, the status whose canonical HTTP rendering is 404). This method implements the separate, spec-defined mapping used when a gRPC call fails at the HTTP transport level and no grpc-status is available — where 404 means "the server does not implement this method" and therefore maps to UNIMPLEMENTED.

    • percentEncodeMessage

      public static String percentEncodeMessage(String message)
      Percent-encodes a grpc-message value for transmission, per the gRPC wire specification's Percent-Encoded production.

      The spec defines the value as ASCII only:

         Percent-Encoded       = 1*(Percent-Byte-Unescaped / Percent-Byte-Escaped)
         Percent-Byte-Unescaped = %x20-24 / %x26-7E        ; space..'$' and '&'..'~'
         Percent-Byte-Escaped   = "%" 2HEXDIG
       
      so every byte outside 0x20-0x7E, plus % (0x25) itself, is escaped as %XX over the value's UTF-8 bytes. Clients percent-decode on receipt.

      This must be applied at every emission site. Writing the raw string is wrong even for ordinary input:

      • "quota 50% exceeded" — the client decodes %20 (from "50% e") into a space, silently corrupting a plain-ASCII message. No exotic characters required.
      • "paiement refusé" — on HTTP/1.1 and HTTP/2 Netty's AsciiString conversion byte-casts char & 0xFF, so the client receives ISO-8859-1 bytes and grpc-java's UTF-8 decode produces mojibake. In the gRPC-Web trailer frame, which is written as US_ASCII, every non-ASCII character becomes a literal ? — silent data loss.
      • "denied\r\ngrpc-status: 0" — the gRPC-Web trailer frame is a CRLF-delimited block, so an unescaped CRLF injects a second grpc-status line and can turn an error into a success. Encoding CR and LF as %0D/%0A closes this.
      Parameters:
      message - the raw message, may be null
      Returns:
      the percent-encoded message, or null if message was null
    • percentDecodeMessage

      public static String percentDecodeMessage(String message)
      Percent-decodes a grpc-message received from an upstream gRPC server, reversing percentEncodeMessage(String).

      Lenient, matching grpc-java: a % that is not followed by two hex digits is passed through literally rather than treated as an error, so a server that (like MockServer before this was implemented) emits an unencoded message is not made worse.

      Parameters:
      message - the percent-encoded message, may be null
      Returns:
      the decoded message, or null if message was null
    • stripControlCharacters

      public static String stripControlCharacters(String value)
      Removes C0 control characters (and DEL) from a value decoded from an untrusted source.

      Percent-decoding an upstream grpc-message can produce real NUL or CRLF bytes from %00 / %0D%0A. That value flows into LogEntry, the persisted log, verifications and the dashboard, so a hostile upstream -- exactly the party the proxy use case treats as untrusted -- could forge log lines. The printable text is preserved.

    • isGrpcContentType

      public static boolean isGrpcContentType(String contentType)