Skip to content

Commit

Permalink
fix: force raw codec if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Dec 21, 2022
1 parent 7a11aa8 commit bb82c3e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
12 changes: 11 additions & 1 deletion iroh-gateway/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,18 @@ impl<T: ContentLoader + std::marker::Unpin> Client<T> {
pub async fn retrieve_path_metadata(
&self,
path: iroh_resolver::resolver::Path,
format: Option<ResponseFormat>,
) -> Result<Out, String> {
info!("retrieve path metadata {}", path);
if let Some(f) = format {
if f == ResponseFormat::Raw {
return self
.resolver
.resolve_raw(path)
.await
.map_err(|e| e.to_string());
}
}
self.resolver.resolve(path).await.map_err(|e| e.to_string())
}

Expand All @@ -116,7 +126,7 @@ impl<T: ContentLoader + std::marker::Unpin> Client<T> {
let path_metadata = if let Some(path_metadata) = path_metadata {
path_metadata
} else {
self.retrieve_path_metadata(path.clone()).await?
self.retrieve_path_metadata(path.clone(), None).await?
};
let metadata = path_metadata.metadata().clone();
record_ttfb_metrics(start_time, &metadata.source);
Expand Down
14 changes: 9 additions & 5 deletions iroh-gateway/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ async fn request_preprocessing<T: ContentLoader + Unpin>(
}
}

let path_metadata = match state.client.retrieve_path_metadata(path.clone()).await {
// parse query params
let format = get_response_format(request_headers, &query_params.format)
.map_err(|err| GatewayError::new(StatusCode::BAD_REQUEST, &err))?;

let path_metadata = match state
.client
.retrieve_path_metadata(path.clone(), Some(format.clone()))
.await
{
Ok(metadata) => metadata,
Err(e) => {
if e == "offline" {
Expand Down Expand Up @@ -217,10 +225,6 @@ async fn request_preprocessing<T: ContentLoader + Unpin>(
));
}

// parse query params
let format = get_response_format(request_headers, &query_params.format)
.map_err(|err| GatewayError::new(StatusCode::BAD_REQUEST, &err))?;

if let Some(resp) = etag_check(request_headers, &CidOrDomain::Cid(*resolved_cid), &format) {
return Ok(RequestPreprocessingResult::RespondImmediately(resp));
}
Expand Down
25 changes: 21 additions & 4 deletions iroh-resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ impl<T: ContentLoader> Resolver<T> {
let this = self.clone();
self.resolve_recursive_mapped(root, None, move |cid, ctx| {
let this = this.clone();
async move { this.resolve_with_ctx(ctx, Path::from_cid(cid)).await }
async move { this.resolve_with_ctx(ctx, Path::from_cid(cid), false).await }
})
}

Expand Down Expand Up @@ -781,18 +781,35 @@ impl<T: ContentLoader> Resolver<T> {
pub async fn resolve(&self, path: Path) -> Result<Out> {
let ctx = LoaderContext::from_path(self.next_id(), self.session_closer.clone());

self.resolve_with_ctx(ctx, path).await
self.resolve_with_ctx(ctx, path, false).await
}

pub async fn resolve_with_ctx(&self, mut ctx: LoaderContext, path: Path) -> Result<Out> {
/// Resolves through a given path, returning the [`Cid`] and raw bytes of the final leaf.
/// Forces the RAW codec.
#[tracing::instrument(skip(self))]
pub async fn resolve_raw(&self, path: Path) -> Result<Out> {
let ctx = LoaderContext::from_path(self.next_id(), self.session_closer.clone());

self.resolve_with_ctx(ctx, path, true).await
}

pub async fn resolve_with_ctx(
&self,
mut ctx: LoaderContext,
path: Path,
force_raw: bool,
) -> Result<Out> {
// Resolve the root block.
let (root_cid, loaded_cid) = self.resolve_root(&path, &mut ctx).await?;
match loaded_cid.source {
Source::Store(_) => inc!(ResolverMetrics::CacheHit),
_ => inc!(ResolverMetrics::CacheMiss),
}

let codec = Codec::try_from(root_cid.codec()).context("unknown codec")?;
let codec = match force_raw {
true => Codec::Raw,
false => Codec::try_from(root_cid.codec()).context("unknown codec")?,
};

match codec {
Codec::DagPb => {
Expand Down

0 comments on commit bb82c3e

Please sign in to comment.