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-04-19 13:24:37 +00:00
|
|
|
"fmt"
|
2013-06-18 18:59:56 +00:00
|
|
|
"net"
|
2013-04-11 02:48:21 +00:00
|
|
|
"net/http"
|
2013-06-18 18:59:56 +00:00
|
|
|
"os"
|
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"
|
|
|
|
"github.com/docker/docker/api/server/router/local"
|
|
|
|
"github.com/docker/docker/api/server/router/network"
|
2015-03-31 23:21:37 +00:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-05-20 23:48:39 +00:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
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-07-21 05:15:44 +00:00
|
|
|
// Config provides the configuration for the API server
|
|
|
|
type Config struct {
|
2015-04-16 19:48:04 +00:00
|
|
|
Logging bool
|
|
|
|
EnableCors bool
|
|
|
|
CorsHeaders string
|
|
|
|
Version string
|
|
|
|
SocketGroup string
|
2015-05-07 16:49:07 +00:00
|
|
|
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-07-21 05:15:44 +00:00
|
|
|
cfg *Config
|
2015-04-27 21:11:29 +00:00
|
|
|
start chan struct{}
|
|
|
|
servers []serverCloser
|
2015-09-23 23:42:08 +00:00
|
|
|
routers []router.Router
|
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-09-29 17:51:40 +00:00
|
|
|
func New(cfg *Config) *Server {
|
2015-09-23 23:42:08 +00:00
|
|
|
return &Server{
|
2015-04-17 22:18:28 +00:00
|
|
|
cfg: cfg,
|
|
|
|
start: make(chan struct{}),
|
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-04-17 21:32:18 +00:00
|
|
|
type serverCloser interface {
|
|
|
|
Serve() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
// ServeAPI loops through all of the protocols sent in to docker and spawns
|
2015-04-17 21:32:18 +00:00
|
|
|
// off a go routine to setup a serving http.Server for each.
|
2015-07-21 05:15:44 +00:00
|
|
|
func (s *Server) ServeAPI(protoAddrs []string) error {
|
2015-04-17 21:32:18 +00:00
|
|
|
var chErrors = make(chan error, len(protoAddrs))
|
|
|
|
|
|
|
|
for _, protoAddr := range protoAddrs {
|
|
|
|
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
|
|
|
|
if len(protoAddrParts) != 2 {
|
|
|
|
return fmt.Errorf("bad format, expected PROTO://ADDR")
|
|
|
|
}
|
2015-04-27 21:11:29 +00:00
|
|
|
srv, err := s.newServer(protoAddrParts[0], protoAddrParts[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-28 19:15:03 +00:00
|
|
|
s.servers = append(s.servers, srv...)
|
2015-04-27 21:11:29 +00:00
|
|
|
|
2015-05-28 19:15:03 +00:00
|
|
|
for _, s := range srv {
|
|
|
|
logrus.Infof("Listening for HTTP on %s (%s)", protoAddrParts[0], protoAddrParts[1])
|
|
|
|
go func(s serverCloser) {
|
|
|
|
if err := s.Serve(); err != nil && strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
chErrors <- err
|
|
|
|
}(s)
|
|
|
|
}
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(protoAddrs); i++ {
|
|
|
|
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-07-21 05:15:44 +00:00
|
|
|
func (s *Server) initTCPSocket(addr string) (l net.Listener, err error) {
|
2015-05-07 16:49:07 +00:00
|
|
|
if s.cfg.TLSConfig == nil || s.cfg.TLSConfig.ClientAuth != tls.RequireAndVerifyClientCert {
|
2015-05-20 23:48:39 +00:00
|
|
|
logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
|
|
|
|
}
|
2015-07-25 08:35:07 +00:00
|
|
|
if l, err = sockets.NewTCPSocket(addr, s.cfg.TLSConfig, s.start); err != nil {
|
2015-05-20 23:48:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-08-31 14:55:51 +00:00
|
|
|
if err := handlerFunc(ctx, w, r, mux.Vars(r)); 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:42:08 +00:00
|
|
|
// InitRouters initializes a list of routers for the server.
|
|
|
|
func (s *Server) InitRouters(d *daemon.Daemon) {
|
|
|
|
s.addRouter(local.NewRouter(d))
|
|
|
|
s.addRouter(network.NewRouter(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 07:15:07 +00:00
|
|
|
// we keep enableCors just for legacy usage, need to be removed in the future
|
2015-09-23 23:42:08 +00:00
|
|
|
func (s *Server) CreateMux() *mux.Router {
|
|
|
|
m := mux.NewRouter()
|
2013-11-19 19:25:17 +00:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
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")
|
|
|
|
for _, router := range s.routers {
|
|
|
|
for _, r := range router.Routes() {
|
|
|
|
f := s.makeHTTPHandler(r.Handler())
|
|
|
|
r.Register(m, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptConnections allows clients to connect to the API server.
|
|
|
|
// Referenced Daemon is notified about this server, and waits for the
|
|
|
|
// daemon acknowledgement before the incoming connections are accepted.
|
|
|
|
func (s *Server) AcceptConnections() {
|
|
|
|
// close the lock so the listeners start accepting connections
|
|
|
|
select {
|
|
|
|
case <-s.start:
|
|
|
|
default:
|
|
|
|
close(s.start)
|
|
|
|
}
|
2013-06-10 22:02:40 +00:00
|
|
|
}
|