Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Last active November 12, 2020 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmolesUC/6775b738b229cc5cf977ae71ea1ece5a to your computer and use it in GitHub Desktop.
Save dmolesUC/6775b738b229cc5cf977ae71ea1ece5a to your computer and use it in GitHub Desktop.
Escaping URIs in Ruby
#!/usr/bin/env ruby
# coding: utf-8
require 'addressable'
require 'cgi'
require 'erb'
require 'uri'
# base_url = 'https://user:password !$&' + "'" + '()*+,;=@host.domain/path:?#@-.+_~/ path!$&' + "'" + '()*+,;=.cgi'
# query = 'query1.-_~ *+,;=value1.-_~ *+'
# fragment = '#fragment !$&' + "'" + '()*+,;=/'
base_url = 'https://username:p $$w!rd@some-host.some-domain.org/path; ?#+/file;(*+=).cgi'
query = 'key~1=¿ val+ue;?&key_2=val#ue'
fragment = 'some (&rela#tive?) anchor!'
full_url = base_url + '?' + query + '#' + fragment
puts 'Base URL: ' + base_url
puts 'URI.escape(): ' + URI.escape(base_url)
puts 'Addressable::URI.escape(): ' + Addressable::URI.escape(base_url)
puts 'ERB::Util.url_encode(): ' + ERB::Util.url_encode(base_url)
puts 'CGI.escape(): ' + CGI.escape(base_url)
puts 'URI.encode_www_form_component(): ' + URI.encode_www_form_component(base_url)
puts "\n"
puts 'Query string: ' + query
puts 'URI.escape(): ' + URI.escape(query)
puts 'Addressable::URI.escape(): ' + Addressable::URI.escape(query)
puts 'ERB::Util.url_encode(): ' + ERB::Util.url_encode(query)
puts 'CGI.escape(): ' + CGI.escape(query)
puts 'URI.encode_www_form_component(): ' + URI.encode_www_form_component(query)
puts "\n"
puts 'Fragment: ' + fragment
puts 'URI.escape(): ' + URI.escape(fragment)
puts 'Addressable::URI.escape(): ' + Addressable::URI.escape(fragment)
puts 'ERB::Util.url_encode(): ' + ERB::Util.url_encode(fragment)
puts 'CGI.escape(): ' + CGI.escape(fragment)
puts 'URI.encode_www_form_component(): ' + URI.encode_www_form_component(fragment)
puts "\n"
puts 'Full URL: ' + full_url
puts 'URI.escape(): ' + URI.escape(full_url)
puts 'Addressable::URI.escape(): ' + Addressable::URI.escape(full_url)
puts 'ERB::Util.url_encode(): ' + ERB::Util.url_encode(full_url)
puts 'CGI.escape(): ' + CGI.escape(full_url)
puts 'URI.encode_www_form_component(): ' + URI.encode_www_form_component(full_url)
puts "\n"
puts 'Full URL (separate parts): ' + base_url + ' ? ' + query + ' # ' + fragment
puts 'URI.escape(): ' + URI.escape(base_url) + '?' + URI.escape(query) + '#' + URI.escape(fragment)
puts 'Addressable::URI.escape(): ' + Addressable::URI.escape(base_url) + '?' + Addressable::URI.escape(query) + '#' + Addressable::URI.escape(fragment)
puts 'ERB::Util.url_encode(): ' + ERB::Util.url_encode(base_url) + '?' + ERB::Util.url_encode(query) + '#' + ERB::Util.url_encode(fragment)
puts 'CGI.escape(): ' + CGI.escape(base_url) + '?' + CGI.escape(query) + '#' + CGI.escape(fragment)
puts 'URI.encode_www_form_component(): ' + URI.encode_www_form_component(base_url) + '?' + URI.encode_www_form_component(query) + '#' + URI.encode_www_form_component(fragment)
# Base URL: https://username:p $$w!rd@some-host.some-domain.org/path; ?#+/file;(*+=).cgi
# URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?%23+/file;(*+=).cgi
# Addressable::URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?#+/file;(*+=).cgi
# ERB::Util.url_encode(): https%3A%2F%2Fusername%3Ap%20%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B%20%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi
# CGI.escape(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi
# URI.encode_www_form_component(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28*%2B%3D%29.cgi
#
# Query string: key~1=¿ val+ue;?&key_2=val#ue
# URI.escape(): key~1=%C2%BF%20val+ue;?&key_2=val%23ue
# Addressable::URI.escape(): key~1=%C2%BF%20val+ue;?&key_2=val#ue
# ERB::Util.url_encode(): key~1%3D%C2%BF%20val%2Bue%3B%3F%26key_2%3Dval%23ue
# CGI.escape(): key~1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue
# URI.encode_www_form_component(): key%7E1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue
#
# Fragment: some (&rela#tive?) anchor!
# URI.escape(): some%20(&rela%23tive?)%20anchor!
# Addressable::URI.escape(): some%20(&rela#tive?)%20anchor!
# ERB::Util.url_encode(): some%20%28%26rela%23tive%3F%29%20anchor%21
# CGI.escape(): some+%28%26rela%23tive%3F%29+anchor%21
# URI.encode_www_form_component(): some+%28%26rela%23tive%3F%29+anchor%21
#
# Full URL: https://username:p $$w!rd@some-host.some-domain.org/path; ?#+/file;(*+=).cgi?key~1=¿ val+ue;?&key_2=val#ue#some (&rela#tive?) anchor!
# URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?%23+/file;(*+=).cgi?key~1=%C2%BF%20val+ue;?&key_2=val%23ue%23some%20(&rela%23tive?)%20anchor!
# Addressable::URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?#+/file;(*+=).cgi?key~1=%C2%BF%20val+ue;?&key_2=val%23ue%23some%20(&rela%23tive?)%20anchor!
# ERB::Util.url_encode(): https%3A%2F%2Fusername%3Ap%20%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B%20%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi%3Fkey~1%3D%C2%BF%20val%2Bue%3B%3F%26key_2%3Dval%23ue%23some%20%28%26rela%23tive%3F%29%20anchor%21
# CGI.escape(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi%3Fkey~1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue%23some+%28%26rela%23tive%3F%29+anchor%21
# URI.encode_www_form_component(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28*%2B%3D%29.cgi%3Fkey%7E1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue%23some+%28%26rela%23tive%3F%29+anchor%21
#
# Full URL (separate parts): https://username:p $$w!rd@some-host.some-domain.org/path; ?#+/file;(*+=).cgi ? key~1=¿ val+ue;?&key_2=val#ue # some (&rela#tive?) anchor!
# URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?%23+/file;(*+=).cgi?key~1=%C2%BF%20val+ue;?&key_2=val%23ue#some%20(&rela%23tive?)%20anchor!
# Addressable::URI.escape(): https://username:p%20$$w!rd@some-host.some-domain.org/path;%20?#+/file;(*+=).cgi?key~1=%C2%BF%20val+ue;?&key_2=val#ue#some%20(&rela#tive?)%20anchor!
# ERB::Util.url_encode(): https%3A%2F%2Fusername%3Ap%20%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B%20%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi?key~1%3D%C2%BF%20val%2Bue%3B%3F%26key_2%3Dval%23ue#some%20%28%26rela%23tive%3F%29%20anchor%21
# CGI.escape(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28%2A%2B%3D%29.cgi?key~1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue#some+%28%26rela%23tive%3F%29+anchor%21
# URI.encode_www_form_component(): https%3A%2F%2Fusername%3Ap+%24%24w%21rd%40some-host.some-domain.org%2Fpath%3B+%3F%23%2B%2Ffile%3B%28*%2B%3D%29.cgi?key%7E1%3D%C2%BF+val%2Bue%3B%3F%26key_2%3Dval%23ue#some+%28%26rela%23tive%3F%29+anchor%21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment