aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-localhttp / src / url.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use crate::Error; |
| 17 | use std::fmt; |
| 18 | |
| 19 | /// A parsed `http://` URL: host, port and request target. |
| 20 | /// |
| 21 | /// Only the `http` scheme parses. `https` fails with [`Error::HttpsRefused`], |
| 22 | /// every other scheme with [`Error::UnsupportedScheme`]. |
| 23 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 24 | pub struct Url { |
| 25 | /// Host without IPv6 brackets, lowercased. |
| 26 | host: String, |
| 27 | port: u16, |
| 28 | /// `host[:port]` as sent in the `Host` header (IPv6 keeps its brackets). |
| 29 | authority: String, |
| 30 | /// Path plus query, always starting with `/`. The fragment is dropped. |
| 31 | target: String, |
| 32 | } |
| 33 | |
| 34 | impl Url { |
| 35 | pub fn parse(input: &str) -> Result<Url, Error> { |
| 36 | let invalid = |reason: &'static str| Error::InvalidUrl { |
| 37 | url: input.to_string(), |
| 38 | reason, |
| 39 | }; |
| 40 | let text = input.trim(); |
| 41 | let Some(sep) = text.find("://") else { |
| 42 | return Err(invalid("missing scheme")); |
| 43 | }; |
| 44 | match text[..sep].to_ascii_lowercase().as_str() { |
| 45 | "http" => {} |
| 46 | "https" => { |
| 47 | return Err(Error::HttpsRefused { |
| 48 | url: input.to_string(), |
| 49 | }) |
| 50 | } |
| 51 | _ => { |
| 52 | return Err(Error::UnsupportedScheme { |
| 53 | url: input.to_string(), |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | let rest = &text[sep + 3..]; |
| 58 | let rest = rest.split('#').next().unwrap_or(""); |
| 59 | let (authority, target) = match rest.find(['/', '?']) { |
| 60 | Some(i) => (&rest[..i], &rest[i..]), |
| 61 | None => (rest, ""), |
| 62 | }; |
| 63 | if target.bytes().any(|b| b <= b' ' || b == 0x7f) { |
| 64 | return Err(invalid("space or control character in path or query")); |
| 65 | } |
| 66 | let target = if target.is_empty() { |
| 67 | "/".to_string() |
| 68 | } else if target.starts_with('?') { |
| 69 | format!("/{target}") |
| 70 | } else { |
| 71 | target.to_string() |
| 72 | }; |
| 73 | if authority.is_empty() { |
| 74 | return Err(invalid("missing host")); |
| 75 | } |
| 76 | if authority.contains('@') { |
| 77 | return Err(invalid("credentials in the URL are not supported")); |
| 78 | } |
| 79 | let authority = authority.to_ascii_lowercase(); |
| 80 | let (host, port_text) = if let Some(after_bracket) = authority.strip_prefix('[') { |
| 81 | let Some(end) = after_bracket.find(']') else { |
| 82 | return Err(invalid("unclosed IPv6 bracket")); |
| 83 | }; |
| 84 | let host = &after_bracket[..end]; |
| 85 | let tail = &after_bracket[end + 1..]; |
| 86 | if !host |
| 87 | .bytes() |
| 88 | .all(|b| b.is_ascii_hexdigit() || b == b':' || b == b'.') |
| 89 | { |
| 90 | return Err(invalid("invalid IPv6 host")); |
| 91 | } |
| 92 | match tail.strip_prefix(':') { |
| 93 | Some(port) => (host.to_string(), Some(port)), |
| 94 | None if tail.is_empty() => (host.to_string(), None), |
| 95 | None => return Err(invalid("unexpected text after IPv6 host")), |
| 96 | } |
| 97 | } else { |
| 98 | let (host, port) = match authority.rsplit_once(':') { |
| 99 | Some((h, p)) => (h, Some(p)), |
| 100 | None => (authority.as_str(), None), |
| 101 | }; |
| 102 | if !host |
| 103 | .bytes() |
| 104 | .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_')) |
| 105 | { |
| 106 | return Err(invalid("invalid host")); |
| 107 | } |
| 108 | (host.to_string(), port) |
| 109 | }; |
| 110 | if host.is_empty() { |
| 111 | return Err(invalid("missing host")); |
| 112 | } |
| 113 | let port = match port_text { |
| 114 | None => 80, |
| 115 | Some(p) if !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()) => { |
| 116 | match p.parse::<u16>() { |
| 117 | Ok(0) | Err(_) => return Err(invalid("port out of range")), |
| 118 | Ok(n) => n, |
| 119 | } |
| 120 | } |
| 121 | Some(_) => return Err(invalid("invalid port")), |
| 122 | }; |
| 123 | Ok(Url { |
| 124 | host, |
| 125 | port, |
| 126 | authority, |
| 127 | target, |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | pub fn host(&self) -> &str { |
| 132 | &self.host |
| 133 | } |
| 134 | |
| 135 | pub fn port(&self) -> u16 { |
| 136 | self.port |
| 137 | } |
| 138 | |
| 139 | /// The `Host` header value. |
| 140 | pub fn authority(&self) -> &str { |
| 141 | &self.authority |
| 142 | } |
| 143 | |
| 144 | /// Path plus query as written on the request line. |
| 145 | pub fn target(&self) -> &str { |
| 146 | &self.target |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl fmt::Display for Url { |
| 151 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 152 | write!(f, "http://{}{}", self.authority, self.target) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #[cfg(test)] |
| 157 | mod tests { |
| 158 | use super::*; |
| 159 | |
| 160 | #[test] |
| 161 | fn parses_host_port_path_and_query() { |
| 162 | let u = Url::parse("http://127.0.0.1:18080/api/cortex/recall?q=a&limit=5").unwrap(); |
| 163 | assert_eq!(u.host(), "127.0.0.1"); |
| 164 | assert_eq!(u.port(), 18080); |
| 165 | assert_eq!(u.authority(), "127.0.0.1:18080"); |
| 166 | assert_eq!(u.target(), "/api/cortex/recall?q=a&limit=5"); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn defaults_port_80_and_root_path() { |
| 171 | let u = Url::parse("http://localhost").unwrap(); |
| 172 | assert_eq!(u.port(), 80); |
| 173 | assert_eq!(u.target(), "/"); |
| 174 | assert_eq!(u.authority(), "localhost"); |
| 175 | let q = Url::parse("http://localhost?x=1").unwrap(); |
| 176 | assert_eq!(q.target(), "/?x=1"); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn scheme_is_case_insensitive_and_fragment_dropped() { |
| 181 | let u = Url::parse("HTTP://Example.LAN:8080/a#frag").unwrap(); |
| 182 | assert_eq!(u.host(), "example.lan"); |
| 183 | assert_eq!(u.target(), "/a"); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn ipv6_literal_keeps_brackets_in_authority() { |
| 188 | let u = Url::parse("http://[::1]:9000/h").unwrap(); |
| 189 | assert_eq!(u.host(), "::1"); |
| 190 | assert_eq!(u.authority(), "[::1]:9000"); |
| 191 | assert_eq!(u.port(), 9000); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn https_is_refused_in_any_case() { |
| 196 | for s in [ |
| 197 | "https://127.0.0.1/", |
| 198 | "HTTPS://localhost:443/x", |
| 199 | " https://a/ ", |
| 200 | ] { |
| 201 | assert!( |
| 202 | matches!(Url::parse(s), Err(Error::HttpsRefused { .. })), |
| 203 | "{s}" |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn other_schemes_and_bad_urls_are_refused() { |
| 210 | assert!(matches!( |
| 211 | Url::parse("ftp://host/"), |
| 212 | Err(Error::UnsupportedScheme { .. }) |
| 213 | )); |
| 214 | for bad in [ |
| 215 | "127.0.0.1:80/x", |
| 216 | "http://", |
| 217 | "http:///path", |
| 218 | "http://user:pw@host/", |
| 219 | "http://host:0/", |
| 220 | "http://host:65536/", |
| 221 | "http://host:/", |
| 222 | "http://host:abc/", |
| 223 | "http://ho st/", |
| 224 | "http://host/a b", |
| 225 | "http://host/a\r\nX: y", |
| 226 | "http://[::1/", |
| 227 | "http://[::1]x/", |
| 228 | ] { |
| 229 | assert!( |
| 230 | matches!(Url::parse(bad), Err(Error::InvalidUrl { .. })), |
| 231 | "{bad} should be InvalidUrl, got {:?}", |
| 232 | Url::parse(bad) |
| 233 | ); |
| 234 | } |
| 235 | } |
| 236 | } |