Class: Middleware::EnforceHostname

Inherits:
Object
  • Object
show all
Defined in:
lib/middleware/enforce_hostname.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, settings = nil) ⇒ EnforceHostname

Returns a new instance of EnforceHostname.



5
6
7
# File 'lib/middleware/enforce_hostname.rb', line 5

def initialize(app, settings = nil)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/middleware/enforce_hostname.rb', line 9

def call(env)
  # enforces hostname to match the hostname of our connection
  # this middleware lives after rails multisite so at this point
  # Discourse.current_hostname MUST be canonical, enforce it so
  # all Rails helpers are guaranteed to use it unconditionally and
  # never generate incorrect links
  env[Rack::Request::HTTP_X_FORWARDED_HOST] = nil

  allowed_hostnames = RailsMultisite::ConnectionManagement.current_db_hostnames
  requested_hostname = env[Rack::HTTP_HOST]

  env[Discourse::REQUESTED_HOSTNAME] = requested_hostname
  env[Rack::HTTP_HOST] = allowed_hostnames.find { |h| h == requested_hostname } ||
    Discourse.current_hostname_with_port

  @app.call(env)
end