Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Couch DB database URL through rewritten URL, with query parameters

Tags:

couchdb

I have my web site run out of a Couch DB instance, so I have my vhost configured to point to /dbname/_design/app/_rewrite.

I want to be able to access the index page from a web browser, while still accessing the Couch DB API over Ajax, so I set up a pair of rewrite rules in my rewrites field:

[ { "from": "/dbname/*", "to: ../../*" },
  { "from": "/*", "to: *" } ]

These rules work fine: I can access individual documents through a /dbname/docname URL, and I can point my web browser at the root of the site and access my attachments that way.

I'd now like to access the information on the database itself, in order to pass a since parameter to the _changes API.

  1. /dbname/ works fine
  2. /dbname/?name=value doesn't redirect properly. In the Couch DB log, I see lines like 'GET' /dbname/_design/..?name=value 404, whereas I'd expect to see 'GET' /dbname/?name=value 200.

The second case is needed for Ajax from IE, where the jquery.couch.js code adds a fake query string to avoid caching.

How can I phrase my rewrite rules so that Couch DB rewrites /dbname/?name=value correctly?

Edit: To clarify, query strings work OK as long as there is something after the last / in the URL.

  • /dbname/docname?rev=xxx works
  • /dbname/_changes?since=1 works
  • /dbname/?_=dummy doesn't work; it rewrites to /dbname/_design/..?_=dummy
like image 549
Tim Robinson Avatar asked Feb 20 '11 19:02

Tim Robinson


1 Answers

I tried to duplicate your problem but it is working. Below is my interaction. (Note, I use the IP address, 127.0.0.1:5984, to ensure no vhost/rewrite problems, then I access the "production" site via localhost:5984.

There is a bug it seems with query parameters being appended to rewrites ending with "..". Instead of rewriting to ../?key=val it writes to ..?key=val which CouchDB does not parse.

I do not think it is necessary to query a database URL with parameters. So one workaround is to always make sure you never do that. (E.g. if you blindly append no-op parameters to all queries to simplify the code, you'd have to alter that.)

Another workaround is to enable rewrite to the root CouchDB URL. This requires setting /_config/httpd/secure_rewrites to false.

{ "from":"/api/*", "to":"../../../*" }

Now you can query http://localhost:5984/api/x?key=val or http://localhost:5984/api/x/_changes?since=5. (You cannot query the root URL with parameters—it's still the bug, but in a less trafficked place.)

Following is the initial terminal session:

$ mkdir t
$ cd t
$ curl -XDELETE 127.0.0.1:5984/x 
{"ok":true}
$ curl -XPUT 127.0.0.1:5984/x 
{"ok":true}
$ curl 127.0.0.1:5984
{"couchdb":"Welcome","version":"1.0.1"}

$ echo -n _design/test > _id
$ mkdir shows
$ echo 'function() { return "hello world!\n" }' > shows/hello.js
$ cat > rewrites.json
[ { "from":"/db/*", "to":"../../*" }
, { "from":"/*"   , "to":"*"}
]

$ echo '{}' > .couchapprc
$ couchapp push http://127.0.0.1:5984/x
$ curl -XPUT http://127.0.0.1:5984/_config/vhosts/localhost:5984 -d '"/x/_design/test/_rewrite"'
"/x/_design/test/_rewrite"

$ curl localhost:5984 # This is the design document.
{"_id":"_design/test","_rev":"1-e523efd669aa5375e711f8e4b764da7a","shows":{"hello":"function() { return \"hello world!\\n\" }"},"couchapp":{"signatures":{},"objects":{},"manifest":["rewrites.json","shows/","shows/hello.js"]},"rewrites":[{"to":"../../*","from":"/db/*"},{"to":"*","from":"/*"}]}
$ curl localhost:5984/_show/hello
hello world!

$ curl localhost:5984/db # This is the DB.
{"db_name":"x","doc_count":1,"doc_del_count":0,"update_seq":1,"purge_seq":0,"compact_running":false,"disk_size":4185,"instance_start_time":"1298269455135987","disk_format_version":5,"committed_update_seq":1}
$ curl localhost:5984/db/_changes
{"results":[
{"seq":1,"id":"_design/test","changes":[{"rev":"1-e523efd669aa5375e711f8e4b764da7a"}]}
],
"last_seq":1}

$ curl localhost:5984/db/_changes?since=1 # Parameters accepted!
{"results":[

],
"last_seq":1}
like image 118
JasonSmith Avatar answered Dec 20 '22 01:12

JasonSmith