summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-01-10 11:53:16 +0300
committerWolfy-J <[email protected]>2019-01-10 11:53:16 +0300
commit35bd2c861daf7a3895dc9d99642b53f2107bf5bf (patch)
treea47f3ef674824e0138783d443bb6c0afb483314e /service
parentd1532db3043a1038f287fe31d1f2537d37d4d3a9 (diff)
always serve files
Diffstat (limited to 'service')
-rw-r--r--service/http/uploads_config.go2
-rw-r--r--service/static/config.go21
-rw-r--r--service/static/config_test.go8
-rw-r--r--service/static/service.go7
-rw-r--r--service/static/service_test.go37
5 files changed, 67 insertions, 8 deletions
diff --git a/service/http/uploads_config.go b/service/http/uploads_config.go
index 3f655064..9f62d779 100644
--- a/service/http/uploads_config.go
+++ b/service/http/uploads_config.go
@@ -31,7 +31,7 @@ func (cfg *UploadsConfig) TmpDir() string {
return os.TempDir()
}
-// Forbids must return true if file extension is not allowed for the upload.
+// AlwaysForbid must return true if file extension is not allowed for the upload.
func (cfg *UploadsConfig) Forbids(filename string) bool {
ext := strings.ToLower(path.Ext(filename))
diff --git a/service/static/config.go b/service/static/config.go
index 5df7b013..ebc9af2a 100644
--- a/service/static/config.go
+++ b/service/static/config.go
@@ -16,6 +16,10 @@ type Config struct {
// Forbid specifies list of file extensions which are forbidden for access.
// Example: .php, .exe, .bat, .htaccess and etc.
Forbid []string
+
+ // Serve specifies list of exceptions which must always be served by static
+ // service, even if file not found.
+ Always []string
}
// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
@@ -45,8 +49,8 @@ func (c *Config) Valid() error {
return nil
}
-// Forbids must return true if file extension is not allowed for the upload.
-func (c *Config) Forbids(filename string) bool {
+// 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.Forbid {
@@ -57,3 +61,16 @@ func (c *Config) Forbids(filename string) bool {
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.Always {
+ if ext == v {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/service/static/config_test.go b/service/static/config_test.go
index e3fa8d16..0221e116 100644
--- a/service/static/config_test.go
+++ b/service/static/config_test.go
@@ -29,10 +29,10 @@ func Test_Config_Hydrate_Error(t *testing.T) {
func TestConfig_Forbids(t *testing.T) {
cfg := Config{Forbid: []string{".php"}}
- assert.True(t, cfg.Forbids("index.php"))
- assert.True(t, cfg.Forbids("index.PHP"))
- assert.True(t, cfg.Forbids("phpadmin/index.bak.php"))
- assert.False(t, cfg.Forbids("index.html"))
+ 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) {
diff --git a/service/static/service.go b/service/static/service.go
index 2cb419fe..b824e787 100644
--- a/service/static/service.go
+++ b/service/static/service.go
@@ -45,12 +45,17 @@ func (s *Service) middleware(f http.HandlerFunc) http.HandlerFunc {
func (s *Service) handleStatic(w http.ResponseWriter, r *http.Request) bool {
fPath := path.Clean(r.URL.Path)
- if s.cfg.Forbids(fPath) {
+ if s.cfg.AlwaysForbid(fPath) {
return false
}
f, err := s.root.Open(fPath)
if err != nil {
+ if s.cfg.AlwaysServe(fPath) {
+ w.WriteHeader(404)
+ return true
+ }
+
return false
}
defer f.Close()
diff --git a/service/static/service_test.go b/service/static/service_test.go
index fbc26a58..af616418 100644
--- a/service/static/service_test.go
+++ b/service/static/service_test.go
@@ -234,6 +234,43 @@ func Test_Files_Forbid(t *testing.T) {
assert.Equal(t, "WORLD", b)
}
+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": ":6029",
+ "maxRequest": 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() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ _, r, _ := get("http://localhost:6029/favicon.ico")
+ assert.Equal(t, 404, r.StatusCode)
+}
+
func Test_Files_NotFound(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)