summaryrefslogtreecommitdiff
path: root/plugins/http/static
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/http/static')
-rw-r--r--plugins/http/static/etag.go71
-rw-r--r--plugins/http/static/static.go88
2 files changed, 0 insertions, 159 deletions
diff --git a/plugins/http/static/etag.go b/plugins/http/static/etag.go
deleted file mode 100644
index 5d41cc53..00000000
--- a/plugins/http/static/etag.go
+++ /dev/null
@@ -1,71 +0,0 @@
-package static
-
-import (
- "hash/crc32"
- "io"
- "net/http"
- "os"
- "unsafe"
-
- httpConfig "github.com/spiral/roadrunner/v2/plugins/http/config"
-)
-
-const etag string = "Etag"
-
-// weak Etag prefix
-var weakPrefix = []byte(`W/`)
-
-// CRC32 table
-var crc32q = crc32.MakeTable(0x48D90782)
-
-func SetEtag(cfg *httpConfig.Static, f *os.File, w http.ResponseWriter) {
- // read the file content
- body, err := io.ReadAll(f)
- if err != nil {
- return
- }
-
- // skip for 0 body
- if len(body) == 0 {
- return
- }
-
- // preallocate
- calculatedEtag := make([]byte, 0, 64)
-
- // write weak
- if cfg.Weak {
- calculatedEtag = append(calculatedEtag, weakPrefix...)
- }
-
- calculatedEtag = append(calculatedEtag, '"')
- calculatedEtag = appendUint(calculatedEtag, uint32(len(body)))
- calculatedEtag = append(calculatedEtag, '-')
- calculatedEtag = appendUint(calculatedEtag, crc32.Checksum(body, crc32q))
- calculatedEtag = append(calculatedEtag, '"')
-
- w.Header().Set(etag, byteToSrt(calculatedEtag))
-}
-
-// appendUint appends n to dst and returns the extended dst.
-func appendUint(dst []byte, n uint32) []byte {
- var b [20]byte
- buf := b[:]
- i := len(buf)
- var q uint32
- for n >= 10 {
- i--
- q = n / 10
- buf[i] = '0' + byte(n-q*10)
- n = q
- }
- i--
- buf[i] = '0' + byte(n)
-
- dst = append(dst, buf[i:]...)
- return dst
-}
-
-func byteToSrt(b []byte) string {
- return *(*string)(unsafe.Pointer(&b))
-}
diff --git a/plugins/http/static/static.go b/plugins/http/static/static.go
deleted file mode 100644
index d0278466..00000000
--- a/plugins/http/static/static.go
+++ /dev/null
@@ -1,88 +0,0 @@
-package static
-
-import (
- "io/fs"
- "net/http"
- "path/filepath"
- "strings"
-
- httpConfig "github.com/spiral/roadrunner/v2/plugins/http/config"
-)
-
-type ExtensionFilter struct {
- allowed map[string]struct{}
- forbidden map[string]struct{}
-}
-
-func NewExtensionFilter(allow, forbid []string) *ExtensionFilter {
- ef := &ExtensionFilter{
- allowed: make(map[string]struct{}, len(allow)),
- forbidden: make(map[string]struct{}, len(forbid)),
- }
-
- for i := 0; i < len(forbid); i++ {
- // skip empty lines
- if forbid[i] == "" {
- continue
- }
- ef.forbidden[forbid[i]] = struct{}{}
- }
-
- for i := 0; i < len(allow); i++ {
- // skip empty lines
- if allow[i] == "" {
- continue
- }
- ef.allowed[allow[i]] = struct{}{}
- }
-
- // check if any forbidden items presented in the allowed
- // if presented, delete such items from allowed
- for k := range ef.allowed {
- if _, ok := ef.forbidden[k]; ok {
- delete(ef.allowed, k)
- }
- }
-
- return ef
-}
-
-type FileSystem struct {
- ef *ExtensionFilter
- // embedded
- http.FileSystem
-}
-
-// Open wrapper around http.FileSystem Open method, name here is the name of the
-func (f FileSystem) Open(name string) (http.File, error) {
- file, err := f.FileSystem.Open(name)
- if err != nil {
- return nil, err
- }
-
- fstat, err := file.Stat()
- if err != nil {
- return nil, fs.ErrNotExist
- }
-
- if fstat.IsDir() {
- return nil, fs.ErrPermission
- }
-
- ext := strings.ToLower(filepath.Ext(fstat.Name()))
- if _, ok := f.ef.forbidden[ext]; ok {
- return nil, fs.ErrPermission
- }
-
- // if file extension is allowed, append it to the FileInfo slice
- if _, ok := f.ef.allowed[ext]; ok {
- return file, nil
- }
-
- return nil, fs.ErrNotExist
-}
-
-// FS is a constructor for the http.FileSystem
-func FS(config *httpConfig.Static) http.FileSystem {
- return FileSystem{NewExtensionFilter(config.Allow, config.Forbid), http.Dir(config.Dir)}
-}