@@ -72,7 +72,7 @@ pub struct RegistryBuilder {
7272 /// Write the registry in configuration.
7373 configure_registry : bool ,
7474 /// API responders.
75- custom_responders : HashMap < & ' static str , Box < dyn Send + Fn ( & Request ) -> Response > > ,
75+ custom_responders : HashMap < & ' static str , Box < dyn Send + Fn ( & Request , & HttpServer ) -> Response > > ,
7676}
7777
7878pub struct TestRegistry {
@@ -117,7 +117,7 @@ impl RegistryBuilder {
117117
118118 /// Adds a custom HTTP response for a specific url
119119 #[ must_use]
120- pub fn add_responder < R : ' static + Send + Fn ( & Request ) -> Response > (
120+ pub fn add_responder < R : ' static + Send + Fn ( & Request , & HttpServer ) -> Response > (
121121 mut self ,
122122 url : & ' static str ,
123123 responder : R ,
@@ -497,20 +497,23 @@ pub struct Response {
497497 pub body : Vec < u8 > ,
498498}
499499
500- struct HttpServer {
500+ pub struct HttpServer {
501501 listener : TcpListener ,
502502 registry_path : PathBuf ,
503503 dl_path : PathBuf ,
504504 token : Option < String > ,
505- custom_responders : HashMap < & ' static str , Box < dyn Send + Fn ( & Request ) -> Response > > ,
505+ custom_responders : HashMap < & ' static str , Box < dyn Send + Fn ( & Request , & HttpServer ) -> Response > > ,
506506}
507507
508508impl HttpServer {
509509 pub fn new (
510510 registry_path : PathBuf ,
511511 dl_path : PathBuf ,
512512 token : Option < String > ,
513- api_responders : HashMap < & ' static str , Box < dyn Send + Fn ( & Request ) -> Response > > ,
513+ api_responders : HashMap <
514+ & ' static str ,
515+ Box < dyn Send + Fn ( & Request , & HttpServer ) -> Response > ,
516+ > ,
514517 ) -> HttpServerHandle {
515518 let listener = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
516519 let addr = listener. local_addr ( ) . unwrap ( ) ;
@@ -620,7 +623,7 @@ impl HttpServer {
620623
621624 // Check for custom responder
622625 if let Some ( responder) = self . custom_responders . get ( req. url . path ( ) ) {
623- return responder ( & req) ;
626+ return responder ( & req, self ) ;
624627 }
625628 let path: Vec < _ > = req. url . path ( ) [ 1 ..] . split ( '/' ) . collect ( ) ;
626629 match ( req. method . as_str ( ) , path. as_slice ( ) ) {
@@ -668,7 +671,7 @@ impl HttpServer {
668671 }
669672
670673 /// Unauthorized response
671- fn unauthorized ( & self , _req : & Request ) -> Response {
674+ pub fn unauthorized ( & self , _req : & Request ) -> Response {
672675 Response {
673676 code : 401 ,
674677 headers : vec ! [ ] ,
@@ -677,7 +680,7 @@ impl HttpServer {
677680 }
678681
679682 /// Not found response
680- fn not_found ( & self , _req : & Request ) -> Response {
683+ pub fn not_found ( & self , _req : & Request ) -> Response {
681684 Response {
682685 code : 404 ,
683686 headers : vec ! [ ] ,
@@ -686,16 +689,25 @@ impl HttpServer {
686689 }
687690
688691 /// Respond OK without doing anything
689- fn ok ( & self , _req : & Request ) -> Response {
692+ pub fn ok ( & self , _req : & Request ) -> Response {
690693 Response {
691694 code : 200 ,
692695 headers : vec ! [ ] ,
693696 body : br#"{"ok": true, "msg": "completed!"}"# . to_vec ( ) ,
694697 }
695698 }
696699
700+ /// Return an internal server error (HTTP 500)
701+ pub fn internal_server_error ( & self , _req : & Request ) -> Response {
702+ Response {
703+ code : 500 ,
704+ headers : vec ! [ ] ,
705+ body : br#"internal server error"# . to_vec ( ) ,
706+ }
707+ }
708+
697709 /// Serve the download endpoint
698- fn dl ( & self , req : & Request ) -> Response {
710+ pub fn dl ( & self , req : & Request ) -> Response {
699711 let file = self
700712 . dl_path
701713 . join ( req. url . path ( ) . strip_prefix ( "/dl/" ) . unwrap ( ) ) ;
@@ -711,7 +723,7 @@ impl HttpServer {
711723 }
712724
713725 /// Serve the registry index
714- fn index ( & self , req : & Request ) -> Response {
726+ pub fn index ( & self , req : & Request ) -> Response {
715727 let file = self
716728 . registry_path
717729 . join ( req. url . path ( ) . strip_prefix ( "/index/" ) . unwrap ( ) ) ;
@@ -761,7 +773,7 @@ impl HttpServer {
761773 }
762774 }
763775
764- fn publish ( & self , req : & Request ) -> Response {
776+ pub fn publish ( & self , req : & Request ) -> Response {
765777 if let Some ( body) = & req. body {
766778 // Get the metadata of the package
767779 let ( len, remaining) = body. split_at ( 4 ) ;
0 commit comments