Class: Jets::Rack::Request
- Inherits:
-
Object
- Object
- Jets::Rack::Request
- Defined in:
- lib/jets/rack/request.rb
Instance Method Summary collapse
-
#initialize(event, controller) ⇒ Request
constructor
A new instance of Request.
- #process ⇒ Object
-
#set_headers!(request) ⇒ Object
Set request headers.
Constructor Details
#initialize(event, controller) ⇒ Request
Returns a new instance of Request.
5 6 7 8 |
# File 'lib/jets/rack/request.rb', line 5 def initialize(event, controller) @event = event @controller = controller # Jets::Controller instance end |
Instance Method Details
#process ⇒ Object
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 |
# File 'lib/jets/rack/request.rb', line 10 def process http_method = @event['httpMethod'] # GET, POST, PUT, DELETE, etc params = @controller.params(raw: true, path_parameters: false) uri = URI("http://localhost:9292#{@controller.request.path}") # local rack server http = Net::HTTP.new(uri.host, uri.port) # Rails sets _method=patch or _method=put as workaround # Falls back to GET when testing in lambda console http_class = params['_method'] || http_method || 'GET' http_class.capitalize! request_class = "Net::HTTP::#{http_class}".constantize # IE: Net::HTTP::Get request = request_class.new(@controller.request.path) if %w[Post Patch Put].include?(http_class) params = HashConverter.encode(params) request.set_form_data(params) end request = set_headers!(request) # TODO: handle binary response = http.request(request) { status: response.code.to_i, headers: response.each_header.to_h, body: response.body, } end |
#set_headers!(request) ⇒ Object
Set request headers. Forwards original request info from remote API gateway. By this time, the server/api_gateway.rb middleware.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jets/rack/request.rb', line 42 def set_headers!(request) headers = @event['headers'] # from api gateway if headers # remote API Gateway # Forward headers from API Gateway over to the sub http request. # It's important to forward the headers. Here are some examples: # # "Turbolinks-Referrer"=>"http://localhost:8888/posts/122", # "Referer"=>"http://localhost:8888/posts/122", # "Accept-Encoding"=>"gzip, deflate", # "Accept-Language"=>"en-US,en;q=0.9,pt;q=0.8", # "Cookie"=>"_demo_session=...", # "If-None-Match"=>"W/\"9fa479205fc6d24ca826d46f1f6cf461\"", headers.each do |k,v| request[k] = v end # Note by the time headers get to rack later in the they get changed to: # # request['X-Forwarded-Host'] vs env['HTTP_X_FORWARDED_HOST'] # request['X-Forwarded-For'] = headers['X-Forwarded-For'] # "1.1.1.1, 2.2.2.2" # can be comma separated list request['X-Forwarded-Host'] = headers['Host'] # uhghn8z6t1.execute-api.us-east-1.amazonaws.com request['X-Forwarded-Port'] = headers['X-Forwarded-Port'] # 443 request['X-Forwarded-Proto'] = headers['X-Forwarded-Proto'] # https # scheme end request end |