From couchdb-commits-return-168-apmail-incubator-couchdb-commits-archive=incubator.apache.org@incubator.apache.org Fri Apr 04 22:43:03 2008 Return-Path: Delivered-To: apmail-incubator-couchdb-commits-archive@locus.apache.org Received: (qmail 44754 invoked from network); 4 Apr 2008 22:43:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Apr 2008 22:43:03 -0000 Received: (qmail 76648 invoked by uid 500); 4 Apr 2008 22:43:03 -0000 Delivered-To: apmail-incubator-couchdb-commits-archive@incubator.apache.org Received: (qmail 76609 invoked by uid 500); 4 Apr 2008 22:43:03 -0000 Mailing-List: contact couchdb-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: couchdb-dev@incubator.apache.org Delivered-To: mailing list couchdb-commits@incubator.apache.org Received: (qmail 76600 invoked by uid 99); 4 Apr 2008 22:43:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Apr 2008 15:43:03 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Apr 2008 22:42:20 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2E83D1A9838; Fri, 4 Apr 2008 15:42:40 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r644977 - /incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl Date: Fri, 04 Apr 2008 22:42:39 -0000 To: couchdb-commits@incubator.apache.org From: cmlenz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080404224240.2E83D1A9838@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cmlenz Date: Fri Apr 4 15:42:36 2008 New Revision: 644977 URL: http://svn.apache.org/viewvc?rev=644977&view=rev Log: mochiweb branch: add attachment support, fix db_info. Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl?rev=644977&r1=644976&r2=644977&view=diff ============================================================================== --- incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl (original) +++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl Fri Apr 4 15:42:36 2008 @@ -156,11 +156,7 @@ handle_db_request(Req, 'GET', {DbName, Db, []}) -> {ok, DbInfo} = couch_db:get_db_info(Db), - send_json(Req, {obj, [ - {"db_name", DbName}, - {"doc_count", proplists:get_value(doc_count, DbInfo)}, - {"update_seq", proplists:get_value(last_update_seq, DbInfo)} - ]}); + send_json(Req, {obj, [{db_name, DbName} | DbInfo]}); handle_db_request(Req, 'POST', {_DbName, Db, []}) -> % TODO: Etag handling @@ -371,9 +367,15 @@ % Document request handlers -handle_db_request(Req, Method, {DbName, Db, [Resource]}) -> - DocId = mochiweb_util:unquote(Resource), - handle_doc_request(Req, Method, DbName, Db, DocId). +handle_db_request(Req, Method, {DbName, Db, [DocId]}) -> + UnquotedDocId = mochiweb_util:unquote(DocId), + handle_doc_request(Req, Method, DbName, Db, UnquotedDocId); + +handle_db_request(Req, Method, {DbName, Db, [DocId, FileName]}) -> + UnquotedDocId = mochiweb_util:unquote(DocId), + UnquotedFileName = mochiweb_util:unquote(FileName), + handle_attachment_request(Req, Method, DbName, Db, UnquotedDocId, + UnquotedFileName). handle_doc_request(Req, 'DELETE', _DbName, Db, DocId) -> % TODO: Etag handling @@ -460,6 +462,34 @@ handle_doc_request(_Req, _Method, _DbName, _Db, _DocId) -> throw({method_not_allowed, "DELETE,GET,HEAD,PUT"}). + +% Attachment request handlers + +handle_attachment_request(Req, 'GET', _DbName, Db, DocId, FileName) -> + case couch_db:open_doc(Db, DocId, []) of + {ok, #doc{attachments=Attachments}} -> + case proplists:get_value(FileName, Attachments) of + undefined -> + throw({not_found, missing}); + {Type, Bin} -> + Resp = Req:respond({200, [ + {"content-type", Type}, + {"content-length", integer_to_list(couch_doc:bin_size(Bin))} + ], chunked}), + couch_doc:bin_foldl(Bin, + fun(BinSegment, []) -> + Resp:write_chunk(BinSegment) + end, + [] + ), + Resp:write_chunk("") + end; + Error -> + throw(Error) + end; + +handle_attachment_request(_Req, _Method, _DbName, _Db, _DocId, _FileName) -> + throw({method_not_allowed, "GET,HEAD"}). % View request handling internals