Exemple #1
0
        public override Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            try
            {
                appHost.AssertFeatures(format);

                if (appHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(TypeConstants.EmptyTask);
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString[Keywords.Callback];
                var doJsonp  = HostContext.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = httpReq.Dto = CreateRequest(httpReq, operationName);

                if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(TypeConstants.EmptyTask);
                }

                var rawResponse = GetResponse(httpReq, request);

                if (httpRes.IsClosed)
                {
                    return(TypeConstants.EmptyTask);
                }

                return(HandleResponse(rawResponse, response =>
                {
                    response = appHost.ApplyResponseConverters(httpReq, response);

                    if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                    {
                        return TypeConstants.EmptyTask;
                    }

                    if (doJsonp && !(response is CompressedResult))
                    {
                        return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                    }

                    return httpRes.WriteToResponse(httpReq, response);
                },
                                      ex => !HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq))));
            }
            catch (Exception ex)
            {
                return(!HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq)));
            }
        }
        public override Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            try
            {
                appHost.AssertFeatures(format);

                if (appHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(TypeConstants.EmptyTask);
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString[Keywords.Callback];
                var doJsonp  = HostContext.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = httpReq.Dto = CreateRequest(httpReq, operationName);

                if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(TypeConstants.EmptyTask);
                }

                return(appHost.ApplyRequestFiltersAsync(httpReq, httpRes, request)
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        return t;
                    }

                    if (t.IsCanceled)
                    {
                        httpRes.StatusCode = (int)HttpStatusCode.PartialContent;
                        httpRes.EndRequest();
                    }

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    var rawResponse = GetResponse(httpReq, request);

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    return HandleResponse(rawResponse, response =>
                    {
                        UpdateResponseContentType(httpReq, response);
                        response = appHost.ApplyResponseConverters(httpReq, response);

                        if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                        {
                            return TypeConstants.EmptyTask;
                        }

                        if (doJsonp && !(response is CompressedResult))
                        {
                            return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                        }

                        return httpRes.WriteToResponse(httpReq, response);
                    });
                })
                       .Unwrap()
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        var taskEx = t.Exception.UnwrapIfSingleException();
                        return !HostContext.Config.WriteErrorsToResponse
                                ? taskEx.ApplyResponseConverters(httpReq).AsTaskException()
                                : HandleException(httpReq, httpRes, operationName, taskEx.ApplyResponseConverters(httpReq));
                    }
                    return t;
                })
                       .Unwrap());
            }
            catch (Exception ex)
            {
                return(!HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq)));
            }
        }