github.com/go-kivik/kivik/v4@v4.3.2/couchdb/chttp/encode.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package chttp 14 15 import ( 16 "net/url" 17 "strings" 18 ) 19 20 const ( 21 prefixDesign = "_design/" 22 prefixLocal = "_local/" 23 ) 24 25 // EncodeDocID encodes a document ID according to CouchDB's path encoding rules. 26 // 27 // In particular: 28 // - '_design/' and '_local/' prefixes are unaltered. 29 // - 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 30 // explanation. 31 func EncodeDocID(docID string) string { 32 for _, prefix := range []string{prefixDesign, prefixLocal} { 33 if strings.HasPrefix(docID, prefix) { 34 return prefix + encodeDocID(strings.TrimPrefix(docID, prefix)) 35 } 36 } 37 return encodeDocID(docID) 38 } 39 40 func encodeDocID(docID string) string { 41 docID = url.QueryEscape(docID) 42 return strings.ReplaceAll(docID, "+", "%20") // Ensure space is encoded as %20, not '+', so that if CouchDB ever fixes the encoding, we won't break 43 }