moby/api/server/server.go

101 lines
3.2 KiB
Go
Raw Normal View History

package server // import "github.com/docker/docker/api/server"
2013-04-11 02:48:21 +00:00
import (
"context"
2013-04-11 02:48:21 +00:00
"net/http"
"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"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/server/middleware"
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/debug"
"github.com/docker/docker/dockerversion"
"github.com/gorilla/mux"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2013-04-11 02:48:21 +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.]+}"
// Server contains instance details for the server
type Server struct {
middlewares []middleware.Middleware
}
// 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)
}
func (s *Server) makeHTTPHandler(handler httputils.APIFunc, operation string) http.HandlerFunc {
return otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 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.
Fix some linting issues These showed locally when running `make validate`. CI doesn't seem to have the same (possibly it's disabled in the configuration) builder/fscache/fscache.go:618::error: github.com/docker/docker/vendor/github.com/tonistiigi/fsutil.StatInfo composite literal uses unkeyed fields (vet) client/swarm_unlock_test.go:44::error: github.com/docker/docker/api/types/swarm.UnlockRequest composite literal uses unkeyed fields (vet) client/swarm_unlock_test.go:20::error: github.com/docker/docker/api/types/swarm.UnlockRequest composite literal uses unkeyed fields (vet) cmd/dockerd/daemon_unix.go:113::error: github.com/docker/docker/cmd/dockerd/hack.MalformedHostHeaderOverride composite literal uses unkeyed fields (vet) cmd/dockerd/daemon_unix.go:110::error: github.com/docker/docker/cmd/dockerd/hack.MalformedHostHeaderOverride composite literal uses unkeyed fields (vet) daemon/graphdriver/overlay/overlay.go:171::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/graphdriver/overlay/overlay.go:413::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/graphdriver/overlay2/overlay.go:203::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/graphdriver/overlay2/overlay.go:584::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/graphdriver/zfs/zfs.go:109::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/graphdriver/zfs/zfs.go:388::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) daemon/volumes_windows.go:27::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) integration/service/network_test.go:31::error: github.com/docker/docker/api/types/network.NetworkingConfig composite literal uses unkeyed fields (vet) api/server/server.go:129:10:warning: should not use basic type string as key in context.WithValue (golint) integration/service/network_test.go:54::error: github.com/docker/docker/api/types/network.NetworkingConfig composite literal uses unkeyed fields (vet) libcontainerd/client_daemon_linux.go:61::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) libcontainerd/client_daemon_linux.go:74::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) pkg/archive/archive_windows.go:76::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) plugin/manager_linux.go:56::error: github.com/docker/docker/pkg/idtools.IDPair composite literal uses unkeyed fields (vet) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
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
ctx := context.WithValue(r.Context(), dockerversion.UAStringKey{}, r.Header.Get("User-Agent"))
r = r.WithContext(ctx)
handlerFunc := s.handlerWithGlobalMiddlewares(handler)
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)
if statusCode >= 500 {
log.G(ctx).Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
}
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)
}
}), operation).ServeHTTP
}
type pageNotFoundError struct{}
func (pageNotFoundError) Error() string {
return "page not found"
}
func (pageNotFoundError) NotFound() {}
// CreateMux returns a new mux with all the routers registered.
func (s *Server) CreateMux(routers ...router.Router) *mux.Router {
m := mux.NewRouter()
2013-06-21 01:18:36 +00:00
log.G(context.TODO()).Debug("Registering routers")
for _, apiRouter := range routers {
for _, r := range apiRouter.Routes() {
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
log.G(context.TODO()).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)
}
}
debugRouter := debug.NewRouter()
for _, r := range debugRouter.Routes() {
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
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{})
m.HandleFunc(versionMatcher+"/{path:.*}", notFoundHandler)
m.NotFoundHandler = notFoundHandler
m.MethodNotAllowedHandler = notFoundHandler
return m
}