lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

uri() mangles db names and doc ids starting/ending with "/" #96

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

>>> from couchdb.client import uri
>>> uri('http://localhost:5984/foo', '/bar/')

What is the expected output?

'http://localhost:5984/foo/%2Fbar%2F'

What do you see instead?

'http://localhost:5984/foo/bar'

What version of the product are you using?

couchdb-python r196

Please provide any additional information below.

Slashes are stripped from path components here:

http://code.google.com/p/couchdb-
python/source/browse/trunk/couchdb/client.py#1049

The docstring for uri() gives this example:

>>> uri('http://example.org/', '/_all_dbs')
'http://example.org/_all_dbs'

However, in none of the five places in couchdb-python where uri is actually 
called (all in client.py) is it possible to pass a path with a leading or 
trailing slash that should be stripped.

(1) Line 163: Database(uri(self.resource.uri, name), ...)

"/" is allowed in database names (except at the start of the string, but 
the validate_dbname function protects against this case).

(2) Line 586: TemporaryView(uri(self.resource.uri, '_temp_view'), ...)

Here the path parameter is hard-coded, and does not contain "/".

(3) Line 685: PermanentView(uri(self.resource.uri, *name.split('/')), ...)

Here the path parameter is variable, but because it is constructed 
precisely by splitting on "/", no individual path component will ever 
contain "/".

(4) Line 967: type(self)(self.http, uri(self.uri, path))

Resource objects may instantiate new Resource objects via their __call__ 
method. This is used three times (in the attachment machinery), and 'path' 
is always a document id.

(5) Line 1004: self.http.request(uri(self.uri, path, **params)

This is the most hydra-like call to uri(), in that all of Resource's 
delete/get/head/post/put methods funnel here. However, a review of the code 
shows that:

  o Often these methods are called with no 'path' argument.
  o Where these methods are called with a hard-coded 'path' argument, those 
arguments have no "/" in them.
  o Where these methods are called with a variable 'path' argument, the 
argument is either a db name, a doc id, or an attachment filename. (This 
last will never[?] contain a slash due to OS-level filesystem naming 
constraints, but shouldn't be stripped even if it does).

The bottom line is that the call to strip() appears to exist only for the 
docstring (disregarding any usage downstream of couchdb-python). I submit, 
therefore, that the best fix is to simply remove that call. Patch attached 
(with doctests).

Original issue reported on code.google.com by whit537@gmail.com on 18 Oct 2009 at 2:49

Attachments:

GoogleCodeExporter commented 8 years ago
Fixed in rdfcfbd6c7e. Thanks for the patch!

Original comment by djc.ochtman on 10 Dec 2009 at 3:55