From 74d4af37a324f5f46f98a71f70888f57fbe389ef Mon Sep 17 00:00:00 2001 From: HuKeping Date: Fri, 25 Dec 2015 16:48:41 +0800 Subject: [PATCH] Stop injecting to the helper function GetCryptoService is a helper function and we have injected a specific http.ResponseWriter object for it to write back error message. Meanwhile the caller for that function checks whether the cryptoService is nil or not and return immediately if it is nil. I think it's not a good idea to write back HTTP response in the helper function, it's the caller's work and thus there is no need to inject the specific ResponseWriter object into it. Signed-off-by: Hu Keping --- signer/api/api.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/signer/api/api.go b/signer/api/api.go index 967fbbe7be..9d771a474b 100644 --- a/signer/api/api.go +++ b/signer/api/api.go @@ -3,6 +3,7 @@ package api import ( "crypto/rand" "encoding/json" + "fmt" "net/http" "github.com/docker/notary/signer" @@ -28,18 +29,16 @@ func Handlers(cryptoServices signer.CryptoServiceIndex) *mux.Router { // algorithm specified in the HTTP request. If the algorithm isn't specified // or isn't supported, an error is returned to the client and this function // returns a nil CryptoService -func getCryptoService(w http.ResponseWriter, algorithm string, cryptoServices signer.CryptoServiceIndex) signed.CryptoService { +func getCryptoService(algorithm string, cryptoServices signer.CryptoServiceIndex) (signed.CryptoService, error) { if algorithm == "" { - http.Error(w, "algorithm not specified", http.StatusBadRequest) - return nil + return nil, fmt.Errorf("algorithm not specified") } if service, ok := cryptoServices[algorithm]; ok { - return service + return service, nil } - http.Error(w, "algorithm "+algorithm+" not supported", http.StatusBadRequest) - return nil + return nil, fmt.Errorf("algorithm " + algorithm + " not supported") } // KeyInfo returns a Handler that given a specific Key ID param, returns the public key bits of that key @@ -79,9 +78,9 @@ func KeyInfo(cryptoServices signer.CryptoServiceIndex) http.Handler { func CreateKey(cryptoServices signer.CryptoServiceIndex) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - cryptoService := getCryptoService(w, vars["Algorithm"], cryptoServices) - if cryptoService == nil { - // Error handled inside getCryptoService + cryptoService, err := getCryptoService(vars["Algorithm"], cryptoServices) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return }