From 10542e057389ef8d282bec4aadf7e99dd9b8eac6 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 14 Jun 2013 11:19:50 -0500 Subject: [PATCH] Optimize and improve credential stripping code. Since we already have the specific username and password we want to strip from errors, use a specific search string rather than a generic regular expression. This is quite a bit more efficient than using regular expressions and also has the benefit of being more accurate. Also, rather than using the added overhead of fmt to convert the error to a string, just call Error() directly on it to get the string. Finally, instead of just stripping it, replace it with the literal string ":" to avoid any possible confusion in the error messages where it might otherwise appear the url was being constructed incorrectly. ok jcv@ --- jsonfxns.go | 11 ++++++----- test_coverage.txt | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/jsonfxns.go b/jsonfxns.go index a703acd5..ccee3661 100644 --- a/jsonfxns.go +++ b/jsonfxns.go @@ -11,7 +11,7 @@ import ( "io" "io/ioutil" "net/http" - "regexp" + "strings" ) // MarshallAndSend takes the reply structure, marshalls it to json, and @@ -33,13 +33,14 @@ func MarshallAndSend(rawReply Reply, w io.Writer) (string, error) { // than net/rpc/jsonrpc since that one doesn't support http connections and is // therefore useless. func jsonRpcSend(user string, password string, server string, message []byte) (*http.Response, error) { - resp, err := http.Post("http://"+user+":"+password+"@"+server, + credentials := user + ":" + password + resp, err := http.Post("http://"+credentials+"@"+server, "application/json", bytes.NewBuffer(message)) if err != nil { // We do not want to log the username/password in the errors. - re := regexp.MustCompile(`http://\w+:\w+`) - errString := re.ReplaceAllString(fmt.Sprintf("%v", err), "") - err = fmt.Errorf(errString) + replaceStr := ":" + str := strings.Replace(err.Error(), credentials, replaceStr, -1) + err = fmt.Errorf("%v", str) } return resp, err } diff --git a/test_coverage.txt b/test_coverage.txt index 863b0e8d..7203dd6f 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,11 +1,11 @@ github.com/conformal/btcjson/jsonapi.go CreateMessage 100.00% (310/310) +github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (7/7) github.com/conformal/btcjson/jsonfxns.go MarshallAndSend 100.00% (7/7) github.com/conformal/btcjson/jsonfxns.go GetRaw 100.00% (6/6) -github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (6/6) github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5) github.com/conformal/btcjson/jsonapi.go IsValidIdType 100.00% (3/3) github.com/conformal/btcjson/jsonapi.go RpcCommand 66.67% (18/27) github.com/conformal/btcjson/jsonapi.go readResultCmd 40.00% (20/50) -github.com/conformal/btcjson --------------- 90.58% (375/414) +github.com/conformal/btcjson --------------- 90.60% (376/415)