Class: TsipParser::Uri
- Inherits:
-
Object
- Object
- TsipParser::Uri
- Defined in:
- lib/tsip_parser/uri.rb
Overview
‘TsipParser::Uri` is a TypedData wrapper around a Rust `tsip_parser::Uri`. Scalar accessors (`scheme`, `user`, …) are Rust methods defined by the native extension. This file adds the Ruby-side concerns: memoizing `params` / `headers` so mutations stick, plus serialization / equality.
Constant Summary collapse
- PCT_ESCAPE_CHARS =
Mirror crate 0.2.1’s ‘append_pct_escaped` — used for fields that are pct-decoded on parse (userinfo, URI header key/value). Uppercase hex because re-parse re-decodes, so case doesn’t affect the fixed point.
"@:;?<>%&= \t\r\n"- PARAM_ESCAPE_CHARS =
Mirror crate 0.2.1’s ‘append_param_escaped`. URI-level params are stored literally (no pct-decode on parse), so only bytes that would re-tokenize the URI body on re-parse need escaping. `%` is NOT escaped here: the stored value already contains any `%` the user put in, and escaping `%` would turn `%3c` into `%253c` on re-render. Lowercase hex — on re-parse, `downcase_str` lowercases keys, so matching case reaches a fixed point in one cycle rather than two.
";?&=<>"
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #append_bracket_host(buf) ⇒ Object
- #append_to(buf) ⇒ Object
- #hash ⇒ Object
- #headers ⇒ Object
-
#params ⇒ Object
Materialize the params Hash lazily.
- #to_s ⇒ Object
Class Method Details
.append_param_escaped(buf, src) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tsip_parser/uri.rb', line 102 def self.append_param_escaped(buf, src) src.each_char do |ch| if PARAM_ESCAPE_CHARS.include?(ch) buf << format("%%%02x", ch.ord) else buf << ch end end buf end |
.append_pct_escaped(buf, src) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tsip_parser/uri.rb', line 83 def self.append_pct_escaped(buf, src) src.each_char do |ch| if PCT_ESCAPE_CHARS.include?(ch) buf << format("%%%02X", ch.ord) else buf << ch end end buf end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
113 114 115 116 117 |
# File 'lib/tsip_parser/uri.rb', line 113 def ==(other) other.is_a?(Uri) && scheme == other.scheme && user == other.user && host.downcase == other.host.downcase && port == other.port end |
#append_bracket_host(buf) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/tsip_parser/uri.rb', line 70 def append_bracket_host(buf) h = host if h.include?(":") && !h.start_with?("[") buf << "[" << h << "]" else buf << h end end |
#append_to(buf) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tsip_parser/uri.rb', line 31 def append_to(buf) buf << scheme << ":" u = user if u Uri.append_pct_escaped(buf, u) pw = password if pw buf << ":" Uri.append_pct_escaped(buf, pw) end buf << "@" end append_bracket_host(buf) p = port buf << ":" << p.to_s if p params.each do |k, v| buf << ";" Uri.append_param_escaped(buf, k) vs = v.to_s unless vs.empty? buf << "=" Uri.append_param_escaped(buf, vs) end end h = headers unless h.empty? buf << "?" first = true h.each do |k, v| buf << "&" unless first first = false Uri.append_pct_escaped(buf, k) buf << "=" Uri.append_pct_escaped(buf, v.to_s) end end buf end |
#hash ⇒ Object
119 120 121 |
# File 'lib/tsip_parser/uri.rb', line 119 def hash [scheme, user, host.downcase, port].hash end |
#headers ⇒ Object
17 18 19 20 |
# File 'lib/tsip_parser/uri.rb', line 17 def headers return @headers if defined?(@headers) @headers = _rust_headers end |
#params ⇒ Object
Materialize the params Hash lazily. Once touched, subsequent reads —and any in-place mutations — hit the cached ivar, so tsip-core-style ‘uri.params = “tls”` patterns keep working.
12 13 14 15 |
# File 'lib/tsip_parser/uri.rb', line 12 def params return @params if defined?(@params) @params = _rust_params end |
#to_s ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/tsip_parser/uri.rb', line 22 def to_s # Hot path: nothing has been touched, so the Rust-side `Display` impl # reproduces the canonical string without ever materializing a Hash. return _rust_to_s unless defined?(@params) || defined?(@headers) out = +"" append_to(out) out end |