Skip to content

Commit 066a7a8

Browse files
committed
fix: Working remote manifest fetch.
1 parent dd850a6 commit 066a7a8

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
@@ -3276,47 +3276,48 @@ impl Store {
32763276
};
32773277

32783278
//const MANIFEST_CONTENT_TYPE: &str = "application/x-c2pa-manifest-store"; // todo verify once these are served
3279-
//const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB
3279+
const DEFAULT_MANIFEST_RESPONSE_SIZE: usize = 10 * 1024 * 1024; // 10 MB
32803280
let parsed_url = Url::parse(url)
32813281
.map_err(|e| Error::RemoteManifestFetch(format!("invalid URL: {}", e)))?;
3282-
let path_with_query = parsed_url[url::Position::BeforeHost..].to_string();
3282+
let authority = parsed_url.authority();
3283+
let path_with_query = parsed_url[url::Position::AfterPort..].to_string();
3284+
let scheme = match parsed_url.scheme() {
3285+
"http" => Scheme::Http,
3286+
"https" => Scheme::Https,
3287+
_ => {
3288+
return Err(Error::RemoteManifestFetch(
3289+
"unsupported URL scheme".to_string(),
3290+
))
3291+
}
3292+
};
32833293

32843294
let request = OutgoingRequest::new(Fields::new());
32853295
request.set_path_with_query(Some(&path_with_query)).unwrap();
3286-
request.set_scheme(Some(&Scheme::Https)).unwrap();
3296+
request.set_authority(Some(&authority)).unwrap();
3297+
request.set_scheme(Some(&scheme)).unwrap();
32873298
match outgoing_handler::handle(request, None) {
32883299
Ok(resp) => {
32893300
resp.subscribe().block();
32903301
let response = resp
32913302
.get()
3292-
.expect("HTTP request response missing")
3293-
.expect("HTTP request response requested more than once")
3294-
.expect("HTTP request failed");
3303+
.ok_or(Error::RemoteManifestFetch(
3304+
"HTTP request response missing".to_string(),
3305+
))?
3306+
.map_err(|_| {
3307+
Error::RemoteManifestFetch(
3308+
"HTTP request response requested more than once".to_string(),
3309+
)
3310+
})?
3311+
.map_err(|_| Error::RemoteManifestFetch("HTTP request failed".to_string()))?;
32953312
if response.status() == 200 {
3296-
let raw_header = response.headers().get("Content-Length");
3297-
if raw_header.first().map(|val| val.is_empty()).unwrap_or(true) {
3298-
return Err(Error::RemoteManifestFetch(
3299-
"url returned no content length".to_string(),
3300-
));
3301-
}
3302-
let str_parsed_header = match std::str::from_utf8(raw_header.first().unwrap()) {
3303-
Ok(s) => s,
3304-
Err(e) => {
3305-
return Err(Error::RemoteManifestFetch(format!(
3306-
"error parsing content length header: {}",
3307-
e
3308-
)))
3309-
}
3310-
};
3311-
let content_length: usize = match str_parsed_header.parse() {
3312-
Ok(s) => s,
3313-
Err(e) => {
3314-
return Err(Error::RemoteManifestFetch(format!(
3315-
"error parsing content length header: {}",
3316-
e
3317-
)))
3318-
}
3319-
};
3313+
let content_length: usize = response
3314+
.headers()
3315+
.get("Content-Length")
3316+
.first()
3317+
.and_then(|val| if val.is_empty() { None } else { Some(val) })
3318+
.and_then(|val| std::str::from_utf8(val).ok())
3319+
.and_then(|str_parsed_header| str_parsed_header.parse().ok())
3320+
.unwrap_or(DEFAULT_MANIFEST_RESPONSE_SIZE);
33203321
let body = {
33213322
let mut buf = Vec::with_capacity(content_length);
33223323
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)