From 4745cc9d9afbebf74320fb1ebc2aa820774bd4d7 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Wed, 30 Sep 2015 14:03:27 -0700 Subject: [PATCH] apiserver: Fix handling patch requests with charset Fixes #14872 --- pkg/apiserver/apiserver_test.go | 2 +- pkg/apiserver/resthandler.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index 82a2c292175..36b78b593ce 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -1659,7 +1659,7 @@ func TestPatch(t *testing.T) { client := http.Client{} request, err := http.NewRequest("PATCH", server.URL+"/api/version/namespaces/default/simple/"+ID, bytes.NewReader([]byte(`{"labels":{"foo":"bar"}}`))) - request.Header.Set("Content-Type", "application/merge-patch+json") + request.Header.Set("Content-Type", "application/merge-patch+json; charset=UTF-8") _, err = client.Do(request) if err != nil { t.Errorf("unexpected error: %v", err) diff --git a/pkg/apiserver/resthandler.go b/pkg/apiserver/resthandler.go index ca0aea0cd31..a12f5240f0b 100644 --- a/pkg/apiserver/resthandler.go +++ b/pkg/apiserver/resthandler.go @@ -21,6 +21,7 @@ import ( "net/http" "net/url" gpath "path" + "strings" "time" "k8s.io/kubernetes/pkg/admission" @@ -736,6 +737,10 @@ func setListSelfLink(obj runtime.Object, req *restful.Request, namer ScopeNamer) } func getPatchedJS(contentType string, originalJS, patchJS []byte, obj runtime.Object) ([]byte, error) { + // Remove "; charset=" if included in header. + if idx := strings.Index(contentType, ";"); idx > 0 { + contentType = contentType[:idx] + } patchType := api.PatchType(contentType) switch patchType { case api.JSONPatchType: