use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderValue, WWW_AUTHENTICATE};
use reqwest::{Method, RequestBuilder, Response, StatusCode, Url, redirect};
use secrecy::{ExposeSecret as _, SecretString};
use crate::digest::{Challenge, DigestAuth};
use crate::logging::{self, HideValues, Redact};
use crate::tls::PinnedVerifier;
use crate::{
Arguments, DescriptionCache, DeviceDescription, Error, Fingerprint, Scpd, Service, soap,
};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Debug)]
pub struct Client {
http: reqwest::Client,
verifier: Arc<PinnedVerifier>,
base: Url,
credentials: Option<(String, SecretString)>,
auth: Mutex<Option<DigestAuth>>,
redact: Arc<dyn Redact>,
cache: Option<Arc<dyn DescriptionCache>>,
description: Mutex<Option<Arc<str>>>,
}
#[derive(Debug)]
pub struct ClientBuilder {
base: Url,
credentials: Option<(String, SecretString)>,
timeout: Duration,
pin: Option<Fingerprint>,
redact: Arc<dyn Redact>,
cache: Option<Arc<dyn DescriptionCache>>,
}
impl ClientBuilder {
#[must_use]
pub fn credentials(mut self, username: impl Into<String>, password: SecretString) -> Self {
self.credentials = Some((username.into(), password));
self
}
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
#[must_use]
pub fn pin(mut self, fingerprint: Fingerprint) -> Self {
self.pin = Some(fingerprint);
self
}
#[must_use]
pub fn redact(mut self, redact: Arc<dyn Redact>) -> Self {
self.redact = redact;
self
}
#[must_use]
pub fn cache(mut self, cache: Arc<dyn DescriptionCache>) -> Self {
self.cache = Some(cache);
self
}
pub fn build(self) -> Result<Client, Error> {
match (self.base.scheme(), self.pin) {
("https", Some(_)) | ("http", None) => {}
("https", None) => {
return Err(Error::InvalidUrl(format!(
"{} needs a pinned certificate",
self.base
)));
}
_ => {
return Err(Error::InvalidUrl(format!(
"{} is neither https with a pinned certificate nor http",
self.base
)));
}
}
let verifier = PinnedVerifier::new(self.pin);
let http = reqwest::Client::builder()
.tls_backend_preconfigured(verifier.config())
.timeout(self.timeout)
.redirect(redirect::Policy::none())
.no_proxy()
.build()?;
Ok(Client {
http,
verifier,
base: self.base,
credentials: self.credentials,
auth: Mutex::new(None),
redact: self.redact,
cache: self.cache,
description: Mutex::new(None),
})
}
}
impl Client {
pub fn builder(base: Url) -> ClientBuilder {
ClientBuilder {
base,
credentials: None,
timeout: DEFAULT_TIMEOUT,
pin: None,
redact: Arc::new(HideValues),
cache: None,
}
}
pub async fn get(&self, path: &str) -> Result<String, Error> {
self.get_with_headers(path, &[]).await
}
pub async fn get_with_headers(
&self,
path: &str,
headers: &[(&str, &str)],
) -> Result<String, Error> {
let url = self.url(path)?;
let request = headers
.iter()
.fold(self.http.get(url.clone()), |request, (name, value)| {
request.header(*name, *value)
});
let (http, request) = request.build_split();
let request = RequestBuilder::from_parts(http, request?);
let response = self.send(request, &Method::GET, &url, None).await?;
self.document(response).await
}
pub async fn post_form(&self, path: &str, form: &[(&str, &str)]) -> Result<String, Error> {
let url = self.url(path)?;
let body = form_urlencoded::Serializer::new(String::new())
.extend_pairs(form)
.finish();
let request = self
.http
.post(url.clone())
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(body);
let response = self.send(request, &Method::POST, &url, None).await?;
self.document(response).await
}
pub async fn description(&self) -> Result<DeviceDescription, Error> {
let xml = self.get("/tr64desc.xml").await?;
let description = DeviceDescription::parse(&xml)?;
if self.cache.is_some() {
*lock(&self.description) = Some(xml.into());
}
Ok(description)
}
pub async fn scpd(&self, service: &Service) -> Result<Scpd, Error> {
let url = &service.scpd_url;
let cache = self
.cache
.as_ref()
.and_then(|cache| Some((cache, lock(&self.description).clone()?)));
if let Some((cache, description)) = &cache
&& let Some(scpd) = cache.scpd(description, url)
{
return Ok(scpd);
}
let xml = self.get(url).await?;
let scpd = Scpd::parse(&xml)?;
if let Some((cache, description)) = &cache {
cache.store(description, url, &xml, &scpd);
}
Ok(scpd)
}
pub async fn call(
&self,
service: &Service,
action: &str,
arguments: &[(&str, &str)],
) -> Result<Arguments, Error> {
let url = self.url(&service.control_url)?;
let body = soap::envelope(&service.service_type, action, arguments)?;
if tracing::enabled!(tracing::Level::TRACE) {
let shown =
logging::arguments(&*self.redact, service, action, arguments.iter().copied());
tracing::trace!(action, arguments = %shown, "call");
}
let soap_action = format!("\"{}#{action}\"", service.service_type);
let request = self
.http
.post(url.clone())
.header("SOAPAction", soap_action)
.header(CONTENT_TYPE, r#"text/xml; charset="utf-8""#)
.body(body);
let response = self
.send(request, &Method::POST, &url, Some((service, action)))
.await?;
let out = soap::parse_response(&text(response).await?, action)?;
if tracing::enabled!(tracing::Level::TRACE) {
let shown = logging::arguments(&*self.redact, service, action, out.iter());
tracing::trace!(action, arguments = %shown, "answer");
}
Ok(out)
}
async fn document(&self, response: Response) -> Result<String, Error> {
let body = text(response).await?;
tracing::trace!(bytes = body.len(), "document");
Ok(body)
}
fn url(&self, path: &str) -> Result<Url, Error> {
let url = self
.base
.join(path)
.map_err(|e| Error::InvalidUrl(format!("{path:?}: {e}")))?;
if url.origin() == self.base.origin() {
Ok(url)
} else {
Err(Error::InvalidUrl(format!(
"{url} is not on the device {}",
self.base.origin().ascii_serialization()
)))
}
}
async fn send(
&self,
request: RequestBuilder,
method: &Method,
url: &Url,
call: Option<(&Service, &str)>,
) -> Result<Response, Error> {
let start = Instant::now();
let result = self.authorized(request, method, url).await;
let elapsed_ms = start.elapsed().as_millis();
let action = call.map(|(service, action)| format!("{}#{action}", service.service_type));
let shown = logging::url(url, &*self.redact);
match &result {
Ok(response) => {
let status = response.status().as_u16();
tracing::info!(%method, url = %shown, action, status, elapsed_ms, "request");
}
Err(error) => {
let error = logging::error(error);
tracing::info!(%method, url = %shown, action, %error, elapsed_ms, "request failed");
}
}
let response = result?;
check_status(response, url).await
}
async fn authorized(
&self,
request: RequestBuilder,
method: &Method,
url: &Url,
) -> Result<Response, Error> {
let uri = match url.query() {
Some(query) => format!("{}?{query}", url.path()),
None => url.path().to_owned(),
};
let cached = self.cached_authorization(method, &uri);
if cached.is_some() {
tracing::debug!("answering the previous Digest challenge again");
}
let mut response = self.execute(with_authorization(&request, cached)).await?;
if response.status() == StatusCode::UNAUTHORIZED {
*self.lock() = None;
let (auth, header) = self.answer(&response, method, &uri)?;
response = self
.execute(with_authorization(&request, Some(header)))
.await?;
if response.status() == StatusCode::UNAUTHORIZED {
tracing::debug!("the device rejected the Digest answer, not trying again");
return Err(Error::AuthenticationFailed);
}
tracing::debug!("the device accepted the Digest answer");
*self.lock() = Some(auth);
}
Ok(response)
}
async fn execute(&self, request: RequestBuilder) -> Result<Response, Error> {
request.send().await.map_err(|e| self.verifier.explain(e))
}
fn answer(
&self,
response: &Response,
method: &Method,
uri: &str,
) -> Result<(DigestAuth, String), Error> {
let (username, password) = self
.credentials
.as_ref()
.ok_or(Error::CredentialsRequired)?;
let challenge = challenge(response.headers().get_all(WWW_AUTHENTICATE))?;
tracing::debug!(%challenge, "the device asks for Digest authentication");
let mut auth = DigestAuth::new(challenge);
let header = auth.authorization(username, password.expose_secret(), method.as_str(), uri);
Ok((auth, header))
}
fn cached_authorization(&self, method: &Method, uri: &str) -> Option<String> {
let (username, password) = self.credentials.as_ref()?;
self.lock().as_mut().map(|auth| {
auth.authorization(username, password.expose_secret(), method.as_str(), uri)
})
}
fn lock(&self) -> std::sync::MutexGuard<'_, Option<DigestAuth>> {
lock(&self.auth)
}
}
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
mutex
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn with_authorization(request: &RequestBuilder, authorization: Option<String>) -> RequestBuilder {
let request = request
.try_clone()
.expect("request bodies are held in memory");
match authorization {
Some(value) => request.header(AUTHORIZATION, value),
None => request,
}
}
async fn check_status(response: Response, url: &Url) -> Result<Response, Error> {
let status = response.status();
if status.is_success() {
return Ok(response);
}
let body = text(response).await.unwrap_or_default();
if status == StatusCode::INTERNAL_SERVER_ERROR
&& let Some(fault) = soap::parse_fault(&body)
{
return Err(fault);
}
Err(Error::Status {
status: status.as_u16(),
url: url.to_string(),
})
}
async fn text(mut response: Response) -> Result<String, Error> {
let delimited = response.content_length().is_none();
let mut body = Vec::new();
loop {
match response.chunk().await {
Ok(Some(chunk)) => body.extend_from_slice(&chunk),
Ok(None) => break,
Err(error) if delimited && ends_early(&error) => break,
Err(error) => return Err(error.into()),
}
}
Ok(String::from_utf8_lossy(&body).into_owned())
}
fn ends_early(error: &(dyn std::error::Error + 'static)) -> bool {
std::iter::successors(Some(error), |e| e.source()).any(|e| {
e.downcast_ref::<std::io::Error>()
.is_some_and(|e| e.kind() == std::io::ErrorKind::UnexpectedEof)
})
}
fn challenge<'a>(headers: impl IntoIterator<Item = &'a HeaderValue>) -> Result<Challenge, Error> {
let mut first_error = None;
for header in headers {
match Challenge::parse(&String::from_utf8_lossy(header.as_bytes())) {
Ok(challenge) => return Ok(challenge),
Err(e) => {
first_error.get_or_insert(e);
}
}
}
Err(first_error
.unwrap_or_else(|| Error::UnsupportedAuth("no WWW-Authenticate header".to_owned())))
}