From 5f09414f85edfa60ab54ce8b9f8b03874e0670dc Mon Sep 17 00:00:00 2001 From: Sergey Nartimov Date: Tue, 3 Jan 2012 00:55:42 +0300 Subject: deprecate ActiveSupport::Base64 extend and define ::Base64 if needed --- activesupport/lib/active_support/base64.rb | 57 ++++++++++++---------- .../lib/active_support/message_encryptor.rb | 6 +-- .../lib/active_support/message_verifier.rb | 6 +-- activesupport/lib/active_support/xml_mini.rb | 8 +-- 4 files changed, 41 insertions(+), 36 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/base64.rb b/activesupport/lib/active_support/base64.rb index 35014cb3d5..8ffbb77108 100644 --- a/activesupport/lib/active_support/base64.rb +++ b/activesupport/lib/active_support/base64.rb @@ -1,42 +1,47 @@ begin require 'base64' rescue LoadError -end - -module ActiveSupport - if defined? ::Base64 - Base64 = ::Base64 - else - # Base64 provides utility methods for encoding and de-coding binary data - # using a base 64 representation. A base 64 representation of binary data - # consists entirely of printable US-ASCII characters. The Base64 module - # is included in Ruby 1.8, but has been removed in Ruby 1.9. - module Base64 - # Encodes a string to its base 64 representation. Each 60 characters of - # output is separated by a newline character. - # - # ActiveSupport::Base64.encode64("Original unencoded string") - # # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n" - def self.encode64(data) - [data].pack("m") - end + # The Base64 module isn't available in ealier versions of Ruby 1.9. + module Base64 + # Encodes a string to its base 64 representation. Each 60 characters of + # output is separated by a newline character. + # + # ActiveSupport::Base64.encode64("Original unencoded string") + # # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n" + def self.encode64(data) + [data].pack("m") + end - # Decodes a base 64 encoded string to its original representation. - # - # ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==") - # # => "Original unencoded string" - def self.decode64(data) - data.unpack("m").first - end + # Decodes a base 64 encoded string to its original representation. + # + # ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==") + # # => "Original unencoded string" + def self.decode64(data) + data.unpack("m").first end end +end +unless Base64.respond_to?(:strict_encode64) + # Included in Ruby 1.9 + def Base64.strict_encode64(value) + encode64(value).gsub(/\n/, '') + end +end + +module ActiveSupport + Base64 = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('ActiveSupport::Base64', '::Base64') + + # *DEPRECATED*: Use +Base64.strict_encode64+ instead. + # # Encodes the value as base64 without the newline breaks. This makes the base64 encoding readily usable as URL parameters # or memcache keys without further processing. # # ActiveSupport::Base64.encode64s("Original unencoded string") # # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==" def Base64.encode64s(value) + ActiveSupport::Deprecation.warn "encode64s " \ + "is deprecated. Use Base64.strict_encode64 instead", caller encode64(value).gsub(/\n/, '') end end diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb index 9ef2b29580..535eec4485 100644 --- a/activesupport/lib/active_support/message_encryptor.rb +++ b/activesupport/lib/active_support/message_encryptor.rb @@ -1,5 +1,5 @@ require 'openssl' -require 'active_support/base64' +require 'base64' module ActiveSupport # MessageEncryptor is a simple way to encrypt values which get stored somewhere @@ -73,12 +73,12 @@ module ActiveSupport encrypted_data = cipher.update(@serializer.dump(value)) encrypted_data << cipher.final - [encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--") + [encrypted_data, iv].map {|v| ::Base64.strict_encode64(v)}.join("--") end def _decrypt(encrypted_message) cipher = new_cipher - encrypted_data, iv = encrypted_message.split("--").map {|v| ActiveSupport::Base64.decode64(v)} + encrypted_data, iv = encrypted_message.split("--").map {|v| ::Base64.decode64(v)} cipher.decrypt cipher.key = @secret diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index 9d7c81142a..bbb239e194 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -1,4 +1,4 @@ -require 'active_support/base64' +require 'base64' require 'active_support/core_ext/object/blank' module ActiveSupport @@ -42,14 +42,14 @@ module ActiveSupport data, digest = signed_message.split("--") if data.present? && digest.present? && secure_compare(digest, generate_digest(data)) - @serializer.load(ActiveSupport::Base64.decode64(data)) + @serializer.load(::Base64.decode64(data)) else raise InvalidSignature end end def generate(value) - data = ActiveSupport::Base64.encode64s(@serializer.dump(value)) + data = ::Base64.strict_encode64(@serializer.dump(value)) "#{data}--#{generate_digest(data)}" end diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 1ea9a9d7e1..4df3fd0efa 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -48,7 +48,7 @@ module ActiveSupport "symbol" => Proc.new { |symbol| symbol.to_s }, "date" => Proc.new { |date| date.to_s(:db) }, "datetime" => Proc.new { |time| time.xmlschema }, - "binary" => Proc.new { |binary| ActiveSupport::Base64.encode64(binary) }, + "binary" => Proc.new { |binary| ::Base64.encode64(binary) }, "yaml" => Proc.new { |yaml| yaml.to_yaml } } unless defined?(FORMATTING) @@ -64,7 +64,7 @@ module ActiveSupport "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) }, "string" => Proc.new { |string| string.to_s }, "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml }, - "base64Binary" => Proc.new { |bin| ActiveSupport::Base64.decode64(bin) }, + "base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) }, "binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) }, "file" => Proc.new { |file, entity| _parse_file(file, entity) } } @@ -148,14 +148,14 @@ module ActiveSupport def _parse_binary(bin, entity) #:nodoc: case entity['encoding'] when 'base64' - ActiveSupport::Base64.decode64(bin) + ::Base64.decode64(bin) else bin end end def _parse_file(file, entity) - f = StringIO.new(ActiveSupport::Base64.decode64(file)) + f = StringIO.new(::Base64.decode64(file)) f.extend(FileLike) f.original_filename = entity['name'] f.content_type = entity['content_type'] -- cgit v1.2.3