From fe934a16760dcbf975d9f8b4923eee53747bfd25 Mon Sep 17 00:00:00 2001 From: Benjamin Nortier Date: Thu, 20 Sep 2012 11:01:35 +0100 Subject: [PATCH 1/3] Document Id and Rev in response headers --- src/couch_mrview/src/couch_mrview_show.erl | 14 +++++++++++++- src/couchdb/couch_httpd.erl | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/couch_mrview/src/couch_mrview_show.erl b/src/couch_mrview/src/couch_mrview_show.erl index 426b431f8f7..98ecab24988 100644 --- a/src/couch_mrview/src/couch_mrview_show.erl +++ b/src/couch_mrview/src/couch_mrview_show.erl @@ -144,9 +144,21 @@ send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) -> couch_doc:validate_docid(NewDoc#doc.id), {ok, NewRev} = couch_db:update_doc(Db, NewDoc, Options), NewRevStr = couch_doc:rev_to_str(NewRev), + DocIdHeader = case DocId of + null -> + [{<<"json">>, {Props}}] = JsonResp0, + case lists:keyfind(<<"id">>, 1, Props) of + {_, NewDocId} -> + [{<<"X-Couch-Id">>, NewDocId}]; + false -> + [] + end; + DocId -> + [{<<"X-Couch-Id">>, DocId}] + end, {[ {<<"code">>, 201}, - {<<"headers">>, {[{<<"X-Couch-Update-NewRev">>, NewRevStr}]}} + {<<"headers">>, {[{<<"X-Couch-Update-NewRev">>, NewRevStr}] ++ DocIdHeader}} | JsonResp0]}; [<<"up">>, _Other, {JsonResp0}] -> {[{<<"code">>, 200} | JsonResp0]} diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 90d5c80b6b6..d69cae8d112 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -690,8 +690,19 @@ send_json(Req, Code, Headers, Value) -> {"Content-Type", negotiate_content_type(Req)}, {"Cache-Control", "must-revalidate"} ], + IdAndRevHeaders = case Value of + {Props} when is_list(Props) -> + case {lists:keyfind(id, 1, Props), lists:keyfind(rev, 1, Props)} of + {{_, Id}, {_, Rev}} -> + [{"X-Couch-Id", Id}, {"X-Couch-Rev", Rev}]; + _ -> + [] + end; + _ -> + [] + end, Body = [start_jsonp(), ?JSON_ENCODE(Value), end_jsonp(), $\n], - send_response(Req, Code, DefaultHeaders ++ Headers, Body). + send_response(Req, Code, DefaultHeaders ++ IdAndRevHeaders ++ Headers, Body). start_json_response(Req, Code) -> start_json_response(Req, Code, []). From b38374034a57db924a2650038f078dbe4c61b715 Mon Sep 17 00:00:00 2001 From: Benjamin Nortier Date: Fri, 21 Sep 2012 16:46:46 +0100 Subject: [PATCH 2/3] Only return X-Couch-Id (rev is available in ETag) --- src/couchdb/couch_httpd.erl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index d69cae8d112..485a9a4d441 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -690,19 +690,19 @@ send_json(Req, Code, Headers, Value) -> {"Content-Type", negotiate_content_type(Req)}, {"Cache-Control", "must-revalidate"} ], - IdAndRevHeaders = case Value of - {Props} when is_list(Props) -> - case {lists:keyfind(id, 1, Props), lists:keyfind(rev, 1, Props)} of - {{_, Id}, {_, Rev}} -> - [{"X-Couch-Id", Id}, {"X-Couch-Rev", Rev}]; - _ -> - [] - end; - _ -> - [] - end, + IdHeader = case Value of + {Props} when is_list(Props) -> + case lists:keyfind(id, 1, Props) of + {_, Id} -> + [{"X-Couch-Id", Id}]; + _ -> + [] + end; + _ -> + [] + end, Body = [start_jsonp(), ?JSON_ENCODE(Value), end_jsonp(), $\n], - send_response(Req, Code, DefaultHeaders ++ IdAndRevHeaders ++ Headers, Body). + send_response(Req, Code, DefaultHeaders ++ IdHeader ++ Headers, Body). start_json_response(Req, Code) -> start_json_response(Req, Code, []). From a1f224ea46d4919b3dba5ebc40c5bea0e5fc0100 Mon Sep 17 00:00:00 2001 From: Benjamin Nortier Date: Fri, 2 Nov 2012 12:46:20 +0000 Subject: [PATCH 3/3] Test the X-Couch-Id header --- share/www/script/test/update_documents.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/share/www/script/test/update_documents.js b/share/www/script/test/update_documents.js index bbaa07da797..1dc1b4e9c9c 100644 --- a/share/www/script/test/update_documents.js +++ b/share/www/script/test/update_documents.js @@ -107,6 +107,7 @@ couchTests.update_documents = function(debug) { var resp = db.save(doc); T(resp.ok); var docid = resp.id; + T(equals(docid, db.last_req.getResponseHeader("X-Couch-Id"))); // update error var xhr = CouchDB.request("POST", "/test_suite_db/_design/update/_update/"); @@ -117,7 +118,8 @@ couchTests.update_documents = function(debug) { xhr = CouchDB.request("PUT", "/test_suite_db/_design/update/_update/hello/"+docid); T(xhr.status == 201); T(xhr.responseText == "

hello doc

"); - T(/charset=utf-8/.test(xhr.getResponseHeader("Content-Type"))) + T(/charset=utf-8/.test(xhr.getResponseHeader("Content-Type"))); + T(equals(docid, xhr.getResponseHeader("X-Couch-Id"))); doc = db.open(docid); T(doc.world == "hello");