[PATCH pages.sr.ht v3] Handle HEAD and conditional requests
Export this patch
---
v2 -> v3: use the object path instead of request URL
server.go | 15 ++++++ ---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/server.go b/server.go
index 841232f..f014c98 100644
--- a/server.go
+++ b/server.go
@@ -207,10 +207,13 @@ func ServeHTTP(conf ini.File, db *sql.DB, mc *minio.Client) *http.Server {
paths = append(paths, *notFound)
}
+ var s3path string
var object *minio.Object
+ var objectInfo minio.ObjectInfo
Nit: no need to declare these here, can use := inside of the for loop instead.
for _, cand := range paths {
- s3path := path.Join(prefix, "sites", r.Host, version, cand)
- _, err := mc.StatObject(ctx, bucket, s3path, minio.StatObjectOptions{})
+ s3path = path.Join(prefix, "sites", r.Host, version, cand)
+ var err error
+ objectInfo, err = mc.StatObject(ctx, bucket, s3path, minio.StatObjectOptions{})
The err declaration can be dropped here too.
if err != nil {
continue
}
@@ -227,13 +230,7 @@ func ServeHTTP(conf ini.File, db *sql.DB, mc *minio.Client) *http.Server {
}
defer object.Close()
- mimetype := mime.TypeByExtension(path.Ext(r.URL.Path))
- if mimetype != "" {
- w.Header().Add("Content-Type", mimetype)
- }
- if _, err := io.Copy(w, object); err != nil {
- log.Println(err.Error())
- }
+ http.ServeContent(w, r, s3path, objectInfo.LastModified, object)
})
return srv
}
--
2.35.1
Just adding my 2 cents here, as I was also working on this (sort of): I
think this provides good benefit. I also tested it a bit and couldn't
find any problems. I was slightly worried that ServeContent() doing a
seek to EOF to determine the file size might break the streaming
properties for larger files, but it seems minio has that covered [1].
ServeContent() also does exactly the same MIME type lookup, so removing
that makes sense, too.
[1] https://github.com/minio/minio-go/issues/593
Conrad
Thanks!
To git@git.sr.ht:~sircmpwn/pages.sr.ht
9cd00b9..edd71fe master -> master
pages.sr.ht/patches: SUCCESS in 1m28s
[Handle HEAD and conditional requests][0] v3 from [Umar Getagazov][1]
[0]: https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/29065
[1]: mailto:umar@handlerug.me
✓ #688082 SUCCESS pages.sr.ht/patches/archlinux.yml https://builds.sr.ht/~sircmpwn/job/688082
✓ #688081 SUCCESS pages.sr.ht/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/688081