diff options
| author | ILYA Khlopotov <iilyak@apache.org> | 2020-07-09 10:47:31 -0700 |
|---|---|---|
| committer | ILYA Khlopotov <iilyak@apache.org> | 2020-07-10 09:24:57 -0700 |
| commit | ddbaf4f0e88558ea00a9fb4e4cf9eb2365af9ef7 (patch) | |
| tree | 182d12b7ede4b385d982239627dd2eb2a4433b4b /src | |
| parent | 7c3d752a46070ad9b0f9ed3db7ddd17f4fd939c3 (diff) | |
| download | couchdb-ddbaf4f0e88558ea00a9fb4e4cf9eb2365af9ef7.tar.gz | |
Simplify using `req_body` for JSON requests
Currently the EPI plugins have no easy way to modify body of the document in
before request. There are complicated approaches via overiding compression header.
This is due to the fact that `chttp:json_body/1` expects compressed body.
We can rely on the fact that `MochiReq:recv_body/1` returns binary to allow
passing of already parsed JSON terms (objects and lists).
Diffstat (limited to 'src')
| -rw-r--r-- | src/chttpd/src/chttpd.erl | 10 | ||||
| -rw-r--r-- | src/chttpd/src/chttpd_misc.erl | 3 | ||||
| -rw-r--r-- | src/chttpd/test/eunit/chttpd_handlers_tests.erl | 2 |
3 files changed, 8 insertions, 7 deletions
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl index eca936fed..5a3e3fa38 100644 --- a/src/chttpd/src/chttpd.erl +++ b/src/chttpd/src/chttpd.erl @@ -423,8 +423,7 @@ possibly_hack(#httpd{path_parts=[<<"_replicate">>]}=Req) -> {Props0} = chttpd:json_body_obj(Req), Props1 = fix_uri(Req, Props0, <<"source">>), Props2 = fix_uri(Req, Props1, <<"target">>), - put(post_body, {Props2}), - Req; + Req#httpd{req_body={Props2}}; possibly_hack(Req) -> Req. @@ -677,13 +676,16 @@ body(#httpd{mochi_req=MochiReq, req_body=ReqBody}) -> validate_ctype(Req, Ctype) -> couch_httpd:validate_ctype(Req, Ctype). -json_body(Httpd) -> +json_body(#httpd{req_body=undefined} = Httpd) -> case body(Httpd) of undefined -> throw({bad_request, "Missing request body"}); Body -> ?JSON_DECODE(maybe_decompress(Httpd, Body)) - end. + end; + +json_body(#httpd{req_body=ReqBody}) -> + ReqBody. json_body_obj(Httpd) -> case json_body(Httpd) of diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl index 565b1210c..07d53714a 100644 --- a/src/chttpd/src/chttpd_misc.erl +++ b/src/chttpd/src/chttpd_misc.erl @@ -302,10 +302,9 @@ handle_task_status_req(#httpd{method='GET'}=Req) -> handle_task_status_req(Req) -> send_method_not_allowed(Req, "GET,HEAD"). -handle_replicate_req(#httpd{method='POST', user_ctx=Ctx} = Req) -> +handle_replicate_req(#httpd{method='POST', user_ctx=Ctx, req_body=PostBody} = Req) -> chttpd:validate_ctype(Req, "application/json"), %% see HACK in chttpd.erl about replication - PostBody = get(post_body), case replicate(PostBody, Ctx) of {ok, {continuous, RepId}} -> send_json(Req, 202, {[{ok, true}, {<<"_local_id">>, RepId}]}); diff --git a/src/chttpd/test/eunit/chttpd_handlers_tests.erl b/src/chttpd/test/eunit/chttpd_handlers_tests.erl index f3e8f5dcd..649d82e86 100644 --- a/src/chttpd/test/eunit/chttpd_handlers_tests.erl +++ b/src/chttpd/test/eunit/chttpd_handlers_tests.erl @@ -70,7 +70,7 @@ request_replicate(Url, Body) -> Headers = [{"Content-Type", "application/json"}], Handler = {chttpd_misc, handle_replicate_req}, request(post, Url, Headers, Body, Handler, fun(Req) -> - chttpd:send_json(Req, 200, get(post_body)) + chttpd:send_json(Req, 200, Req#httpd.req_body) end). request(Method, Url, Headers, Body, {M, F}, MockFun) -> |
