зеркало из https://github.com/microsoft/docker.git
Merge pull request #1394 from crosbymichael/1391-json-requests
Use mime pkg to parse Content-Type
This commit is contained in:
Коммит
0dbc51f4d2
11
api.go
11
api.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"mime"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -81,6 +82,14 @@ func getBoolParam(value string) (bool, error) {
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func matchesContentType(contentType, expectedType string) bool {
|
||||||
|
mimetype, _, err := mime.ParseMediaType(contentType)
|
||||||
|
if err != nil {
|
||||||
|
utils.Debugf("Error parsing media type: %s error: %s", contentType, err.Error())
|
||||||
|
}
|
||||||
|
return err == nil && mimetype == expectedType
|
||||||
|
}
|
||||||
|
|
||||||
func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||||
authConfig := &auth.AuthConfig{}
|
authConfig := &auth.AuthConfig{}
|
||||||
err := json.NewDecoder(r.Body).Decode(authConfig)
|
err := json.NewDecoder(r.Body).Decode(authConfig)
|
||||||
|
@ -594,7 +603,7 @@ func postContainersStart(srv *Server, version float64, w http.ResponseWriter, r
|
||||||
|
|
||||||
// allow a nil body for backwards compatibility
|
// allow a nil body for backwards compatibility
|
||||||
if r.Body != nil {
|
if r.Body != nil {
|
||||||
if r.Header.Get("Content-Type") == "application/json" {
|
if matchesContentType(r.Header.Get("Content-Type"), "application/json") {
|
||||||
if err := json.NewDecoder(r.Body).Decode(hostConfig); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(hostConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
14
api_test.go
14
api_test.go
|
@ -1142,6 +1142,20 @@ func TestDeleteImages(t *testing.T) {
|
||||||
} */
|
} */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJsonContentType(t *testing.T) {
|
||||||
|
if !matchesContentType("application/json", "application/json") {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !matchesContentType("application/json; charset=utf-8", "application/json") {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if matchesContentType("dockerapplication/json", "application/json") {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Mocked types for tests
|
// Mocked types for tests
|
||||||
type NopConn struct {
|
type NopConn struct {
|
||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
|
|
|
@ -1567,7 +1567,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
|
||||||
return fmt.Errorf("Error: %s", body)
|
return fmt.Errorf("Error: %s", body)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Header.Get("Content-Type") == "application/json" {
|
if matchesContentType(resp.Header.Get("Content-Type"), "application/json") {
|
||||||
return utils.DisplayJSONMessagesStream(resp.Body, out)
|
return utils.DisplayJSONMessagesStream(resp.Body, out)
|
||||||
} else {
|
} else {
|
||||||
if _, err := io.Copy(out, resp.Body); err != nil {
|
if _, err := io.Copy(out, resp.Body); err != nil {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче