2014-03-28 22:59:29 +00:00
|
|
|
package server
|
2013-04-11 02:48:21 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-07 16:49:07 +00:00
|
|
|
"crypto/tls"
|
2013-06-18 18:59:56 +00:00
|
|
|
"net"
|
2013-04-11 02:48:21 +00:00
|
|
|
"net/http"
|
2013-04-19 13:24:37 +00:00
|
|
|
"strings"
|
2014-03-28 22:59:29 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-23 23:42:08 +00:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
|
|
"github.com/docker/docker/api/server/router"
|
2015-11-12 11:06:47 +00:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
2015-09-18 17:48:16 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2015-09-29 21:32:07 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"golang.org/x/net/context"
|
2013-04-11 02:48:21 +00:00
|
|
|
)
|
|
|
|
|
2015-09-25 10:19:17 +00: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 05:15:44 +00:00
|
|
|
// Config provides the configuration for the API server
|
|
|
|
type Config struct {
|
2016-01-13 00:38:18 +00:00
|
|
|
Logging bool
|
|
|
|
EnableCors bool
|
|
|
|
CorsHeaders string
|
|
|
|
AuthorizationPluginNames []string
|
|
|
|
Version string
|
|
|
|
SocketGroup string
|
|
|
|
TLSConfig *tls.Config
|
2015-04-16 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// Server contains instance details for the server
|
2015-04-17 21:32:18 +00:00
|
|
|
type Server struct {
|
2015-12-10 23:35:10 +00:00
|
|
|
cfg *Config
|
|
|
|
servers []*HTTPServer
|
|
|
|
routers []router.Router
|
|
|
|
authZPlugins []authorization.Plugin
|
|
|
|
routerSwapper *routerSwapper
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// New returns a new instance of the server based on the specified configuration.
|
2015-10-05 16:32:08 +00:00
|
|
|
// It allocates resources which will be needed for ServeAPI(ports, unix-sockets).
|
2016-02-11 18:30:23 +00:00
|
|
|
func New(cfg *Config) *Server {
|
|
|
|
return &Server{
|
2015-11-25 19:05:31 +00:00
|
|
|
cfg: cfg,
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
2016-02-11 18:30:23 +00: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 16:32:08 +00:00
|
|
|
}
|
2016-02-11 18:30:23 +00:00
|
|
|
s.servers = append(s.servers, httpServer)
|
2015-10-05 16:32:08 +00:00
|
|
|
}
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// Close closes servers and thus stop receiving requests
|
2015-04-27 21:11:29 +00:00
|
|
|
func (s *Server) Close() {
|
|
|
|
for _, srv := range s.servers {
|
|
|
|
if err := srv.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// serveAPI loops through all initialized servers and spawns goroutine
|
|
|
|
// with Server method for each. It sets createMux() as Handler also.
|
|
|
|
func (s *Server) serveAPI() error {
|
|
|
|
s.initRouterSwapper()
|
|
|
|
|
2015-10-05 16:32:08 +00:00
|
|
|
var chErrors = make(chan error, len(s.servers))
|
|
|
|
for _, srv := range s.servers {
|
2015-12-10 23:35:10 +00:00
|
|
|
srv.srv.Handler = s.routerSwapper
|
2015-10-05 16:32:08 +00:00
|
|
|
go func(srv *HTTPServer) {
|
|
|
|
var err error
|
2015-10-05 23:17:25 +00:00
|
|
|
logrus.Infof("API listen on %s", srv.l.Addr())
|
2015-10-05 16:32:08 +00:00
|
|
|
if err = srv.Serve(); err != nil && strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
chErrors <- err
|
|
|
|
}(srv)
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 16:32:08 +00:00
|
|
|
for i := 0; i < len(s.servers); i++ {
|
2015-04-17 21:32:18 +00:00
|
|
|
err := <-chErrors
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// HTTPServer contains an instance of http server and the listener.
|
|
|
|
// srv *http.Server, contains configuration to create a http server and a mux router with all api end points.
|
|
|
|
// l net.Listener, is a TCP or Socket listener that dispatches incoming request to the router.
|
|
|
|
type HTTPServer struct {
|
2014-11-07 20:21:19 +00:00
|
|
|
srv *http.Server
|
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// Serve starts listening for inbound requests.
|
|
|
|
func (s *HTTPServer) Serve() error {
|
2014-11-07 20:21:19 +00:00
|
|
|
return s.srv.Serve(s.l)
|
|
|
|
}
|
2015-07-21 05:15:44 +00:00
|
|
|
|
|
|
|
// Close closes the HTTPServer from listening for the inbound requests.
|
|
|
|
func (s *HTTPServer) Close() error {
|
2014-11-07 20:21:19 +00:00
|
|
|
return s.l.Close()
|
|
|
|
}
|
|
|
|
|
2015-02-09 07:15:07 +00:00
|
|
|
func writeCorsHeaders(w http.ResponseWriter, r *http.Request, corsHeaders string) {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("CORS header is enabled and set to: %s", corsHeaders)
|
2015-02-09 07:15:07 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Origin", corsHeaders)
|
2014-11-01 16:22:28 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
|
2015-08-24 09:57:22 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS")
|
2013-06-04 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 23:42:08 +00:00
|
|
|
func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
|
2013-07-13 14:59:07 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2015-09-23 23:42:08 +00:00
|
|
|
// log the handler call
|
|
|
|
logrus.Debugf("Calling %s %s", r.Method, r.URL.Path)
|
2015-09-15 23:01:49 +00:00
|
|
|
|
2015-08-31 14:55:51 +00: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.
|
2015-09-29 17:51:40 +00:00
|
|
|
ctx := context.Background()
|
2015-09-23 23:42:08 +00:00
|
|
|
handlerFunc := s.handleWithGlobalMiddlewares(handler)
|
2015-06-04 13:30:14 +00:00
|
|
|
|
2015-10-14 17:31:09 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
if vars == nil {
|
|
|
|
vars = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handlerFunc(ctx, w, r, vars); err != nil {
|
2015-09-23 23:42:08 +00:00
|
|
|
logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.URL.Path, utils.GetErrorMessage(err))
|
|
|
|
httputils.WriteError(w, err)
|
2013-07-13 14:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 20:16:59 +00:00
|
|
|
// AddRouters initializes a list of routers for the server.
|
|
|
|
func (s *Server) AddRouters(routers ...router.Router) {
|
|
|
|
for _, r := range routers {
|
|
|
|
s.addRouter(r)
|
|
|
|
}
|
2015-09-23 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addRouter adds a new router to the server.
|
|
|
|
func (s *Server) addRouter(r router.Router) {
|
|
|
|
s.routers = append(s.routers, r)
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// createMux initializes the main router the server uses.
|
|
|
|
func (s *Server) createMux() *mux.Router {
|
2015-09-23 23:42:08 +00:00
|
|
|
m := mux.NewRouter()
|
2015-12-10 23:35:10 +00:00
|
|
|
if utils.IsDebugEnabled() {
|
2015-09-23 23:42:08 +00:00
|
|
|
profilerSetup(m, "/debug/")
|
2013-11-19 19:25:17 +00:00
|
|
|
}
|
2013-06-21 01:18:36 +00:00
|
|
|
|
2015-09-23 23:42:08 +00:00
|
|
|
logrus.Debugf("Registering routers")
|
2015-09-25 10:19:17 +00:00
|
|
|
for _, apiRouter := range s.routers {
|
|
|
|
for _, r := range apiRouter.Routes() {
|
2015-09-23 23:42:08 +00:00
|
|
|
f := s.makeHTTPHandler(r.Handler())
|
2015-09-25 10:19:17 +00: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 15:46:41 +00:00
|
|
|
}
|
2013-05-07 23:33:12 +00:00
|
|
|
}
|
2013-07-05 17:55:15 +00:00
|
|
|
|
2015-09-23 23:42:08 +00:00
|
|
|
return m
|
|
|
|
}
|
2015-12-10 23:35:10 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initRouterSwapper() {
|
|
|
|
s.routerSwapper = &routerSwapper{
|
|
|
|
router: s.createMux(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload reads configuration changes and modifies the
|
|
|
|
// server according to those changes.
|
|
|
|
// Currently, only the --debug configuration is taken into account.
|
2016-02-10 20:16:59 +00:00
|
|
|
func (s *Server) Reload(debug bool) {
|
2015-12-10 23:35:10 +00:00
|
|
|
debugEnabled := utils.IsDebugEnabled()
|
|
|
|
switch {
|
2016-02-10 20:16:59 +00:00
|
|
|
case debugEnabled && !debug: // disable debug
|
2015-12-10 23:35:10 +00:00
|
|
|
utils.DisableDebug()
|
|
|
|
s.routerSwapper.Swap(s.createMux())
|
2016-02-10 20:16:59 +00:00
|
|
|
case debug && !debugEnabled: // enable debug
|
2015-12-10 23:35:10 +00:00
|
|
|
utils.EnableDebug()
|
|
|
|
s.routerSwapper.Swap(s.createMux())
|
|
|
|
}
|
|
|
|
}
|