You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ii...@apache.org on 2020/07/10 16:47:27 UTC

[couchdb] branch prototype/fdb-layer updated: Simplify using `req_body` for JSON requests

This is an automated email from the ASF dual-hosted git repository.

iilyak pushed a commit to branch prototype/fdb-layer
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/prototype/fdb-layer by this push:
     new ddbaf4f  Simplify using `req_body` for JSON requests
     new eda40cc  Merge pull request #2991 from cloudant/req_body-json
ddbaf4f is described below

commit ddbaf4f0e88558ea00a9fb4e4cf9eb2365af9ef7
Author: ILYA Khlopotov <ii...@apache.org>
AuthorDate: Thu Jul 9 10:47:31 2020 -0700

    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).
---
 src/chttpd/src/chttpd.erl                       | 10 ++++++----
 src/chttpd/src/chttpd_misc.erl                  |  3 +--
 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 eca936f..5a3e3fa 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 565b121..07d5371 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 f3e8f5d..649d82e 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) ->