summaryrefslogtreecommitdiff
path: root/plugins/static
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-12-21 19:42:23 +0300
committerValery Piashchynski <[email protected]>2020-12-21 19:42:23 +0300
commitee8b4075c0f836d698d1ae505c87c17147de447a (patch)
tree531d980e5bfb94ee39b03952a97e0445f7955409 /plugins/static
parent0ad45031047bb479e06ce0a0f496c6db9b2641c9 (diff)
Move plugins to the roadrunner-plugins repository
Diffstat (limited to 'plugins/static')
-rw-r--r--plugins/static/config.go76
-rw-r--r--plugins/static/config_test.go48
-rw-r--r--plugins/static/plugin.go110
-rw-r--r--plugins/static/tests/configs/.rr-http-static-disabled.yaml33
-rw-r--r--plugins/static/tests/configs/.rr-http-static-files-disable.yaml33
-rw-r--r--plugins/static/tests/configs/.rr-http-static-files.yaml34
-rw-r--r--plugins/static/tests/configs/.rr-http-static.yaml32
-rw-r--r--plugins/static/tests/static_plugin_test.go423
8 files changed, 0 insertions, 789 deletions
diff --git a/plugins/static/config.go b/plugins/static/config.go
deleted file mode 100644
index f5d26b2d..00000000
--- a/plugins/static/config.go
+++ /dev/null
@@ -1,76 +0,0 @@
-package static
-
-import (
- "os"
- "path"
- "strings"
-
- "github.com/spiral/errors"
-)
-
-// Config describes file location and controls access to them.
-type Config struct {
- Static struct {
- // Dir contains name of directory to control access to.
- Dir string
-
- // Forbid specifies list of file extensions which are forbidden for access.
- // Example: .php, .exe, .bat, .htaccess and etc.
- Forbid []string
-
- // Always specifies list of extensions which must always be served by static
- // service, even if file not found.
- Always []string
-
- // Request headers to add to every static.
- Request map[string]string
-
- // Response headers to add to every static.
- Response map[string]string
- }
-}
-
-// Valid returns nil if config is valid.
-func (c *Config) Valid() error {
- const op = errors.Op("static plugin validation")
- st, err := os.Stat(c.Static.Dir)
- if err != nil {
- if os.IsNotExist(err) {
- return errors.E(op, errors.Errorf("root directory '%s' does not exists", c.Static.Dir))
- }
-
- return err
- }
-
- if !st.IsDir() {
- return errors.E(op, errors.Errorf("invalid root directory '%s'", c.Static.Dir))
- }
-
- return nil
-}
-
-// AlwaysForbid must return true if file extension is not allowed for the upload.
-func (c *Config) AlwaysForbid(filename string) bool {
- ext := strings.ToLower(path.Ext(filename))
-
- for _, v := range c.Static.Forbid {
- if ext == v {
- return true
- }
- }
-
- return false
-}
-
-// AlwaysServe must indicate that file is expected to be served by static service.
-func (c *Config) AlwaysServe(filename string) bool {
- ext := strings.ToLower(path.Ext(filename))
-
- for _, v := range c.Static.Always {
- if ext == v {
- return true
- }
- }
-
- return false
-}
diff --git a/plugins/static/config_test.go b/plugins/static/config_test.go
deleted file mode 100644
index de88ded3..00000000
--- a/plugins/static/config_test.go
+++ /dev/null
@@ -1,48 +0,0 @@
-package static
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestConfig_Forbids(t *testing.T) {
- cfg := Config{Static: struct {
- Dir string
- Forbid []string
- Always []string
- Request map[string]string
- Response map[string]string
- }{Dir: "", Forbid: []string{".php"}, Always: nil, Request: nil, Response: nil}}
-
- assert.True(t, cfg.AlwaysForbid("index.php"))
- assert.True(t, cfg.AlwaysForbid("index.PHP"))
- assert.True(t, cfg.AlwaysForbid("phpadmin/index.bak.php"))
- assert.False(t, cfg.AlwaysForbid("index.html"))
-}
-
-func TestConfig_Valid(t *testing.T) {
- assert.NoError(t, (&Config{Static: struct {
- Dir string
- Forbid []string
- Always []string
- Request map[string]string
- Response map[string]string
- }{Dir: "./"}}).Valid())
-
- assert.Error(t, (&Config{Static: struct {
- Dir string
- Forbid []string
- Always []string
- Request map[string]string
- Response map[string]string
- }{Dir: "./config.go"}}).Valid())
-
- assert.Error(t, (&Config{Static: struct {
- Dir string
- Forbid []string
- Always []string
- Request map[string]string
- Response map[string]string
- }{Dir: "./dir/"}}).Valid())
-}
diff --git a/plugins/static/plugin.go b/plugins/static/plugin.go
deleted file mode 100644
index d12f84a0..00000000
--- a/plugins/static/plugin.go
+++ /dev/null
@@ -1,110 +0,0 @@
-package static
-
-import (
- "net/http"
- "path"
-
- "github.com/spiral/errors"
- "github.com/spiral/roadrunner/v2/interfaces/config"
- "github.com/spiral/roadrunner/v2/interfaces/log"
-)
-
-// ID contains default service name.
-const PluginName = "static"
-
-const RootPluginName = "http"
-
-// Plugin serves static files. Potentially convert into middleware?
-type Plugin struct {
- // server configuration (location, forbidden files and etc)
- cfg *Config
-
- log log.Logger
-
- // root is initiated http directory
- root http.Dir
-}
-
-// Init must return configure service and return true if service hasStatus enabled. Must return error in case of
-// misconfiguration. Services must not be used without proper configuration pushed first.
-func (s *Plugin) Init(cfg config.Configurer, log log.Logger) error {
- const op = errors.Op("static plugin init")
- err := cfg.UnmarshalKey(RootPluginName, &s.cfg)
- if err != nil {
- return errors.E(op, errors.Disabled, err)
- }
-
- s.log = log
- s.root = http.Dir(s.cfg.Static.Dir)
-
- err = s.cfg.Valid()
- if err != nil {
- return errors.E(op, errors.Disabled, err)
- }
-
- return nil
-}
-
-func (s *Plugin) Name() string {
- return PluginName
-}
-
-// middleware must return true if request/response pair is handled within the middleware.
-func (s *Plugin) Middleware(next http.Handler) http.HandlerFunc {
- // Define the http.HandlerFunc
- return func(w http.ResponseWriter, r *http.Request) {
- if s.cfg.Static.Request != nil {
- for k, v := range s.cfg.Static.Request {
- r.Header.Add(k, v)
- }
- }
-
- if s.cfg.Static.Response != nil {
- for k, v := range s.cfg.Static.Response {
- w.Header().Set(k, v)
- }
- }
-
- if !s.handleStatic(w, r) {
- next.ServeHTTP(w, r)
- }
- }
-}
-
-func (s *Plugin) handleStatic(w http.ResponseWriter, r *http.Request) bool {
- fPath := path.Clean(r.URL.Path)
-
- if s.cfg.AlwaysForbid(fPath) {
- return false
- }
-
- f, err := s.root.Open(fPath)
- if err != nil {
- s.log.Error("file open error", "error", err)
- if s.cfg.AlwaysServe(fPath) {
- w.WriteHeader(404)
- return true
- }
-
- return false
- }
- defer func() {
- err = f.Close()
- if err != nil {
- s.log.Error("file closing error", "error", err)
- }
- }()
-
- d, err := f.Stat()
- if err != nil {
- return false
- }
-
- // do not serve directories
- if d.IsDir() {
- return false
- }
-
- http.ServeContent(w, r, d.Name(), d.ModTime(), f)
- return true
-}
diff --git a/plugins/static/tests/configs/.rr-http-static-disabled.yaml b/plugins/static/tests/configs/.rr-http-static-disabled.yaml
deleted file mode 100644
index e8917c06..00000000
--- a/plugins/static/tests/configs/.rr-http-static-disabled.yaml
+++ /dev/null
@@ -1,33 +0,0 @@
-server:
- command: "php ../../../tests/http/client.php pid pipes"
- user: ""
- group: ""
- env:
- "RR_HTTP": "true"
- relay: "pipes"
- relayTimeout: "20s"
-
-http:
- debug: true
- address: 127.0.0.1:21234
- maxRequestSize: 1024
- middleware: [ "gzip", "static" ]
- trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
- uploads:
- forbid: [ ".php", ".exe", ".bat" ]
- static:
- dir: "abc" #not exists
- forbid: [ ".php", ".htaccess" ]
- request:
- "Example-Request-Header": "Value"
- # Automatically add headers to every response.
- response:
- "X-Powered-By": "RoadRunner"
- pool:
- numWorkers: 2
- maxJobs: 0
- allocateTimeout: 60s
- destroyTimeout: 60s
-logs:
- mode: development
- level: error \ No newline at end of file
diff --git a/plugins/static/tests/configs/.rr-http-static-files-disable.yaml b/plugins/static/tests/configs/.rr-http-static-files-disable.yaml
deleted file mode 100644
index 1cae9ed7..00000000
--- a/plugins/static/tests/configs/.rr-http-static-files-disable.yaml
+++ /dev/null
@@ -1,33 +0,0 @@
-server:
- command: "php ../../../tests/http/client.php echo pipes"
- user: ""
- group: ""
- env:
- "RR_HTTP": "true"
- relay: "pipes"
- relayTimeout: "20s"
-
-http:
- debug: true
- address: 127.0.0.1:45877
- maxRequestSize: 1024
- middleware: [ "gzip", "static" ]
- trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
- uploads:
- forbid: [ ".php", ".exe", ".bat" ]
- static:
- dir: "../../../tests"
- forbid: [ ".php" ]
- request:
- "Example-Request-Header": "Value"
- # Automatically add headers to every response.
- response:
- "X-Powered-By": "RoadRunner"
- pool:
- numWorkers: 2
- maxJobs: 0
- allocateTimeout: 60s
- destroyTimeout: 60s
-logs:
- mode: development
- level: error \ No newline at end of file
diff --git a/plugins/static/tests/configs/.rr-http-static-files.yaml b/plugins/static/tests/configs/.rr-http-static-files.yaml
deleted file mode 100644
index 32d0a6c7..00000000
--- a/plugins/static/tests/configs/.rr-http-static-files.yaml
+++ /dev/null
@@ -1,34 +0,0 @@
-server:
- command: "php ../../../tests/http/client.php echo pipes"
- user: ""
- group: ""
- env:
- "RR_HTTP": "true"
- relay: "pipes"
- relayTimeout: "20s"
-
-http:
- debug: true
- address: 127.0.0.1:34653
- maxRequestSize: 1024
- middleware: [ "gzip", "static" ]
- trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
- uploads:
- forbid: [ ".php", ".exe", ".bat" ]
- static:
- dir: "../../../tests"
- forbid: [ ".php", ".htaccess" ]
- always: [ ".ico" ]
- request:
- "Example-Request-Header": "Value"
- # Automatically add headers to every response.
- response:
- "X-Powered-By": "RoadRunner"
- pool:
- numWorkers: 2
- maxJobs: 0
- allocateTimeout: 60s
- destroyTimeout: 60s
-logs:
- mode: development
- level: error \ No newline at end of file
diff --git a/plugins/static/tests/configs/.rr-http-static.yaml b/plugins/static/tests/configs/.rr-http-static.yaml
deleted file mode 100644
index d3bd05f5..00000000
--- a/plugins/static/tests/configs/.rr-http-static.yaml
+++ /dev/null
@@ -1,32 +0,0 @@
-server:
- command: "php ../../../tests/http/client.php pid pipes"
- user: ""
- group: ""
- env:
- "RR_HTTP": "true"
- relay: "pipes"
- relayTimeout: "20s"
-
-http:
- debug: true
- address: 127.0.0.1:21603
- maxRequestSize: 1024
- middleware: [ "gzip", "static" ]
- trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
- uploads:
- forbid: [ ".php", ".exe", ".bat" ]
- static:
- dir: "../../../tests"
- forbid: [ "" ]
- request:
- "input": "custom-header"
- response:
- "output": "output-header"
- pool:
- numWorkers: 2
- maxJobs: 0
- allocateTimeout: 60s
- destroyTimeout: 60s
-logs:
- mode: development
- level: error \ No newline at end of file
diff --git a/plugins/static/tests/static_plugin_test.go b/plugins/static/tests/static_plugin_test.go
deleted file mode 100644
index 5bad54bf..00000000
--- a/plugins/static/tests/static_plugin_test.go
+++ /dev/null
@@ -1,423 +0,0 @@
-package tests
-
-import (
- "bytes"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "os/signal"
- "sync"
- "syscall"
- "testing"
- "time"
-
- "github.com/golang/mock/gomock"
- "github.com/spiral/endure"
- "github.com/spiral/roadrunner/v2/mocks"
- "github.com/spiral/roadrunner/v2/plugins/config"
- "github.com/spiral/roadrunner/v2/plugins/gzip"
- httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
- "github.com/spiral/roadrunner/v2/plugins/logger"
- "github.com/spiral/roadrunner/v2/plugins/server"
- "github.com/spiral/roadrunner/v2/plugins/static"
- "github.com/stretchr/testify/assert"
-)
-
-func TestStaticPlugin(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- assert.NoError(t, err)
-
- cfg := &config.Viper{
- Path: "configs/.rr-http-static.yaml",
- Prefix: "rr",
- }
-
- err = cont.RegisterAll(
- cfg,
- &logger.ZapLogger{},
- &server.Plugin{},
- &httpPlugin.Plugin{},
- &gzip.Gzip{},
- &static.Plugin{},
- )
- assert.NoError(t, err)
-
- err = cont.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- ch, err := cont.Serve()
- assert.NoError(t, err)
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- tt := time.NewTimer(time.Second * 10)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-ch:
- assert.Fail(t, "error", e.Error.Error())
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- case <-sig:
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- case <-tt.C:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("ServeSample", serveStaticSample)
- t.Run("StaticNotForbid", staticNotForbid)
- t.Run("StaticHeaders", staticHeaders)
- wg.Wait()
-}
-
-func staticHeaders(t *testing.T) {
- req, err := http.NewRequest("GET", "http://localhost:21603/client.php", nil)
- if err != nil {
- t.Fatal(err)
- }
-
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- t.Fatal(err)
- }
-
- if resp.Header.Get("Output") != "output-header" {
- t.Fatal("can't find output header in response")
- }
-
- b, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Fatal(err)
- }
-
- assert.Equal(t, all("../../../tests/client.php"), string(b))
- assert.Equal(t, all("../../../tests/client.php"), string(b))
-}
-
-func staticNotForbid(t *testing.T) {
- b, r, err := get("http://localhost:21603/client.php")
- assert.NoError(t, err)
- assert.Equal(t, all("../../../tests/client.php"), b)
- assert.Equal(t, all("../../../tests/client.php"), b)
- _ = r.Body.Close()
-}
-
-func serveStaticSample(t *testing.T) {
- b, r, err := get("http://localhost:21603/sample.txt")
- assert.NoError(t, err)
- assert.Equal(t, "sample", b)
- _ = r.Body.Close()
-}
-
-func TestStaticDisabled(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- assert.NoError(t, err)
-
- cfg := &config.Viper{
- Path: "configs/.rr-http-static-disabled.yaml",
- Prefix: "rr",
- }
-
- err = cont.RegisterAll(
- cfg,
- &logger.ZapLogger{},
- &server.Plugin{},
- &httpPlugin.Plugin{},
- &gzip.Gzip{},
- &static.Plugin{},
- )
- assert.NoError(t, err)
-
- err = cont.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- ch, err := cont.Serve()
- assert.NoError(t, err)
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- tt := time.NewTimer(time.Second * 10)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-ch:
- assert.Fail(t, "error", e.Error.Error())
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- case <-sig:
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- case <-tt.C:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("StaticDisabled", staticDisabled)
- wg.Wait()
-}
-
-func staticDisabled(t *testing.T) {
- _, r, err := get("http://localhost:21234/sample.txt")
- assert.Error(t, err)
- assert.Nil(t, r)
-}
-
-func TestStaticFilesDisabled(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- assert.NoError(t, err)
-
- cfg := &config.Viper{
- Path: "configs/.rr-http-static-files-disable.yaml",
- Prefix: "rr",
- }
-
- err = cont.RegisterAll(
- cfg,
- &logger.ZapLogger{},
- &server.Plugin{},
- &httpPlugin.Plugin{},
- &gzip.Gzip{},
- &static.Plugin{},
- )
- assert.NoError(t, err)
-
- err = cont.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- ch, err := cont.Serve()
- assert.NoError(t, err)
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- tt := time.NewTimer(time.Second * 10)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-ch:
- assert.Fail(t, "error", e.Error.Error())
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- case <-sig:
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- case <-tt.C:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("StaticFilesDisabled", staticFilesDisabled)
- wg.Wait()
-}
-
-func staticFilesDisabled(t *testing.T) {
- b, r, err := get("http://localhost:45877/client.php?hello=world")
- if err != nil {
- t.Fatal(err)
- }
- assert.Equal(t, "WORLD", b)
- _ = r.Body.Close()
-}
-
-func TestStaticFilesForbid(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- assert.NoError(t, err)
-
- cfg := &config.Viper{
- Path: "configs/.rr-http-static-files.yaml",
- Prefix: "rr",
- }
-
- controller := gomock.NewController(t)
- mockLogger := mocks.NewMockLogger(controller)
-
- mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").AnyTimes()
- mockLogger.EXPECT().Error("file open error", "error", gomock.Any()).AnyTimes()
-
- err = cont.RegisterAll(
- cfg,
- mockLogger,
- &server.Plugin{},
- &httpPlugin.Plugin{},
- &gzip.Gzip{},
- &static.Plugin{},
- )
- assert.NoError(t, err)
-
- err = cont.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- ch, err := cont.Serve()
- assert.NoError(t, err)
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- tt := time.NewTimer(time.Second * 10)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-ch:
- assert.Fail(t, "error", e.Error.Error())
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- case <-sig:
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- case <-tt.C:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("StaticTestFilesDir", staticTestFilesDir)
- t.Run("StaticNotFound", staticNotFound)
- t.Run("StaticFilesForbid", staticFilesForbid)
- t.Run("StaticFilesAlways", staticFilesAlways)
- wg.Wait()
-}
-
-func staticTestFilesDir(t *testing.T) {
- b, r, err := get("http://localhost:34653/http?hello=world")
- assert.NoError(t, err)
- assert.Equal(t, "WORLD", b)
- _ = r.Body.Close()
-}
-
-func staticNotFound(t *testing.T) {
- b, _, _ := get("http://localhost:34653/client.XXX?hello=world")
- assert.Equal(t, "WORLD", b)
-}
-
-func staticFilesAlways(t *testing.T) {
- _, r, err := get("http://localhost:34653/favicon.ico")
- assert.NoError(t, err)
- assert.Equal(t, 404, r.StatusCode)
- _ = r.Body.Close()
-}
-
-func staticFilesForbid(t *testing.T) {
- b, r, err := get("http://localhost:34653/client.php?hello=world")
- if err != nil {
- t.Fatal(err)
- }
- assert.Equal(t, "WORLD", b)
- _ = r.Body.Close()
-}
-
-// HELPERS
-func get(url string) (string, *http.Response, error) {
- r, err := http.Get(url)
- if err != nil {
- return "", nil, err
- }
-
- b, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return "", nil, err
- }
-
- err = r.Body.Close()
- if err != nil {
- return "", nil, err
- }
-
- return string(b), r, err
-}
-
-func all(fn string) string {
- f, _ := os.Open(fn)
-
- b := new(bytes.Buffer)
- _, err := io.Copy(b, f)
- if err != nil {
- return ""
- }
-
- err = f.Close()
- if err != nil {
- return ""
- }
-
- return b.String()
-}