Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rely on Rack::Request, not on env

  • Loading branch information...
commit 0979a20aa9453d0a8fc8ba8f4d27571683782bcb 1 parent fc9c8ce
@DanielVartanov DanielVartanov authored
Showing with 12 additions and 13 deletions.
  1. +10 −11 lib/rack/canonical_host.rb
  2. +2 −2 spec/canonical_host_spec.rb
View
21 lib/rack/canonical_host.rb
@@ -1,6 +1,5 @@
module Rack
class CanonicalHost
-
VERSION = 0.1
def initialize(app, options = {})
@@ -10,18 +9,12 @@ def initialize(app, options = {})
@ignore = options.fetch(:ignore) { [] }
end
-
- def request_host
- @env['HTTP_HOST'].split(':').first
- end
-
def call(env)
- @env = env
- if request_host != @host && !@ignore.include?(request_host)
- uri = URI.parse ''
+ request = rack_request(env)
+
+ if request.host != @host && !@ignore.include?(request.host)
+ uri = URI.parse request.url
uri.host = @host
- uri.query = env['QUERY_STRING'] || ''
- uri.path = env['REQUEST_PATH'] || ''
uri.scheme = @scheme
status = 301
@@ -33,5 +26,11 @@ def call(env)
@app.call(env)
end
end
+
+ protected
+
+ def rack_request(env)
+ Rack::Request.new(env)
+ end
end
end
View
4 spec/canonical_host_spec.rb
@@ -33,7 +33,7 @@
response = Rack::MockRequest.new(app)
.get('/', {
'HTTP_HOST' => 'foo.example.org',
- 'REQUEST_PATH' => '/login'
+ 'PATH_INFO' => '/login'
})
response.headers['Location'].should =~ /\/login/
@@ -43,7 +43,7 @@
response = Rack::MockRequest.new(app)
.get('/', {
'HTTP_HOST' => 'foo.example.org',
- 'REQUEST_PATH' => '/login',
+ 'PATH_INFO' => '/login',
'QUERY_STRING' => 'a=10&b=20'
})
Please sign in to comment.
Something went wrong with that request. Please try again.