couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickno...@apache.org
Subject [1/3] couchdb commit: updated refs/heads/1956-attachment-handling to 9bda4f6
Date Tue, 25 Feb 2014 20:43:00 GMT
Repository: couchdb
Updated Branches:
  refs/heads/1956-attachment-handling [created] 9bda4f667


Enable posting of multipart/related documents

Add case to db_req POST processing for multipart/related documents,
modelled on the PUT equivalent.


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/58989257
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/58989257
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/58989257

Branch: refs/heads/1956-attachment-handling
Commit: 5898925752306e6786dc07df1801342fd3319fce
Parents: b4b6fe1
Author: NickNorth <North.N@gmail.com>
Authored: Tue Feb 25 19:51:59 2014 +0000
Committer: NickNorth <North.N@gmail.com>
Committed: Tue Feb 25 20:40:09 2014 +0000

----------------------------------------------------------------------
 src/couchdb/couch_httpd_db.erl | 42 +++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/58989257/src/couchdb/couch_httpd_db.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl
index 0a7c17c..6a78b13 100644
--- a/src/couchdb/couch_httpd_db.erl
+++ b/src/couchdb/couch_httpd_db.erl
@@ -244,17 +244,29 @@ db_req(#httpd{method='GET',path_parts=[_DbName]}=Req, Db) ->
     send_json(Req, {DbInfo});
 
 db_req(#httpd{method='POST',path_parts=[_DbName]}=Req, Db) ->
-    couch_httpd:validate_ctype(Req, "application/json"),
-    Doc = couch_doc:from_json_obj(couch_httpd:json_body(Req)),
-    validate_attachment_names(Doc),
-    Doc2 = case Doc#doc.id of
-        <<"">> ->
-            Doc#doc{id=couch_uuids:new(), revs={0, []}};
-        _ ->
-            Doc
-    end,
-    DocId = Doc2#doc.id,
-    update_doc(Req, Db, DocId, Doc2);
+    case couch_util:to_list(couch_httpd:header_value(Req, "Content-Type")) of
+    ("application/json" ++ _) ->
+        Doc = couch_doc:from_json_obj(couch_httpd:json_body(Req)),
+        validate_attachment_names(Doc),
+        Doc2 = maybe_add_docid(Doc),
+        DocId = Doc2#doc.id,
+        update_doc(Req, Db, DocId, Doc2);
+    ("multipart/related;" ++ _) = ContentType ->
+        {ok, Doc0, WaitFun, Parser} = couch_doc:doc_from_multi_part_stream(
+            ContentType, fun() -> receive_request_data(Req) end),
+        validate_attachment_names(Doc0),
+        Doc2 = maybe_add_docid(Doc0),
+        DocId = Doc2#doc.id,
+        try
+            Result = update_doc(Req, Db, DocId, Doc2),
+            WaitFun(),
+            Result
+        catch throw:Err ->
+            % Document rejected by a validate_doc_update function.
+            couch_doc:abort_multi_part_stream(Parser),
+            throw(Err)
+        end
+    end;
 
 db_req(#httpd{path_parts=[_DbName]}=Req, _Db) ->
     send_method_not_allowed(Req, "DELETE,GET,HEAD,POST");
@@ -762,6 +774,14 @@ update_doc(Req, Db, DocId, #doc{deleted=Deleted}=Doc, Headers, UpdateType)
->
                 {rev, NewRevStr}]})
     end.
 
+maybe_add_docid(Doc) ->
+    case Doc#doc.id of
+        <<"">> ->
+            Doc#doc{id=couch_uuids:new(), revs={0, []}};
+        _  ->
+            Doc
+    end.
+
 couch_doc_from_req(Req, DocId, #doc{revs=Revs}=Doc) ->
     validate_attachment_names(Doc),
     Rev = case couch_httpd:qs_value(Req, "rev") of


Mime
View raw message