summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-27 09:44:05 +0000
committerGitHub <[email protected]>2020-08-27 09:44:05 +0000
commit9103939fa98de53170d2bf0e8cd74529786d7ab2 (patch)
tree06f7b96b861ad32642a8a70fdbeaf49ae755c3d0
parent88b394324165e3e16253c27552b0d4d7d6628a81 (diff)
parent66848c53fa63439c5c77857187b86f5bf31d1321 (diff)
Merge #345
345: Adding headers to static files r=48d90782 a=siad007 Fixes #336 Related to #335 Co-authored-by: Siad Ardroumli <[email protected]> Co-authored-by: Valery Piashchynski <[email protected]>
-rw-r--r--.rr.yaml8
-rw-r--r--service/static/config.go6
-rw-r--r--service/static/config_test.go2
-rw-r--r--service/static/service.go12
-rw-r--r--service/static/service_test.go101
5 files changed, 109 insertions, 20 deletions
diff --git a/.rr.yaml b/.rr.yaml
index ab2bb843..2d47a1d5 100644
--- a/.rr.yaml
+++ b/.rr.yaml
@@ -162,6 +162,14 @@ static:
# list of extensions for forbid for serving.
forbid: [".php", ".htaccess"]
+ # Automatically add headers to every request.
+ request:
+ "Example-Request-Header": "Value"
+
+ # Automatically add headers to every response.
+ response:
+ "X-Powered-By": "RoadRunner"
+
# health service configuration
health:
# http host to serve health requests.
diff --git a/service/static/config.go b/service/static/config.go
index eda459a7..3ca20a83 100644
--- a/service/static/config.go
+++ b/service/static/config.go
@@ -20,6 +20,12 @@ type Config struct {
// 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
}
// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
diff --git a/service/static/config_test.go b/service/static/config_test.go
index 442d87f9..8bf0d372 100644
--- a/service/static/config_test.go
+++ b/service/static/config_test.go
@@ -16,7 +16,7 @@ func (cfg *mockCfg) Unmarshal(out interface{}) error {
}
func Test_Config_Hydrate(t *testing.T) {
- cfg := &mockCfg{`{"dir": "./"}`}
+ cfg := &mockCfg{`{"dir": "./", "request":{"foo": "bar"}, "response":{"xxx": "yyy"}}`}
c := &Config{}
assert.NoError(t, c.Hydrate(cfg))
diff --git a/service/static/service.go b/service/static/service.go
index b824e787..95b99860 100644
--- a/service/static/service.go
+++ b/service/static/service.go
@@ -36,6 +36,18 @@ func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) {
func (s *Service) middleware(f http.HandlerFunc) http.HandlerFunc {
// Define the http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
+ if s.cfg.Request != nil {
+ for k, v := range s.cfg.Request {
+ r.Header.Add(k, v)
+ }
+ }
+
+ if s.cfg.Response != nil {
+ for k, v := range s.cfg.Response {
+ w.Header().Set(k, v)
+ }
+ }
+
if !s.handleStatic(w, r) {
f(w, r)
}
diff --git a/service/static/service_test.go b/service/static/service_test.go
index 1a137cbc..842662c9 100644
--- a/service/static/service_test.go
+++ b/service/static/service_test.go
@@ -37,25 +37,6 @@ func (cfg *testCfg) Unmarshal(out interface{}) error {
return j.Unmarshal([]byte(cfg.target), out)
}
-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 Test_Files(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
@@ -442,6 +423,88 @@ func Test_Files_NotForbid(t *testing.T) {
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