module GRPCWeb::RackHandler

Placeholder

Constants

ACCEPT_HEADER
INTERNAL_SERVER_ERROR
NOT_FOUND
UNSUPPORTED_MEDIA_TYPE

Public Class Methods

call(service, service_method, env) click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 21
def call(service, service_method, env)
  rack_request = Rack::Request.new(env)
  return not_found_response(rack_request.path) unless rack_request.post?
  return unsupported_media_type_response unless valid_content_types?(rack_request)

  content_type = rack_request.content_type
  accept = rack_request.get_header(ACCEPT_HEADER)
  body = rack_request.body.read
  request = GRPCWeb::GRPCWebRequest.new(service, service_method, content_type, accept, body)
  response = GRPCWeb::GRPCRequestProcessor.process(request)

  [200, { 'Content-Type' => response.content_type }, [response.body]]
rescue Google::Protobuf::ParseError => e
  invalid_response(e.message)
rescue StandardError => e
  ::GRPCWeb.on_error.call(e, service, service_method)
  error_response
end

Private Class Methods

error_response() click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 71
def error_response
  [
    INTERNAL_SERVER_ERROR,
    { 'Content-Type' => 'text/plain' },
    ['Request failed with an unexpected error.'],
  ]
end
invalid_response(message) click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 67
def invalid_response(message)
  [422, { 'Content-Type' => 'text/plain' }, ["Invalid request format: #{message}"]]
end
not_found_response(path) click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 51
def not_found_response(path)
  [
    NOT_FOUND,
    { 'Content-Type' => 'text/plain', 'X-Cascade' => 'pass' },
    ["Not Found: #{path}"],
  ]
end
unsupported_media_type_response() click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 59
def unsupported_media_type_response
  [
    UNSUPPORTED_MEDIA_TYPE,
    { 'Content-Type' => 'text/plain' },
    ['Unsupported Media Type: Invalid Content-Type or Accept header'],
  ]
end
valid_content_types?(rack_request) click to toggle source
# File lib/grpc_web/server/rack_handler.rb, line 42
def valid_content_types?(rack_request)
  return false unless ALL_CONTENT_TYPES.include?(rack_request.content_type)

  accept = rack_request.get_header(ACCEPT_HEADER)
  return true if UNSPECIFIED_CONTENT_TYPES.include?(accept)

  ALL_CONTENT_TYPES.include?(accept)
end