2018-02-05 16:05:59 -05:00
|
|
|
package server // import "github.com/docker/docker/api/server"
|
2013-04-10 22:48:21 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-05-07 12:49:07 -04:00
|
|
|
"crypto/tls"
|
2013-06-18 14:59:56 -04:00
|
|
|
"net"
|
2013-04-10 22:48:21 -04:00
|
|
|
"net/http"
|
2013-04-19 09:24:37 -04:00
|
|
|
"strings"
|
2014-03-28 18:59:29 -04:00
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/router"
|
2017-04-08 14:43:42 -04:00
|
|
|
"github.com/docker/docker/api/server/router/debug"
|
2016-12-22 16:51:47 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-09-29 17:32:07 -04:00
|
|
|
"github.com/gorilla/mux"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-04-10 22:48:21 -04:00
|
|
|
)
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
// versionMatcher defines a variable matcher to be parsed by the router
|
|
|
|
// when a request is about to be served.
|
|
|
|
const versionMatcher = "/v{version:[0-9.]+}"
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// Config provides the configuration for the API server
|
|
|
|
type Config struct {
|
2016-04-08 19:22:39 -04:00
|
|
|
Logging bool
|
|
|
|
CorsHeaders string
|
|
|
|
Version string
|
|
|
|
SocketGroup string
|
|
|
|
TLSConfig *tls.Config
|
2015-04-16 15:48:04 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// Server contains instance details for the server
|
2015-04-17 17:32:18 -04:00
|
|
|
type Server struct {
|
2015-12-10 18:35:10 -05:00
|
|
|
cfg *Config
|
|
|
|
servers []*HTTPServer
|
|
|
|
routers []router.Router
|
|
|
|
routerSwapper *routerSwapper
|
2016-04-08 19:22:39 -04:00
|
|
|
middlewares []middleware.Middleware
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// New returns a new instance of the server based on the specified configuration.
|
2015-10-05 12:32:08 -04:00
|
|
|
// It allocates resources which will be needed for ServeAPI(ports, unix-sockets).
|
2016-02-11 13:30:23 -05:00
|
|
|
func New(cfg *Config) *Server {
|
|
|
|
return &Server{
|
2015-11-25 14:05:31 -05:00
|
|
|
cfg: cfg,
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
2016-02-11 13:30:23 -05:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
// UseMiddleware appends a new middleware to the request chain.
|
|
|
|
// This needs to be called before the API routes are configured.
|
|
|
|
func (s *Server) UseMiddleware(m middleware.Middleware) {
|
|
|
|
s.middlewares = append(s.middlewares, m)
|
|
|
|
}
|
|
|
|
|
2016-02-11 13:30:23 -05:00
|
|
|
// Accept sets a listener the server accepts connections into.
|
|
|
|
func (s *Server) Accept(addr string, listeners ...net.Listener) {
|
|
|
|
for _, listener := range listeners {
|
|
|
|
httpServer := &HTTPServer{
|
|
|
|
srv: &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
},
|
|
|
|
l: listener,
|
2015-10-05 12:32:08 -04:00
|
|
|
}
|
2016-02-11 13:30:23 -05:00
|
|
|
s.servers = append(s.servers, httpServer)
|
2015-10-05 12:32:08 -04:00
|
|
|
}
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// Close closes servers and thus stop receiving requests
|
2015-04-27 17:11:29 -04:00
|
|
|
func (s *Server) Close() {
|
|
|
|
for _, srv := range s.servers {
|
|
|
|
if err := srv.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// serveAPI loops through all initialized servers and spawns goroutine
|
2016-10-05 00:20:31 -04:00
|
|
|
// with Serve method for each. It sets createMux() as Handler also.
|
2015-12-10 18:35:10 -05:00
|
|
|
func (s *Server) serveAPI() error {
|
2015-10-05 12:32:08 -04:00
|
|
|
var chErrors = make(chan error, len(s.servers))
|
|
|
|
for _, srv := range s.servers {
|
2015-12-10 18:35:10 -05:00
|
|
|
srv.srv.Handler = s.routerSwapper
|
2015-10-05 12:32:08 -04:00
|
|
|
go func(srv *HTTPServer) {
|
|
|
|
var err error
|
2015-10-05 19:17:25 -04:00
|
|
|
logrus.Infof("API listen on %s", srv.l.Addr())
|
2015-10-05 12:32:08 -04:00
|
|
|
if err = srv.Serve(); err != nil && strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
chErrors <- err
|
|
|
|
}(srv)
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
|
|
|
|
2017-05-14 19:12:28 -04:00
|
|
|
for range s.servers {
|
2015-04-17 17:32:18 -04:00
|
|
|
err := <-chErrors
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// HTTPServer contains an instance of http server and the listener.
|
2016-11-18 02:51:36 -05:00
|
|
|
// srv *http.Server, contains configuration to create an http server and a mux router with all api end points.
|
2015-07-21 01:15:44 -04:00
|
|
|
// l net.Listener, is a TCP or Socket listener that dispatches incoming request to the router.
|
|
|
|
type HTTPServer struct {
|
2014-11-07 15:21:19 -05:00
|
|
|
srv *http.Server
|
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
// Serve starts listening for inbound requests.
|
|
|
|
func (s *HTTPServer) Serve() error {
|
2014-11-07 15:21:19 -05:00
|
|
|
return s.srv.Serve(s.l)
|
|
|
|
}
|
2015-07-21 01:15:44 -04:00
|
|
|
|
|
|
|
// Close closes the HTTPServer from listening for the inbound requests.
|
|
|
|
func (s *HTTPServer) Close() error {
|
2014-11-07 15:21:19 -05:00
|
|
|
return s.l.Close()
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
|
2013-07-13 10:59:07 -04:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2015-08-31 10:55:51 -04:00
|
|
|
// Define the context that we'll pass around to share info
|
|
|
|
// like the docker-request-id.
|
|
|
|
//
|
|
|
|
// The 'context' will be used for global data that should
|
|
|
|
// apply to all requests. Data that is specific to the
|
|
|
|
// immediate function being called should still be passed
|
|
|
|
// as 'args' on the function call.
|
2018-05-17 10:29:52 -04:00
|
|
|
|
|
|
|
// use intermediate variable to prevent "should not use basic type
|
|
|
|
// string as key in context.WithValue" golint errors
|
|
|
|
var ki interface{} = dockerversion.UAStringKey
|
|
|
|
ctx := context.WithValue(context.Background(), ki, r.Header.Get("User-Agent"))
|
2016-08-11 22:50:22 -04:00
|
|
|
handlerFunc := s.handlerWithGlobalMiddlewares(handler)
|
2015-06-04 09:30:14 -04:00
|
|
|
|
2015-10-14 13:31:09 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
if vars == nil {
|
|
|
|
vars = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handlerFunc(ctx, w, r, vars); err != nil {
|
2017-02-06 10:49:16 -05:00
|
|
|
statusCode := httputils.GetHTTPErrorStatusCode(err)
|
2017-04-05 19:59:32 -04:00
|
|
|
if statusCode >= 500 {
|
|
|
|
logrus.Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
|
2017-02-06 10:49:16 -05:00
|
|
|
}
|
2016-05-21 07:56:04 -04:00
|
|
|
httputils.MakeErrorHandler(err)(w, r)
|
2013-07-13 10:59:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 13:58:24 -05:00
|
|
|
// InitRouter initializes the list of routers for the server.
|
|
|
|
// This method also enables the Go profiler if enableProfiler is true.
|
2017-04-08 14:43:42 -04:00
|
|
|
func (s *Server) InitRouter(routers ...router.Router) {
|
2016-10-13 12:34:19 -04:00
|
|
|
s.routers = append(s.routers, routers...)
|
2015-09-23 19:42:08 -04:00
|
|
|
|
2016-02-16 13:58:24 -05:00
|
|
|
m := s.createMux()
|
|
|
|
s.routerSwapper = &routerSwapper{
|
|
|
|
router: m,
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type pageNotFoundError struct{}
|
|
|
|
|
|
|
|
func (pageNotFoundError) Error() string {
|
|
|
|
return "page not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pageNotFoundError) NotFound() {}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// createMux initializes the main router the server uses.
|
|
|
|
func (s *Server) createMux() *mux.Router {
|
2015-09-23 19:42:08 -04:00
|
|
|
m := mux.NewRouter()
|
2013-06-20 21:18:36 -04:00
|
|
|
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Debug("Registering routers")
|
2015-09-25 06:19:17 -04:00
|
|
|
for _, apiRouter := range s.routers {
|
|
|
|
for _, r := range apiRouter.Routes() {
|
2015-09-23 19:42:08 -04:00
|
|
|
f := s.makeHTTPHandler(r.Handler())
|
2015-09-25 06:19:17 -04:00
|
|
|
|
|
|
|
logrus.Debugf("Registering %s, %s", r.Method(), r.Path())
|
|
|
|
m.Path(versionMatcher + r.Path()).Methods(r.Method()).Handler(f)
|
|
|
|
m.Path(r.Path()).Methods(r.Method()).Handler(f)
|
2013-04-29 11:46:41 -04:00
|
|
|
}
|
2013-05-07 19:33:12 -04:00
|
|
|
}
|
2013-07-05 13:55:15 -04:00
|
|
|
|
2017-04-08 14:43:42 -04:00
|
|
|
debugRouter := debug.NewRouter()
|
|
|
|
s.routers = append(s.routers, debugRouter)
|
|
|
|
for _, r := range debugRouter.Routes() {
|
|
|
|
f := s.makeHTTPHandler(r.Handler())
|
|
|
|
m.Path("/debug" + r.Path()).Handler(f)
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
notFoundHandler := httputils.MakeErrorHandler(pageNotFoundError{})
|
2016-05-21 07:56:04 -04:00
|
|
|
m.HandleFunc(versionMatcher+"/{path:.*}", notFoundHandler)
|
|
|
|
m.NotFoundHandler = notFoundHandler
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return m
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
// Wait blocks the server goroutine until it exits.
|
|
|
|
// It sends an error message if there is any error during
|
|
|
|
// the API execution.
|
|
|
|
func (s *Server) Wait(waitChan chan error) {
|
|
|
|
if err := s.serveAPI(); err != nil {
|
|
|
|
logrus.Errorf("ServeAPI error: %v", err)
|
|
|
|
waitChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
waitChan <- nil
|
|
|
|
}
|