diff options
-rwxr-xr-x | .github/workflows/ci-build.yml | 4 | ||||
-rwxr-xr-x | .golangci.yml | 19 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | plugins/gzip/plugin.go | 3 | ||||
-rw-r--r-- | plugins/static/config.go | 76 | ||||
-rw-r--r-- | plugins/static/config_test.go | 48 | ||||
-rw-r--r-- | plugins/static/plugin.go | 110 | ||||
-rw-r--r-- | plugins/static/tests/configs/.rr-http-static.yaml | 30 | ||||
-rw-r--r-- | plugins/static/tests/static_plugin_test.go | 142 | ||||
-rw-r--r-- | plugins/static/tests/static_tests_old.go | 476 |
10 files changed, 899 insertions, 13 deletions
diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 374f690a..6a7af214 100755 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -77,12 +77,14 @@ jobs: go test -v -race ./plugins/http/attributes -tags=debug -coverprofile=attributes.txt -covermode=atomic go test -v -race ./plugins/http/tests -tags=debug -coverprofile=http_tests.txt -covermode=atomic go test -v -race ./plugins/gzip/tests -tags=debug -coverprofile=gzip.txt -covermode=atomic + go test -v -race -cover ./plugins/static/tests -tags=debug -coverprofile=static.txt -covermode=atomic + go test -v -race -cover ./plugins/static -tags=debug -coverprofile=static_root.txt -covermode=atomic - name: Run code coverage uses: codecov/codecov-action@v1 with: token: ${{ secrets.CODECOV_TOKEN }} - files: gzip.txt, lib.txt, rpc_config.txt, rpc.txt, plugin_config.txt, logger.txt, server.txt, metrics.txt, informer.txt attributes.txt http_tests.txt + files: static.txt, static_root.txt, gzip.txt, lib.txt, rpc_config.txt, rpc.txt, plugin_config.txt, logger.txt, server.txt, metrics.txt, informer.txt attributes.txt http_tests.txt flags: unittests name: codecov-umbrella fail_ci_if_error: false diff --git a/.golangci.yml b/.golangci.yml index 59dc0ae6..a49abafb 100755 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,7 @@ run: - plugins/http/tests/plugin_test_old.go - plugins/http/tests/rpc_test_old.go - plugins/http/tests/config_test.go + - plugins/static/tests/static_plugin_test.go linters: disable-all: true enable: @@ -11,28 +12,28 @@ linters: - deadcode - depguard - dogsled -# - dupl + # - dupl - errcheck - exhaustive -# - funlen + # - funlen - gochecknoinits -# - goconst + # - goconst - gocritic - gocyclo - gofmt - goimports - golint -# - gomnd + # - gomnd - goprintffuncname - gosec -# - gosimple + # - gosimple - govet - ineffassign - interfacer -# - lll + # - lll - misspell - nakedret -# - noctx + # - noctx - nolintlint - rowserrcheck - scopelint @@ -41,8 +42,8 @@ linters: - stylecheck - typecheck - unconvert -# - unparam -# - unused + # - unparam + # - unused - varcheck - whitespace @@ -11,4 +11,6 @@ test: go test -v -race -cover ./plugins/resetter/tests -tags=debug go test -v -race -cover ./plugins/http/attributes -tags=debug go test -v -race -cover ./plugins/http/tests -tags=debug - go test -v -race -cover ./plugins/gzip/tests -tags=debug
\ No newline at end of file + go test -v -race -cover ./plugins/gzip/tests -tags=debug + go test -v -race -cover ./plugins/static/tests -tags=debug + go test -v -race -cover ./plugins/static -tags=debug
\ No newline at end of file diff --git a/plugins/gzip/plugin.go b/plugins/gzip/plugin.go index f5a0f4ea..e5b9e4f5 100644 --- a/plugins/gzip/plugin.go +++ b/plugins/gzip/plugin.go @@ -8,8 +8,7 @@ import ( const PluginName = "gzip" -type Gzip struct { -} +type Gzip struct{} func (g *Gzip) Init() error { return nil diff --git a/plugins/static/config.go b/plugins/static/config.go new file mode 100644 index 00000000..f5d26b2d --- /dev/null +++ b/plugins/static/config.go @@ -0,0 +1,76 @@ +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 new file mode 100644 index 00000000..de88ded3 --- /dev/null +++ b/plugins/static/config_test.go @@ -0,0 +1,48 @@ +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 new file mode 100644 index 00000000..cf5cee25 --- /dev/null +++ b/plugins/static/plugin.go @@ -0,0 +1,110 @@ +package static + +import ( + "net/http" + "path" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/interfaces/log" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +// 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.yaml b/plugins/static/tests/configs/.rr-http-static.yaml new file mode 100644 index 00000000..f717dcf8 --- /dev/null +++ b/plugins/static/tests/configs/.rr-http-static.yaml @@ -0,0 +1,30 @@ +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: [ ".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
\ No newline at end of file diff --git a/plugins/static/tests/static_plugin_test.go b/plugins/static/tests/static_plugin_test.go new file mode 100644 index 00000000..ef09952d --- /dev/null +++ b/plugins/static/tests/static_plugin_test.go @@ -0,0 +1,142 @@ +package tests + +import ( + "bytes" + "io" + "io/ioutil" + "net/http" + "os" + "os/signal" + "sync" + "syscall" + "testing" + "time" + + j "github.com/json-iterator/go" + "github.com/spiral/endure" + "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" +) + +var json = j.ConfigCompatibleWithStandardLibrary + +func TestStaticPlugin(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + 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) + + go func() { + tt := time.NewTimer(time.Second * 10) + 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 + } + } + }() + + t.Run("ServeSample", serveStaticSample) + wg.Wait() +} + +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 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 tmpDir() string { + p := os.TempDir() + + r, _ := j.Marshal(p) + + return string(r) +} + +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() +} diff --git a/plugins/static/tests/static_tests_old.go b/plugins/static/tests/static_tests_old.go new file mode 100644 index 00000000..96a65178 --- /dev/null +++ b/plugins/static/tests/static_tests_old.go @@ -0,0 +1,476 @@ +package tests + +//package static +// +//import ( +//"bytes" +//json "github.com/json-iterator/go" +//"github.com/sirupsen/logrus" +//"github.com/sirupsen/logrus/hooks/test" +//"github.com/spiral/roadrunner/service" +//rrhttp "github.com/spiral/roadrunner/service/http" +//"github.com/stretchr/testify/assert" +//"io" +//"io/ioutil" +//"net/http" +//"os" +//"testing" +//"time" +//) +// +//type testCfg struct { +// httpCfg string +// static string +// target string +//} +// + +// +//func Test_Disabled(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, +// })) +// +// s, st := c.Get(ID) +// assert.NotNil(t, s) +// assert.Equal(t, service.StatusInactive, st) +//} +// +//func Test_Files_Disable(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":false, "dir":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8030", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Second) +// +// b, _, err := get("http://localhost:8030/client.php?hello=world") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Error(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.Error(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8031", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +//} +// +//func Test_Files_Error2(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.Error(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]`, +// httpCfg: `{ +// "enable": true, +// "address": ":8032", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +//} +// +//func Test_Files_Forbid(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8033", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// time.Sleep(time.Millisecond * 500) +// +// b, _, err := get("http://localhost:8033/client.php?hello=world") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Always(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[".php"], "always":[".ico"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8034", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// _, r, err := get("http://localhost:8034/favicon.ico") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, 404, r.StatusCode) +// c.Stop() +//} +// +//func Test_Files_NotFound(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8035", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8035/client.XXX?hello=world") +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Dir(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8036", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8036/http?hello=world") +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_NotForbid(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8037", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php pid pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8037/client.php") +// assert.Equal(t, all("../../tests/client.php"), b) +// assert.Equal(t, all("../../tests/client.php"), b) +// c.Stop() +//} +// +//func TestStatic_Headers(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(rrhttp.ID, &rrhttp.Service{}) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[], "request":{"input": "custom-header"}, "response":{"output": "output-header"}}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8037", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php pid pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// req, err := http.NewRequest("GET", "http://localhost:8037/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)) +// c.Stop() +//} +// +//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 tmpDir() string { +// p := os.TempDir() +// j := json.ConfigCompatibleWithStandardLibrary +// r, _ := j.Marshal(p) +// +// return string(r) +//} +// +//func all(fn string) string { +// f, _ := os.Open(fn) +// +// b := &bytes.Buffer{} +// _, err := io.Copy(b, f) +// if err != nil { +// return "" +// } +// +// err = f.Close() +// if err != nil { +// return "" +// } +// +// return b.String() +//} |