Package org.mockserver.grpc
Class GrpcWebTranslator
java.lang.Object
org.mockserver.grpc.GrpcWebTranslator
Translates between gRPC-Web framing and standard gRPC framing.
gRPC-Web uses the same length-prefixed message framing as gRPC but differs in two ways:
- Trailers-in-body: gRPC status trailers are sent as a special frame
with flag byte
0x80followed by ASCII-encoded trailer lines (grpc-status: N\r\ngrpc-message: ...\r\n) - Base64 encoding (
-textvariant): when the content-type isapplication/grpc-web-text, the entire body (message frames + trailer frame) is base64-encoded
This class provides the conversion layer so gRPC-Web requests can be fed into the existing gRPC pipeline unchanged and gRPC responses can be re-framed as gRPC-Web.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]buildTrailerFrame(String grpcStatus, String grpcMessage) Builds a gRPC-Web trailer frame: flag byte0x80, 4-byte length, then ASCII-encoded trailer lines.static byte[]decodeRequestBody(byte[] body, String contentType) Decodes the request body from gRPC-Web framing to standard gRPC framing.static byte[]encodeResponseBody(byte[] messageFrameBody, String grpcStatus, String grpcMessage, boolean isTextVariant) Builds a gRPC-Web response body from a standard gRPC response.static booleanisGrpcWebContentType(String contentType) Returnstrueif the content-type indicates a gRPC-Web request (binary or text variant).static booleanisGrpcWebTextContentType(String contentType) Returnstrueif the content-type is the base64-encoded text variant.static StringparseTrailerFrame(byte[] trailerBody) Parses trailers from a gRPC-Web trailer frame body.static StringresponseContentType(String requestContentType) Returns the appropriate gRPC-Web response content-type based on the original request content-type.
-
Field Details
-
GRPC_WEB_CONTENT_TYPE
- See Also:
-
GRPC_WEB_PROTO_CONTENT_TYPE
- See Also:
-
GRPC_WEB_TEXT_CONTENT_TYPE
- See Also:
-
GRPC_WEB_TEXT_PROTO_CONTENT_TYPE
- See Also:
-
TRAILER_FLAG
public static final byte TRAILER_FLAGThe flag byte that marks a trailer frame in gRPC-Web.- See Also:
-
-
Constructor Details
-
GrpcWebTranslator
public GrpcWebTranslator()
-
-
Method Details
-
isGrpcWebContentType
Returnstrueif the content-type indicates a gRPC-Web request (binary or text variant). -
isGrpcWebTextContentType
Returnstrueif the content-type is the base64-encoded text variant. -
decodeRequestBody
Decodes the request body from gRPC-Web framing to standard gRPC framing. For the-textvariant, the body is base64-decoded first. The returned bytes are standard gRPC length-prefixed message frames that can be fed directly intoGrpcFrameCodec.decode(byte[]).- Parameters:
body- the raw request bodycontentType- the content-type header value- Returns:
- standard gRPC-framed message bytes
-
encodeResponseBody
public static byte[] encodeResponseBody(byte[] messageFrameBody, String grpcStatus, String grpcMessage, boolean isTextVariant) Builds a gRPC-Web response body from a standard gRPC response.The response consists of message frame(s) followed by a trailer frame (flag
0x80) containing thegrpc-statusand optionalgrpc-messageas ASCII lines.- Parameters:
messageFrameBody- the gRPC-framed message body (may be empty/null)grpcStatus- the gRPC status code as a string (e.g. "0")grpcMessage- the gRPC status message (may be null)isTextVariant- if true, the entire body is base64-encoded- Returns:
- the gRPC-Web response body bytes
-
buildTrailerFrame
Builds a gRPC-Web trailer frame: flag byte0x80, 4-byte length, then ASCII-encoded trailer lines. -
parseTrailerFrame
Parses trailers from a gRPC-Web trailer frame body. The trailer frame body is the ASCII text after the 5-byte header.- Parameters:
trailerBody- the raw trailer body bytes (without the 5-byte frame header)- Returns:
- parsed trailer lines as a string
-
responseContentType
Returns the appropriate gRPC-Web response content-type based on the original request content-type.- Parameters:
requestContentType- the original gRPC-Web request content-type- Returns:
- the matching gRPC-Web response content-type
-