From e369fb16012c7f29cafa6a1d86839cffb861434f Mon Sep 17 00:00:00 2001 From: Hendrik Beskow Date: Sun, 23 Feb 2014 13:26:00 +0100 Subject: [PATCH 1/5] Proxy Support --- .travis.yml | 9 ++++++++ lib/webmachine/application.rb | 2 ++ lib/webmachine/configuration.rb | 6 ++++-- lib/webmachine/request.rb | 38 +++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) 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/application.rb b/lib/webmachine/application.rb index f486437a..7b7e65ce 100644 --- a/lib/webmachine/application.rb +++ b/lib/webmachine/application.rb @@ -93,6 +93,8 @@ def routes(&block) # @return [Application] self def configure yield configuration if block_given? + Thread.current[:webmachine] ||= {} + Thread.current[:webmachine][:configuration] = configuration self 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..c2a40d31 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -32,6 +32,13 @@ class Request # request, if present def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body + + if (Thread.current[:webmachine] && Thread.current[:webmachine][:configuration] && + Thread.current[:webmachine][:configuration].runs_behind_proxy == true) + + filter_headers + modify_request_uri + end end def_delegators :headers, :[] @@ -164,5 +171,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 Thread.current[:webmachine][: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['x-forwarded-host'] if @headers['x-forwarded-host'] + uri.port = @headers['x-forwarded-port'].to_i if @headers['x-forwarded-port'] + end + + def scheme + if @headers['x-forwarded-https'] == 'on' || @headers['x-forwarded-ssl'] == 'on' + 'https' + elsif @headers['x-forwarded-scheme'] + @headers['x-forwarded-scheme'] + elsif @headers['x-forwarded-proto'] + @headers['x-forwarded-proto'].split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' + else + uri.scheme + end + end + end # class Request end # module Webmachine From 0ef60f9cf710183623f789ad7cda8e2bcbf12ef1 Mon Sep 17 00:00:00 2001 From: Hendrik Beskow Date: Sun, 23 Feb 2014 14:33:16 +0100 Subject: [PATCH 2/5] Spec fixup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Webmachine::Headers[‘foo’] always returns something, even when its not what was requested oO --- lib/webmachine/application.rb | 5 +++-- lib/webmachine/request.rb | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/webmachine/application.rb b/lib/webmachine/application.rb index 7b7e65ce..8a6cdd1f 100644 --- a/lib/webmachine/application.rb +++ b/lib/webmachine/application.rb @@ -45,6 +45,9 @@ def initialize(configuration = Configuration.default, dispatcher = Dispatcher.ne @configuration = configuration @dispatcher = dispatcher + Thread.current[:webmachine] ||= {} + Thread.current[:webmachine][:configuration] = @configuration + yield self if block_given? end @@ -93,8 +96,6 @@ def routes(&block) # @return [Application] self def configure yield configuration if block_given? - Thread.current[:webmachine] ||= {} - Thread.current[:webmachine][:configuration] = configuration self end diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index c2a40d31..ffb6e627 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -34,7 +34,7 @@ def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body if (Thread.current[:webmachine] && Thread.current[:webmachine][:configuration] && - Thread.current[:webmachine][:configuration].runs_behind_proxy == true) + Thread.current[:webmachine][:configuration].runs_behind_proxy == true) filter_headers modify_request_uri @@ -174,10 +174,10 @@ def options? 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| + headers.each_key do |header| if header[0..1] == 'x-' unless Thread.current[:webmachine][:configuration].trusted_headers.include?(header) - @headers.delete(header) + headers.delete(header) end end end @@ -186,17 +186,17 @@ def filter_headers # When running behind a proxy updates the request.uri so that redirects work correctly def modify_request_uri uri.scheme = scheme - uri.host = @headers['x-forwarded-host'] if @headers['x-forwarded-host'] - uri.port = @headers['x-forwarded-port'].to_i if @headers['x-forwarded-port'] + uri.host = headers['x-forwarded-host'] if headers.fetch('x-forwarded-host', nil) + uri.port = headers['x-forwarded-port'].to_i if headers.fetch('x-forwarded-port', nil) end def scheme - if @headers['x-forwarded-https'] == 'on' || @headers['x-forwarded-ssl'] == 'on' + if headers['x-forwarded-https'] == 'on' || headers['x-forwarded-ssl'] == 'on' 'https' - elsif @headers['x-forwarded-scheme'] - @headers['x-forwarded-scheme'] - elsif @headers['x-forwarded-proto'] - @headers['x-forwarded-proto'].split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' + elsif headers.fetch('x-forwarded-scheme', nil) + headers['x-forwarded-scheme'] + elsif headers.fetch('x-forwarded-proto', nil) + headers['x-forwarded-proto'].split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' else uri.scheme end From c72ee37565b3f03bcef2a8dbe16edde643992b65 Mon Sep 17 00:00:00 2001 From: Hendrik Beskow Date: Sun, 23 Feb 2014 20:02:35 +0100 Subject: [PATCH 3/5] Fetching them headers --- lib/webmachine/request.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index ffb6e627..7ccdd45b 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -186,17 +186,17 @@ def filter_headers # When running behind a proxy updates the request.uri so that redirects work correctly def modify_request_uri uri.scheme = scheme - uri.host = headers['x-forwarded-host'] if headers.fetch('x-forwarded-host', nil) - uri.port = headers['x-forwarded-port'].to_i if headers.fetch('x-forwarded-port', nil) + 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['x-forwarded-https'] == 'on' || headers['x-forwarded-ssl'] == 'on' + if headers.fetch('x-forwarded-https', nil) == 'on' || headers.fetch('x-forwarded-ssl', nil) == 'on' 'https' elsif headers.fetch('x-forwarded-scheme', nil) - headers['x-forwarded-scheme'] + headers.fetch('x-forwarded-scheme') elsif headers.fetch('x-forwarded-proto', nil) - headers['x-forwarded-proto'].split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' + headers.fetch('x-forwarded-proto').split(',').any?{|x| x.strip == 'https' } ? 'https' : 'http' else uri.scheme end From 36a6dad0a91184c17171b3a94eb3f01ae54342ff Mon Sep 17 00:00:00 2001 From: Hendrik Beskow Date: Mon, 24 Feb 2014 03:10:04 +0100 Subject: [PATCH 4/5] Use class variables for configuration --- lib/webmachine/application.rb | 8 +++++--- lib/webmachine/request.rb | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/webmachine/application.rb b/lib/webmachine/application.rb index 8a6cdd1f..3936cb3f 100644 --- a/lib/webmachine/application.rb +++ b/lib/webmachine/application.rb @@ -21,6 +21,10 @@ module Webmachine class Application extend Forwardable + class << self + attr_accessor :configuration + end + def_delegators :dispatcher, :add_route # @return [Configuration] the current configuration @@ -45,14 +49,12 @@ def initialize(configuration = Configuration.default, dispatcher = Dispatcher.ne @configuration = configuration @dispatcher = dispatcher - Thread.current[:webmachine] ||= {} - Thread.current[:webmachine][:configuration] = @configuration - yield self if block_given? end # Starts this Application serving requests def run + self.class.configuration = configuration.dup adapter.run end diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index 7ccdd45b..72caa921 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -33,9 +33,7 @@ class Request def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body - if (Thread.current[:webmachine] && Thread.current[:webmachine][:configuration] && - Thread.current[:webmachine][:configuration].runs_behind_proxy == true) - + if (Application.configuration && Application.configuration.runs_behind_proxy == true) filter_headers modify_request_uri end @@ -176,7 +174,7 @@ def options? def filter_headers headers.each_key do |header| if header[0..1] == 'x-' - unless Thread.current[:webmachine][:configuration].trusted_headers.include?(header) + unless Application.configuration.trusted_headers.include?(header) headers.delete(header) end end From eeed58111151192eb0379ef63e35b0355d4680e4 Mon Sep 17 00:00:00 2001 From: Hendrik Beskow Date: Mon, 24 Feb 2014 03:21:04 +0100 Subject: [PATCH 5/5] Store current Application in a instance var --- lib/webmachine/adapter.rb | 3 +++ lib/webmachine/adapters/webrick.rb | 6 ++++-- lib/webmachine/application.rb | 6 +----- lib/webmachine/request.rb | 6 +++--- 4 files changed, 11 insertions(+), 10 deletions(-) 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 3936cb3f..ff59d6b6 100644 --- a/lib/webmachine/application.rb +++ b/lib/webmachine/application.rb @@ -21,10 +21,6 @@ module Webmachine class Application extend Forwardable - class << self - attr_accessor :configuration - end - def_delegators :dispatcher, :add_route # @return [Configuration] the current configuration @@ -54,7 +50,7 @@ def initialize(configuration = Configuration.default, dispatcher = Dispatcher.ne # Starts this Application serving requests def run - self.class.configuration = configuration.dup + adapter.application = self.dup adapter.run end diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index 72caa921..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" @@ -33,7 +33,7 @@ class Request def initialize(method, uri, headers, body) @method, @uri, @headers, @body = method, uri, headers, body - if (Application.configuration && Application.configuration.runs_behind_proxy == true) + if (application && application.configuration.runs_behind_proxy == true) filter_headers modify_request_uri end @@ -174,7 +174,7 @@ def options? def filter_headers headers.each_key do |header| if header[0..1] == 'x-' - unless Application.configuration.trusted_headers.include?(header) + unless application.configuration.trusted_headers.include?(header) headers.delete(header) end end