1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/api/server/router/volume/volume_routes.go

97 lines
2.8 KiB
Go
Raw Normal View History

package volume // import "github.com/docker/docker/api/server/router/volume"
import (
"context"
"encoding/json"
Return 400 status instead of 500 for empty volume create body The `POST /volumes/create` expects a request body to be provided. If no body was provided, a 500 status was returned. A 500 status is incorrect, because the request is invalid (it's not a server error). Before this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.51.0 > Accept: */* > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.30 < Content-Length: 18 < Content-Type: application/json < Date: Wed, 19 Jul 2017 11:29:26 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/17.06.0-ce (linux) < {"message":"EOF"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact After this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/var/run/docker.sock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > < HTTP/1.1 400 Bad Request < Api-Version: 1.36 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Tue, 09 Jan 2018 15:00:13 GMT < Content-Length: 42 < {"message":"no body provided in request"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-01-09 10:01:43 -05:00
"io"
"net/http"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/types/filters"
volumetypes "github.com/docker/docker/api/types/volume"
Return 400 status instead of 500 for empty volume create body The `POST /volumes/create` expects a request body to be provided. If no body was provided, a 500 status was returned. A 500 status is incorrect, because the request is invalid (it's not a server error). Before this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.51.0 > Accept: */* > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.30 < Content-Length: 18 < Content-Type: application/json < Date: Wed, 19 Jul 2017 11:29:26 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/17.06.0-ce (linux) < {"message":"EOF"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact After this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/var/run/docker.sock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > < HTTP/1.1 400 Bad Request < Api-Version: 1.36 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Tue, 09 Jan 2018 15:00:13 GMT < Content-Length: 42 < {"message":"no body provided in request"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-01-09 10:01:43 -05:00
"github.com/docker/docker/errdefs"
"github.com/docker/docker/volume/service/opts"
"github.com/pkg/errors"
)
func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
filters, err := filters.FromJSON(r.Form.Get("filters"))
if err != nil {
return errdefs.InvalidParameter(errors.Wrap(err, "error reading volume filters"))
}
volumes, warnings, err := v.backend.List(ctx, filters)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, &volumetypes.VolumeListOKBody{Volumes: volumes, Warnings: warnings})
}
func (v *volumeRouter) getVolumeByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
volume, err := v.backend.Get(ctx, vars["name"], opts.WithGetResolveStatus)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, volume)
}
func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
if err := httputils.CheckForJSON(r); err != nil {
return err
}
var req volumetypes.VolumeCreateBody
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Return 400 status instead of 500 for empty volume create body The `POST /volumes/create` expects a request body to be provided. If no body was provided, a 500 status was returned. A 500 status is incorrect, because the request is invalid (it's not a server error). Before this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.51.0 > Accept: */* > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.30 < Content-Length: 18 < Content-Type: application/json < Date: Wed, 19 Jul 2017 11:29:26 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/17.06.0-ce (linux) < {"message":"EOF"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact After this change: $ curl --unix-socket /var/run/docker.sock -v -X POST http://localhost/volumes/create * Trying /var/run/docker.sock... * Connected to localhost (/var/run/docker.sock) port 80 (#0) > POST /volumes/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > < HTTP/1.1 400 Bad Request < Api-Version: 1.36 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Tue, 09 Jan 2018 15:00:13 GMT < Content-Length: 42 < {"message":"no body provided in request"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-01-09 10:01:43 -05:00
if err == io.EOF {
return errdefs.InvalidParameter(errors.New("got EOF while reading request body"))
}
API: properly handle invalid JSON to return a 400 status The API did not treat invalid JSON payloads as a 400 error, as a result returning a 500 error; Before this change, an invalid JSON body would return a 500 error; ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" \ -d '{invalid json' ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > Content-Length: 13 > * upload completely sent off: 13 out of 13 bytes < HTTP/1.1 500 Internal Server Error < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:55:20 GMT < Content-Length: 79 < {"message":"invalid character 'i' looking for beginning of object key string"} ``` Empty request: ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.54.0 > Accept: */* > Content-Type: application/json > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.38 < Content-Length: 18 < Content-Type: application/json < Date: Mon, 05 Nov 2018 12:00:18 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/18.06.1-ce (linux) < {"message":"EOF"} ``` After this change, a 400 is returned; ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" \ -d '{invalid json' ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > Content-Length: 13 > * upload completely sent off: 13 out of 13 bytes < HTTP/1.1 400 Bad Request < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:57:15 GMT < Content-Length: 79 < {"message":"invalid character 'i' looking for beginning of object key string"} ``` Empty request: ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > < HTTP/1.1 400 Bad Request < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:59:22 GMT < Content-Length: 49 < {"message":"got EOF while reading request body"} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-05 08:50:33 -05:00
return errdefs.InvalidParameter(err)
}
volume, err := v.backend.Create(ctx, req.Name, req.Driver, opts.WithCreateOptions(req.DriverOpts), opts.WithCreateLabels(req.Labels))
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusCreated, volume)
}
func (v *volumeRouter) deleteVolumes(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
force := httputils.BoolValue(r, "force")
if err := v.backend.Remove(ctx, vars["name"], opts.WithPurgeOnError(force)); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
func (v *volumeRouter) postVolumesPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
if err != nil {
return err
}
pruneReport, err := v.backend.Prune(ctx, pruneFilters)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
}