开发者

Rails: base64 and character escaping problem

开发者 https://www.devze.com 2023-01-08 23:19 出处:网络
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.

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 &quot;bar&quot; &lt;baz&gt;"
0

精彩评论

暂无评论...
验证码 换一张
取 消