Skip to content

Commit d5dd29b

Browse files
committed
Fix do not redirect index.html to /
1 parent ca79a5c commit d5dd29b

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

components/blobserve/pkg/blobserve/blobserve.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (reg *Server) serve(w http.ResponseWriter, req *http.Request) {
214214

215215
// The blobFor operation's context must be independent of this request. Even if we do not
216216
// serve this request in time, we might want to serve another from the same ref in the future.
217-
blob, hash, err := reg.refstore.BlobFor(context.Background(), ref, false)
217+
blobFS, hash, err := reg.refstore.BlobFor(context.Background(), ref, false)
218218
if err == errdefs.ErrNotFound {
219219
http.Error(w, fmt.Sprintf("image %s not found: %q", html.EscapeString(ref), err), http.StatusNotFound)
220220
return
@@ -245,39 +245,43 @@ func (reg *Server) serve(w http.ResponseWriter, req *http.Request) {
245245
w.Header().Set("Cache-Control", "no-cache")
246246
}
247247

248+
var fs http.FileSystem = blobFS
249+
if workdir != "" {
250+
fs = prefixingFilesystem{Prefix: workdir, FS: blobFS}
251+
}
252+
248253
// http.FileServer has a special case where ServeFile redirects any request where r.URL.Path
249254
// ends in "/index.html" to the same path, without the final "index.html".
250255
// We do not want this behaviour to make the gitpod-ide-index mechanism in ws-proxy work.
251-
imagePath := strings.TrimPrefix(req.URL.Path, pathPrefix)
252-
if imagePath == "/index.html" || imagePath == "/" {
253-
fn := filepath.Join(workdir, "index.html")
254-
255-
fc, err := blob.Open(fn)
256+
resourcePath := strings.TrimPrefix(req.URL.Path, pathPrefix)
257+
if resourcePath == "/" {
258+
resourcePath = "/index.html"
259+
}
260+
if strings.HasSuffix(resourcePath, "/index.html") {
261+
fc, err := fs.Open(resourcePath)
256262
if err != nil {
257-
log.WithError(err).WithField("fn", fn).Error("cannot stat index.html")
263+
log.WithError(err).WithField("fn", resourcePath).Error("cannot open resource")
258264
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
259265
return
260266
}
261267
defer fc.Close()
262268

263-
var modTime time.Time
264-
if s, err := fc.Stat(); err == nil {
265-
modTime = s.ModTime()
269+
stat, err := fc.Stat()
270+
if err != nil {
271+
log.WithError(err).WithField("fn", resourcePath).Error("cannot stat resource")
272+
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
273+
return
266274
}
267275

268276
content, err := inlineVars(req, fc, inlineReplacements)
269277
if err != nil {
270278
log.WithError(err).Error()
271279
}
272-
http.ServeContent(w, req, "index.html", modTime, content)
280+
281+
http.ServeContent(w, req, stat.Name(), stat.ModTime(), content)
273282
return
274283
}
275284

276-
var fs http.FileSystem
277-
fs = blob
278-
if workdir != "" {
279-
fs = prefixingFilesystem{Prefix: workdir, FS: fs}
280-
}
281285
http.StripPrefix(pathPrefix, http.FileServer(fs)).ServeHTTP(w, req)
282286
}
283287

0 commit comments

Comments
 (0)