class Lamby::RackHttp

Public Class Methods

handle?(event) click to toggle source
# File lib/lamby/rack_http.rb, line 6
def handle?(event)
  event.key?('version') && 
    ( event.dig('requestContext', 'http') || 
      event.dig('requestContext', 'httpMethod') )
end

Public Instance Methods

response(handler) click to toggle source
Calls superclass method Lamby::Rack#response
# File lib/lamby/rack_http.rb, line 14
def response(handler)
  if handler.base64_encodeable?
    { isBase64Encoded: true, body: handler.body64 }
  else
    super
  end.tap do |r|
    if cookies = handler.set_cookies
      if payload_version_one?
        r[:multiValueHeaders] ||= {}
        r[:multiValueHeaders]['Set-Cookie'] = cookies
      else
        r[:cookies] = cookies
      end
    end
  end
end

Private Instance Methods

cookies() click to toggle source
# File lib/lamby/rack_http.rb, line 70
def cookies
  event['cookies'] || []
end
env_base() click to toggle source
# File lib/lamby/rack_http.rb, line 33
def env_base
  { ::Rack::REQUEST_METHOD => request_method,
    ::Rack::SCRIPT_NAME => '',
    ::Rack::PATH_INFO => path_info,
    ::Rack::QUERY_STRING => query_string,
    ::Rack::SERVER_NAME => server_name,
    ::Rack::SERVER_PORT => server_port,
    ::Rack::SERVER_PROTOCOL => server_protocol,
    ::Rack::RACK_VERSION => ::Rack::VERSION,
    ::Rack::RACK_URL_SCHEME => 'https',
    ::Rack::RACK_INPUT => StringIO.new(body || ''),
    ::Rack::RACK_ERRORS => $stderr,
    ::Rack::RACK_MULTITHREAD => false,
    ::Rack::RACK_MULTIPROCESS => false,
    ::Rack::RACK_RUNONCE => false,
    LAMBDA_EVENT => event,
    LAMBDA_CONTEXT => context
  }.tap do |env|
    ct = content_type
    cl = content_length
    env['CONTENT_TYPE'] = ct if ct
    env['CONTENT_LENGTH'] = cl if cl
  end
end
env_headers() click to toggle source
Calls superclass method Lamby::Rack#env_headers
# File lib/lamby/rack_http.rb, line 58
def env_headers
  super.tap do |hdrs|
    if cookies.any?
      hdrs[HTTP_COOKIE] = cookies.join('; ')
    end
  end
end
path_info() click to toggle source

Using custom domain names with v1.0 yields a good `path` parameter sans stage. However, v2.0 and others do not. So we are just going to remove stage no matter waht from other places for both.

# File lib/lamby/rack_http.rb, line 78
def path_info
  stage = event.dig('requestContext', 'stage')
  spath = event.dig('requestContext', 'http', 'path') || event.dig('requestContext', 'path')
  spath.sub /\A\/#{stage}/, ''
end
payload_version_one?() click to toggle source
# File lib/lamby/rack_http.rb, line 101
def payload_version_one?
  event['version'] == '1.0'
end
request_method() click to toggle source
# File lib/lamby/rack_http.rb, line 66
def request_method
  event.dig('requestContext', 'http', 'method') || event['httpMethod']
end
server_name() click to toggle source
# File lib/lamby/rack_http.rb, line 84
def server_name
  headers['x-forwarded-host'] ||
    headers['X-Forwarded-Host'] ||
    headers['host'] ||
    headers['Host']
end
server_port() click to toggle source
# File lib/lamby/rack_http.rb, line 91
def server_port
  headers['x-forwarded-port'] || headers['X-Forwarded-Port']
end
server_protocol() click to toggle source
# File lib/lamby/rack_http.rb, line 95
def server_protocol
  event.dig('requestContext', 'http', 'protocol') ||
    event.dig('requestContext', 'protocol') ||
    'HTTP/1.1'
end