@@ -72,7 +72,7 @@ pub struct RegistryBuilder {
72
72
/// Write the registry in configuration.
73
73
configure_registry : bool ,
74
74
/// 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 > > ,
76
76
}
77
77
78
78
pub struct TestRegistry {
@@ -117,7 +117,7 @@ impl RegistryBuilder {
117
117
118
118
/// Adds a custom HTTP response for a specific url
119
119
#[ 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 > (
121
121
mut self ,
122
122
url : & ' static str ,
123
123
responder : R ,
@@ -497,20 +497,23 @@ pub struct Response {
497
497
pub body : Vec < u8 > ,
498
498
}
499
499
500
- struct HttpServer {
500
+ pub struct HttpServer {
501
501
listener : TcpListener ,
502
502
registry_path : PathBuf ,
503
503
dl_path : PathBuf ,
504
504
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 > > ,
506
506
}
507
507
508
508
impl HttpServer {
509
509
pub fn new (
510
510
registry_path : PathBuf ,
511
511
dl_path : PathBuf ,
512
512
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
+ > ,
514
517
) -> HttpServerHandle {
515
518
let listener = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
516
519
let addr = listener. local_addr ( ) . unwrap ( ) ;
@@ -620,7 +623,7 @@ impl HttpServer {
620
623
621
624
// Check for custom responder
622
625
if let Some ( responder) = self . custom_responders . get ( req. url . path ( ) ) {
623
- return responder ( & req) ;
626
+ return responder ( & req, self ) ;
624
627
}
625
628
let path: Vec < _ > = req. url . path ( ) [ 1 ..] . split ( '/' ) . collect ( ) ;
626
629
match ( req. method . as_str ( ) , path. as_slice ( ) ) {
@@ -668,7 +671,7 @@ impl HttpServer {
668
671
}
669
672
670
673
/// Unauthorized response
671
- fn unauthorized ( & self , _req : & Request ) -> Response {
674
+ pub fn unauthorized ( & self , _req : & Request ) -> Response {
672
675
Response {
673
676
code : 401 ,
674
677
headers : vec ! [ ] ,
@@ -677,7 +680,7 @@ impl HttpServer {
677
680
}
678
681
679
682
/// Not found response
680
- fn not_found ( & self , _req : & Request ) -> Response {
683
+ pub fn not_found ( & self , _req : & Request ) -> Response {
681
684
Response {
682
685
code : 404 ,
683
686
headers : vec ! [ ] ,
@@ -686,16 +689,25 @@ impl HttpServer {
686
689
}
687
690
688
691
/// Respond OK without doing anything
689
- fn ok ( & self , _req : & Request ) -> Response {
692
+ pub fn ok ( & self , _req : & Request ) -> Response {
690
693
Response {
691
694
code : 200 ,
692
695
headers : vec ! [ ] ,
693
696
body : br#"{"ok": true, "msg": "completed!"}"# . to_vec ( ) ,
694
697
}
695
698
}
696
699
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
+
697
709
/// Serve the download endpoint
698
- fn dl ( & self , req : & Request ) -> Response {
710
+ pub fn dl ( & self , req : & Request ) -> Response {
699
711
let file = self
700
712
. dl_path
701
713
. join ( req. url . path ( ) . strip_prefix ( "/dl/" ) . unwrap ( ) ) ;
@@ -711,7 +723,7 @@ impl HttpServer {
711
723
}
712
724
713
725
/// Serve the registry index
714
- fn index ( & self , req : & Request ) -> Response {
726
+ pub fn index ( & self , req : & Request ) -> Response {
715
727
let file = self
716
728
. registry_path
717
729
. join ( req. url . path ( ) . strip_prefix ( "/index/" ) . unwrap ( ) ) ;
@@ -761,7 +773,7 @@ impl HttpServer {
761
773
}
762
774
}
763
775
764
- fn publish ( & self , req : & Request ) -> Response {
776
+ pub fn publish ( & self , req : & Request ) -> Response {
765
777
if let Some ( body) = & req. body {
766
778
// Get the metadata of the package
767
779
let ( len, remaining) = body. split_at ( 4 ) ;
0 commit comments