## 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'+(Date.now()+'').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',val:Date.now().toString(36)})\n\t}\n}"
}
````
3. Call `http://couchdb_ip/zz/_design/n/_rewrite`.
## Solution
The issue is caused by cleaning-up rewritten request prematurely here:
https://github.com/apache/couchdb/blob/master/src/chttpd/src/chttpd_rewrite.erl#L67.
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
end,
case Body of
undefined -> MochiReq:cleanup();
_ -> erlang:put(mochiweb_request_body, Body)
end,
````
It fixes issue completely.
## Your Environment
* Version used: 2.1.2, 2.2.0
[ Full content available at: https://github.com/apache/couchdb/issues/1612 ]
This message was relayed via gitbox.apache.org for [email protected]