middleware.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package server
  2. import (
  3. "net/http"
  4. "runtime"
  5. "strings"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api"
  8. "github.com/docker/docker/api/server/httputils"
  9. "github.com/docker/docker/autogen/dockerversion"
  10. "github.com/docker/docker/errors"
  11. "github.com/docker/docker/pkg/version"
  12. "golang.org/x/net/context"
  13. )
  14. // middleware is an adapter to allow the use of ordinary functions as Docker API filters.
  15. // Any function that has the appropriate signature can be register as a middleware.
  16. type middleware func(handler httputils.APIFunc) httputils.APIFunc
  17. // loggingMiddleware logs each request when logging is enabled.
  18. func (s *Server) loggingMiddleware(handler httputils.APIFunc) httputils.APIFunc {
  19. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  20. if s.cfg.Logging {
  21. logrus.Infof("%s %s", r.Method, r.RequestURI)
  22. }
  23. return handler(ctx, w, r, vars)
  24. }
  25. }
  26. // userAgentMiddleware checks the User-Agent header looking for a valid docker client spec.
  27. func (s *Server) userAgentMiddleware(handler httputils.APIFunc) httputils.APIFunc {
  28. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  29. if strings.Contains(r.Header.Get("User-Agent"), "Docker-Client/") {
  30. dockerVersion := version.Version(s.cfg.Version)
  31. userAgent := strings.Split(r.Header.Get("User-Agent"), "/")
  32. // v1.20 onwards includes the GOOS of the client after the version
  33. // such as Docker/1.7.0 (linux)
  34. if len(userAgent) == 2 && strings.Contains(userAgent[1], " ") {
  35. userAgent[1] = strings.Split(userAgent[1], " ")[0]
  36. }
  37. if len(userAgent) == 2 && !dockerVersion.Equal(version.Version(userAgent[1])) {
  38. logrus.Debugf("Warning: client and server don't have the same version (client: %s, server: %s)", userAgent[1], dockerVersion)
  39. }
  40. }
  41. return handler(ctx, w, r, vars)
  42. }
  43. }
  44. // corsMiddleware sets the CORS header expectations in the server.
  45. func (s *Server) corsMiddleware(handler httputils.APIFunc) httputils.APIFunc {
  46. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  47. // If "api-cors-header" is not given, but "api-enable-cors" is true, we set cors to "*"
  48. // otherwise, all head values will be passed to HTTP handler
  49. corsHeaders := s.cfg.CorsHeaders
  50. if corsHeaders == "" && s.cfg.EnableCors {
  51. corsHeaders = "*"
  52. }
  53. if corsHeaders != "" {
  54. writeCorsHeaders(w, r, corsHeaders)
  55. }
  56. return handler(ctx, w, r, vars)
  57. }
  58. }
  59. // versionMiddleware checks the api version requirements before passing the request to the server handler.
  60. func versionMiddleware(handler httputils.APIFunc) httputils.APIFunc {
  61. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  62. apiVersion := version.Version(vars["version"])
  63. if apiVersion == "" {
  64. apiVersion = api.Version
  65. }
  66. if apiVersion.GreaterThan(api.Version) {
  67. return errors.ErrorCodeNewerClientVersion.WithArgs(apiVersion, api.Version)
  68. }
  69. if apiVersion.LessThan(api.MinVersion) {
  70. return errors.ErrorCodeOldClientVersion.WithArgs(apiVersion, api.Version)
  71. }
  72. w.Header().Set("Server", "Docker/"+dockerversion.VERSION+" ("+runtime.GOOS+")")
  73. ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion)
  74. return handler(ctx, w, r, vars)
  75. }
  76. }
  77. // handleWithGlobalMiddlwares wraps the handler function for a request with
  78. // the server's global middlewares. The order of the middlewares is backwards,
  79. // meaning that the first in the list will be evaludated last.
  80. //
  81. // Example: handleWithGlobalMiddlewares(s.getContainersName)
  82. //
  83. // s.loggingMiddleware(
  84. // s.userAgentMiddleware(
  85. // s.corsMiddleware(
  86. // versionMiddleware(s.getContainersName)
  87. // )
  88. // )
  89. // )
  90. // )
  91. func (s *Server) handleWithGlobalMiddlewares(handler httputils.APIFunc) httputils.APIFunc {
  92. middlewares := []middleware{
  93. versionMiddleware,
  94. s.corsMiddleware,
  95. s.userAgentMiddleware,
  96. s.loggingMiddleware,
  97. }
  98. h := handler
  99. for _, m := range middlewares {
  100. h = m(h)
  101. }
  102. return h
  103. }