summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Lehnardt <jan@apache.org>2009-03-26 17:40:27 +0000
committerJan Lehnardt <jan@apache.org>2009-03-26 17:40:27 +0000
commit1dd554d884644bbf6f5a7d07b7c8fe51feb7a4c4 (patch)
tree44f37e75467603a54edd79280cebc22d963de60f
parent39a927058a066e7c7c31cb4388371f2c8b639e4b (diff)
return a Location header on newly created documents using PUT requests
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@758768 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--share/www/script/test/basics.js7
-rw-r--r--src/couchdb/couch_httpd_db.erl9
2 files changed, 14 insertions, 2 deletions
diff --git a/share/www/script/test/basics.js b/share/www/script/test/basics.js
index ebfe5db2..7149c350 100644
--- a/share/www/script/test/basics.js
+++ b/share/www/script/test/basics.js
@@ -148,6 +148,13 @@ couchTests.basics = function(debug) {
T(locs[4] == resp.id);
T(locs[3] == "test_suite_db");
+ // document put's should return a Location header
+ var xhr = CouchDB.request("PUT", "/test_suite_db/newdoc", {
+ body: JSON.stringify({"a":1})
+ });
+ TEquals("/test_suite_db/newdoc", xhr.getResponseHeader("Location"),
+ "should return Location header to newly created document");
+
// deleting a non-existent doc should be 404
xhr = CouchDB.request("DELETE", "/test_suite_db/doc-does-not-exist");
T(xhr.status == 404);
diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl
index 92ad98c8..78b127b4 100644
--- a/src/couchdb/couch_httpd_db.erl
+++ b/src/couchdb/couch_httpd_db.erl
@@ -496,7 +496,8 @@ db_doc_req(#httpd{method='POST'}=Req, Db, DocId) ->
]});
db_doc_req(#httpd{method='PUT'}=Req, Db, DocId) ->
- update_doc(Req, Db, DocId, couch_httpd:json_body(Req));
+ update_doc(Req, Db, DocId, couch_httpd:json_body(Req),
+ [{"Location", "/" ++ ?b2l(Db#db.name) ++ "/" ++ ?b2l(DocId)}]);
db_doc_req(#httpd{method='COPY'}=Req, Db, SourceDocId) ->
SourceRev =
@@ -529,6 +530,9 @@ update_result_to_json(Error) ->
update_doc(Req, Db, DocId, Json) ->
+ update_doc(Req, Db, DocId, Json, []).
+
+update_doc(Req, Db, DocId, Json, Headers) ->
#doc{deleted=Deleted} = Doc = couch_doc:from_json_obj(Json),
validate_attachment_names(Doc),
ExplicitDocRev =
@@ -551,8 +555,9 @@ update_doc(Req, Db, DocId, Json) ->
end,
{ok, NewRev} = couch_db:update_doc(Db, Doc#doc{id=DocId, revs=Revs}, Options),
NewRevStr = couch_doc:rev_to_str(NewRev),
+ ResponseHeaders = [{"Etag", <<"\"", NewRevStr/binary, "\"">>}] ++ Headers,
send_json(Req, if Deleted -> 200; true -> 201 end,
- [{"Etag", <<"\"", NewRevStr/binary, "\"">>}], {[
+ ResponseHeaders, {[
{ok, true},
{id, DocId},
{rev, NewRevStr}]}).