Line data Source code
1 : #ifndef HEADER_fd_src_waltz_grpc_fd_grpc_codec_h 2 : #define HEADER_fd_src_waltz_grpc_fd_grpc_codec_h 3 : 4 : /* fd_grpc_codec.h provides helpers for gRPC over HTTP/2. 5 : 6 : https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md */ 7 : 8 : #include "../h2/fd_h2_base.h" 9 : #include "../h2/fd_h2_hdr_match.h" 10 : 11 : /* gRPC protocol status codes 12 : https://github.com/grpc/grpc/blob/v1.71.0/doc/statuscodes.md */ 13 : 14 0 : #define FD_GRPC_STATUS_OK 0 15 0 : #define FD_GRPC_STATUS_CANCELLED 1 16 0 : #define FD_GRPC_STATUS_UNKNOWN 2 17 0 : #define FD_GRPC_STATUS_INVALID_ARGUMENT 3 18 0 : #define FD_GRPC_STATUS_DEADLINE_EXCEEDED 4 19 0 : #define FD_GRPC_STATUS_NOT_FOUND 5 20 0 : #define FD_GRPC_STATUS_ALREADY_EXISTS 6 21 0 : #define FD_GRPC_STATUS_PERMISSION_DENIED 7 22 0 : #define FD_GRPC_STATUS_RESOURCE_EXHAUSTED 8 23 0 : #define FD_GRPC_STATUS_FAILED_PRECONDITION 9 24 0 : #define FD_GRPC_STATUS_ABORTED 10 25 0 : #define FD_GRPC_STATUS_OUT_OF_RANGE 11 26 0 : #define FD_GRPC_STATUS_UNIMPLEMENTED 12 27 0 : #define FD_GRPC_STATUS_INTERNAL 13 28 0 : #define FD_GRPC_STATUS_UNAVAILABLE 14 29 0 : #define FD_GRPC_STATUS_DATA_LOSS 15 30 0 : #define FD_GRPC_STATUS_UNAUTHENTICATED 16 31 : 32 : /* Internal IDs for gRPC headers */ 33 : 34 0 : #define FD_GRPC_HDR_STATUS 0x101 35 0 : #define FD_GRPC_HDR_MESSAGE 0x102 36 : 37 : /* fd_grpc_hdr_t is the header part of a Length-Prefixed-Message. */ 38 : 39 : struct __attribute__((packed)) fd_grpc_hdr { 40 : uchar compressed; /* in [0,1] */ 41 : uint msg_sz; /* net order */ 42 : /* msg_sz bytes follow ... */ 43 : }; 44 : 45 : typedef struct fd_grpc_hdr fd_grpc_hdr_t; 46 : 47 : struct fd_grpc_req_hdrs { 48 : char const * host; /* excluding port */ 49 : ulong host_len; /* <=255 */ 50 : ushort port; 51 : char const * path; 52 : ulong path_len; 53 : uint https : 1; /* 1 if https, 0 if http */ 54 : char const * bearer_auth; 55 : ulong bearer_auth_len; 56 : }; 57 : 58 : typedef struct fd_grpc_req_hdrs fd_grpc_req_hdrs_t; 59 : 60 : struct fd_grpc_resp_hdrs { 61 : /* Headers */ 62 : 63 : uint h2_status; /* caller must init (typically 0); if :status header 64 : absent, value is left unchanged; else HTTP status 65 : in [100,999] */ 66 : uint is_grpc_proto : 1; 67 : 68 : /* Trailers */ 69 : 70 : uint grpc_status; /* caller must init to FD_GRPC_STATUS_UNKNOWN before 71 : parsing; if grpc-status trailer absent, value is 72 : left unchanged; 0 is FD_GRPC_STATUS_OK */ 73 : char grpc_msg[ 1008 ]; 74 : uint grpc_msg_len; 75 : }; 76 : 77 : typedef struct fd_grpc_resp_hdrs fd_grpc_resp_hdrs_t; 78 : 79 : FD_PROTOTYPES_BEGIN 80 : 81 : /* fd_grpc_h2_gen_request_hdrs generates a HEADERS frame with gRPC 82 : request headers. Returns 1 on success and 0 on failure */ 83 : 84 : int 85 : fd_grpc_h2_gen_request_hdrs( fd_grpc_req_hdrs_t const * req, 86 : fd_h2_rbuf_t * rbuf_tx, 87 : char const * version, 88 : ulong version_len ); 89 : 90 : /* fd_grpc_h2_rec_response_hdrs consumes a HEADERS frame and recovers 91 : selected gRPC request headers. Ignores unknown headers. Returns 92 : FD_H2_SUCCESS on success, or FD_H2_ERR_PROTOCOL on parse failure. 93 : Logs reason for failure. */ 94 : 95 : int 96 : fd_grpc_h2_read_response_hdrs( fd_grpc_resp_hdrs_t * resp, 97 : fd_h2_hdr_matcher_t const * matcher, 98 : uchar const * payload, 99 : ulong payload_sz ); 100 : 101 : char const * 102 : fd_grpc_status_cstr( uint status ); 103 : 104 : FD_PROTOTYPES_END 105 : 106 : #endif /* HEADER_fd_src_waltz_grpc_fd_grpc_codec_h */