2018-02-05 21:05:59 +00:00
|
|
|
package server // import "github.com/docker/docker/api/server"
|
2013-04-11 02:48:21 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2013-04-11 02:48:21 +00:00
|
|
|
"net/http"
|
2014-03-28 22:59:29 +00:00
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
errdefs: move GetHTTPErrorStatusCode to api/server/httpstatus
This reverts the changes made in 2a9c987e5a72549775ffa4dc31595ceff4f06a78, which
moved the GetHTTPErrorStatusCode() utility to the errdefs package.
While it seemed to make sense at the time to have the errdefs package provide
conversion both from HTTP status codes errdefs and the reverse, a side-effect
of the move was that the errdefs package now had a dependency on various external
modules, to handle conversio of errors coming from those sub-systems, such as;
- github.com/containerd/containerd
- github.com/docker/distribution
- google.golang.org/grpc
This patch moves the conversion from (errdef-) errors to HTTP status-codes to a
api/server/httpstatus package, which is only used by the API server, and should
not be needed by client-code using the errdefs package.
The MakeErrorHandler() utility was moved to the API server itself, as that's the
only place it's used. While the same applies to the GetHTTPErrorStatusCode func,
I opted for keeping that in its own package for a slightly cleaner interface.
Why not move it into the api/server/httputils package?
The api/server/httputils package is also imported in the client package, which
uses the httputils.ParseForm() and httputils.HijackConnection() functions as
part of the TestTLSCloseWriter() test. While this is only used in tests, I
wanted to avoid introducing the indirect depdencencies outside of the api/server
code.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-21 10:27:39 +00:00
|
|
|
"github.com/docker/docker/api/server/httpstatus"
|
2015-09-23 23:42:08 +00:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-04-08 23:22:39 +00:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
2015-09-23 23:42:08 +00:00
|
|
|
"github.com/docker/docker/api/server/router"
|
2017-04-08 18:43:42 +00:00
|
|
|
"github.com/docker/docker/api/server/router/debug"
|
2016-12-22 21:51:47 +00:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-09-29 21:32:07 +00:00
|
|
|
"github.com/gorilla/mux"
|
2023-07-14 17:59:45 +00:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
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
|
|
|
// Server contains instance details for the server
|
2015-04-17 21:32:18 +00:00
|
|
|
type Server struct {
|
2019-09-04 20:47:16 +00:00
|
|
|
middlewares []middleware.Middleware
|
2015-04-17 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 23:22:39 +00: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)
|
|
|
|
}
|
|
|
|
|
2023-09-23 14:03:39 +00:00
|
|
|
func (s *Server) makeHTTPHandler(handler httputils.APIFunc, operation string) http.HandlerFunc {
|
2023-07-14 17:59:45 +00:00
|
|
|
return otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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.
|
2018-05-17 14:29:52 +00:00
|
|
|
|
|
|
|
// use intermediate variable to prevent "should not use basic type
|
|
|
|
// string as key in context.WithValue" golint errors
|
2018-11-27 22:13:43 +00:00
|
|
|
ctx := context.WithValue(r.Context(), dockerversion.UAStringKey{}, r.Header.Get("User-Agent"))
|
2023-07-14 17:59:45 +00:00
|
|
|
|
2018-11-27 22:13:43 +00:00
|
|
|
r = r.WithContext(ctx)
|
2016-08-12 02:50:22 +00:00
|
|
|
handlerFunc := s.handlerWithGlobalMiddlewares(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 {
|
errdefs: move GetHTTPErrorStatusCode to api/server/httpstatus
This reverts the changes made in 2a9c987e5a72549775ffa4dc31595ceff4f06a78, which
moved the GetHTTPErrorStatusCode() utility to the errdefs package.
While it seemed to make sense at the time to have the errdefs package provide
conversion both from HTTP status codes errdefs and the reverse, a side-effect
of the move was that the errdefs package now had a dependency on various external
modules, to handle conversio of errors coming from those sub-systems, such as;
- github.com/containerd/containerd
- github.com/docker/distribution
- google.golang.org/grpc
This patch moves the conversion from (errdef-) errors to HTTP status-codes to a
api/server/httpstatus package, which is only used by the API server, and should
not be needed by client-code using the errdefs package.
The MakeErrorHandler() utility was moved to the API server itself, as that's the
only place it's used. While the same applies to the GetHTTPErrorStatusCode func,
I opted for keeping that in its own package for a slightly cleaner interface.
Why not move it into the api/server/httputils package?
The api/server/httputils package is also imported in the client package, which
uses the httputils.ParseForm() and httputils.HijackConnection() functions as
part of the TestTLSCloseWriter() test. While this is only used in tests, I
wanted to avoid introducing the indirect depdencencies outside of the api/server
code.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-21 10:27:39 +00:00
|
|
|
statusCode := httpstatus.FromError(err)
|
2017-04-05 23:59:32 +00:00
|
|
|
if statusCode >= 500 {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
|
2017-02-06 15:49:16 +00:00
|
|
|
}
|
errdefs: move GetHTTPErrorStatusCode to api/server/httpstatus
This reverts the changes made in 2a9c987e5a72549775ffa4dc31595ceff4f06a78, which
moved the GetHTTPErrorStatusCode() utility to the errdefs package.
While it seemed to make sense at the time to have the errdefs package provide
conversion both from HTTP status codes errdefs and the reverse, a side-effect
of the move was that the errdefs package now had a dependency on various external
modules, to handle conversio of errors coming from those sub-systems, such as;
- github.com/containerd/containerd
- github.com/docker/distribution
- google.golang.org/grpc
This patch moves the conversion from (errdef-) errors to HTTP status-codes to a
api/server/httpstatus package, which is only used by the API server, and should
not be needed by client-code using the errdefs package.
The MakeErrorHandler() utility was moved to the API server itself, as that's the
only place it's used. While the same applies to the GetHTTPErrorStatusCode func,
I opted for keeping that in its own package for a slightly cleaner interface.
Why not move it into the api/server/httputils package?
The api/server/httputils package is also imported in the client package, which
uses the httputils.ParseForm() and httputils.HijackConnection() functions as
part of the TestTLSCloseWriter() test. While this is only used in tests, I
wanted to avoid introducing the indirect depdencencies outside of the api/server
code.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-21 10:27:39 +00:00
|
|
|
makeErrorHandler(err)(w, r)
|
2013-07-13 14:59:07 +00:00
|
|
|
}
|
2023-09-23 14:03:39 +00:00
|
|
|
}), operation).ServeHTTP
|
2013-07-13 14:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
type pageNotFoundError struct{}
|
|
|
|
|
|
|
|
func (pageNotFoundError) Error() string {
|
|
|
|
return "page not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pageNotFoundError) NotFound() {}
|
|
|
|
|
2023-04-12 16:26:38 +00:00
|
|
|
// CreateMux returns a new mux with all the routers registered.
|
|
|
|
func (s *Server) CreateMux(routers ...router.Router) *mux.Router {
|
2015-09-23 23:42:08 +00:00
|
|
|
m := mux.NewRouter()
|
2013-06-21 01:18:36 +00:00
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debug("Registering routers")
|
2023-04-12 16:26:38 +00:00
|
|
|
for _, apiRouter := range routers {
|
2015-09-25 10:19:17 +00:00
|
|
|
for _, r := range apiRouter.Routes() {
|
2023-09-23 14:03:39 +00:00
|
|
|
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
|
2015-09-25 10:19:17 +00:00
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Registering %s, %s", r.Method(), r.Path())
|
2015-09-25 10:19:17 +00:00
|
|
|
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
|
|
|
|
2017-04-08 18:43:42 +00:00
|
|
|
debugRouter := debug.NewRouter()
|
|
|
|
for _, r := range debugRouter.Routes() {
|
2023-09-23 14:03:39 +00:00
|
|
|
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
|
2017-04-08 18:43:42 +00:00
|
|
|
m.Path("/debug" + r.Path()).Handler(f)
|
|
|
|
}
|
|
|
|
|
errdefs: move GetHTTPErrorStatusCode to api/server/httpstatus
This reverts the changes made in 2a9c987e5a72549775ffa4dc31595ceff4f06a78, which
moved the GetHTTPErrorStatusCode() utility to the errdefs package.
While it seemed to make sense at the time to have the errdefs package provide
conversion both from HTTP status codes errdefs and the reverse, a side-effect
of the move was that the errdefs package now had a dependency on various external
modules, to handle conversio of errors coming from those sub-systems, such as;
- github.com/containerd/containerd
- github.com/docker/distribution
- google.golang.org/grpc
This patch moves the conversion from (errdef-) errors to HTTP status-codes to a
api/server/httpstatus package, which is only used by the API server, and should
not be needed by client-code using the errdefs package.
The MakeErrorHandler() utility was moved to the API server itself, as that's the
only place it's used. While the same applies to the GetHTTPErrorStatusCode func,
I opted for keeping that in its own package for a slightly cleaner interface.
Why not move it into the api/server/httputils package?
The api/server/httputils package is also imported in the client package, which
uses the httputils.ParseForm() and httputils.HijackConnection() functions as
part of the TestTLSCloseWriter() test. While this is only used in tests, I
wanted to avoid introducing the indirect depdencencies outside of the api/server
code.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-21 10:27:39 +00:00
|
|
|
notFoundHandler := makeErrorHandler(pageNotFoundError{})
|
2016-05-21 11:56:04 +00:00
|
|
|
m.HandleFunc(versionMatcher+"/{path:.*}", notFoundHandler)
|
|
|
|
m.NotFoundHandler = notFoundHandler
|
2018-11-27 22:10:36 +00:00
|
|
|
m.MethodNotAllowedHandler = notFoundHandler
|
2016-05-21 11:56:04 +00:00
|
|
|
|
2015-09-23 23:42:08 +00:00
|
|
|
return m
|
|
|
|
}
|