1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use mime types to parse Content-Type

This commit is contained in:
Michael Crosby 2013-08-03 20:55:54 +00:00
parent b6c4b325a4
commit 754ed9043d
3 changed files with 25 additions and 2 deletions

11
api.go
View file

@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"log"
"mime"
"net"
"net/http"
"os"
@ -81,6 +82,14 @@ func getBoolParam(value string) (bool, error) {
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 {
authConfig := &auth.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
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 {
return err
}

View file

@ -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
type NopConn struct {
io.ReadCloser

View file

@ -1567,7 +1567,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
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)
} else {
if _, err := io.Copy(out, resp.Body); err != nil {