Skip to content

Commit 5aea52b

Browse files
author
Chris Elder
committed
[FAB-6738] GetState in CouchDB fails to return database
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. Note: This commit also contains the fix from: https://gerrit.hyperledger.org/r/#/c/13993 (FAB-6355) Change-Id: I46a0a85728a36c0a8bd727ad28a75fc02b42206b Signed-off-by: Chris Elder <[email protected]>
1 parent 5226188 commit 5aea52b

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

core/ledger/util/couchdb/couchdb.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -1395,13 +1395,22 @@ func IsJSON(s string) bool {
13951395
return json.Unmarshal([]byte(s), &js) == nil
13961396
}
13971397

1398-
// encodePathElement uses Golang for encoding and in addition, replaces a '/' by %2F.
1399-
// Otherwise, in the regular encoding, a '/' is treated as a path separator in the url
1398+
// encodePathElement uses Golang for url path encoding, additionally:
1399+
// '/' is replaced by %2F, otherwise path encoding will treat as path separator and ignore it
1400+
// '+' is replaced by %2B, otherwise path encoding will ignore it, while CouchDB will unencode the plus as a space
1401+
// Note that all other URL special characters have been tested successfully without need for special handling
14001402
func encodePathElement(str string) string {
1403+
1404+
logger.Debugf("Entering encodePathElement() string=%s", str)
1405+
14011406
u := &url.URL{}
14021407
u.Path = str
1403-
encodedStr := u.String()
1408+
encodedStr := u.EscapedPath() // url encode using golang url path encoding rules
14041409
encodedStr = strings.Replace(encodedStr, "/", "%2F", -1)
1410+
encodedStr = strings.Replace(encodedStr, "+", "%2B", -1)
1411+
1412+
logger.Debugf("Exiting encodePathElement() encodedStr=%s", encodedStr)
1413+
14051414
return encodedStr
14061415
}
14071416

core/ledger/util/couchdb/couchdb_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ func TestDBBadConnectionDef(t *testing.T) {
111111

112112
}
113113

114+
func TestEncodePathElement(t *testing.T) {
115+
116+
encodedString := encodePathElement("testelement")
117+
testutil.AssertEquals(t, encodedString, "testelement")
118+
119+
encodedString = encodePathElement("test element")
120+
testutil.AssertEquals(t, encodedString, "test%20element")
121+
122+
encodedString = encodePathElement("/test element")
123+
testutil.AssertEquals(t, encodedString, "%2Ftest%20element")
124+
125+
encodedString = encodePathElement("/test element:")
126+
testutil.AssertEquals(t, encodedString, "%2Ftest%20element:")
127+
128+
encodedString = encodePathElement("/test+ element:")
129+
testutil.AssertEquals(t, encodedString, "%2Ftest%2B%20element:")
130+
131+
}
132+
114133
func TestBadCouchDBInstance(t *testing.T) {
115134

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

0 commit comments

Comments
 (0)