github.com/boynux/docker@v1.11.0-rc4/api/server/middleware/version.go (about)

     1  package middleware
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"runtime"
     7  
     8  	"github.com/docker/docker/api/server/httputils"
     9  	"github.com/docker/docker/pkg/version"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  type badRequestError struct {
    14  	error
    15  }
    16  
    17  func (badRequestError) HTTPErrorStatusCode() int {
    18  	return http.StatusBadRequest
    19  }
    20  
    21  // NewVersionMiddleware creates a new Version middleware.
    22  func NewVersionMiddleware(versionCheck string, defaultVersion, minVersion version.Version) Middleware {
    23  	serverVersion := version.Version(versionCheck)
    24  
    25  	return func(handler httputils.APIFunc) httputils.APIFunc {
    26  		return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    27  			apiVersion := version.Version(vars["version"])
    28  			if apiVersion == "" {
    29  				apiVersion = defaultVersion
    30  			}
    31  
    32  			if apiVersion.GreaterThan(defaultVersion) {
    33  				return badRequestError{fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, defaultVersion)}
    34  			}
    35  			if apiVersion.LessThan(minVersion) {
    36  				return badRequestError{fmt.Errorf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", apiVersion, minVersion)}
    37  			}
    38  
    39  			header := fmt.Sprintf("Docker/%s (%s)", serverVersion, runtime.GOOS)
    40  			w.Header().Set("Server", header)
    41  			ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion)
    42  			return handler(ctx, w, r, vars)
    43  		}
    44  	}
    45  }