Ejemplo n.º 1
0
        private async Task HandleRequest(StreamSocket client)
        {
            HttpContext context = null;
            try
            {
                HttpRequest request = await ReadRequest(client);
                if (request == null)
                {
                    return;
                }

                context = new HttpContext(request, new HttpResponse());

                EventHandler<RequestReceivedEventArgs> handler = RequestReceived;
                if (handler == null)
                {
                    context.Response.StatusCode = HttpStatusCode.NotImplemented;
                }
                else
                {
                    var eventArgs = new RequestReceivedEventArgs(context);
                    handler.Invoke(this, eventArgs);

                    if (!eventArgs.IsHandled)
                    {
                        context.Response.StatusCode = HttpStatusCode.BadRequest;
                    }
                }

                ////if (GetConnectionMustBeClosed(context))
                ////{
                ////    return;
                ////}
            }
            catch (Exception exception)
            {
                if (context != null)
                {
                    context.Response.StatusCode = HttpStatusCode.InternalServerError;
                    context.Response.Body = new JsonBody(ExceptionToJson(exception));
                }
            }
            finally
            {
                if (context != null)
                {
                    await TrySendResponse(client, context);
                }
            }
        }
        private void ExecuteActions(object sender, RequestReceivedEventArgs e)
        {
            string requestUri = e.Context.Request.Uri.Trim('/');

            if (!requestUri.StartsWith(_baseUri, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            string relativeUri = requestUri.Substring(_baseUri.Length).Trim('/');
            foreach (var handler in _handlers)
            {
                if (!handler.Method.Equals(e.Context.Request.Method))
                {
                    continue;
                }

                if (e.Context.Request.PlainBody.Length == 0 && handler.IsJsonBodyRequired)
                {
                    continue;
                }

                if (handler.HandleRequestsWithDifferentSubUrl)
                {
                    if (!relativeUri.StartsWith(handler.Uri, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                }
                else
                {
                    if (!handler.Uri.Equals(relativeUri, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                }

                InvokeHandlerAction(handler, e.Context);
                e.IsHandled = true;
                return;
            }
        }