@@ -6,10 +6,13 @@ import (
6
6
"net/http"
7
7
"os"
8
8
"os/signal"
9
+ "path/filepath"
9
10
"strings"
10
11
"sync"
11
12
"syscall"
13
+ "time"
12
14
15
+ "github.com/github/git-bundle-server/internal/bundles"
13
16
"github.com/github/git-bundle-server/internal/common"
14
17
"github.com/github/git-bundle-server/internal/core"
15
18
"github.com/github/git-bundle-server/internal/log"
@@ -70,7 +73,7 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
70
73
defer exitRegion ()
71
74
72
75
path := r .URL .Path
73
- owner , repo , file , err := b .parseRoute (ctx , path )
76
+ owner , repo , filename , err := b .parseRoute (ctx , path )
74
77
if err != nil {
75
78
w .WriteHeader (http .StatusNotFound )
76
79
fmt .Printf ("Failed to parse route: %s\n " , err )
@@ -97,20 +100,35 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
97
100
return
98
101
}
99
102
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 )
102
121
}
103
122
104
- fileToServe := repository .WebDir + "/" + file
105
- data , err := os .ReadFile (fileToServe )
123
+ file , err := os .OpenFile (fileToServe , os .O_RDONLY , 0 )
106
124
if err != nil {
107
125
w .WriteHeader (http .StatusNotFound )
108
- fmt .Printf ("Failed to read file\n " )
126
+ fmt .Printf ("Failed to open file\n " )
109
127
return
110
128
}
111
129
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 )
114
132
}
115
133
116
134
func (b * bundleWebServer ) StartServerAsync (ctx context.Context ) {
0 commit comments