2014-03-28 18:59:29 -04:00
|
|
|
package server
|
2013-04-10 22:48:21 -04:00
|
|
|
|
|
|
|
import (
|
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-06-18 14:59:56 -04:00
|
|
|
"os"
|
2013-04-19 09:24:37 -04:00
|
|
|
"strings"
|
2014-03-28 18:59:29 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
|
|
"github.com/docker/docker/api/server/router"
|
2015-11-04 20:38:05 -05:00
|
|
|
"github.com/docker/docker/api/server/router/container"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/router/local"
|
|
|
|
"github.com/docker/docker/api/server/router/network"
|
2015-12-03 13:11:19 -05:00
|
|
|
"github.com/docker/docker/api/server/router/system"
|
2015-10-27 15:06:35 -04:00
|
|
|
"github.com/docker/docker/api/server/router/volume"
|
2015-03-31 19:21:37 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-05-20 19:48:39 -04:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
2015-09-18 13:48:16 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2015-09-29 17:32:07 -04:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"golang.org/x/net/context"
|
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 {
|
2015-04-16 15:48:04 -04:00
|
|
|
Logging bool
|
|
|
|
EnableCors bool
|
|
|
|
CorsHeaders string
|
|
|
|
Version string
|
|
|
|
SocketGroup string
|
2015-05-07 12:49:07 -04:00
|
|
|
TLSConfig *tls.Config
|
2015-10-05 12:32:08 -04:00
|
|
|
Addrs []Addr
|
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-07-21 01:15:44 -04:00
|
|
|
cfg *Config
|
2015-10-05 12:32:08 -04:00
|
|
|
servers []*HTTPServer
|
2015-09-23 19:42:08 -04:00
|
|
|
routers []router.Router
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 12:32:08 -04:00
|
|
|
// Addr contains string representation of address and its protocol (tcp, unix...).
|
|
|
|
type Addr struct {
|
|
|
|
Proto string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
func New(cfg *Config) (*Server, error) {
|
|
|
|
s := &Server{
|
2015-11-25 14:05:31 -05:00
|
|
|
cfg: cfg,
|
2015-04-17 17:32:18 -04:00
|
|
|
}
|
2015-10-05 12:32:08 -04:00
|
|
|
for _, addr := range cfg.Addrs {
|
|
|
|
srv, err := s.newServer(addr.Proto, addr.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Server created for HTTP on %s (%s)", addr.Proto, addr.Addr)
|
|
|
|
s.servers = append(s.servers, srv...)
|
|
|
|
}
|
|
|
|
return s, nil
|
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-10-05 12:32:08 -04:00
|
|
|
// ServeAPI loops through all initialized servers and spawns goroutine
|
2015-11-24 21:17:37 -05:00
|
|
|
// with Server method for each. It sets CreateMux() as Handler also.
|
2015-10-05 12:32:08 -04:00
|
|
|
func (s *Server) ServeAPI() error {
|
|
|
|
var chErrors = make(chan error, len(s.servers))
|
|
|
|
for _, srv := range s.servers {
|
2015-11-24 21:17:37 -05:00
|
|
|
srv.srv.Handler = s.CreateMux()
|
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
|
|
|
}
|
|
|
|
|
2015-10-05 12:32:08 -04:00
|
|
|
for i := 0; i < len(s.servers); i++ {
|
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.
|
|
|
|
// 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 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-02-09 02:15:07 -05:00
|
|
|
func writeCorsHeaders(w http.ResponseWriter, r *http.Request, corsHeaders string) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("CORS header is enabled and set to: %s", corsHeaders)
|
2015-02-09 02:15:07 -05:00
|
|
|
w.Header().Add("Access-Control-Allow-Origin", corsHeaders)
|
2014-11-01 12:22:28 -04:00
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
|
2015-08-24 05:57:22 -04:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS")
|
2013-06-03 21:39:00 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
func (s *Server) initTCPSocket(addr string) (l net.Listener, err error) {
|
2015-05-07 12:49:07 -04:00
|
|
|
if s.cfg.TLSConfig == nil || s.cfg.TLSConfig.ClientAuth != tls.RequireAndVerifyClientCert {
|
2015-05-20 19:48:39 -04:00
|
|
|
logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
|
|
|
|
}
|
2015-11-25 14:05:31 -05:00
|
|
|
if l, err = sockets.NewTCPSocket(addr, s.cfg.TLSConfig); err != nil {
|
2015-05-20 19:48:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-09-23 19:42:08 -04:00
|
|
|
// log the handler call
|
|
|
|
logrus.Debugf("Calling %s %s", r.Method, r.URL.Path)
|
2015-09-15 19:01:49 -04:00
|
|
|
|
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.
|
2015-09-29 13:51:40 -04:00
|
|
|
ctx := context.Background()
|
2015-09-23 19:42:08 -04:00
|
|
|
handlerFunc := s.handleWithGlobalMiddlewares(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 {
|
2015-09-23 19:42:08 -04: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 10:59:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
// InitRouters initializes a list of routers for the server.
|
|
|
|
func (s *Server) InitRouters(d *daemon.Daemon) {
|
2015-12-03 13:11:19 -05:00
|
|
|
s.addRouter(container.NewRouter(d))
|
2015-09-23 19:42:08 -04:00
|
|
|
s.addRouter(local.NewRouter(d))
|
|
|
|
s.addRouter(network.NewRouter(d))
|
2015-12-03 13:11:19 -05:00
|
|
|
s.addRouter(system.NewRouter(d))
|
2015-10-27 15:06:35 -04:00
|
|
|
s.addRouter(volume.NewRouter(d))
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// addRouter adds a new router to the server.
|
|
|
|
func (s *Server) addRouter(r router.Router) {
|
|
|
|
s.routers = append(s.routers, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMux initializes the main router the server uses.
|
2015-02-09 02:15:07 -05:00
|
|
|
// we keep enableCors just for legacy usage, need to be removed in the future
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *Server) CreateMux() *mux.Router {
|
|
|
|
m := mux.NewRouter()
|
2013-11-19 14:25:17 -05:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
2015-09-23 19:42:08 -04:00
|
|
|
profilerSetup(m, "/debug/")
|
2013-11-19 14:25:17 -05:00
|
|
|
}
|
2013-06-20 21:18:36 -04:00
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
logrus.Debugf("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
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return m
|
|
|
|
}
|