Beispiel #1
0
        // Attempt to write the response over the wire to the client
        private static void ProcessResponse(ICouchbaseListenerContext context, ICouchbaseResponseState responseState)
        {
            CouchbaseLiteResponse responseObject = CheckForAltMethod(context, responseState.Response);

            if (!responseState.IsAsync)
            {
                try {
                    Log.To.Router.I(TAG, "Processing response for {0}...", context.RequestUrl.PathAndQuery);
                    Log.To.Router.V(TAG, "Processing request range...");
                    responseObject.ProcessRequestRanges();
                    Log.To.Router.V(TAG, "Writing headers...");
                    responseObject.WriteHeaders();
                    Log.To.Router.V(TAG, "Writing body...");
                    responseObject.WriteToContext();
                    Log.To.Router.I(TAG, "{0} {1} => {2} ({3})", context.Method, context.RequestUrl.PathAndQuery, responseObject.Status, responseObject.StatusMessage);
                } catch (Exception e) {
                    Log.To.Router.E(TAG, "Exception writing response", e);
                    responseState = context.CreateResponse(StatusCode.Exception).AsDefaultState();
                }
            }
            else
            {
                _UnfinishedResponses.Add(responseState);
            }
        }
Beispiel #2
0
        // Attempt to write the response over the wire to the client
        private static void ProcessResponse(ICouchbaseListenerContext context, ICouchbaseResponseState responseState)
        {
            CouchbaseLiteResponse responseObject = CheckForAltMethod(context, responseState.Response);

            if (!responseState.IsAsync)
            {
                try {
                    responseObject.ProcessRequestRanges();
                    responseObject.WriteHeaders();
                    responseObject.WriteToContext();
                } catch (Exception e) {
                    Log.E(TAG, "Exception writing response", e);
                    responseState = context.CreateResponse(StatusCode.Exception).AsDefaultState();
                }
            }
            else
            {
                _UnfinishedResponses.Add(responseState);
            }
        }
Beispiel #3
0
        // Check for an incorrect request method on a request
        private static CouchbaseLiteResponse CheckForAltMethod(ICouchbaseListenerContext context, CouchbaseLiteResponse response)
        {
            if (response.Status != RouteCollection.EndpointNotFoundStatus)
            {
                return(response);
            }

            var  request      = context.RequestUrl;
            bool hasAltMethod = _Delete.HasLogicForRequest(request) || _Get.HasLogicForRequest(request) ||
                                _Post.HasLogicForRequest(request) || _Put.HasLogicForRequest(request);

            if (hasAltMethod)
            {
                return(context.CreateResponse(StatusCode.MethodNotAllowed));
            }

            return(context.CreateResponse(StatusCode.NotFound));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="response">The response to write to</param>
 public DBMonitorCouchbaseResponseState(CouchbaseLiteResponse response) : this()
 {
     Response = response;
 }
        // Check for an incorrect request method on a request
        private static CouchbaseLiteResponse CheckForAltMethod(ICouchbaseListenerContext context, CouchbaseLiteResponse response)
        {
            if (response.Status != RouteCollection.EndpointNotFoundStatus) {
                return response;
            }
                
            var request = context.RequestUrl;
            bool hasAltMethod = _Delete.HasLogicForRequest(request) || _Get.HasLogicForRequest(request)
                                || _Post.HasLogicForRequest(request) || _Put.HasLogicForRequest(request);

            if (hasAltMethod) {
                return context.CreateResponse(StatusCode.MethodNotAllowed);
            }

            return context.CreateResponse(StatusCode.NotFound);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="response">The response to write to</param>
 public DBMonitorCouchbaseResponseState(CouchbaseLiteResponse response) : this()
 {
     Response = response;
 }
Beispiel #7
0
        public static void WriteChangesBodyJson(CouchbaseLiteResponse response, object context)
        {
            var c = (WriteChangesContext)context;

            response.WriteData(Encoding.UTF8.GetBytes("{\"results\":["), false);
            var  since = c.Since;
            bool first = true;
            var  comma = new[] { (byte)',' };

            if (!c.IncludeConflicts)
            {
                foreach (var change in c.Changes)
                {
                    if (!first)
                    {
                        response.WriteData(comma, false);
                    }

                    first = false;
                    var dictToWrite = ChangesDictForRev(change, c.ResponseState);
                    response.WriteData(Manager.GetObjectMapper().WriteValueAsBytes(dictToWrite), false);
                    since = change.Sequence;
                }
            }
            else
            {
                string lastDocId = null;
                IDictionary <string, object> lastEntry = null;
                var entries = new List <IDictionary <string, object> >();
                foreach (var rev in c.Changes)
                {
                    string docId = rev.DocID;
                    if (docId.Equals(lastDocId))
                    {
                        ((IList)lastEntry["changes"]).Add(new Dictionary <string, object> {
                            { "rev", rev.RevID }
                        });
                    }
                    else
                    {
                        if (lastEntry != null)
                        {
                            if (!first)
                            {
                                response.WriteData(comma, false);
                            }

                            first = false;
                            response.WriteData(Manager.GetObjectMapper().WriteValueAsBytes(lastEntry), false);
                        }

                        lastEntry = ChangesDictForRev(rev, c.ResponseState);
                        since     = rev.Sequence;
                        lastDocId = docId;
                    }
                }

                if (lastEntry != null)
                {
                    if (!first)
                    {
                        response.WriteData(comma, false);
                    }

                    first = false;
                    response.WriteData(Manager.GetObjectMapper().WriteValueAsBytes(lastEntry), false);
                }
            }

            var finalBytes = Encoding.UTF8.GetBytes($"],\"last_seq\":{since}}}");

            response.WriteData(finalBytes, false);
        }
Beispiel #8
0
        // Check for an incorrect request method on a request
        private static CouchbaseLiteResponse CheckForAltMethod(ICouchbaseListenerContext context, CouchbaseLiteResponse response)
        {
            if (response.Status != RouteCollection.EndpointNotFoundStatus)
            {
                return(response);
            }

            Log.To.Router.I(TAG, "{0} method not found for endpoint {1}, searching for alternate...",
                            context.Method, context.RequestUrl.PathAndQuery);
            var  request      = context.RequestUrl;
            bool hasAltMethod = _Delete.HasLogicForRequest(request) || _Get.HasLogicForRequest(request) ||
                                _Post.HasLogicForRequest(request) || _Put.HasLogicForRequest(request);

            if (hasAltMethod)
            {
                Log.To.Router.I(TAG, "Suitable method found; returning 406");
                return(context.CreateResponse(StatusCode.MethodNotAllowed));
            }

            Log.To.Router.I(TAG, "No suitable method found; returning 404", context.RequestUrl.PathAndQuery);
            return(context.CreateResponse(StatusCode.NotFound));
        }
        // Check for an incorrect request method on a request
        private static CouchbaseLiteResponse CheckForAltMethod(ICouchbaseListenerContext context, CouchbaseLiteResponse response)
        {
            if (response.Status != RouteCollection.EndpointNotFoundStatus) {
                return response;
            }
                
            Log.To.Router.I(TAG, "{0} method not found for endpoint {1}, searching for alternate...",
                context.Method, context.RequestUrl.PathAndQuery);
            var request = context.RequestUrl;
            bool hasAltMethod = _Delete.HasLogicForRequest(request) || _Get.HasLogicForRequest(request)
                                || _Post.HasLogicForRequest(request) || _Put.HasLogicForRequest(request);

            if (hasAltMethod) {
                Log.To.Router.I(TAG, "Suitable method found; returning 406");
                return context.CreateResponse(StatusCode.MethodNotAllowed);
            }

            Log.To.Router.I(TAG, "No suitable method found; returning 404", context.RequestUrl.PathAndQuery);
            return context.CreateResponse(StatusCode.NotFound);
        }