@@ -15,9 +15,13 @@ use axum_server::tls_rustls::RustlsConfig;
1515use rcgen:: generate_simple_self_signed;
1616use std:: future:: IntoFuture ;
1717use std:: net:: SocketAddr ;
18+ use dav_server:: DavHandler ;
19+ use dav_server:: fakels:: FakeLs ;
20+ use dav_server:: localfs:: LocalFs ;
21+ use dav_server:: memls:: MemLs ;
1822use tokio:: net:: TcpListener ;
1923use tokio:: signal;
20- use tracing:: { error, info, trace} ;
24+ use tracing:: { debug , error, info, trace} ;
2125#[ derive( Clone ) ]
2226struct GitServer {
2327 instance_url : String ,
@@ -195,37 +199,35 @@ async fn init(Path((user, repo_name)): Path<(String, String)>) -> impl IntoRespo
195199 }
196200}
197201
198- /// Handle file or directory requests for a repository.
199202async fn handle_repo (
200203 Path ( ( user, repo_name, path) ) : Path < ( String , String , String ) > ,
204+ uri : Uri ,
201205) -> impl IntoResponse {
202- let repo_path = format ! ( "repos/{}/{}" , user, repo_name) ;
203- let file_path = format ! ( "{}/{}" , repo_path, path) ;
206+ let repo_path = format ! ( "/repos/{}/{}" , user, repo_name) ;
204207
205- match tokio:: fs:: metadata ( & file_path) . await {
206- Ok ( metadata) => {
207- if metadata. is_dir ( ) {
208- info ! ( "Directory: {}" , file_path) ;
209- ( StatusCode :: OK , format ! ( "Directory: {}" , file_path) ) . into_response ( )
210- } else {
211- match tokio:: fs:: read ( & file_path) . await {
212- Ok ( contents) => ( StatusCode :: OK , contents) . into_response ( ) ,
213- Err ( _) => (
214- StatusCode :: NOT_FOUND ,
215- format ! ( "File not found: {}" , file_path) ,
216- )
217- . into_response ( ) ,
218- }
219- }
208+ let relative_uri = {
209+ if let Some ( query) = uri. query ( ) {
210+ format ! ( "/{}?{}" , path, query)
211+ } else {
212+ format ! ( "/{}" , path)
220213 }
221- Err ( _) => (
222- StatusCode :: NOT_FOUND ,
223- format ! ( "Path not found: {}" , file_path) ,
224- )
225- . into_response ( ) ,
226- }
214+ } ;
215+ debug ! ( "Repository path: {}, relative URI: {}" , repo_path. escape_debug( ) , relative_uri. escape_debug( ) ) ;
216+
217+ let dav_server = DavHandler :: builder ( )
218+ . filesystem ( LocalFs :: new ( repo_path. clone ( ) , true , false , false ) )
219+ . locksystem ( MemLs :: new ( ) )
220+ . build_handler ( ) ;
221+
222+ let req = Request :: builder ( )
223+ . uri ( relative_uri)
224+ . body ( Body :: empty ( ) )
225+ . unwrap ( ) ;
226+ debug ! ( "DAV Request: {:?}" , req) ;
227+ dav_server. handle ( req) . await
227228}
228229
230+
229231/// Fallback handler for unmatched routes on the main router.
230232async fn fallback ( uri : Uri , State ( _state) : State < GitServer > , method : Method ) -> impl IntoResponse {
231233 let msg = format ! ( "404 - Not Found: {} {}" , method, uri) ;
0 commit comments