Sha256: e9e0a1578c447af27f08d9fd767ce1408f54e556a1727d93831dae5860f3f411

Contents?: true

Size: 774 Bytes

Versions: 42

Compression:

Stored size: 774 Bytes

Contents

module Rack
  class MethodOverride
    HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS PATCH)

    METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
    HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze

    def initialize(app)
      @app = app
    end

    def call(env)
      if env["REQUEST_METHOD"] == "POST"
        method = method_override(env)
        if HTTP_METHODS.include?(method)
          env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
          env["REQUEST_METHOD"] = method
        end
      end

      @app.call(env)
    end

    def method_override(env)
      req = Request.new(env)
      method = req.POST[METHOD_OVERRIDE_PARAM_KEY] ||
        env[HTTP_METHOD_OVERRIDE_HEADER]
      method.to_s.upcase
    end
  end
end

Version data entries

42 entries across 37 versions & 11 rubygems

Version Path
rack-1.5.0.beta.2 lib/rack/methodoverride.rb
rack-1.5.0.beta.1 lib/rack/methodoverride.rb