Sha256: b1f8bcfc4742684b57da5a5e9b27cce6083b19d17cc258d72347097f83e23d3f

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

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

    METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
    HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze
    ALLOWED_METHODS = ["POST"]

    def initialize(app)
      @app = app
    end

    def call(env)
      if allowed_methods.include?(env[REQUEST_METHOD])
        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 = method_override_param(req) ||
        env[HTTP_METHOD_OVERRIDE_HEADER]
      begin
        method.to_s.upcase
      rescue ArgumentError
        env["rack.errors"].puts "Invalid string for method"
      end
    end

    private

    def allowed_methods
      ALLOWED_METHODS
    end

    def method_override_param(req)
      req.POST[METHOD_OVERRIDE_PARAM_KEY]
    rescue Utils::InvalidParameterError, Utils::ParameterTypeError
      req.env["rack.errors"].puts "Invalid or incomplete POST params"
    rescue EOFError
      req.env["rack.errors"].puts "Bad request content body"
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rack-1.6.13 lib/rack/methodoverride.rb
rack-1.6.12 lib/rack/methodoverride.rb
rack-1.6.11 lib/rack/methodoverride.rb
mrcooper-logstash-output-azuresearch-0.2.2 vendor/jruby/2.5.0/gems/rack-1.6.10/lib/rack/methodoverride.rb
rack-1.6.10 lib/rack/methodoverride.rb