@@ -6,10 +6,13 @@ import (
66 "net/http"
77 "os"
88 "os/signal"
9+ "path/filepath"
910 "strings"
1011 "sync"
1112 "syscall"
13+ "time"
1214
15+ "github.com/github/git-bundle-server/internal/bundles"
1316 "github.com/github/git-bundle-server/internal/common"
1417 "github.com/github/git-bundle-server/internal/core"
1518 "github.com/github/git-bundle-server/internal/log"
@@ -70,7 +73,7 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
7073 defer exitRegion ()
7174
7275 path := r .URL .Path
73- owner , repo , file , err := b .parseRoute (ctx , path )
76+ owner , repo , filename , err := b .parseRoute (ctx , path )
7477 if err != nil {
7578 w .WriteHeader (http .StatusNotFound )
7679 fmt .Printf ("Failed to parse route: %s\n " , err )
@@ -97,20 +100,35 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
97100 return
98101 }
99102
100- if file == "" {
101- file = "bundle-list"
103+ var fileToServe string
104+ if filename == "" {
105+ if path [len (path )- 1 ] == '/' {
106+ // Trailing slash, so the bundle URIs should be relative to the
107+ // request's URL as if it were a directory
108+ fileToServe = filepath .Join (repository .WebDir , bundles .BundleListFilename )
109+ } else {
110+ // No trailing slash, so the bundle URIs should be relative to the
111+ // request's URL as if it were a file
112+ fileToServe = filepath .Join (repository .WebDir , bundles .RepoBundleListFilename )
113+ }
114+ } else if filename == bundles .BundleListFilename || filename == bundles .RepoBundleListFilename {
115+ // If the request identifies a non-bundle "reserved" file, return 404
116+ w .WriteHeader (http .StatusNotFound )
117+ fmt .Printf ("Failed to open file\n " )
118+ return
119+ } else {
120+ fileToServe = filepath .Join (repository .WebDir , filename )
102121 }
103122
104- fileToServe := repository .WebDir + "/" + file
105- data , err := os .ReadFile (fileToServe )
123+ file , err := os .OpenFile (fileToServe , os .O_RDONLY , 0 )
106124 if err != nil {
107125 w .WriteHeader (http .StatusNotFound )
108- fmt .Printf ("Failed to read file\n " )
126+ fmt .Printf ("Failed to open file\n " )
109127 return
110128 }
111129
112- fmt .Printf ("Successfully serving content for %s/%s\n " , route , file )
113- w . Write ( data )
130+ fmt .Printf ("Successfully serving content for %s/%s\n " , route , filename )
131+ http . ServeContent ( w , r , filename , time . UnixMicro ( 0 ), file )
114132}
115133
116134func (b * bundleWebServer ) StartServerAsync (ctx context.Context ) {
0 commit comments