1
2
3
4
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Minimal `rtsp://` URL parser.
//!
//! We don't depend on a general URL crate here because RTSP URLs have a few
//! quirks (no fragment, path is opaque to the protocol) and we need to be
//! able to strip credentials and resolve `a=control:` relative URIs.
use crate::error::NetError;
/// Parsed RTSP URL.
#[derive(Debug, Clone)]
pub struct RtspUrl {
/// Scheme; always `rtsp` for now.
pub scheme: String,
/// Optional `user:pass` extracted from the userinfo section.
pub userinfo: Option<(String, String)>,
/// Host portion (no port).
pub host: String,
/// Port; defaults to 554.
pub port: u16,
/// Path including any leading `/` (may be empty).
pub path: String,
/// Query string after `?` (without the `?`), or empty.
pub query: String,
}
impl RtspUrl {
/// Parse a full `rtsp://[user[:pass]@]host[:port]/path[?query]`.
///
/// # Errors
///
/// Returns [`NetError::InvalidUrl`] if the scheme is not `rtsp://`,
/// if an IPv6 literal is missing its closing `]`, or if the port
/// is not a valid `u16`.
///
/// # Example
///
/// ```
/// use oximedia_net::rtsp::RtspUrl;
///
/// let u = RtspUrl::parse("rtsp://admin:secret@cam.local:8554/live").unwrap();
/// assert_eq!(u.host, "cam.local");
/// assert_eq!(u.port, 8554);
/// assert_eq!(
/// u.userinfo,
/// Some(("admin".to_string(), "secret".to_string()))
/// );
/// ```
pub fn parse(input: &str) -> Result<Self, NetError> {
let rest = input.strip_prefix("rtsp://").ok_or_else(|| {
NetError::InvalidUrl(format!("expected rtsp:// scheme, got {input:?}"))
})?;
let (authority, path_query) = match rest.find('/') {
Some(idx) => (&rest[..idx], &rest[idx..]),
None => (rest, ""),
};
let (userinfo, host_port) = match authority.rfind('@') {
Some(idx) => {
let (u, _) = authority.split_at(idx);
let creds = match u.split_once(':') {
Some((user, pass)) => (user.to_string(), pass.to_string()),
None => (u.to_string(), String::new()),
};
(Some(creds), &authority[idx + 1..])
}
None => (None, authority),
};
let (host, port) = if let Some(stripped) = host_port.strip_prefix('[') {
// IPv6 literal: `[2001:db8::1]:554`
let end = stripped
.find(']')
.ok_or_else(|| NetError::InvalidUrl("missing ] in IPv6 literal".into()))?;
let host = stripped[..end].to_string();
let port = match stripped[end + 1..].strip_prefix(':') {
Some(p) => p
.parse::<u16>()
.map_err(|e| NetError::InvalidUrl(format!("bad port: {e}")))?,
None => 554,
};
(host, port)
} else {
match host_port.rsplit_once(':') {
Some((h, p)) => (
h.to_string(),
p.parse::<u16>()
.map_err(|e| NetError::InvalidUrl(format!("bad port: {e}")))?,
),
None => (host_port.to_string(), 554),
}
};
let (path, query) = match path_query.split_once('?') {
Some((p, q)) => (p.to_string(), q.to_string()),
None => (path_query.to_string(), String::new()),
};
Ok(Self {
scheme: "rtsp".into(),
userinfo,
host,
port,
path,
query,
})
}
/// URL with userinfo stripped, suitable for the request-URI on the wire.
///
/// # Example
///
/// ```
/// use oximedia_net::rtsp::RtspUrl;
///
/// let u = RtspUrl::parse("rtsp://admin:secret@cam/live").unwrap();
/// // Credentials must not leak onto the wire.
/// assert_eq!(u.request_uri(), "rtsp://cam/live");
/// ```
#[must_use]
pub fn request_uri(&self) -> String {
let mut out = format!("rtsp://{}", self.host);
if self.port != 554 {
out.push_str(&format!(":{}", self.port));
}
if self.path.is_empty() {
out.push('/');
} else {
out.push_str(&self.path);
}
if !self.query.is_empty() {
out.push('?');
out.push_str(&self.query);
}
out
}
/// Authority portion `host:port` for `connect()`.
///
/// # Example
///
/// ```
/// use oximedia_net::rtsp::RtspUrl;
/// let u = RtspUrl::parse("rtsp://10.0.0.5:8554/s").unwrap();
/// assert_eq!(u.authority(), "10.0.0.5:8554");
/// ```
#[must_use]
pub fn authority(&self) -> String {
format!("{}:{}", self.host, self.port)
}
/// Resolve an `a=control:` value against this URL.
///
/// Rules per RFC 2326 §C.1.1:
/// - `*` → use the base URL as-is.
/// - absolute `rtsp://` → use as-is.
/// - anything else → append to the base, using `/` between unless the
/// base already ends in one.
///
/// # Example
///
/// ```
/// use oximedia_net::rtsp::RtspUrl;
///
/// let u = RtspUrl::parse("rtsp://cam/live").unwrap();
/// assert_eq!(u.resolve_control("trackID=1"), "rtsp://cam/live/trackID=1");
/// assert_eq!(u.resolve_control("*"), "rtsp://cam/live");
/// assert_eq!(
/// u.resolve_control("rtsp://other/t"),
/// "rtsp://other/t"
/// );
/// ```
#[must_use]
pub fn resolve_control(&self, control: &str) -> String {
if control == "*" {
return self.request_uri();
}
if control.starts_with("rtsp://") || control.starts_with("rtsps://") {
return control.to_string();
}
let base = self.request_uri();
if base.ends_with('/') || control.starts_with('/') {
format!("{base}{control}")
} else {
format!("{base}/{control}")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_plain_url() {
let u = RtspUrl::parse("rtsp://camera.local/live").unwrap();
assert_eq!(u.host, "camera.local");
assert_eq!(u.port, 554);
assert_eq!(u.path, "/live");
assert!(u.userinfo.is_none());
}
#[test]
fn parses_url_with_port() {
let u = RtspUrl::parse("rtsp://10.0.0.5:8554/stream1").unwrap();
assert_eq!(u.port, 8554);
}
#[test]
fn parses_url_with_credentials() {
let u = RtspUrl::parse("rtsp://admin:hunter2@camera/live").unwrap();
assert_eq!(
u.userinfo,
Some(("admin".to_string(), "hunter2".to_string()))
);
// request_uri must NOT leak credentials onto the wire.
assert_eq!(u.request_uri(), "rtsp://camera/live");
}
#[test]
fn parses_query_string() {
let u = RtspUrl::parse("rtsp://h/path?token=abc").unwrap();
assert_eq!(u.path, "/path");
assert_eq!(u.query, "token=abc");
assert_eq!(u.request_uri(), "rtsp://h/path?token=abc");
}
#[test]
fn parses_ipv6_literal() {
let u = RtspUrl::parse("rtsp://[2001:db8::1]:554/s").unwrap();
assert_eq!(u.host, "2001:db8::1");
assert_eq!(u.port, 554);
}
#[test]
fn resolves_absolute_control() {
let u = RtspUrl::parse("rtsp://c/path").unwrap();
assert_eq!(
u.resolve_control("rtsp://other/track1"),
"rtsp://other/track1"
);
}
#[test]
fn resolves_star_control() {
let u = RtspUrl::parse("rtsp://c/path").unwrap();
assert_eq!(u.resolve_control("*"), "rtsp://c/path");
}
#[test]
fn resolves_relative_control() {
let u = RtspUrl::parse("rtsp://c/path").unwrap();
assert_eq!(u.resolve_control("trackID=1"), "rtsp://c/path/trackID=1");
}
#[test]
fn rejects_non_rtsp_scheme() {
assert!(RtspUrl::parse("http://x/y").is_err());
}
}