diff --git a/.travis.yml b/.travis.yml index 90c6ea05..8cd8c686 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,17 @@ rvm: - 1.9.3 - 2.0.0 + - 2.1.0 + - ruby-head - jruby + - jruby-head - rbx + +matrix: + allow_failures: + - rvm: ruby-head + - rvm: jruby-head + bundler_args: --without guard docs before_install: - gem install bundler -v '= 1.5.1' diff --git a/lib/webmachine/adapter.rb b/lib/webmachine/adapter.rb index 626a37e3..c48401ca 100644 --- a/lib/webmachine/adapter.rb +++ b/lib/webmachine/adapter.rb @@ -11,6 +11,9 @@ class Adapter # @return [Webmachine::Dispatcher] the application's dispatcher. attr_reader :dispatcher + # @return [Webmachine::Application] + attr_accessor :application + # @param [Webmachine::Configuration] configuration the application's # configuration. # @param [Webmachine::Dispatcher] dispatcher the application's dispatcher. diff --git a/lib/webmachine/adapters/webrick.rb b/lib/webmachine/adapters/webrick.rb index cc4955f5..1c24d798 100644 --- a/lib/webmachine/adapters/webrick.rb +++ b/lib/webmachine/adapters/webrick.rb @@ -16,7 +16,8 @@ class WEBrick < Adapter def run options = DEFAULT_OPTIONS.merge({ :Port => configuration.port, - :BindAddress => configuration.ip + :BindAddress => configuration.ip, + :application => application }).merge(configuration.adapter_options) @server = Server.new(dispatcher, options) trap("INT") { shutdown } @@ -30,7 +31,7 @@ def shutdown # WEBRick::HTTPServer that is run by the WEBrick adapter. class Server < ::WEBrick::HTTPServer def initialize(dispatcher, options) - @dispatcher = dispatcher + @dispatcher, @application = dispatcher, options[:application] super(options) end @@ -42,6 +43,7 @@ def service(wreq, wres) wreq.request_uri, header, LazyRequestBody.new(wreq)) + request.application = @application response = Webmachine::Response.new @dispatcher.dispatch(request, response) wres.status = response.code.to_i diff --git a/lib/webmachine/application.rb b/lib/webmachine/application.rb index f486437a..ff59d6b6 100644 --- a/lib/webmachine/application.rb +++ b/lib/webmachine/application.rb @@ -50,6 +50,7 @@ def initialize(configuration = Configuration.default, dispatcher = Dispatcher.ne # Starts this Application serving requests def run + adapter.application = self.dup adapter.run end diff --git a/lib/webmachine/configuration.rb b/lib/webmachine/configuration.rb index d6b66ef2..64b3000c 100644 --- a/lib/webmachine/configuration.rb +++ b/lib/webmachine/configuration.rb @@ -8,11 +8,13 @@ module Webmachine # @attr [Fixnum] port the port to bind to, defaults to 8080 # @attr [Symbol] adapter the adapter to use, defaults to :WEBrick # @attr [Hash] adapter_options adapter-specific options, defaults to {} - Configuration = Struct.new(:ip, :port, :adapter, :adapter_options) + # @attr [Boolean] runs_behind_proxy whether Webmachine runs behind a proxy or not, defaults to false + # @attr [Array] trusted_headers a list of trusted headers when running behind a proxy, defaults to [] + Configuration = Struct.new(:ip, :port, :adapter, :adapter_options, :runs_behind_proxy, :trusted_headers) # @return [Configuration] the default configuration def Configuration.default - new("0.0.0.0", 8080, :WEBrick, {}) + new("0.0.0.0", 8080, :WEBrick, {}, false, []) end # Yields the current configuration to the passed block. diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index 9b561c6d..1170be00 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -7,7 +7,7 @@ module Webmachine class Request extend Forwardable attr_reader :method, :uri, :headers, :body - attr_accessor :disp_path, :path_info, :path_tokens + attr_accessor :disp_path, :path_info, :path_tokens, :application GET_METHOD = "GET" HEAD_METHOD = "HEAD" @@ -32,6 +32,11 @@ class Request # request, if present def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body + + if (application && application.configuration.runs_behind_proxy == true) + filter_headers + modify_request_uri + end end def_delegators :headers, :[] @@ -164,5 +169,36 @@ def options? method == OPTIONS_METHOD end + private + # When running behind a proxy Webmachine removes headers which start with x- which aren't trusted + def filter_headers + headers.each_key do |header| + if header[0..1] == 'x-' + unless application.configuration.trusted_headers.include?(header) + headers.delete(header) + end + end + end + end + + # When running behind a proxy updates the request.uri so that redirects work correctly + def modify_request_uri + uri.scheme = scheme + uri.host = headers.fetch('x-forwarded-host') if headers.fetch('x-forwarded-host', nil) + uri.port = headers.fetch('x-forwarded-port').to_i if headers.fetch('x-forwarded-port', nil) + end + + def scheme + if headers.fetch('x-forwarded-https', nil) == 'on' || headers.fetch('x-forwarded-ssl', nil) == 'on' + 'https' + elsif headers.fetch('x-forwarded-scheme', nil) + headers.fetch('x-forwarded-scheme') + elsif headers.fetch('x-forwarded-proto', nil) + headers.fetch('x-forwarded-proto').split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' + else + uri.scheme + end + end + end # class Request end # module Webmachine