benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 113 forks source link

Special characters in the document ID not working #4

Closed karalabe closed 14 years ago

karalabe commented 15 years ago

Couchdb can store for example '+' characters in the ID field, but if I ask couchbeam to store such a thing, a very different result is achieved:

couchbeam:save_doc(test, "test", {[{<<"_id">>, <<"a++b++c++d">>}]}). will result in a doc id: "a b c d" If i do the same from Futon, then the pluses are inserted correctly.

karalabe commented 15 years ago

Yet again a quick fix. The problem is that the doc id is passed in the URI, so it has to be UrlEncoded. The proper way would be to write a URL encoder function, but I used now a shortcut and called the encoder from edoc_lib (erlang really lacks a url encoder in the http libs).

In the couchbeam.erl I replaced the encode_docid functions with these: encode_docid(DocId) when is_binary(DocId) -> encode_docid(?b2l(DocId)); encode_docid(DocId) when is_list(DocId) -> edoc_lib:escape_uri(DocId).

Problem solved :)

benoitc commented 15 years ago

thanks I as the fix in new version.

benoitc commented 14 years ago

Pb solved in latest couchbeam version :

1> couchbeam:start(). \ Found 0 name clashes in code paths ok 2> couchbeam_server:start_connection(). <0.61.0> 3> couchbeam_server:open_or_create_db(default, {test, "testdb"}).
<0.72.0> 4> couchbeam_db:save_doc(test, {[{<<"_id">>, <<"a++b++c++d">>}]}).
{[{<<"_id">>,<<"a b c d">>}, {<<"_rev">>,<<"1-967a00dff5e02add41819138abb3284d">>}]} 5> couchbeam_db:open_doc(test, "a++b++c++d").
{[{<<"_id">>,<<"a b c d">>}, {<<"_rev">>,<<"1-967a00dff5e02add41819138abb3284d">>}]}

karalabe commented 14 years ago

I don't really think this is the good solution. CouchDB allows special characters like a plus sign to be inserted into the database ID field. If you convert the pluses into something else, then you potentially cripple couchbeam because it won't be compatible with other programs which use these signs.

benoitc commented 14 years ago

could you give me the real doc id you want ? I woudl be interrested by a test case to find a proper way to fix it.

karalabe commented 14 years ago

Consider this example:

"`~!@#$%^&*()_+-=[]{}|;':",./<> ?" (without outer quotation marks)

In CouchDB this is a completely valid document ID. Try it out using Futon (The CouchDB web interface)

benoitc commented 14 years ago

Fixed in latest head, sorry for the delay and thanks for pointing to edoc:escape_uri function.

karalabe commented 14 years ago

Next week I'm on "holiday", so maybe I'll look at that escape function and implement it into couchbeam (fork and if you like it you can merge it back). It should be implemented into couchbeam itself because there are a whole lot of dependencies for edoc (edoc even requires the erlang compiler), so if I want to use couchbeam in a release now, I need to insert at least 4-5 other dependencies. Sorry for not being able to look at it until now, but I had lots of stuff to do and the couchbeam/couchdb part was only a small fraction of it.

benoitc commented 14 years ago

You're right less dependancies is better.... I imported url_encode function from ibrowse project to replace erldoc:escape_uri function. commited.