Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issue #1: slashes in IDs not encoded. #3

Closed
wants to merge 3 commits into from

Conversation

pegli
Copy link

@pegli pegli commented Apr 15, 2015

This was a moderately complicated fix. The old CouchDB wiki has this to say about slashes in document IDs:

You can have / as part of the document ID but if you refer to a document in a URL
you must always encode it as %2F. One special case is _design/ documents, those
accept either / or %2F for the / after _design, although / is preferred and %2F is still
needed for the rest of the DocID.

(https://wiki.apache.org/couchdb/HTTP_Document_API#line-75)

I initially modified couchdb.go to break up the document ID on slash characters and reassemble it with %2F separators, with a special case for IDs that start with _design, where I prefixed _design/ and assembled the rest of the ID with escaped slashes. Here are some examples of the transformed IDs:

user/34734612 -> user%2F34734612
AC/DC/240V -> AC%2FDC%2F240V
_design/lookup/users -> _design/lookup%2Fusers

I added new unit tests to couchdb_test.go with handlers that responded to the escaped document IDs and found that these tests failed. After some investigation, I found issue #5684 in the golang repo, which was filed because url.URL was decoding escape sequences in URL paths. My encoding code converted slashes to %2F but url.URL converted them back to slashes, which caused the test to fail.

The documentation for url.URL contained a workaround for this issue: the Opaque field of a URL struct can be set with the value of the encoded URL path, and that encoded value will be used in http.Request as the request path. Unfortunately, this involved replacing the call to http.NewRequest in the newRequest function with code that constructs a Request structure manually. This is the part of the PR that should be reviewed most strictly to make sure I didn't inadvertently miss anything in the request construction. All the unit tests, including my new ones that test getting document IDs containing slashes, are passing.

Let me know what you think or if there is anything I can do to improve this code.

…r non-design document doc IDs and substitute %2F for / in the name. Modified the newRequest function to construct a url.URL object that does not decode path elements (see notes at http://godoc.org/net/url#URL)
@pegli
Copy link
Author

pegli commented Apr 20, 2015

I added another commit which refactors the ID encoding into a new function in http.go named encid(). This was necessary because I discovered that database names also need to have slashes converted to %2F.

You might be wondering at this point why it is so important to us to make so many large changes just to support a minor feature of CouchDB. For me, I need slashes in database names and document IDs in order to be compatible with Hoodie, a web application development platform based on CouchDB. Hoodie expects document and database identifiers to be in the format <type>/<id> instead of using opaque generated IDs. For example, if I have a document that holds address information, that document's ID might be address/2x345b, and it might be stored in a database named user/3tn32s1. You can find more details about Hoodie's behavior in this FAQ entry.

@fjl
Copy link
Owner

fjl commented Nov 8, 2020

Fixed in 9bc16a4.

@fjl fjl closed this Nov 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants