mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-11 16:25:39 +00:00
Some checks failed
Build Snapshot Release / build (push) Has been cancelled
Release Please / release-please (push) Has been cancelled
Test / test-nix (push) Has been cancelled
Test / test-win (push) Has been cancelled
Update Restic / update-restic-version (push) Has been cancelled
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package webui
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var etagCacheMu sync.Mutex
|
|
var etagCache = make(map[string]string)
|
|
|
|
func calcEtag(path string, data []byte) string {
|
|
etagCacheMu.Lock()
|
|
defer etagCacheMu.Unlock()
|
|
etag, ok := etagCache[path]
|
|
if ok {
|
|
return etag
|
|
}
|
|
|
|
md5sum := md5.Sum(data)
|
|
etag = "\"" + hex.EncodeToString(md5sum[:]) + "\""
|
|
etagCache[path] = etag
|
|
return etag
|
|
}
|
|
|
|
func Handler() http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
r.URL.Path += "index.html"
|
|
}
|
|
|
|
f, err := content.Open(contentPrefix + r.URL.Path + ".gz")
|
|
if err == nil {
|
|
defer f.Close()
|
|
// Check if gzip encoding is supported
|
|
var fr io.Reader = f
|
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
gzr, err := gzip.NewReader(f)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer gzr.Close()
|
|
fr = gzr
|
|
} else {
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
}
|
|
serveFile(fr, w, r, r.URL.Path)
|
|
return
|
|
}
|
|
|
|
f, err = content.Open(contentPrefix + r.URL.Path)
|
|
if err == nil {
|
|
defer f.Close()
|
|
serveFile(f, w, r, r.URL.Path)
|
|
return
|
|
}
|
|
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
|
})
|
|
}
|
|
|
|
func serveFile(f io.Reader, w http.ResponseWriter, r *http.Request, path string) {
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("ETag", calcEtag(path, data))
|
|
http.ServeContent(w, r, path, time.Time{}, bytes.NewReader(data))
|
|
}
|