12

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
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138

1 Answers1

11

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}
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • Given the database above, what happens if you query `localhost:5984/db/?dummy=1`? – Tim Robinson Feb 21 '11 at 09:11
  • 1
    You are right. This looks like perhaps a rewriter bug. I will amend my answer with some workaround ideas. – JasonSmith Feb 21 '11 at 11:13
  • Thanks for the advice - I'll omit the query string where it causes a problem. It's being added by the Futon /_utils/script/jquery.couch.js script. When you're using IE, it adds a `?_=` parameter to all CouchDB calls, apparently to stop IE from caching the response. The place where it causes a problem is the `_changes` API, where jquery.couch.js first queries the `/dbname/` URL to obtain `update_seq`. – Tim Robinson Feb 22 '11 at 08:57
  • PS I might try hiding the database API behind some update and show functions, avoiding the need for the strange ../../ rewrite. However, it's not clear how I'd access the `_changes` API in this case. I can start a new SO question for this, though. – Tim Robinson Feb 22 '11 at 09:00