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

前端 未结 1 637
终归单人心
终归单人心 2021-02-07 23:07

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 i

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 23:41

    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}
    

    0 讨论(0)
提交回复
热议问题