-
Notifications
You must be signed in to change notification settings - Fork 30
/
rack_rest.rb
45 lines (41 loc) 路 1.32 KB
/
rack_rest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Lamby
class RackRest < Lamby::Rack
def response(handler)
if handler.base64_encodeable?
{ isBase64Encoded: true, body: handler.body64 }
else
super
end.tap do |r|
if cookies = handler.set_cookies
r[:multiValueHeaders] ||= {}
r[:multiValueHeaders]['Set-Cookie'] = cookies
end
end
end
private
def env_base
{ ::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_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
end
end