Skip to content

Commit

Permalink
Add prefix header (#62)
Browse files Browse the repository at this point in the history
* Add prefix header

* rubocop
  • Loading branch information
Konstantin Munteanu authored May 12, 2020
1 parent 38b5c1e commit 20acb4a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion api_valve.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['[email protected]']
s.homepage = 'https://github.com/mkon/api_valve'
Expand Down
11 changes: 6 additions & 5 deletions lib/api_valve/forwarder/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions spec/api_valve/forwarder/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 20acb4a

Please sign in to comment.