Forráskód Böngészése

Merge pull request #38569 from thaJeztah/forget_about_it

Add Cache-Control headers to disable caching /_ping endpoint
Yong Tang 6 éve
szülő
commit
393838ca5e

+ 3 - 0
api/server/router/system/system_routes.go

@@ -27,6 +27,9 @@ func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request,
 }
 }
 
 
 func (s *systemRouter) pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
 func (s *systemRouter) pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
+	w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
+	w.Header().Add("Pragma", "no-cache")
+
 	builderVersion := build.BuilderVersion(*s.features)
 	builderVersion := build.BuilderVersion(*s.features)
 	if bv := builderVersion; bv != "" {
 	if bv := builderVersion; bv != "" {
 		w.Header().Set("Builder-Version", string(bv))
 		w.Header().Set("Builder-Version", string(bv))

+ 13 - 0
api/swagger.yaml

@@ -7115,10 +7115,23 @@ paths:
             Docker-Experimental:
             Docker-Experimental:
               type: "boolean"
               type: "boolean"
               description: "If the server is running with experimental mode enabled"
               description: "If the server is running with experimental mode enabled"
+            Cache-Control:
+              type: "string"
+              default: "no-cache, no-store, must-revalidate"
+            Pragma:
+              type: "string"
+              default: "no-cache"
         500:
         500:
           description: "server error"
           description: "server error"
           schema:
           schema:
             $ref: "#/definitions/ErrorResponse"
             $ref: "#/definitions/ErrorResponse"
+          headers:
+            Cache-Control:
+              type: "string"
+              default: "no-cache, no-store, must-revalidate"
+            Pragma:
+              type: "string"
+              default: "no-cache"
       tags: ["System"]
       tags: ["System"]
   /commit:
   /commit:
     post:
     post:

+ 3 - 0
docs/api/version-history.md

@@ -17,6 +17,9 @@ keywords: "API, Docker, rcli, REST, documentation"
 
 
 [Docker Engine API v1.40](https://docs.docker.com/engine/api/v1.40/) documentation
 [Docker Engine API v1.40](https://docs.docker.com/engine/api/v1.40/) documentation
 
 
+* `GET /_ping` now sets `Cache-Control` and `Pragma` headers to prevent the result
+  from being cached. This change is not versioned, and affects all API versions
+  if the daemon has this patch.
 * `GET /services` now returns `Sysctls` as part of the `ContainerSpec`.
 * `GET /services` now returns `Sysctls` as part of the `ContainerSpec`.
 * `GET /services/{id}` now returns `Sysctls` as part of the `ContainerSpec`.
 * `GET /services/{id}` now returns `Sysctls` as part of the `ContainerSpec`.
 * `POST /services/create` now accepts `Sysctls` as part of the `ContainerSpec`.
 * `POST /services/create` now accepts `Sysctls` as part of the `ContainerSpec`.

+ 29 - 0
integration/system/ping_test.go

@@ -0,0 +1,29 @@
+package system // import "github.com/docker/docker/integration/system"
+
+import (
+	"net/http"
+	"strings"
+	"testing"
+
+	"github.com/docker/docker/internal/test/request"
+	"gotest.tools/assert"
+)
+
+func TestPingCacheHeaders(t *testing.T) {
+	defer setupTest(t)()
+
+	res, _, err := request.Get("/_ping")
+	assert.NilError(t, err)
+	assert.Equal(t, res.StatusCode, http.StatusOK)
+
+	assert.Equal(t, hdr(res, "Cache-Control"), "no-cache, no-store, must-revalidate")
+	assert.Equal(t, hdr(res, "Pragma"), "no-cache")
+}
+
+func hdr(res *http.Response, name string) string {
+	val, ok := res.Header[http.CanonicalHeaderKey(name)]
+	if !ok || len(val) == 0 {
+		return ""
+	}
+	return strings.Join(val, ", ")
+}