anchor.diff:  db_doc_req(Req, _Db, _DocId) ->
       send_method_not_allowed(Req, "DELETE,GET,HEAD,POST,PUT,COPY").
   
  +send_doc(Req, Doc, Options) ->
  +    case Doc#doc.meta of
  +    [] ->
  +        DiskEtag = couch_httpd:doc_etag(Doc),
  +        % output etag only when we have no meta
  +        couch_httpd:etag_respond(Req, DiskEtag, fun() ->
  +            send_doc_efficiently(Req, Doc, [{"Etag", DiskEtag}], Options)
  +        end);
  +    _ ->
  +        send_doc_efficiently(Req, Doc, [], Options)
  +    end.
  +
  +send_doc_efficiently(Req, #doc{atts=[]}=Doc, Headers, Options) ->
  +        send_json(Req, 200, Headers, couch_doc:to_json_obj(Doc, Options));
  +send_doc_efficiently(Req, #doc{atts=Atts}=Doc, Headers, Options) ->
  +    case lists:member(attachments, Options) of
  +    true ->
  +        AcceptedTypes = case couch_httpd:header_value(Req, "Accept") of
  +            undefined       -> [];
  +            AcceptHeader    -> string:tokens(AcceptHeader, ", ")
  +        end,
  +        case lists:member("multipart/related", AcceptedTypes) of
  +        false ->
  +            send_json(Req, 200, Headers, couch_doc:to_json_obj(Doc, Options));
  +        true ->
  +            Boundary = couch_uuids:random(),
  +            JsonBytes = ?JSON_ENCODE(couch_doc:to_json_obj(Doc, 
  +                    [attachments, follows|Options])),
  +            {ContentType, Len} = couch_doc:len_doc_to_multi_part_stream(
  +                    Boundary,JsonBytes, Atts,false),
  +            CType = {<<"Content-Type">>, ContentType},
  +            {ok, Resp} = start_response_length(Req, 200, [CType|Headers], Len),
  +            couch_doc:doc_to_multi_part_stream(Boundary,JsonBytes,Atts,
  +                    fun(Data) -> couch_httpd:send(Resp, Data) end, false)
  +        end;
  +    false ->
  +        send_json(Req, 200, Headers, couch_doc:to_json_obj(Doc, Options))
  +    end.
  +
  +send_docs_multipart(Req, Results, Options) ->
  +    OuterBoundary = couch_uuids:random(),
  +    InnerBoundary = couch_uuids:random(),
  +    CType = {"Content-Type", 
  +        "multipart/mixed; boundary=\"" ++ ?b2l(OuterBoundary) ++ "\""},
  +    {ok, Resp} = start_chunked_response(Req, 200, [CType]),
  +    couch_httpd:send_chunk(Resp, <<"--", OuterBoundary/binary>>),
  +    lists:foreach(
  +        fun({ok, #doc{atts=Atts}=Doc}) ->
  +            JsonBytes = ?JSON_ENCODE(couch_doc:to_json_obj(Doc, 
  +                    [attachments,follows|Options])),
  +            {ContentType, _Len} = couch_doc:len_doc_to_multi_part_stream(
  +                    InnerBoundary, JsonBytes, Atts, false),
  +            couch_httpd:send_chunk(Resp, <<"\r\nContent-Type: ",
  +                    ContentType/binary, "\r\n\r\n">>),
  +            couch_doc:doc_to_multi_part_stream(InnerBoundary, JsonBytes, Atts,
  +                    fun(Data) -> couch_httpd:send_chunk(Resp, Data)
  +                    end, false),
  +             couch_httpd:send_chunk(Resp, <<"\r\n--", OuterBoundary/binary>>);
  +        ({{not_found, missing}, RevId}) ->
  +             RevStr = couch_doc:rev_to_str(RevId),
  +             Json = ?JSON_ENCODE({[{"missing", RevStr}]}),
  +             couch_httpd:send_chunk(Resp, 
  +                [<<"\r\nContent-Type: application/json; error=\"true\"\r\n\r\n">>, 
  +                Json,
  +                <<"\r\n--", OuterBoundary/binary>>])
  +         end, Results),
  +    couch_httpd:send_chunk(Resp, <<"--">>),
  +    couch_httpd:last_chunk(Resp).
  +
   receive_request_data(Req) ->
       {couch_httpd:recv(Req, 0), fun() -> receive_request_data(Req) end}.
   
  
positive.code: % Attachment request handlers
  
  db_attachment_req(#httpd{method='GET'}=Req, Db, DocId, FileNameParts) ->
      FileName = list_to_binary(mochiweb_util:join(lists:map(fun binary_to_list/1, FileNameParts),"/")),
      #doc_query_args{
          rev=Rev,
          options=Options
  
negative.code:     couch_doc_open(Db, DocId, nil, []),
      case chttpd:qs_value(Req, "rev") of
      undefined ->
          Body = {[{<<"_deleted">>,true}]};
      Rev ->
          Body = {[{<<"_rev">>, ?l2b(Rev)},{<<"_deleted">>,true}]}
      end,
      update_doc(Req, Db, DocId, couch_doc_from_req(Req, DocId, Body));
  
  db_doc_req(#httpd{method='GET'}=Req, Db, DocId) ->
      #doc_query_args{
          rev = Rev,
          open_revs = Revs,
          options = Options,
          atts_since = AttsSince
      } = parse_doc_query(Req),
      case Revs of
      [] ->
          Options2 =
          if AttsSince /= nil ->
              [{atts_since, AttsSince}, attachments | Options];
          true -> Options
          end,
          Doc = couch_doc_open(Db, DocId, Rev, Options2),
          send_doc(Req, Doc, Options2);
      _ ->
          {ok, Results} = fabric:open_revs(Db, DocId, Revs, Options),
          AcceptedTypes = case couch_httpd:header_value(Req, "Accept") of
              undefined       -> [];
              AcceptHeader    -> string:tokens(AcceptHeader, "; ")
          end,
          case lists:member("multipart/mixed", AcceptedTypes) of
          false ->
              {ok, Resp} = start_json_response(Req, 200),
              send_chunk(Resp, "["),
              % We loop through the docs. The first time through the separator
              % is whitespace, then a comma on subsequent iterations.
              lists:foldl(
                  fun(Result, AccSeparator) ->
                      case Result of
                      {ok, Doc} ->
                          JsonDoc = couch_doc:to_json_obj(Doc, Options),
                          Json = ?JSON_ENCODE({[{ok, JsonDoc}]}),
                          send_chunk(Resp, AccSeparator ++ Json);
                      {{not_found, missing}, RevId} ->
                          RevStr = couch_doc:rev_to_str(RevId),
                          Json = ?JSON_ENCODE({[{"missing", RevStr}]}),
                          send_chunk(Resp, AccSeparator ++ Json)
                      end,
                      "," % AccSeparator now has a comma
                  end,
                  "", Results),
              send_chunk(Resp, "]"),
              end_json_response(Resp);
          true ->
              send_docs_multipart(Req, Results, Options)
          end
      end;
  
  db_doc_req(#httpd{method='POST', user_ctx=Ctx}=Req, Db, DocId) ->
      couch_httpd:validate_referer(Req),
      couch_doc:validate_docid(DocId),
      couch_httpd:validate_ctype(Req, "multipart/form-data"),
      Form = couch_httpd:parse_form(Req),
      case proplists:is_defined("_doc", Form) of
      true ->
          Json = ?JSON_DECODE(couch_util:get_value("_doc", Form)),
          Doc = couch_doc_from_req(Req, DocId, Json);
      false ->
          Rev = couch_doc:parse_rev(list_to_binary(couch_util:get_value("_rev", Form))),
          {ok, [{ok, Doc}]} = fabric:open_revs(Db, DocId, [Rev], [])
      end,
      UpdatedAtts = [
          #att{name=validate_attachment_name(Name),
              type=list_to_binary(ContentType),
  