In my app I need to encode a string via base开发者_C百科64, escape it's possible special characters and put it into a URL.
I do the following:
string = "random_email@server.com"
enc = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC')
enc.encrypt('dummy_salt')
encoded = URI.escape(Base64.encode64(enc.update(string) << enc.final))
The problem is, that somehow URI.escape
do not escape '/' character. That's completely unacceptable if the encoded string is intended to be used as a URL parameter.
How come URI.escape
ignores to escape '/'? Should I user any other .escape
then one, which comes from URI? Or should I even use other encoding method (don't think so)?
Any suggestions as to the code are welcome too.
Use CGI.escape
instead :-)
require 'cgi'
puts CGI.escape('/') # => "%2F"
If you need to escape html you can also do:
CGI::escapeHTML('Usage: foo "bar" <baz>')
"Usage: foo "bar" <baz>"
精彩评论