Browse code

Add Cache-Control headers to disable caching /_ping endpoint

The result of this endpoint should not be cached, so it's better to
explicitly disable caching.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/01/15 01:00:47
Showing 4 changed files
... ...
@@ -27,6 +27,9 @@ func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request,
27 27
 }
28 28
 
29 29
 func (s *systemRouter) pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
30
+	w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
31
+	w.Header().Add("Pragma", "no-cache")
32
+
30 33
 	builderVersion := build.BuilderVersion(*s.features)
31 34
 	if bv := builderVersion; bv != "" {
32 35
 		w.Header().Set("Builder-Version", string(bv))
... ...
@@ -7107,10 +7107,23 @@ paths:
7107 7107
             Docker-Experimental:
7108 7108
               type: "boolean"
7109 7109
               description: "If the server is running with experimental mode enabled"
7110
+            Cache-Control:
7111
+              type: "string"
7112
+              default: "no-cache, no-store, must-revalidate"
7113
+            Pragma:
7114
+              type: "string"
7115
+              default: "no-cache"
7110 7116
         500:
7111 7117
           description: "server error"
7112 7118
           schema:
7113 7119
             $ref: "#/definitions/ErrorResponse"
7120
+          headers:
7121
+            Cache-Control:
7122
+              type: "string"
7123
+              default: "no-cache, no-store, must-revalidate"
7124
+            Pragma:
7125
+              type: "string"
7126
+              default: "no-cache"
7114 7127
       tags: ["System"]
7115 7128
   /commit:
7116 7129
     post:
... ...
@@ -17,6 +17,9 @@ keywords: "API, Docker, rcli, REST, documentation"
17 17
 
18 18
 [Docker Engine API v1.40](https://docs.docker.com/engine/api/v1.40/) documentation
19 19
 
20
+* `GET /_ping` now sets `Cache-Control` and `Pragma` headers to prevent the result
21
+  from being cached. This change is not versioned, and affects all API versions
22
+  if the daemon has this patch.
20 23
 * `GET /services` now returns `Sysctls` as part of the `ContainerSpec`.
21 24
 * `GET /services/{id}` now returns `Sysctls` as part of the `ContainerSpec`.
22 25
 * `POST /services/create` now accepts `Sysctls` as part of the `ContainerSpec`.
23 26
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+package system // import "github.com/docker/docker/integration/system"
1
+
2
+import (
3
+	"net/http"
4
+	"strings"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/internal/test/request"
8
+	"gotest.tools/assert"
9
+)
10
+
11
+func TestPingCacheHeaders(t *testing.T) {
12
+	defer setupTest(t)()
13
+
14
+	res, _, err := request.Get("/_ping")
15
+	assert.NilError(t, err)
16
+	assert.Equal(t, res.StatusCode, http.StatusOK)
17
+
18
+	assert.Equal(t, hdr(res, "Cache-Control"), "no-cache, no-store, must-revalidate")
19
+	assert.Equal(t, hdr(res, "Pragma"), "no-cache")
20
+}
21
+
22
+func hdr(res *http.Response, name string) string {
23
+	val, ok := res.Header[http.CanonicalHeaderKey(name)]
24
+	if !ok || len(val) == 0 {
25
+		return ""
26
+	}
27
+	return strings.Join(val, ", ")
28
+}