Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-rewritten post/put hangs or returns bad_request #1612

ermouth opened this issue Sep 19, 2018 · 0 comments

JS-rewritten post/put hangs or returns bad_request #1612

ermouth opened this issue Sep 19, 2018 · 0 comments


Copy link

@ermouth ermouth commented Sep 19, 2018

Expected Behavior

JS rewrite, returning {path:'whatever', method:'PUT', body:'body'} should send body to the rewritten endpoint.

Current Behavior

The body field of rewrite fn returned obj is in some cases dropped. If source request was POST/PUT, the rewritten requests hangs, or, if source request was GET-like, bad_request error is sent as response.

Steps to Reproduce

  1. Create empty bucket named, say, zz
  2. Put design doc with rewrites into the bucket. Below ddoc is intended to create new random doc on each request to _design/n/_rewrite endpoint:
  "_id": "_design/n",
  "rewrites": "function (r) {\n\tvar id = 'x'+('').substr(-8);\n\treturn {\n\t\tpath:'../../'+id,\n\t\tmethod:'PUT',\n\t\theaders:{'content-type':'application/json',Accept:'application/json'},\n\t\tbody:JSON.stringify({_id:id,type:'test',})\n\t}\n}"
  1. Call http://couchdb_ip/zz/_design/n/_rewrite.


The issue is caused by cleaning-up rewritten request prematurely here:
It should only be cleaned if we have no body to send.

That line (L67) should be replaced with this code:

            Body = case couch_util:get_value(<<"body">>, Props) of
                undefined -> erlang:get(mochiweb_request_body);
                B -> B
            case Body of
                undefined -> NewMochiReq:cleanup();
                _ -> erlang:put(mochiweb_request_body, Body)

It seems to fix issue completely.

Your Environment

  • Version used: 2.1.2, 2.2.0
ermouth added a commit to ermouth/couchdb that referenced this issue Sep 21, 2018
@ermouth ermouth mentioned this issue Sep 21, 2018
3 of 3 tasks complete
@wohali wohali closed this in #1620 Oct 2, 2018
wohali added a commit that referenced this issue Oct 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant