/// <summary>
        /// Uploads the supplied content as an attachment to the specified document.
        /// </summary>
        /// <returns>The response state for further HTTP processing</returns>
        /// <param name="context">The context of the Couchbase Lite HTTP request</param>
        /// <remarks>
        /// http://docs.couchdb.org/en/latest/api/document/attachments.html#put--db-docid-attname
        /// </remarks>
        public static ICouchbaseResponseState UpdateAttachment(ICouchbaseListenerContext context)
        {
            var state = new AsyncOpCouchbaseResponseState();

            DatabaseMethods.PerformLogicWithDatabase(context, true, db =>
            {
                var blob     = db.AttachmentWriter;
                var httpBody = new byte[context.ContentLength];
                context.BodyStream.ReadAsync(httpBody, 0, httpBody.Length).ContinueWith(t => {
                    if (t.Result == 0)
                    {
                        state.Response = context.CreateResponse(StatusCode.BadAttachment);
                        state.SignalFinished();
                        return;
                    }

                    blob.AppendData(httpBody);
                    blob.Finish();

                    state.Response = UpdateAttachment(context, db, context.AttachmentName, context.DocumentName, blob);
                    state.SignalFinished();
                });

                return(null);
            });

            return(state);
        }
        /// <summary>
        /// Uploads the supplied content as an attachment to the specified document.
        /// </summary>
        /// <returns>The response state for further HTTP processing</returns>
        /// <param name="context">The context of the Couchbase Lite HTTP request</param>
        /// <remarks>
        /// http://docs.couchdb.org/en/latest/api/document/attachments.html#put--db-docid-attname
        /// <remarks>
        public static ICouchbaseResponseState UpdateAttachment(ICouchbaseListenerContext context)
        {
            var state = new AsyncOpCouchbaseResponseState();
            DatabaseMethods.PerformLogicWithDatabase(context, true, db =>
            {
                
                var blob = db.AttachmentWriter;
                var httpBody = new byte[context.ContentLength];
                context.BodyStream.ReadAsync(httpBody, 0, httpBody.Length).ContinueWith(t => {
                    if(t.Result == 0) {
                        state.Response = context.CreateResponse(StatusCode.BadAttachment);
                        state.SignalFinished();
                        return;
                    }

                    blob.AppendData(httpBody);
                    blob.Finish();

                    state.Response = UpdateAttachment(context, db, context.AttachmentName, context.DocumentName, blob);
                    state.SignalFinished();
                });

                return null;
            });
                
            return state;
        }