Sha256: 5ed9890f1cc44a6e5e1011b5cf6bb9307b47386b20daa0cf197a8fdac8923728

Contents?: true

Size: 598 Bytes

Versions: 1

Compression:

Stored size: 598 Bytes

Contents

module Rack
  module EncodingGuard
    class RejectStrategy < Strategy
      DEFAULT_MESSAGE = 'Bad Request'

      def process(env)
        return bad_request_response if invalid_request?(env)
        yield
      end

      private

      def message
        options.fetch(:with, DEFAULT_MESSAGE)
      end

      def invalid_request?(env)
        Strategy::PROCESSIBLE_KEYS.any? do |key|
          value = env[key].to_s.force_encoding(Encoding::UTF_8)
          !value.valid_encoding?
        end
      end

      def bad_request_response
        [400, {}, [message]]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-encoding_guard-0.1.0 lib/rack/encoding_guard/reject_strategy.rb