Sha256: 7f199f9a6098799f5f7e54080d7d59e940f39829bad3a265bfe95a43c28d130b

Contents?: true

Size: 1.12 KB

Versions: 23

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

# :markup: markdown

module ActionDispatch
  module Http
    class ContentDisposition # :nodoc:
      def self.format(disposition:, filename:)
        new(disposition: disposition, filename: filename).to_s
      end

      attr_reader :disposition, :filename

      def initialize(disposition:, filename:)
        @disposition = disposition
        @filename = filename
      end

      TRADITIONAL_ESCAPED_CHAR = /[^ A-Za-z0-9!\#$+.^_`|~-]/

      def ascii_filename
        'filename="' + percent_escape(I18n.transliterate(filename), TRADITIONAL_ESCAPED_CHAR) + '"'
      end

      RFC_5987_ESCAPED_CHAR = /[^A-Za-z0-9!\#$&+.^_`|~-]/

      def utf8_filename
        "filename*=UTF-8''" + percent_escape(filename, RFC_5987_ESCAPED_CHAR)
      end

      def to_s
        if filename
          "#{disposition}; #{ascii_filename}; #{utf8_filename}"
        else
          "#{disposition}"
        end
      end

      private
        def percent_escape(string, pattern)
          string.gsub(pattern) do |char|
            char.bytes.map { |byte| "%%%02X" % byte }.join
          end
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
actionpack-8.0.1 lib/action_dispatch/http/content_disposition.rb
actionpack-8.0.0.1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.2.1 lib/action_dispatch/http/content_disposition.rb
actionpack-8.0.0 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.2 lib/action_dispatch/http/content_disposition.rb
actionpack-8.0.0.rc2 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.1.2 lib/action_dispatch/http/content_disposition.rb
actionpack-8.0.0.rc1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.1.1 lib/action_dispatch/http/content_disposition.rb
actionpack-8.0.0.beta1 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha9 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha8 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha7 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha4 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha3 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha2 lib/action_dispatch/http/content_disposition.rb
omg-actionpack-8.0.0.alpha1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.0 lib/action_dispatch/http/content_disposition.rb
actionpack-7.2.0.rc1 lib/action_dispatch/http/content_disposition.rb