Skip to content

Commit e3082f7

Browse files
committed
fix: Working remote manifest fetch.
1 parent e1a5b40 commit e3082f7

File tree

6 files changed

+42
-39
lines changed

6 files changed

+42
-39
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test-wasm-web:
3434
# WASI testing requires the WASI SDK https://github.com/WebAssembly/wasi-sdk installed in /opt,
3535
# wasmtime, and the target wasm32-wasip2 on the nightly toolchain
3636
test-wasi:
37-
CC=/opt/wasi-sdk/bin/clang CARGO_TARGET_WASM32_WASIP2_RUNNER="wasmtime -S common --dir ." cargo +nightly test --target wasm32-wasip2 -p c2pa -p c2pa-crypto
37+
CC=/opt/wasi-sdk/bin/clang CARGO_TARGET_WASM32_WASIP2_RUNNER="wasmtime -S cli -S http --dir ." cargo +nightly test --target wasm32-wasip2 -p c2pa -p c2pa-crypto --all-features
3838

3939
# Full local validation, build and test all features including wasm
4040
# Run this before pushing a PR to pre-validate

sdk/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ pub enum Error {
191191
#[error("required JUMBF box not found")]
192192
JumbfBoxNotFound,
193193

194-
#[error("could not fetch the remote manifest")]
194+
#[error("could not fetch the remote manifest {0}")]
195195
RemoteManifestFetch(String),
196196

197-
#[error("must fetch remote manifests from url")]
197+
#[error("must fetch remote manifests from url {0}")]
198198
RemoteManifestUrl(String),
199199

200200
#[error("stopped because of logged error")]

sdk/src/store.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,47 +3286,48 @@ impl Store {
32863286
};
32873287

32883288
//const MANIFEST_CONTENT_TYPE: &str = "application/x-c2pa-manifest-store"; // todo verify once these are served
3289-
//const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB
3289+
const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB
32903290
let parsed_url = Url::parse(url)
32913291
.map_err(|e| Error::RemoteManifestFetch(format!("invalid URL: {}", e)))?;
3292-
let path_with_query = parsed_url[url::Position::BeforeHost..].to_string();
3292+
let authority = parsed_url.authority();
3293+
let path_with_query = parsed_url[url::Position::AfterPort..].to_string();
3294+
let scheme = match parsed_url.scheme() {
3295+
"http" => Scheme::Http,
3296+
"https" => Scheme::Https,
3297+
_ => {
3298+
return Err(Error::RemoteManifestFetch(
3299+
"unsupported URL scheme".to_string(),
3300+
))
3301+
}
3302+
};
32933303

32943304
let request = OutgoingRequest::new(Fields::new());
32953305
request.set_path_with_query(Some(&path_with_query)).unwrap();
3296-
request.set_scheme(Some(&Scheme::Https)).unwrap();
3306+
request.set_authority(Some(&authority)).unwrap();
3307+
request.set_scheme(Some(&scheme)).unwrap();
32973308
match outgoing_handler::handle(request, None) {
32983309
Ok(resp) => {
32993310
resp.subscribe().block();
33003311
let response = resp
33013312
.get()
3302-
.expect("HTTP request response missing")
3303-
.expect("HTTP request response requested more than once")
3304-
.expect("HTTP request failed");
3313+
.ok_or(Error::RemoteManifestFetch(
3314+
"HTTP request response missing".to_string(),
3315+
))?
3316+
.map_err(|_| {
3317+
Error::RemoteManifestFetch(
3318+
"HTTP request response requested more than once".to_string(),
3319+
)
3320+
})?
3321+
.map_err(|_| Error::RemoteManifestFetch("HTTP request failed".to_string()))?;
33053322
if response.status() == 200 {
3306-
let raw_header = response.headers().get("Content-Length");
3307-
if raw_header.first().map(|val| val.is_empty()).unwrap_or(true) {
3308-
return Err(Error::RemoteManifestFetch(
3309-
"url returned no content length".to_string(),
3310-
));
3311-
}
3312-
let str_parsed_header = match std::str::from_utf8(raw_header.first().unwrap()) {
3313-
Ok(s) => s,
3314-
Err(e) => {
3315-
return Err(Error::RemoteManifestFetch(format!(
3316-
"error parsing content length header: {}",
3317-
e
3318-
)))
3319-
}
3320-
};
3321-
let content_length: usize = match str_parsed_header.parse() {
3322-
Ok(s) => s,
3323-
Err(e) => {
3324-
return Err(Error::RemoteManifestFetch(format!(
3325-
"error parsing content length header: {}",
3326-
e
3327-
)))
3328-
}
3329-
};
3323+
let content_length: usize = response
3324+
.headers()
3325+
.get("Content-Length")
3326+
.first()
3327+
.and_then(|val| if val.is_empty() { None } else { Some(val) })
3328+
.and_then(|val| std::str::from_utf8(val).ok())
3329+
.and_then(|str_parsed_header| str_parsed_header.parse().ok())
3330+
.unwrap_or(DEFAULT_MANIFEST_RESPONSE_SIZE);
33303331
let body = {
33313332
let mut buf = Vec::with_capacity(content_length);
33323333
let response_body = response

sdk/tests/integration.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
/// Complete functional integration test with parent and ingredients.
1515
// Isolate from wasm by wrapping in module.
16-
1716
#[cfg(feature = "file_io")]
1817
mod integration_1 {
1918
use std::{io, path::PathBuf};

sdk/tests/test_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod common;
2121
use common::{compare_stream_to_known_good, fixtures_path, test_signer};
2222

2323
#[test]
24-
#[ignore] // TODO: Test does not pass in WASI or native
24+
#[cfg(all(feature = "add_thumbnails", feature = "file_io"))]
2525
fn test_builder_ca_jpg() -> Result<()> {
2626
let manifest_def = std::fs::read_to_string(fixtures_path("simple_manifest.json"))?;
2727
let mut builder = Builder::from_json(&manifest_def)?;

sdk/tests/v2_api_integration.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,13 @@ mod integration_v2 {
147147
dest
148148
};
149149

150-
// write dest to file for debugging
151-
let debug_path = format!("{}/../target/v2_test.jpg", env!("CARGO_MANIFEST_DIR"));
152-
std::fs::write(debug_path, dest.get_ref())?;
153-
dest.rewind()?;
150+
#[cfg(not(target_os = "wasi"))]
151+
{
152+
// write dest to file for debugging
153+
let debug_path = format!("{}/../target/v2_test.jpg", env!("CARGO_MANIFEST_DIR"));
154+
std::fs::write(debug_path, dest.get_ref())?;
155+
dest.rewind()?;
156+
}
154157

155158
let reader = Reader::from_stream(format, &mut dest)?;
156159

0 commit comments

Comments
 (0)