diff --git a/sdk/src/error.rs b/sdk/src/error.rs index 6abedfc07..49d4892ff 100644 --- a/sdk/src/error.rs +++ b/sdk/src/error.rs @@ -191,10 +191,10 @@ pub enum Error { #[error("required JUMBF box not found")] JumbfBoxNotFound, - #[error("could not fetch the remote manifest")] + #[error("could not fetch the remote manifest {0}")] RemoteManifestFetch(String), - #[error("must fetch remote manifests from url")] + #[error("must fetch remote manifests from url {0}")] RemoteManifestUrl(String), #[error("stopped because of logged error")] diff --git a/sdk/src/store.rs b/sdk/src/store.rs index 01e3d8b5d..fb8a9d546 100644 --- a/sdk/src/store.rs +++ b/sdk/src/store.rs @@ -3276,14 +3276,25 @@ impl Store { }; //const MANIFEST_CONTENT_TYPE: &str = "application/x-c2pa-manifest-store"; // todo verify once these are served - //const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB + const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB let parsed_url = Url::parse(url) .map_err(|e| Error::RemoteManifestFetch(format!("invalid URL: {}", e)))?; - let path_with_query = parsed_url[url::Position::BeforeHost..].to_string(); + let authority = parsed_url.authority(); + let path_with_query = parsed_url[url::Position::AfterPort..].to_string(); + let scheme = match parsed_url.scheme() { + "http" => Scheme::Http, + "https" => Scheme::Https, + _ => { + return Err(Error::RemoteManifestFetch( + "unsupported URL scheme".to_string(), + )) + } + }; let request = OutgoingRequest::new(Fields::new()); request.set_path_with_query(Some(&path_with_query)).unwrap(); - request.set_scheme(Some(&Scheme::Https)).unwrap(); + request.set_authority(Some(&authority)).unwrap(); + request.set_scheme(Some(&scheme)).unwrap(); match outgoing_handler::handle(request, None) { Ok(resp) => { resp.subscribe().block(); @@ -3293,30 +3304,14 @@ impl Store { .expect("HTTP request response requested more than once") .expect("HTTP request failed"); if response.status() == 200 { - let raw_header = response.headers().get("Content-Length"); - if raw_header.first().map(|val| val.is_empty()).unwrap_or(true) { - return Err(Error::RemoteManifestFetch( - "url returned no content length".to_string(), - )); - } - let str_parsed_header = match std::str::from_utf8(raw_header.first().unwrap()) { - Ok(s) => s, - Err(e) => { - return Err(Error::RemoteManifestFetch(format!( - "error parsing content length header: {}", - e - ))) - } - }; - let content_length: usize = match str_parsed_header.parse() { - Ok(s) => s, - Err(e) => { - return Err(Error::RemoteManifestFetch(format!( - "error parsing content length header: {}", - e - ))) - } - }; + let content_length: usize = response + .headers() + .get("Content-Length") + .first() + .and_then(|val| if val.is_empty() { None } else { Some(val) }) + .and_then(|val| std::str::from_utf8(val).ok()) + .and_then(|str_parsed_header| str_parsed_header.parse().ok()) + .unwrap_or(DEFAULT_MANIFEST_RESPONSE_SIZE); let body = { let mut buf = Vec::with_capacity(content_length); let response_body = response