Module: GRPCWeb::TextCoder

Extended by:
ContentTypes
Defined in:
lib/grpc_web/server/text_coder.rb

Overview

Placeholder

Constant Summary

Constants included from ContentTypes

ContentTypes::ALL_CONTENT_TYPES, ContentTypes::BASE64_CONTENT_TYPES, ContentTypes::DEFAULT_CONTENT_TYPE, ContentTypes::JSON_CONTENT_TYPE, ContentTypes::PROTO_CONTENT_TYPE, ContentTypes::TEXT_CONTENT_TYPE, ContentTypes::TEXT_PROTO_CONTENT_TYPE, ContentTypes::UNSPECIFIED_CONTENT_TYPES

Class Method Summary collapse

Class Method Details

.decode_request(request) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/grpc_web/server/text_coder.rb', line 13

def decode_request(request)
  return request unless BASE64_CONTENT_TYPES.include?(request.content_type)

  # Body can be several base64 "chunks" concatenated together
  base64_chunks = request.body.scan(%r{[a-zA-Z0-9+/]+={0,2}})
  decoded = base64_chunks.map { |chunk| Base64.decode64(chunk) }.join
  ::GRPCWeb::GRPCWebRequest.new(
    request.service, request.service_method, request.content_type, request.accept, decoded,
  )
end

.encode_response(response) ⇒ Object



24
25
26
27
28
29
# File 'lib/grpc_web/server/text_coder.rb', line 24

def encode_response(response)
  return response unless BASE64_CONTENT_TYPES.include?(response.content_type)

  encoded = Base64.strict_encode64(response.body)
  ::GRPCWeb::GRPCWebResponse.new(response.content_type, encoded)
end