Class: Tep::Url
- Inherits:
-
Object
- Object
- Tep::Url
- Defined in:
- lib/tep/url.rb
Class Method Summary collapse
-
.escape(s) ⇒ Object
Percent-encode the bytes that are unsafe in cookie values, query strings, and similar contexts.
- .hex_char(n) ⇒ Object
- .hex_nibble(c) ⇒ Object
-
.parse_query(s) ⇒ Object
“a=1&b=2&c” -> Hash “a”=>“1”,“b”=>“2”,“c”=>“”.
-
.split_url(u) ⇒ Object
Split a URL into a Hash with str=>str entries: “scheme” “host” “port” “path” “query”.
-
.unescape(s) ⇒ Object
“%41+b” -> “A b”.
Class Method Details
.escape(s) ⇒ Object
Percent-encode the bytes that are unsafe in cookie values, query strings, and similar contexts. RFC 3986 unreserved set: ALPHA / DIGIT / ‘-._~`. Everything else gets `%XX` (uppercase hex).
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tep/url.rb', line 35 def self.escape(s) out = "" i = 0 while i < s.length c = s[i] if (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c == "-" || c == "." || c == "_" || c == "~" out = out + c else b = c.getbyte(0) hi = b / 16 lo = b % 16 out = out + "%" + Url.hex_char(hi) + Url.hex_char(lo) end i += 1 end out end |
.hex_char(n) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/tep/url.rb', line 55 def self.hex_char(n) if n < 10 return ("0".getbyte(0) + n).chr end ("A".getbyte(0) + n - 10).chr end |
.hex_nibble(c) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tep/url.rb', line 62 def self.hex_nibble(c) if c >= "0" && c <= "9" return c.getbyte(0) - "0".getbyte(0) end if c >= "a" && c <= "f" return c.getbyte(0) - "a".getbyte(0) + 10 end if c >= "A" && c <= "F" return c.getbyte(0) - "A".getbyte(0) + 10 end -1 end |
.parse_query(s) ⇒ Object
“a=1&b=2&c” -> Hash “a”=>“1”,“b”=>“2”,“c”=>“”
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/tep/url.rb', line 140 def self.parse_query(s) h = Tep.str_hash if s.length == 0 return h end pairs = s.split("&") pairs.each do |pair| if pair.length > 0 eq = Tep.str_find(pair, "=", 0) if eq < 0 h[Url.unescape(pair)] = "" else k = pair[0, eq] v = pair[eq + 1, pair.length - eq - 1] h[Url.unescape(k)] = Url.unescape(v) end end end h end |
.split_url(u) ⇒ Object
Split a URL into a Hash with str=>str entries:
"scheme" "host" "port" "path" "query"
Recognises ‘host[:port]/path?query` and the same shape with `https://`. Without a scheme, the input is treated as a path (host stays empty); useful for routing relative paths through the same parser. Default ports follow the scheme: 80 for http, 443 for https. Path defaults to “/”. `query` is the raw substring after `?`, no further decoding.
Inlined as one method on purpose: spinel’s analyzer widens Hash-typed parameters when a helper mutates them and the caller then keeps reading; sticking to a single body keeps ‘out` narrowed to StrStrHash throughout.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tep/url.rb', line 89 def self.split_url(u) out = Tep.str_hash out["scheme"] = "" out["host"] = "" out["port"] = "" out["path"] = "/" out["query"] = "" rest = u if rest.length >= 7 && rest[0, 7] == "http://" out["scheme"] = "http" out["port"] = "80" rest = rest[7, rest.length - 7] elsif rest.length >= 8 && rest[0, 8] == "https://" out["scheme"] = "https" out["port"] = "443" rest = rest[8, rest.length - 8] end if out["scheme"].length > 0 slash = Tep.str_find(rest, "/", 0) hostport = rest tail = "/" if slash >= 0 hostport = rest[0, slash] tail = rest[slash, rest.length - slash] end colon = Tep.str_find(hostport, ":", 0) if colon >= 0 out["host"] = hostport[0, colon] out["port"] = hostport[colon + 1, hostport.length - colon - 1] else out["host"] = hostport end rest = tail end qi = Tep.str_find(rest, "?", 0) if qi >= 0 out["path"] = rest[0, qi] out["query"] = rest[qi + 1, rest.length - qi - 1] else out["path"] = rest end if out["path"].length == 0 out["path"] = "/" end out end |
.unescape(s) ⇒ Object
“%41+b” -> “A b”
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tep/url.rb', line 5 def self.unescape(s) out = "" i = 0 n = s.length while i < n c = s[i] if c == "+" out = out + " " i += 1 elsif c == "%" && i + 2 < n hi = Url.hex_nibble(s[i + 1]) lo = Url.hex_nibble(s[i + 2]) if hi >= 0 && lo >= 0 out = out + ((hi * 16 + lo).chr) i += 3 else out = out + c i += 1 end else out = out + c i += 1 end end out end |