@cryptotaxi247 / kubo / commits / 1a06fb6e2

fix(gateway): correct symlink content type

We should be _resolving_ symlinks (sometimes, still need to figure out when to do this WRT IPNS). However, that's a larger feature.

Steven Allen committed Sep 26, 2019 at 13:53 UTC 1a06fb6e2f721b5443495c42d066125ef98edb6c
1 file changed +24 -16
core/corehttp/gateway_handler.go
+24 -16
@@ -382,28 +382,36 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
382 http.Error(w, "cannot serve files with unknown sizes", http.StatusBadGateway)
383 return
384 }
385 +
386 content := &lazySeeker{
387 size: size,
388 reader: file,
389 }
390
390 - ctype := mime.TypeByExtension(gopath.Ext(name))
391 - if ctype == "" {
392 - buf := make([]byte, 512)
393 - n, _ := io.ReadFull(content, buf[:])
394 - ctype = http.DetectContentType(buf[:n])
395 - _, err := content.Seek(0, io.SeekStart)
396 - if err != nil {
397 - http.Error(w, "seeker can't seek", http.StatusInternalServerError)
398 - return
391 + var ctype string
392 + if _, isSymlink := file.(*files.Symlink); isSymlink {
393 + // We should be smarter about resolving symlinks but this is the
394 + // "most correct" we can be without doing that.
395 + ctype = "inode/symlink"
396 + } else {
397 + ctype = mime.TypeByExtension(gopath.Ext(name))
398 + if ctype == "" {
399 + buf := make([]byte, 512)
400 + n, _ := io.ReadFull(content, buf[:])
401 + ctype = http.DetectContentType(buf[:n])
402 + _, err := content.Seek(0, io.SeekStart)
403 + if err != nil {
404 + http.Error(w, "seeker can't seek", http.StatusInternalServerError)
405 + return
406 + }
407 + }
408 + // Strip the encoding from the HTML Content-Type header and let the
409 + // browser figure it out.
410 + //
411 + // Fixes https://github.com/ipfs/go-ipfs/issues/2203
412 + if strings.HasPrefix(ctype, "text/html;") {
413 + ctype = "text/html"
414 }
400 - }
401 - // Strip the encoding from the HTML Content-Type header and let the
402 - // browser figure it out.
403 - //
404 - // Fixes https://github.com/ipfs/go-ipfs/issues/2203
405 - if strings.HasPrefix(ctype, "text/html;") {
406 - ctype = "text/html"
415 }
416 w.Header().Set("Content-Type", ctype)
417