mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
bcb0405239
Signed-off-by: Daniel Nephin <dnephin@docker.com>
52 lines
1.8 KiB
Go
52 lines
1.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"runtime"
|
|
|
|
"github.com/docker/docker/api/errors"
|
|
"github.com/docker/docker/api/types/versions"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
// VersionMiddleware is a middleware that
|
|
// validates the client and server versions.
|
|
type VersionMiddleware struct {
|
|
serverVersion string
|
|
defaultVersion string
|
|
minVersion string
|
|
}
|
|
|
|
// NewVersionMiddleware creates a new VersionMiddleware
|
|
// with the default versions.
|
|
func NewVersionMiddleware(s, d, m string) VersionMiddleware {
|
|
return VersionMiddleware{
|
|
serverVersion: s,
|
|
defaultVersion: d,
|
|
minVersion: m,
|
|
}
|
|
}
|
|
|
|
// WrapHandler returns a new handler function wrapping the previous one in the request chain.
|
|
func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
apiVersion := vars["version"]
|
|
if apiVersion == "" {
|
|
apiVersion = v.defaultVersion
|
|
}
|
|
|
|
if versions.GreaterThan(apiVersion, v.defaultVersion) {
|
|
return errors.NewBadRequestError(fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, v.defaultVersion))
|
|
}
|
|
if versions.LessThan(apiVersion, v.minVersion) {
|
|
return errors.NewBadRequestError(fmt.Errorf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", apiVersion, v.minVersion))
|
|
}
|
|
|
|
header := fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS)
|
|
w.Header().Set("Server", header)
|
|
ctx = context.WithValue(ctx, "api-version", apiVersion)
|
|
return handler(ctx, w, r, vars)
|
|
}
|
|
|
|
}
|