Ejemplo n.º 1
0
        public bool HandleRequest(HttpConnection connection, HttpRequest request)
        {
            var ctx = connection.Context;

            if (ctx == null)
            {
                throw new InvalidOperationException();
            }

            try {
                Debug(ctx, 1, "HANDLE REQUEST");
                DumpHeaders(ctx, request);
                connection.CheckEncryption(ctx);
                var response = HandleRequest(ctx, connection, request, Flags);
                if (response == null)
                {
                    response = HttpResponse.CreateSuccess();
                }
                if (!response.KeepAlive.HasValue && ((Flags & RequestFlags.KeepAlive) != 0))
                {
                    response.KeepAlive = true;
                }
                request.ReadBody();
                connection.WriteResponse(response);
                Debug(ctx, 1, "HANDLE REQUEST DONE", response);
                DumpHeaders(ctx, response);
                tcs.SetResult(response.IsSuccess);
                return(response.KeepAlive ?? false);
            } catch (AssertionException ex) {
                Debug(ctx, 1, "HANDLE REQUEST - ASSERTION FAILED", ex);
                var response = HttpResponse.CreateError(ex.Message);
                connection.WriteResponse(response);
                tcs.SetException(ex);
                return(false);
            } catch (Exception ex) {
                if (ctx.IsCanceled)
                {
                    Debug(ctx, 1, "HANDLE REQUEST - CANCELED", ex);
                    tcs.SetCanceled();
                    return(false);
                }
                Debug(ctx, 1, "HANDLE REQUEST EX", ex);
                var response = HttpResponse.CreateError("Caught unhandled exception", ex);
                connection.WriteResponse(response);
                tcs.SetException(ex);
                return(false);
            }
        }