Skip to content

Commit 8ff4769

Browse files
committed
Allow specifying default node as either a node ticket or blob ticket
for convenience when working with sendme.
1 parent 1a9c4ed commit 8ff4769

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

iroh-gateway/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ http://localhost:8080/node/cytwbysn6cs6jbhdak6ypmrbg2w3nrcrptl5xlts2yurk4mq5cb/b
4646
```
4747

4848
# Features
49-
- [ ] range requests
50-
- [ ] mime type sniffing
49+
- [x] range requests
50+
- [x] mime type sniffing

iroh-gateway/src/args.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
use std::path::PathBuf;
33

44
use clap::Parser;
5-
use iroh::ticket::NodeTicket;
65

76
#[derive(Parser, Debug)]
87
pub struct Args {
9-
/// Node ticket for the default node
8+
/// Ticket for the default node.
9+
///
10+
/// This can be a node ticket or a blob ticket. If it is a blob ticket, the
11+
/// hash is ignored and just the node part is used.
12+
///
13+
/// This is needed for all endpoints except `/ticket`.
1014
#[clap(long)]
11-
pub default_node: Option<NodeTicket>,
15+
pub default_node: Option<String>,
1216

13-
/// Http or https listen addr
17+
/// Http or https listen addr.
18+
///
19+
/// Will listen on http if cert_path is not specified, https otherwise.
1420
#[clap(long, default_value = "0.0.0.0:8080")]
1521
pub addr: String,
1622

@@ -21,7 +27,7 @@ pub struct Args {
2127
#[clap(long)]
2228
pub cert_path: Option<PathBuf>,
2329

24-
/// Magic port for the node, random if not specified
30+
/// Magic port for the node, random if not specified.
2531
#[clap(long)]
2632
pub magic_port: Option<u16>,
2733
}

iroh-gateway/src/main.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ async fn get_collection_inner(
237237
Ok((collection, headers))
238238
}
239239

240-
/// Get the mime type for a hash, either from the cache or by requesting it from the node.
240+
/// Get the collection. This will also fill the mime cache.
241241
async fn get_collection(
242242
gateway: &Gateway,
243243
hash: &Hash,
@@ -335,6 +335,16 @@ async fn handle_local_blob_request(
335335
Ok(res)
336336
}
337337

338+
async fn handle_local_collection_index(
339+
gateway: Extension<Gateway>,
340+
Path(hash): Path<Hash>,
341+
) -> std::result::Result<impl IntoResponse, AppError> {
342+
let connection = gateway.get_default_connection().await?;
343+
let link_prefix = format!("/collection/{}", hash);
344+
let res = collection_index(&gateway, connection, &hash, &link_prefix).await?;
345+
Ok(res)
346+
}
347+
338348
/// Handle a request for a range of bytes from the default node.
339349
async fn handle_local_collection_request(
340350
gateway: Extension<Gateway>,
@@ -347,19 +357,19 @@ async fn handle_local_collection_request(
347357
Ok(res)
348358
}
349359

350-
async fn handle_ticket_request(
360+
async fn handle_ticket_index(
351361
gateway: Extension<Gateway>,
352362
Path(ticket): Path<BlobTicket>,
353363
req: Request<Body>,
354364
) -> std::result::Result<impl IntoResponse, AppError> {
355-
tracing::info!("handle_ticket_request");
365+
tracing::info!("handle_ticket_index");
356366
let byte_range = parse_byte_range(req).await?;
357367
let connection = gateway
358368
.endpoint
359369
.connect(ticket.node_addr().clone(), iroh::bytes::protocol::ALPN)
360370
.await?;
361371
let hash = ticket.hash();
362-
let prefix = format!("/node/{}", ticket.node_addr().node_id);
372+
let prefix = format!("/ticket/{}", ticket);
363373
let res = match ticket.format() {
364374
BlobFormat::Raw => forward_range(&gateway, connection, &hash, byte_range)
365375
.await?
@@ -371,12 +381,19 @@ async fn handle_ticket_request(
371381
Ok(res)
372382
}
373383

374-
async fn handle_local_collection_index(
384+
async fn handle_ticket_request(
375385
gateway: Extension<Gateway>,
376-
Path(hash): Path<Hash>,
386+
Path((ticket, suffix)): Path<(BlobTicket, String)>,
387+
req: Request<Body>,
377388
) -> std::result::Result<impl IntoResponse, AppError> {
378-
let connection = gateway.get_default_connection().await?;
379-
let res = collection_index(&gateway, connection, &hash, "").await?;
389+
tracing::info!("handle_ticket_request");
390+
let byte_range = parse_byte_range(req).await?;
391+
let connection = gateway
392+
.endpoint
393+
.connect(ticket.node_addr().clone(), iroh::bytes::protocol::ALPN)
394+
.await?;
395+
let hash = ticket.hash();
396+
let res = forward_collection_range(&gateway, connection, &hash, &suffix, byte_range).await?;
380397
Ok(res)
381398
}
382399

@@ -398,7 +415,7 @@ async fn collection_index(
398415
res.push_str("<html>\n<head></head>\n");
399416

400417
for (name, child_hash) in collection.iter() {
401-
let url = format!("{}/collection/{}/{}", link_prefix, hash, name);
418+
let url = format!("{}/{}", link_prefix, name);
402419
let url = encode_relative_url(&url)?;
403420
let smo = gateway.mime_cache.lock().unwrap().get(child_hash).cloned();
404421
res.push_str(&format!("<a href=\"{}\">{}</a>", url, name,));
@@ -540,7 +557,15 @@ async fn main() -> anyhow::Result<()> {
540557
let endpoint = MagicEndpoint::builder().bind(magic_port).await?;
541558
let default_node = args
542559
.default_node
543-
.map(|default_node| default_node.node_addr().clone());
560+
.map(|default_node| {
561+
Ok(if let Ok(node_ticket) = default_node.parse::<BlobTicket>() {
562+
node_ticket.node_addr().clone()
563+
} else if let Ok(blob_ticket) = default_node.parse::<BlobTicket>() {
564+
blob_ticket.node_addr().clone()
565+
} else {
566+
anyhow::bail!("invalid default node");
567+
})
568+
}).transpose()?;
544569
let gateway = Gateway(Arc::new(Inner {
545570
endpoint,
546571
default_node,
@@ -555,7 +580,8 @@ async fn main() -> anyhow::Result<()> {
555580
.route("/blob/:blake3_hash", get(handle_local_blob_request))
556581
.route("/collection/:blake3_hash", get(handle_local_collection_index))
557582
.route("/collection/:blake3_hash/*path",get(handle_local_collection_request))
558-
.route("/ticket/:ticket", get(handle_ticket_request))
583+
.route("/ticket/:ticket", get(handle_ticket_index))
584+
.route("/ticket/:ticket/*path", get(handle_ticket_request))
559585
.layer(Extension(gateway));
560586

561587
if let Some(path) = args.cert_path {

0 commit comments

Comments
 (0)