Skip to content

Commit

Permalink
[FAB-6738] GetState in CouchDB fails to return database
Browse files Browse the repository at this point in the history
For a while composer has created composite keys in a specific
format and this has worked fine. Since the introduction of a
change to the GetState protocol, GetState has not been returning
information.

Additional investigation showed that the error occurs due to a
bad URL generated when then key contains a colon ":"

Problem is resolved by encoding the path using url EscapedPath()
method instead of the String() method.

Change-Id: I20ad8177301ea845dd234eb867c95b5cde7a908c
Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
  • Loading branch information
Chris Elder committed Oct 24, 2017
1 parent 1e876e3 commit f0757ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/ledger/util/couchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,11 +1412,17 @@ func IsJSON(s string) bool {
// '+' is replaced by %2B, otherwise path encoding will ignore it, while CouchDB will unencode the plus as a space
// Note that all other URL special characters have been tested successfully without need for special handling
func encodePathElement(str string) string {

logger.Debugf("Entering encodePathElement() string=%s", str)

u := &url.URL{}
u.Path = str
encodedStr := u.String() // url encode using golang url path encoding rules
encodedStr := u.EscapedPath() // url encode using golang url path encoding rules
encodedStr = strings.Replace(encodedStr, "/", "%2F", -1)
encodedStr = strings.Replace(encodedStr, "+", "%2B", -1)

logger.Debugf("Exiting encodePathElement() encodedStr=%s", encodedStr)

return encodedStr
}

Expand Down
19 changes: 19 additions & 0 deletions core/ledger/util/couchdb/couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ func TestDBBadConnectionDef(t *testing.T) {

}

func TestEncodePathElement(t *testing.T) {

encodedString := encodePathElement("testelement")
testutil.AssertEquals(t, encodedString, "testelement")

encodedString = encodePathElement("test element")
testutil.AssertEquals(t, encodedString, "test%20element")

encodedString = encodePathElement("/test element")
testutil.AssertEquals(t, encodedString, "%2Ftest%20element")

encodedString = encodePathElement("/test element:")
testutil.AssertEquals(t, encodedString, "%2Ftest%20element:")

encodedString = encodePathElement("/test+ element:")
testutil.AssertEquals(t, encodedString, "%2Ftest%2B%20element:")

}

func TestBadCouchDBInstance(t *testing.T) {

//TODO continue changes to return and removal of sprintf in followon changes
Expand Down

0 comments on commit f0757ef

Please sign in to comment.