Class: Jets::Shim::Adapter::Web
- Inherits:
-
Base
- Object
- Base
- Jets::Shim::Adapter::Web
show all
- Defined in:
- lib/jets/shim/adapter/web.rb
Overview
Not named Rack to avoid confusion with the Rack gem
Instance Attribute Summary
Attributes inherited from Base
#context, #event, #target
Instance Method Summary
collapse
Methods inherited from Base
#initialize
#log
Instance Method Details
#base_env ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/jets/shim/adapter/web.rb', line 65
def base_env
{
"CONTENT_LENGTH" => content_length,
"HTTP_COOKIE" => http_cookie,
"lambda.context" => context,
"lambda.event" => event,
"rack.input" => StringIO.new(body || ""),
"REMOTE_ADDR" => remote_addr,
"REMOTE_HOST" => host,
"REQUEST_URI" => request_uri
}
end
|
#body ⇒ Object
Decoding base64 from API Gateaway if necessary Rack will be none the wiser
110
111
112
113
114
115
116
|
# File 'lib/jets/shim/adapter/web.rb', line 110
def body
if event["isBase64Encoded"]
Base64.decode64(event["body"])
else
event["body"]
end
end
|
#content_length ⇒ Object
98
99
100
101
|
# File 'lib/jets/shim/adapter/web.rb', line 98
def content_length
bytesize = body.bytesize.to_s if body
["Content-Length"] || bytesize
end
|
#handle ⇒ Object
7
8
9
10
11
12
|
# File 'lib/jets/shim/adapter/web.rb', line 7
def handle
env = to_rack_env
app = Jets::Shim.config.app
triplet = app.call(env)
translate_response(triplet)
end
|
118
119
120
|
# File 'lib/jets/shim/adapter/web.rb', line 118
def
event["headers"] || {}
end
|
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/jets/shim/adapter/web.rb', line 34
def
env = {}
.each do |k, v|
key = k.tr("-", "_").upcase
http_key = "HTTP_#{key}"
next if http_key == "HTTP_HOST"
if .include?(key)
env[[key]] = v
else
env[http_key] ||= v
end
end
env
end
|
#host ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/jets/shim/adapter/web.rb', line 122
def host
ENV["JETS_SHIM_HOST"] ||
shim_host ||
["Host"] ||
["host"]
end
|
#http_cookie ⇒ Object
81
82
83
|
# File 'lib/jets/shim/adapter/web.rb', line 81
def http_cookie
event["cookies"]&.join("; ")
end
|
#https ⇒ Object
138
139
140
141
142
143
|
# File 'lib/jets/shim/adapter/web.rb', line 138
def https
proto = ["X-Forwarded-Proto"] || ["x-forwarded-proto"]
(proto == "https") ? "on" : "off"
end
|
#remote_addr ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/jets/shim/adapter/web.rb', line 85
def remote_addr
addr = ["X-Forwarded-For"] || ["x-forwarded-for"] || ["REMOTE_ADDR"]
addr.split(",").first.strip if addr
end
|
#request_uri ⇒ Object
103
104
105
106
|
# File 'lib/jets/shim/adapter/web.rb', line 103
def request_uri
"#{path_info}?#{query_string}"
end
|
#shim_host ⇒ Object
Can be added by CloudFront function Also, POST requests have an origin header. IE: Updating a record with CRUD.
133
134
135
136
|
# File 'lib/jets/shim/adapter/web.rb', line 133
def shim_host
return unless ["jets-shim-host"]
URI.parse(["jets-shim-host"]).host
end
|
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/jets/shim/adapter/web.rb', line 52
def
{
"CONTENT_TYPE" => "CONTENT_TYPE",
"CONTENT_LENGTH" => "CONTENT_LENGTH"
}
end
|
#to_rack_env ⇒ Object
14
15
16
17
18
|
# File 'lib/jets/shim/adapter/web.rb', line 14
def to_rack_env
rack_env = base_env.merge(env).merge()
rack_env.delete_if { |k, v| v.nil? }
rack_env
end
|
#translate_response(triplet) ⇒ Object
Translate rack triplet to compatible response for the service
21
22
23
24
25
26
|
# File 'lib/jets/shim/adapter/web.rb', line 21
def translate_response(triplet)
adapter_name = self.class.name.split("::").last
response_class = Jets::Shim::Response.const_get(adapter_name)
response = response_class.new(triplet)
response.translate
end
|