Browse code

Merge pull request #38569 from thaJeztah/forget_about_it

Add Cache-Control headers to disable caching /_ping endpoint

Yong Tang authored on 2019/02/01 00:59:11
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))
... ...
@@ -7115,10 +7115,23 @@ paths:
7115 7115
             Docker-Experimental:
7116 7116
               type: "boolean"
7117 7117
               description: "If the server is running with experimental mode enabled"
7118
+            Cache-Control:
7119
+              type: "string"
7120
+              default: "no-cache, no-store, must-revalidate"
7121
+            Pragma:
7122
+              type: "string"
7123
+              default: "no-cache"
7118 7124
         500:
7119 7125
           description: "server error"
7120 7126
           schema:
7121 7127
             $ref: "#/definitions/ErrorResponse"
7128
+          headers:
7129
+            Cache-Control:
7130
+              type: "string"
7131
+              default: "no-cache, no-store, must-revalidate"
7132
+            Pragma:
7133
+              type: "string"
7134
+              default: "no-cache"
7122 7135
       tags: ["System"]
7123 7136
   /commit:
7124 7137
     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
+}