Skip to content

Add rack 3 compatibility #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/lamby/rack_alb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ module Lamby
class RackAlb < Lamby::Rack

class << self

def handle?(event)
event.key?('httpMethod') &&
event.dig('requestContext', 'elb')
end

end

def alb?
Expand All @@ -21,7 +19,7 @@ def multi_value?
def response(handler)
hhdrs = handler.headers
if multi_value?
multivalue_headers = hhdrs.transform_values { |v| Array[v].compact.flatten }
multivalue_headers = hhdrs.transform_values { |v| Array(v).compact.flatten }
multivalue_headers['Set-Cookie'] = handler.set_cookies if handler.set_cookies
end
status_description = "#{handler.status} #{::Rack::Utils::HTTP_STATUS_CODES[handler.status]}"
Expand All @@ -30,20 +28,24 @@ def response(handler)
{ multiValueHeaders: multivalue_headers,
statusDescription: status_description,
isBase64Encoded: base64_encode,
body: body }.compact
body: body }.tap do |r|
r[:statusCode] = [handler.status.to_i, 100].max
r[:headers] = r[:headers].transform_keys(&:downcase) if r[:headers]
end.compact
end

private

def env_base
rack_version = defined?(::Rack::VERSION) ? ::Rack::VERSION : ::Rack::RELEASE
{ ::Rack::REQUEST_METHOD => event['httpMethod'],
::Rack::SCRIPT_NAME => '',
::Rack::PATH_INFO => event['path'] || '',
::Rack::QUERY_STRING => query_string,
::Rack::SERVER_NAME => headers['host'],
::Rack::SERVER_PORT => headers['x-forwarded-port'],
::Rack::SERVER_PROTOCOL => 'HTTP/1.1',
::Rack::RACK_VERSION => ::Rack::VERSION,
::Rack::RACK_VERSION => rack_version,
::Rack::RACK_URL_SCHEME => headers['x-forwarded-proto'],
::Rack::RACK_INPUT => StringIO.new(body || ''),
::Rack::RACK_ERRORS => $stderr,
Expand Down Expand Up @@ -76,4 +78,4 @@ def headers_multi
end

end
end
end
26 changes: 13 additions & 13 deletions lib/lamby/rack_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module Lamby
class RackHttp < Lamby::Rack

class << self

def handle?(event)
event.key?('version') &&
( event.dig('requestContext', 'http') ||
event.dig('requestContext', 'httpMethod') )
end

end

def response(handler)
if handler.base64_encodeable?
{ isBase64Encoded: true, body: handler.body64 }
else
super
end.tap do |r|
response = if handler.base64_encodeable?
{ isBase64Encoded: true, body: handler.body64 }
else
super
end

response.tap do |r|
if cookies = handler.set_cookies
if payload_version_one?
r[:multiValueHeaders] ||= {}
Expand All @@ -25,20 +25,24 @@ def response(handler)
r[:cookies] = cookies
end
end
r[:statusCode] = [handler.status.to_i, 100].max
r[:body] ||= handler.body
r[:headers] = r[:headers].transform_keys(&:downcase) if r[:headers]
end
end

private

def env_base
rack_version = defined?(::Rack::VERSION) ? ::Rack::VERSION : ::Rack::RELEASE
{ ::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_VERSION => rack_version,
::Rack::RACK_URL_SCHEME => 'https',
::Rack::RACK_INPUT => StringIO.new(body || ''),
::Rack::RACK_ERRORS => $stderr,
Expand Down Expand Up @@ -68,10 +72,6 @@ def cookies
event['cookies'] || []
end

# 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.
#
def path_info
stage = event.dig('requestContext', 'stage')
spath = event.dig('requestContext', 'http', 'path') || event.dig('requestContext', 'path')
Expand Down Expand Up @@ -101,4 +101,4 @@ def payload_version_one?
end

end
end
end
22 changes: 13 additions & 9 deletions lib/lamby/rack_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@ module Lamby
class RackRest < Lamby::Rack

class << self

def handle?(event)
event.key?('httpMethod')
end

end

def response(handler)
if handler.base64_encodeable?
{ isBase64Encoded: true, body: handler.body64 }
else
super
end.tap do |r|
response = if handler.base64_encodeable?
{ isBase64Encoded: true, body: handler.body64 }
else
super
end

response.tap do |r|
if cookies = handler.set_cookies
r[:multiValueHeaders] ||= {}
r[:multiValueHeaders]['Set-Cookie'] = cookies
end
r[:statusCode] = [handler.status.to_i, 100].max
r[:body] ||= handler.body
r[:headers] = r[:headers].transform_keys(&:downcase) if r[:headers]
end
end

private

def env_base
rack_version = defined?(::Rack::VERSION) ? ::Rack::VERSION : ::Rack::RELEASE
{ ::Rack::REQUEST_METHOD => event['httpMethod'],
::Rack::SCRIPT_NAME => '',
::Rack::PATH_INFO => event['path'] || '',
::Rack::QUERY_STRING => query_string,
::Rack::SERVER_NAME => headers['Host'],
::Rack::SERVER_PORT => headers['X-Forwarded-Port'],
::Rack::SERVER_PROTOCOL => event.dig('requestContext', 'protocol') || 'HTTP/1.1',
::Rack::RACK_VERSION => ::Rack::VERSION,
::Rack::RACK_VERSION => rack_version,
::Rack::RACK_URL_SCHEME => 'https',
::Rack::RACK_INPUT => StringIO.new(body || ''),
::Rack::RACK_ERRORS => $stderr,
Expand All @@ -47,4 +51,4 @@ def env_base
end

end
end
end