Sha256: 5764b1aa084762adfc093dc2a61b172bb52efbd6095f724737a570402467ce76

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

module Mail
  module Jdec
    module ContentDispositionElementPatch
      def initialize(string)
        if Jdec.enabled?
          # Remove extra trailing semicolon
          string = string.gsub(/;+$/, '')
          # Handles filename=test
          string = string.gsub(/filename\s*=\s*([^"]+?)\s*(;|$)/im) { %Q|filename="#{$1}"#{$2}| }
          # Handles filename=""test""
          string = string.gsub(/filename\s*=\s*"+([^"]+?)"+\s*(;|$)/im) { %Q|filename="#{$1}"#{$2}| }
          # Escape tspecial chars in RFC2231 filename
          string = string.gsub(/filename\*(\d*)(\*?)\s*=\s*(\S+?)'(\S*)'(\S+)(;|$)/i) { %Q|filename*#{$1}#{$2}=#{$3}'#{$4}'#{Escaper.escape($5)}#{$6}| }
          string = string.gsub(/filename\*(\d*)(\*?)\s*=\s*(\S+)(;|$)/i) { %Q|filename*#{$1}#{$2}=#{Escaper.escape($3)}#{$4}| }
        end

        super
      rescue Mail::Field::ParseError => e
        if Jdec.enabled?
          @disposition_type = 'attachment'
          @parameters = ['filename' => Jdec::Decoder.force_utf8(string)]
        else
          raise e
        end
      end
    end

    module Escaper
      def self.escape(str)
        require 'cgi'
        str.gsub(/[#{Regexp.escape(%Q|()<>@,;:\\"/[]?=|)}]/) do |c|
          CGI.escape(c)
        end
      end
    end
  end
end

unless Mail::ContentDispositionElement.included_modules.include?(Mail::Jdec::ContentDispositionElementPatch)
  Mail::ContentDispositionElement.prepend Mail::Jdec::ContentDispositionElementPatch
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mail-jdec-1.0.10 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.9 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.8 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.7 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.6 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.5 lib/mail/jdec/elements/content_disposition_element_patch.rb
mail-jdec-1.0.4 lib/mail/jdec/elements/content_disposition_element_patch.rb