Merge pull request #1394 from crosbymichael/1391-json-requests

Use mime pkg to parse Content-Type
This commit is contained in:
Victor Vieux 2013-08-06 08:59:10 -07:00
Родитель b6c4b325a4 754ed9043d
Коммит 0dbc51f4d2
3 изменённых файлов: 25 добавлений и 2 удалений

11
api.go
Просмотреть файл

@ -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
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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 {