Files
DomiStream/crates/domistream/src/http/mod.rs
T
mnovak 30a7b90034 [domistream] Implementation: HTTP, pairing, RTSP/SDP, ENet, control, input, RTP, FEC, video and audio receivers
Per PROTOCOL.md and the Wolf docs; interop with a real host is unverified. Strict, capped parsers throughout,
shared helpers in util.rs. Where PROTOCOL.md says UNKNOWN the Wolf-documented behaviour is used. The audio
receiver waits for the jitter budget before rebuilding a missing packet, so a late packet beats a rebuilt one.
2026-11-25 14:10:24 +01:00

129 lines
4.7 KiB
Rust

//! The HTTP/HTTPS side of GameStream (`docs/PROTOCOL.md` section 1): request building and strict parsers of the
//! host's XML answers. Pairing has its own module, [`crate::pairing`].
//!
//! DomiStream builds the path and query of each `GET`; the caller sends it over plain HTTP or over TLS (with the
//! client certificate and the pinned host certificate, see [`crate::pairing::ServerCert`]) and hands the response
//! body back to the matching `parse_*` function.
use core::time::Duration;
use crate::Result;
use crate::util::{malformed, percent_encode};
pub mod applist;
pub mod launch;
pub mod serverinfo;
pub mod xml;
pub use applist::{App, parse_app_list};
pub use launch::{LaunchParams, LaunchResponse, parse_launch_response};
pub use serverinfo::{ServerInfo, parse_server_info};
/// Default HTTP port of a GameStream host.
pub const DEFAULT_HTTP_PORT: u16 = 47989;
/// Default HTTPS port of a GameStream host.
pub const DEFAULT_HTTPS_PORT: u16 = 47984;
/// Maximum length of the client's `uniqueid`, in bytes.
pub const MAX_UNIQUE_ID_BYTES: usize = 64;
/// Maximum length of the device name sent while pairing, in bytes.
pub const MAX_DEVICE_NAME_BYTES: usize = 64;
/// Plain HTTP or HTTPS.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Scheme {
/// Plain HTTP (port [`DEFAULT_HTTP_PORT`] unless the host says otherwise).
Http,
/// HTTPS with the client certificate (port from `serverinfo`'s `HttpsPort`).
Https,
}
/// One `GET` request for the caller to send. Always a `GET`; the body is always empty.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HttpRequest {
/// Which port and transport to use.
pub scheme: Scheme,
/// Path and percent-encoded query, e.g. `/serverinfo?uniqueid=0123456789ABCDEF`.
pub path_and_query: String,
/// How long the caller should wait for the answer. `None` = the caller's normal timeout. Pairing step 1 blocks
/// on the host until the PIN is entered, so it asks for a long one.
pub timeout: Option<Duration>,
}
/// Checks a client `uniqueid`: 1..=[`MAX_UNIQUE_ID_BYTES`] of letters, digits, `-` and `_` (our own ids are hex).
pub(crate) fn check_unique_id(unique_id: &str) -> Result<()> {
if unique_id.is_empty()
|| unique_id.len() > MAX_UNIQUE_ID_BYTES
|| !unique_id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
{
return Err(malformed("uniqueid"));
}
Ok(())
}
/// `path?k=v&…` with every value percent-encoded.
pub(crate) fn build(scheme: Scheme, path: &str, params: &[(&str, &str)], timeout: Option<Duration>) -> HttpRequest {
let mut s = String::from(path);
for (i, (k, v)) in params.iter().enumerate() {
s.push(if i == 0 { '?' } else { '&' });
s.push_str(k);
s.push('=');
s.push_str(&percent_encode(v));
}
HttpRequest { scheme, path_and_query: s, timeout }
}
/// Parses `body` as XML after checking it against `max` bytes, and refuses a non-200 status.
pub(crate) fn document(body: &[u8], max: usize, what: &'static str) -> Result<xml::Document> {
crate::util::cap(what, max, body.len())?;
let d = xml::parse(body)?;
if d.status_code != 200 {
return Err(crate::Error::HostStatus { code: d.status_code });
}
Ok(d)
}
/// Builds `GET /serverinfo` for this client.
///
/// # Errors
/// [`crate::Error::Malformed`] for an empty, oversized or non-alphanumeric `unique_id`.
pub fn server_info_request(scheme: Scheme, unique_id: &str) -> Result<HttpRequest> {
check_unique_id(unique_id)?;
Ok(build(scheme, "/serverinfo", &[("uniqueid", unique_id)], None))
}
/// Builds `GET /applist` (HTTPS).
///
/// # Errors
/// As [`server_info_request`].
pub fn app_list_request(unique_id: &str) -> Result<HttpRequest> {
check_unique_id(unique_id)?;
Ok(build(Scheme::Https, "/applist", &[("uniqueid", unique_id)], None))
}
/// Builds `GET /cancel` (HTTPS): quits the app running on the host.
///
/// # Errors
/// As [`server_info_request`].
pub fn cancel_request(unique_id: &str) -> Result<HttpRequest> {
check_unique_id(unique_id)?;
Ok(build(Scheme::Https, "/cancel", &[("uniqueid", unique_id)], None))
}
/// Builds `GET /unpair` (HTTPS): the host forgets this client.
///
/// # Errors
/// As [`server_info_request`].
pub fn unpair_request(unique_id: &str) -> Result<HttpRequest> {
check_unique_id(unique_id)?;
Ok(build(Scheme::Https, "/unpair", &[("uniqueid", unique_id)], None))
}
/// Checks a plain `<root status_code="200"/>` answer (cancel, unpair).
///
/// # Errors
/// [`crate::Error::HostStatus`] for a non-200 status, parser errors as [`xml::parse`].
pub fn parse_status_response(body: &[u8]) -> Result<()> {
document(body, xml::MAX_XML_BYTES, "status answer").map(|_| ())
}