Module: Cybertrain::Query
- Defined in:
- cybertrain/http/query.rb
Overview
Decoding/encoding for query strings and x-www-form-urlencoded bodies: “a=1&b[]=2&b[]=3&post[title]=hi&flag” -> a Params tree.
Class Method Summary collapse
-
.without_key(str, name) ⇒ String
The query string
strwithout itsnameparameters, re-encoded: each other pair is decoded and encoded again (URI.encode_www_form_component, sopost[tags][]comes back aspost%5Btags%5D%5B%5D), in its original order; a pair whose key isnameorname[...]is dropped, empty segments go, and a bareflagcomes back asflag=(the same parameter).
Class Method Details
.without_key(str, name) ⇒ String
The query string str without its name parameters, re-encoded: each
other pair is decoded and encoded again
(URI.encode_www_form_component, so post[tags][] comes back as
post%5Btags%5D%5B%5D), in its original order; a pair whose key is
name or name[...] is dropped, empty segments go, and a bare flag
comes back as flag= (the same parameter). “” when nothing is left. For links
that keep the current query but change one parameter (the paginate
helper’s page). The same scan and limits as parse: utf8! first,
QueryMalformed and QueryTooMany as there.
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'cybertrain/http/query.rb', line 440 def self.without_key(str, name) s = utf8!(str.to_s) len = s.bytesize out = +"" count = 0 pos = 0 while pos < len amp = s.byteindex("&", pos) stop = amp.nil? ? len : amp count += 1 raise QueryTooMany, "too many parameters (limit #{MAX_PAIRS})" if count > MAX_PAIRS if stop > pos pair = s.byteslice(pos, stop - pos).to_s eq = pair.index("=") raw_key = eq.nil? ? pair : pair[0, eq].to_s raw_value = eq.nil? ? "" : pair[(eq + 1)..-1].to_s key = decode_valid(raw_key, true) if split_key(key)[0] != name value = decode_valid(raw_value, true) out << "&" unless out.empty? out << URI.encode_www_form_component(key) << "=" << URI.encode_www_form_component(value) end end pos = stop + 1 end out end |