Class: HasGlobalSession::Encoding::Base64Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/has_global_session/encoding.rb

Overview

Implements URL encoding, but without newlines, and using ‘-’ and ‘_’ as the 62nd and 63rd symbol instead of ‘+’ and ‘/’. This makes for encoded values that can be easily stored in a cookie; however, they cannot be used in a URL query string without URL-escaping them since they will contain ‘=’ characters.

This scheme is almost identical to the scheme “Base 64 Encoding with URL and Filename Safe Alphabet,” described in RFC4648, with the exception that this scheme preserves the ‘=’ padding characters due to limitations of Ruby’s built-in base64 encoding routines.

Class Method Summary collapse

Class Method Details

.dump(object) ⇒ Object

Encode a Ruby (ASCII or binary) string.

Parameters

decoded(String)

The raw string to be encoded

Return

encoded(String)

The B64cookie-encoded result.



62
63
64
65
66
67
# File 'lib/has_global_session/encoding.rb', line 62

def self.dump(object)
  raw = [object].pack('m')
  raw.tr!('+/', '-_')
  raw.gsub!("\n", '')
  return raw
end

.load(string) ⇒ Object

Decode a B64cookie-encoded string.

Parameters

encoded(String)

The encoded string

Return

decoded(String)

The decoded result, which may contain nonprintable bytes



50
51
52
53
# File 'lib/has_global_session/encoding.rb', line 50

def self.load(string)
  tr = string.tr('-_', '+/')
  return tr.unpack('m')[0]
end