Sha256: aad6cac2726dd90ada3b8725dee9fbf210003271eb11f18e31466c5c5fa8a2e5

Contents?: true

Size: 1.16 KB

Versions: 17

Compression:

Stored size: 1.16 KB

Contents

#
# This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
#

module Ballast
  # A list of useful middlewares for Rack.
  module Middlewares
    # Converts the host of an IP based request to the default host.
    class DefaultHost
      # Creates a new middleware instance.
      #
      # @param app [Object] A Rack application.
      # @param path [String] The path of a YAML file with default hosts definition per environment.
      def initialize(app, path)
        @app = app
        @hosts = YAML.load_file(path)
      end

      # Executes the middleware.
      #
      # @param env [Hash] A Rack environment.
      def call(env)
        old_host = env["SERVER_NAME"].ensure_string
        new_host = @hosts[ENV["RACK_ENV"]]

        if old_host =~ /^\d/ && new_host then
          env["ORIG_SERVER_NAME"] = old_host
          env["ORIG_HTTP_HOST"] = env["HTTP_HOST"].dup
          env["SERVER_NAME"] = new_host
          env["HTTP_HOST"].gsub!(old_host, new_host)
        end

        @app.call(env)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ballast-1.9.3 lib/ballast/middlewares/default_host.rb
ballast-1.9.2 lib/ballast/middlewares/default_host.rb
ballast-1.9.1 lib/ballast/middlewares/default_host.rb
ballast-1.9.0 lib/ballast/middlewares/default_host.rb
ballast-1.8.0 lib/ballast/middlewares/default_host.rb
ballast-1.7.0 lib/ballast/middlewares/default_host.rb
ballast-1.6.0 lib/ballast/middlewares/default_host.rb
ballast-1.5.3 lib/ballast/middlewares/default_host.rb
ballast-1.5.2 lib/ballast/middlewares/default_host.rb
ballast-1.5.1 lib/ballast/middlewares/default_host.rb
ballast-1.5.0 lib/ballast/middlewares/default_host.rb
ballast-1.4.0 lib/ballast/middlewares/default_host.rb
ballast-1.3.0 lib/ballast/middlewares/default_host.rb
ballast-1.2.0 lib/ballast/middlewares/default_host.rb
ballast-1.1.2 lib/ballast/middlewares/default_host.rb
ballast-1.1.1 lib/ballast/middlewares/default_host.rb
ballast-1.1.0 lib/ballast/middlewares/default_host.rb