lib/merb/request.rb in merb-0.4.2 vs lib/merb/request.rb in merb-0.5.0

- old
+ new

@@ -326,17 +326,41 @@ def domain(tld_length = 1) host.split('.').last(1 + tld_length).join('.').sub(/:\d+$/,'') end class << self + # Escapes +s+ for use in a URL. + # + # ==== Parameter + # + # +s+ - String to URL escape. + # + def escape(s) + s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { + '%'+$1.unpack('H2'*$1.size).join('%').upcase + }.tr(' ', '+') + end + + # Unescapes a string (i.e., reverse URL escaping). + # + # ==== Parameter + # + # +s+ - String to unescape. + # + def unescape(s) + s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ + [$1.delete('%')].pack('H*') + } + end + # parses a query string or the payload of a POST # request into the params hash. So for example: # /foo?bar=nik&post[title]=heya&post[body]=whatever # parses into: # {:bar => 'nik', :post => {:title => 'heya', :body => 'whatever'}} def query_parse(qs, d = '&;') (qs||'').split(/[#{d}] */n).inject({}) { |h,p| - normalize_params(h, *Mongrel::HttpRequest.unescape(p).split('=',2)) + normalize_params(h, *unescape(p).split('=',2)) }.to_mash end NAME_REGEX = /Content-Disposition:.* name="?([^\";]*)"?/ni.freeze CONTENT_TYPE_REGEX = /Content-Type: (.*)\r\n/ni.freeze