diff --git a/README.md b/README.md index 8c918f3..1752651 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,21 @@ gem 'api_valve' See the [examples](https://github.com/mkon/api_valve/tree/master/examples) section on how to create & configure your own proxy using this gem. + +### Headers + +By default the following headers are forwarded: + +* `Accept` +* `Content-Type` +* `User-Agent` +* `X-Real-IP` +* `X-Request-Id` + +Additionally these headers are generated: + +* `X-Forwarded-For`: The ApiGateway is added to the list +* `X-Forwarded-Host`: Filled with original request host +* `X-Forwarded-Port`: Filled with original request port +* `X-Forwarded-Prefix`: Filled with the path prefix of the forwarder within the Api Gateway (eg `SCRIPT_NAME` env) +* `X-Forwarded-Proto`: Filled with original request scheme diff --git a/api_valve.gemspec b/api_valve.gemspec index d66dfb5..8381a04 100644 --- a/api_valve.gemspec +++ b/api_valve.gemspec @@ -3,7 +3,7 @@ $LOAD_PATH.push File.expand_path('lib', __dir__) # Describe your gem and declare its dependencies: Gem::Specification.new do |s| s.name = 'api_valve' - s.version = ENV.fetch 'VERSION', '0.7.4' + s.version = ENV.fetch 'VERSION', '0.8.0' s.authors = ['mkon'] s.email = ['konstantin@munteanu.de'] s.homepage = 'https://github.com/mkon/api_valve' diff --git a/lib/api_valve/forwarder/request.rb b/lib/api_valve/forwarder/request.rb index 12551eb..b78622f 100644 --- a/lib/api_valve/forwarder/request.rb +++ b/lib/api_valve/forwarder/request.rb @@ -76,11 +76,12 @@ def permission_handler def forwarded_headers { - 'X-Forwarded-For' => x_forwarded_for, - 'X-Forwarded-Host' => original_request.host, - 'X-Forwarded-Port' => original_request.port.to_s, - 'X-Forwarded-Proto' => original_request.scheme - } + 'X-Forwarded-For' => x_forwarded_for, + 'X-Forwarded-Host' => original_request.host, + 'X-Forwarded-Port' => original_request.port.to_s, + 'X-Forwarded-Prefix' => original_request.env['SCRIPT_NAME'].presence, + 'X-Forwarded-Proto' => original_request.scheme + }.compact end def override_path(options) diff --git a/spec/api_valve/forwarder/request_spec.rb b/spec/api_valve/forwarder/request_spec.rb index 5dc6557..6ffefe5 100644 --- a/spec/api_valve/forwarder/request_spec.rb +++ b/spec/api_valve/forwarder/request_spec.rb @@ -16,7 +16,8 @@ 'HTTP_X_FORWARDED_PROTO' => 'https', 'HTTP_X_REQUEST_ID' => 'http-x-request-id-123', 'HTTP_USER_AGENT' => 'Faraday', - 'HTTP_OTHER_HEADER' => 'Ignored' + 'HTTP_OTHER_HEADER' => 'Ignored', + 'SCRIPT_NAME' => '/some-prefix' } end @@ -31,12 +32,13 @@ it 'exposes the headers correctly' do # rubocop:disable RSpec/ExampleLength expect(headers).to eq( - 'User-Agent' => 'Faraday', - 'X-Forwarded-For' => '212.122.121.211, 10.0.0.21', - 'X-Forwarded-Host' => 'api.example.com', - 'X-Forwarded-Port' => '443', - 'X-Forwarded-Proto' => 'https', - 'X-Request-Id' => 'http-x-request-id-123' + 'User-Agent' => 'Faraday', + 'X-Forwarded-For' => '212.122.121.211, 10.0.0.21', + 'X-Forwarded-Host' => 'api.example.com', + 'X-Forwarded-Port' => '443', + 'X-Forwarded-Prefix' => '/some-prefix', + 'X-Forwarded-Proto' => 'https', + 'X-Request-Id' => 'http-x-request-id-123' ) end end