fix(manager): replace Unmarshal with Decode

Instead of repeating io.Read/Unmarshal pattern, use the Decoder,
which also gets us content-type negotiation.
pull/396/head
Matt Butcher 9 years ago
parent 3cd679da63
commit dec2f0605a

@ -17,7 +17,6 @@ limitations under the License.
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -298,19 +297,11 @@ func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler s
func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template { func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.Template{} t := &common.Template{}
if err := json.Unmarshal(j, t); err != nil { if err := httputil.Decode(w, r, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j)) httputil.BadRequest(w, r, err)
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
return nil return nil
} }
return t return t
} }
@ -479,18 +470,12 @@ func getRegistryHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Co
func getRegistry(w http.ResponseWriter, r *http.Request, handler string) *common.Registry { func getRegistry(w http.ResponseWriter, r *http.Request, handler string) *common.Registry {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.Registry{} t := &common.Registry{}
if err := json.Unmarshal(j, t); err != nil { if err := httputil.Decode(w, r, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j)) httputil.BadRequest(w, r, err)
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
return nil return nil
} }
return t return t
} }
@ -611,18 +596,11 @@ func getFileHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Contex
func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential { func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.RegistryCredential{} t := &common.RegistryCredential{}
if err := json.Unmarshal(j, t); err != nil { if err := httputil.Decode(w, r, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j)) httputil.BadRequest(w, r, err)
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
return nil return nil
} }
return t return t
} }

@ -17,14 +17,15 @@ limitations under the License.
package main package main
import ( import (
"github.com/kubernetes/helm/cmd/manager/router"
"github.com/kubernetes/helm/pkg/version"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/kubernetes/helm/cmd/manager/router"
"github.com/kubernetes/helm/pkg/httputil"
"github.com/kubernetes/helm/pkg/version"
) )
var ( var (
@ -52,6 +53,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
httputil.DefaultEncoder.MaxReadLen = c.Config.MaxTemplateLength
// Set up routes // Set up routes
handler := router.NewHandler(c) handler := router.NewHandler(c)
registerDeploymentRoutes(c, handler) registerDeploymentRoutes(c, handler)

Loading…
Cancel
Save