@@ -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 .
241241async 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.
339349async 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