rtflow-rtsp 0.7.0

RTSP message codec, SDP, ranges, authentication and transport for rtflow
Documentation
//! Bounded RTSP/1.0 framing primitives.
use bytes::{Buf, BytesMut};
use rtflow_core::error::{Error, Result};
use std::{collections::BTreeMap, str::FromStr};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Method {
    Options,
    Describe,
    Announce,
    Setup,
    Play,
    Pause,
    Record,
    Teardown,
    GetParameter,
    SetParameter,
    Redirect,
    Unsupported,
}
impl Method {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Options => "OPTIONS",
            Self::Describe => "DESCRIBE",
            Self::Announce => "ANNOUNCE",
            Self::Setup => "SETUP",
            Self::Play => "PLAY",
            Self::Pause => "PAUSE",
            Self::Record => "RECORD",
            Self::Teardown => "TEARDOWN",
            Self::GetParameter => "GET_PARAMETER",
            Self::SetParameter => "SET_PARAMETER",
            Self::Redirect => "REDIRECT",
            Self::Unsupported => "UNSUPPORTED",
        }
    }
}
impl FromStr for Method {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        match s {
            "OPTIONS" => Ok(Self::Options),
            "DESCRIBE" => Ok(Self::Describe),
            "ANNOUNCE" => Ok(Self::Announce),
            "SETUP" => Ok(Self::Setup),
            "PLAY" => Ok(Self::Play),
            "PAUSE" => Ok(Self::Pause),
            "RECORD" => Ok(Self::Record),
            "TEARDOWN" => Ok(Self::Teardown),
            "GET_PARAMETER" => Ok(Self::GetParameter),
            "SET_PARAMETER" => Ok(Self::SetParameter),
            "REDIRECT" => Ok(Self::Redirect),
            _ => Ok(Self::Unsupported),
        }
    }
}
#[derive(Clone, Debug)]
pub struct Request {
    pub method: Method,
    pub uri: String,
    pub headers: BTreeMap<String, String>,
    pub body: Vec<u8>,
}
#[derive(Clone, Debug)]
pub struct Response {
    pub status: u16,
    pub reason: String,
    pub headers: BTreeMap<String, String>,
    pub body: Vec<u8>,
}

impl Request {
    /// Case-insensitive header lookup (parsed keys are lower-case).
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value.as_str())
    }
}

impl Response {
    /// Case-insensitive header lookup (parsed keys are lower-case).
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value.as_str())
    }
}
#[derive(Clone, Debug)]
pub enum Message {
    Request(Request),
    Response(Response),
}
/// Peek the RTSP version token from a complete first line.
///
/// The parser only speaks RTSP/1.0; a server uses this to answer 505 (RTSP
/// Version Not Supported) instead of failing the whole connection when a
/// newer client shows up.
pub fn peek_version(input: &[u8]) -> Option<String> {
    let line_end = input.windows(2).position(|window| window == b"\r\n")?;
    let line = std::str::from_utf8(&input[..line_end]).ok()?;
    let parts: Vec<&str> = line.split_whitespace().collect();
    let version = if parts.first()?.starts_with("RTSP/") {
        parts.first()?
    } else {
        parts.get(2)?
    };
    Some((*version).to_string())
}
pub fn parse(input: &mut BytesMut, max_header: usize, max_body: usize) -> Result<Option<Message>> {
    let Some(pos) = input.windows(4).position(|w| w == b"\r\n\r\n") else {
        if input.len() > max_header {
            return Err(Error::Limit("RTSP header"));
        }
        return Ok(None);
    };
    let end = pos + 4;
    if end > max_header {
        return Err(Error::Limit("RTSP header"));
    }
    let text = std::str::from_utf8(&input[..pos])
        .map_err(|_| Error::Rtsp("header is not UTF-8".into()))?;
    let mut lines = text.split("\r\n");
    let start = lines
        .next()
        .ok_or_else(|| Error::Rtsp("empty message".into()))?
        .to_owned();
    let mut headers = BTreeMap::new();
    for line in lines {
        let (k, v) = line
            .split_once(':')
            .ok_or_else(|| Error::Rtsp("malformed header".into()))?;
        let key = k.trim();
        if key.is_empty()
            || !key
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
            || headers
                .insert(key.to_ascii_lowercase(), v.trim().to_owned())
                .is_some()
        {
            return Err(Error::Rtsp("duplicate or malformed header".into()));
        }
    }
    let len = headers
        .get("content-length")
        .map(|v| {
            v.parse::<usize>()
                .map_err(|_| Error::Rtsp("bad Content-Length".into()))
        })
        .transpose()?
        .unwrap_or(0);
    if len > max_body {
        return Err(Error::Limit("RTSP body"));
    }
    if input.len() < end + len {
        return Ok(None);
    }
    let body = input[end..end + len].to_vec();
    input.advance(end + len);
    let p: Vec<_> = start.split_whitespace().collect();
    if p.first() == Some(&"RTSP/1.0") {
        if p.len() < 3 {
            return Err(Error::Rtsp("bad status line".into()));
        }
        let status = p[1].parse().map_err(|_| Error::Rtsp("bad status".into()))?;
        Ok(Some(Message::Response(Response {
            status,
            reason: p[2..].join(" "),
            headers,
            body,
        })))
    } else {
        if p.len() != 3 || p[2] != "RTSP/1.0" {
            return Err(Error::Rtsp("bad request line".into()));
        }
        Ok(Some(Message::Request(Request {
            method: p[0].parse()?,
            uri: p[1].into(),
            headers,
            body,
        })))
    }
}
/// Current time as an RFC 1123 date (e.g. `Sun, 06 Nov 1994 08:49:37 GMT`).
fn http_date() -> String {
    let secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    http_date_at(secs)
}

fn http_date_at(secs: u64) -> String {
    let days = i64::try_from(secs / 86_400).unwrap_or(0);
    let rem = secs % 86_400;
    let (year, month, day) = civil_from_days(days);
    // 1970-01-01 was a Thursday.
    const WEEKDAYS: [&str; 7] = ["Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"];
    const MONTHS: [&str; 12] = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    let weekday = WEEKDAYS[usize::try_from(days.rem_euclid(7)).unwrap_or(0)];
    let month = MONTHS[usize::try_from(month - 1).unwrap_or(0)];
    format!(
        "{weekday}, {day:02} {month} {year:04} {:02}:{:02}:{:02} GMT",
        rem / 3_600,
        (rem % 3_600) / 60,
        rem % 60
    )
}

/// Proleptic Gregorian date from days since 1970-01-01 (Howard Hinnant).
fn civil_from_days(days: i64) -> (i64, i64, i64) {
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
    let year = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = doy - (153 * mp + 2) / 5 + 1;
    let month = if mp < 10 { mp + 3 } else { mp - 9 };
    (if month <= 2 { year + 1 } else { year }, month, day)
}
pub fn encode_response(r: &Response) -> Vec<u8> {
    let mut out = format!("RTSP/1.0 {} {}\r\n", r.status, r.reason);
    // RFC 2326 ยง12.18: a server that knows the date SHOULD include it; the
    // Server header identifies the implementation (informational, and never
    // overriding a value the caller already set).
    let mut headers = r.headers.clone();
    headers.entry("date".into()).or_insert_with(http_date);
    headers
        .entry("server".into())
        .or_insert_with(|| format!("rtflow/{}", env!("CARGO_PKG_VERSION")));
    for (k, v) in &headers {
        out.push_str(k);
        out.push_str(": ");
        out.push_str(v);
        out.push_str("\r\n")
    }
    if !r.body.is_empty() && !r.headers.contains_key("content-length") {
        out.push_str(&format!("content-length: {}\r\n", r.body.len()))
    }
    out.push_str("\r\n");
    let mut b = out.into_bytes();
    b.extend_from_slice(&r.body);
    b
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn http_date_matches_rfc1123() {
        assert_eq!(
            super::http_date_at(1_796_472_000),
            "Sat, 05 Dec 2026 12:00:00 GMT"
        );
    }

    #[test]
    fn enforces_header_and_body_limits() {
        let mut header = BytesMut::from(&b"OPTIONS rtsp://x/a RTSP/1.0\r\nLong: 123456789\r\n"[..]);
        assert!(matches!(
            parse(&mut header, 16, 16),
            Err(Error::Limit("RTSP header"))
        ));

        let mut body =
            BytesMut::from(&b"ANNOUNCE rtsp://x/a RTSP/1.0\r\nContent-Length: 5\r\n\r\n12345"[..]);
        assert!(matches!(
            parse(&mut body, 1024, 4),
            Err(Error::Limit("RTSP body"))
        ));
    }

    #[test]
    fn rejects_malformed_content_length_without_consuming_partial_data() {
        let wire = b"ANNOUNCE rtsp://x/a RTSP/1.0\r\nContent-Length: nope\r\n\r\n";
        let mut input = BytesMut::from(&wire[..]);
        assert!(parse(&mut input, 1024, 1024).is_err());
        assert_eq!(&input[..], wire);
    }

    #[test]
    fn rejects_duplicate_headers_and_preserves_input() {
        let wire = b"OPTIONS rtsp://x/a RTSP/1.0\r\nCSeq: 1\r\nCSeq: 2\r\n\r\n";
        let mut input = BytesMut::from(&wire[..]);
        assert!(parse(&mut input, 1024, 1024).is_err());
        assert_eq!(&input[..], wire);
    }

    #[test]
    fn preserves_unknown_methods_for_a_501_response() {
        let mut input = BytesMut::from("FROBNICATE rtsp://x/a RTSP/1.0\r\nCSeq: 1\r\n\r\n");
        let Some(Message::Request(request)) = parse(&mut input, 1024, 1024).unwrap() else {
            panic!("expected request");
        };
        assert_eq!(request.method, Method::Unsupported);
    }

    #[test]
    fn incremental() {
        let mut b = BytesMut::from("OPTIONS rtsp://x/a RTSP/1.0\r\nCSeq: 1\r\n\r\n");
        assert!(matches!(
            parse(&mut b, 1000, 1).unwrap(),
            Some(Message::Request(_))
        ));
        assert!(b.is_empty())
    }
}