Author: kocolosk Date: Wed Jun 16 17:13:48 2010 New Revision: 955304 URL: http://svn.apache.org/viewvc?rev=955304&view=rev Log: identity transfer w/ Content-Length when possible. thx rnewson. COUCHDB-745
Modified: couchdb/branches/0.11.x/src/couchdb/couch_httpd_db.erl Modified: couchdb/branches/0.11.x/src/couchdb/couch_httpd_db.erl URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/src/couchdb/couch_httpd_db.erl?rev=955304&r1=955303&r2=955304&view=diff ============================================================================== --- couchdb/branches/0.11.x/src/couchdb/couch_httpd_db.erl (original) +++ couchdb/branches/0.11.x/src/couchdb/couch_httpd_db.erl Wed Jun 16 17:13:48 2010 @@ -877,7 +877,7 @@ db_attachment_req(#httpd{method='GET'}=R case [A || A <- Atts, A#att.name == FileName] of [] -> throw({not_found, "Document is missing attachment"}); - [#att{type=Type, encoding=Enc}=Att] -> + [#att{type=Type, encoding=Enc, disk_len=DiskLen, att_len=AttLen}=Att] -> Etag = couch_httpd:doc_etag(Doc), ReqAcceptsAttEnc = lists:member( atom_to_list(Enc), @@ -893,6 +893,25 @@ db_attachment_req(#httpd{method='GET'}=R _ -> [] end, + Len = case {Enc, ReqAcceptsAttEnc} of + {identity, _} -> + % stored and served in identity form + DiskLen; + {_, false} when DiskLen =/= AttLen -> + % Stored encoded, but client doesn't accept the encoding we used, + % so we need to decode on the fly. DiskLen is the identity length + % of the attachment. + DiskLen; + {_, true} -> + % Stored and served encoded. AttLen is the encoded length. + AttLen; + _ -> + % We received an encoded attachment and stored it as such, so we + % don't know the identity length. The client doesn't accept the + % encoding, and since we cannot serve a correct Content-Length + % header we'll fall back to a chunked response. + undefined + end, AttFun = case ReqAcceptsAttEnc of false -> fun couch_doc:att_foldl_decode/3; @@ -903,9 +922,15 @@ db_attachment_req(#httpd{method='GET'}=R Req, Etag, fun() -> - {ok, Resp} = start_chunked_response(Req, 200, Headers), - AttFun(Att, fun(Seg, _) -> send_chunk(Resp, Seg) end, ok), - last_chunk(Resp) + case Len of + undefined -> + {ok, Resp} = start_chunked_response(Req, 200, Headers), + AttFun(Att, fun(Seg, _) -> send_chunk(Resp, Seg) end, ok), + last_chunk(Resp); + _ -> + {ok, Resp} = start_response_length(Req, 200, Headers, Len), + AttFun(Att, fun(Seg, _) -> send(Resp, Seg) end, ok) + end end ) end;