Skip to content

Commit

Permalink
Working remote manifest fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmurph32 committed Feb 11, 2025
1 parent dd850a6 commit 38f97a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
4 changes: 2 additions & 2 deletions sdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
49 changes: 22 additions & 27 deletions sdk/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 38f97a0

Please sign in to comment.