Skip to content

Commit

Permalink
please the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blind-oracle committed Jun 14, 2024
1 parent 0143b1a commit 4810434
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
28 changes: 8 additions & 20 deletions src/routing/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ impl DomainResolver {
}

// Check if it's a raw domain
let raw = depth == 2 && host.labels().nth(1) == Some("raw");
let raw = depth == 2;
if raw && host.labels().nth(1) != Some("raw") {
return None;
}

// Attempt to extract canister_id
// Strip the optional prefix if any
let label = host.labels().next()?.split("--").last()?;

// Do not allow cases like <id>.foo.ic0.app where
Expand Down Expand Up @@ -505,24 +508,9 @@ mod test {
})
);

// Nested subdomain should not match canister id
// TODO discuss?
assert_eq!(
resolver.resolve(&fqdn!("aaaaa-aa.foo.ic0.app")),
Some(DomainLookup {
domain: domain_ic0_app.clone(),
canister_id: None,
verify: true,
})
);
assert_eq!(
resolver.resolve(&fqdn!("aaaaa-aa.foo.icp0.io")),
Some(DomainLookup {
domain: domain_icp0_io.clone(),
canister_id: None,
verify: true,
})
);
// 2-level non-raw subdomain should not resolve
assert_eq!(resolver.resolve(&fqdn!("aaaaa-aa.foo.ic0.app")), None);
assert_eq!(resolver.resolve(&fqdn!("aaaaa-aa.foo.icp0.io")), None,);

// Resolve custom domain
assert_eq!(
Expand Down
14 changes: 3 additions & 11 deletions src/routing/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use axum::{
extract::{Request, State},
response::{IntoResponse, Response},
response::Response,
Extension,
};
use bytes::Bytes;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub async fn handler(
};

// Pass headers in/out the IC request
let mut resp = PASS_HEADERS
let resp = PASS_HEADERS
.scope(PassHeaders::new(), async {
PASS_HEADERS.with(|x| {
let hdr =
Expand All @@ -85,15 +85,7 @@ pub async fn handler(
req
};

let res = req.send().await;

PASS_HEADERS.with(|x| {
for (k, v) in &x.borrow().headers_in {
println!("{k:?}: {v:?}");
}
});

res
req.send().await
})
.await
.map_err(ErrorCause::from_err)?;
Expand Down
10 changes: 6 additions & 4 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,18 @@ pub fn setup_router(
return router_health.oneshot(request).await;
}

// Redirect to the dashboard if the request is to the root of domain
// and no canister was resolved
if path == "/" && ctx.canister_id.is_none() {
// Redirect to the dashboard if the request is to the root of the base domain
// or to a bare "raw" subdomain w/o canister id.
if path == "/"
&& (ctx.is_base_domain() || ctx.authority.labels().next() == Some("raw"))
{
return Ok(
Redirect::temporary("https://dashboard.internetcomputer.org/")
.into_response(),
);
}

// Otherwise request goes to the canister
// Otherwise request goes to the HTTP->IC handler
router_http.oneshot(request).await
},
)
Expand Down

0 comments on commit 4810434

Please sign in to comment.