Browse code

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>

Sebastiaan van Stijn authored on 2018/01/10 00:01:43
Showing 1 changed files
... ...
@@ -2,11 +2,14 @@ package volume
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
+	"errors"
6
+	"io"
5 7
 	"net/http"
6 8
 
7 9
 	"github.com/docker/docker/api/server/httputils"
8 10
 	"github.com/docker/docker/api/types/filters"
9 11
 	volumetypes "github.com/docker/docker/api/types/volume"
12
+	"github.com/docker/docker/errdefs"
10 13
 	"golang.org/x/net/context"
11 14
 )
12 15
 
... ...
@@ -45,6 +48,9 @@ func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWri
45 45
 
46 46
 	var req volumetypes.VolumesCreateBody
47 47
 	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
48
+		if err == io.EOF {
49
+			return errdefs.InvalidParameter(errors.New("got EOF while reading request body"))
50
+		}
48 51
 		return err
49 52
 	}
50 53