Skip to content

Commit

Permalink
Merge pull request #279 from go-kivik/plussign-v3
Browse files Browse the repository at this point in the history
Support + characters in doc IDs properly
  • Loading branch information
flimzy authored May 16, 2021
2 parents 6f4a6b9 + bd959eb commit 6a9ba41
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ variables:

linter:
stage: test
image: golangci/golangci-lint:v1.39
image: golangci/golangci-lint:v1.40
script:
- go mod download
- golangci-lint run ./...
Expand Down
12 changes: 9 additions & 3 deletions chttp/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ const (
//
// In particular:
// - '_design/' and '_local/' prefixes are unaltered.
// - The rest of the docID is Query-URL encoded (despite being part of the path)
// - The rest of the docID is Query-URL encoded, except that spaces are converted to %20. See https://github.com/apache/couchdb/issues/3565 for an
// explanation.
func EncodeDocID(docID string) string {
for _, prefix := range []string{prefixDesign, prefixLocal} {
if strings.HasPrefix(docID, prefix) {
return prefix + url.PathEscape(strings.TrimPrefix(docID, prefix))
return prefix + encodeDocID(strings.TrimPrefix(docID, prefix))
}
}
return url.PathEscape(docID)
return encodeDocID(docID)
}

func encodeDocID(docID string) string {
docID = url.QueryEscape(docID)
return strings.Replace(docID, "+", "%20", -1) // Ensure space is encoded as %20, not '+', so that if CouchDB ever fixes the encoding, we won't break
}
12 changes: 7 additions & 5 deletions chttp/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chttp

import "testing"
import (
"testing"
)

func TestEncodeDocID(t *testing.T) {
tests := []struct {
Expand All @@ -11,12 +13,12 @@ func TestEncodeDocID(t *testing.T) {
{Input: "foo/bar", Expected: "foo%2Fbar"},
{Input: "_design/foo", Expected: "_design/foo"},
{Input: "_design/foo/bar", Expected: "_design/foo%2Fbar"},
{Input: "[email protected]", Expected: "foo@bar.com"},
{Input: "[email protected]", Expected: "foo+bar@baz.com"},
{Input: "[email protected]", Expected: "foo%40bar.com"},
{Input: "[email protected]", Expected: "foo%2Bbar%40baz.com"},
{Input: "Is this a valid ID?", Expected: "Is%20this%20a%20valid%20ID%3F"},
{Input: "nón-English-çharacters", Expected: "n%C3%B3n-English-%C3%A7haracters"},
{Input: "foo+bar & páces?!*,", Expected: "foo+bar%20&%20p%C3%A1ces%3F%21%2A%2C"},
{Input: "kivik$1234", Expected: "kivik$1234"},
{Input: "foo+bar & páces?!*,", Expected: "foo%2Bbar%20%26%20p%C3%A1ces%3F%21%2A%2C"},
{Input: "kivik$1234", Expected: "kivik%241234"},
{Input: "_users", Expected: "_users"},
}
for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (d *db) GetMeta(ctx context.Context, docID string, options map[string]inter
return resp.ContentLength, rev, err
}

func (d *db) get(ctx context.Context, method string, docID string, options map[string]interface{}) (*http.Response, string, error) {
func (d *db) get(ctx context.Context, method, docID string, options map[string]interface{}) (*http.Response, string, error) {
if docID == "" {
return nil, "", missingArg("docID")
}
Expand Down
19 changes: 17 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,28 @@ Content-Length: 86
}),
id: "http://example.com/",
status: http.StatusBadGateway,
err: `Get "?http://example.com/testdb/http:%2F%2Fexample.com%2F"?: success`,
err: `Get "?http://example.com/testdb/http%3A%2F%2Fexample\.com%2F"?: success`,
}
})
tests.Add("plus sign", func(t *testing.T) interface{} {
return tt{
db: newCustomDB(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("success")
}),
id: "2020-01-30T13:33:00.00+05:30|kl",
status: http.StatusBadGateway,
err: `^Get "?http://example.com/testdb/2020-01-30T13%3A33%3A00\.00%2B05%3A30%7Ckl"?: success$`,
}
})

tests.Run(t, func(t *testing.T, tt tt) {
doc, err := tt.db.Get(context.Background(), tt.id, tt.options)
testy.StatusErrorRE(t, tt.err, tt.status, err)
if !testy.ErrorMatchesRE(tt.err, err) {
t.Errorf("Unexpected error: \n Got: %s\nWant: /%s/", err, tt.err)
}
if err != nil {
return
}
result, err := ioutil.ReadAll(doc.Body)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-kivik/kiviktest/v3 v3.0.4
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00
github.com/pkg/errors v0.9.1
gitlab.com/flimzy/testy v0.3.2
gitlab.com/flimzy/testy v0.8.0
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
47 changes: 41 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/flimzy/diff v0.1.5 h1:QfOwp+TuGCeWWFxFtXqCdepnz0SeaImgNfMm6vWz3y8=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/flimzy/diff v0.1.5/go.mod h1:lFJtC7SPsK0EroDmGTSrdtWKAxOk3rO+q+e04LL05Hs=
github.com/flimzy/testy v0.1.17-0.20190521133342-95b386c3ece6 h1:uw6StVCll2vXdHJMAiKvhfAwcwBYD6d9dgWOIdHMku8=
github.com/flimzy/testy v0.1.17-0.20190521133342-95b386c3ece6/go.mod h1:3szguN8NXqgq9bt9Gu8TQVj698PJWmyx/VY1frwwKrM=
github.com/go-kivik/kivik/v3 v3.0.1/go.mod h1:7tmQDvkta/pcijpUjLMsQ9HJUELiKD5zm6jQ3Gb9cxE=
github.com/go-kivik/kivik/v3 v3.2.0 h1:YQ6j89DrQwNdMdpWioObEP+pYTSAtF58HomS5ROfKc0=
Expand All @@ -13,6 +15,17 @@ github.com/go-kivik/kiviktest/v3 v3.0.4/go.mod h1:sqsz3M2sJxTxAUdOj+2SU21y4phcpY
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/labstack/echo/v4 v4.2.1 h1:LF5Iq7t/jrtUuSutNuiEWtB5eiHfZ5gSe2pcu5exjQw=
github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc=
github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
Expand All @@ -26,23 +39,45 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
gitlab.com/flimzy/testy v0.0.3 h1:UkCz4aDa52cUX6uwvuVrwlTFZC1AesU5W6grDUcVFlg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
gitlab.com/flimzy/testy v0.0.3/go.mod h1:YObF4cq711ubd/3U0ydRQQVz7Cnq/ChgJpVwNr/AJac=
gitlab.com/flimzy/testy v0.3.2 h1:4djQFwBJ1ayM681Zx7Y3+OKns/E9zAfGFsLc967jfdk=
gitlab.com/flimzy/testy v0.3.2/go.mod h1:YObF4cq711ubd/3U0ydRQQVz7Cnq/ChgJpVwNr/AJac=
gitlab.com/flimzy/testy v0.8.0 h1:oUynO9zLAmG6SA4VNEoJhIPJM0rO+538w6T0LrvfKKM=
gitlab.com/flimzy/testy v0.8.0/go.mod h1:alphl7xENfCecd7MeicMmJ961uIPP7nJa6Izy3oIp18=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6 changes: 6 additions & 0 deletions script/start_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export KIVIK_TEST_DSN_COUCH17=http://admin:abc123@localhost:6001/
export KIVIK_TEST_DSN_COUCH22=http://admin:abc123@localhost:6002/
export KIVIK_TEST_DSN_COUCH23=http://admin:abc123@localhost:6003/
export KIVIK_TEST_DSN_COUCH30=http://admin:abc123@localhost:6004/
export KIVIK_TEST_DSN_COUCH31=http://admin:abc123@localhost:6005/

echo "CouchDB 1.7.2"
docker pull couchdb:1.7.2
Expand All @@ -32,3 +33,8 @@ echo "CouchDB 3.0.0"
docker pull apache/couchdb:3.0.0
docker run --name couch30 -p 6004:5984/tcp -d --rm -e COUCHDB_USER -e COUCHDB_PASSWORD apache/couchdb:3.0.0
${DIR}/complete_couch2.sh $KIVIK_TEST_DSN_COUCH30

echo "CouchDB 3.1.1"
docker pull apache/couchdb:3.1.1
docker run --name couch31 -p 6005:5984/tcp -d --rm -e COUCHDB_USER -e COUCHDB_PASSWORD apache/couchdb:3.1.1
${DIR}/complete_couch2.sh $KIVIK_TEST_DSN_COUCH31

0 comments on commit 6a9ba41

Please sign in to comment.