private void InvokeNancy(NancyRequestParameters parameters, ResponseCallBack responseCallBack, Action<Exception> errorCallback)
        {
            try
            {
                parameters.Body.Seek(0, SeekOrigin.Begin);

                var request = new Request(parameters.Method, parameters.Url, parameters.Body, parameters.Headers);

                // Execute the nancy async request handler
                HandleRequest(
                    request,
                    result =>
                    {
                        if (result.Response.Cookies.Count > 0)
                        {
                            result.Response.Headers["Set-Cookie"] = result.Response.GetAllCookies();
                        }
                        result.Response.Headers["Content-Type"] = result.Response.ContentType;
                        responseCallBack.Invoke(GetReturnCode(result), result.Response.Headers, GetResponseBodyBuilder(result));
                    },
                    errorCallback);
            }
            catch (Exception e)
            {
                errorCallback.Invoke(e);
            }
        }
Esempio n. 2
0
        private void InvokeNancy(NancyRequestParameters parameters, ResponseCallBack responseCallBack, Action<Exception> errorCallback)
        {
            try
            {
                parameters.Body.Seek(0, SeekOrigin.Begin);

                var request = new Request(parameters.Method, parameters.Url, parameters.Body, parameters.Headers);

                // Execute the nancy async request handler
                this.engine.HandleRequest(
                    request,
                    (result) =>
                    {
                        var returnCode = GetReturnCode(result);
                        var headers = result.Response.Headers;
                        foreach (var cookie in result.Response.Cookies)
                        {
                            headers.Add("Set-Cookie", cookie.ToString());
                        }
                        headers.Add("Content-Type", result.Response.ContentType);
                        responseCallBack.Invoke(returnCode, headers, GetResponseBodyBuilder(result));
                    },
                    errorCallback);
            }
            catch (Exception e)
            {
                errorCallback.Invoke(e);
            }
        }
        private static Func<ArraySegment<byte>, Action, bool> GetRequestBodyBuilder(NancyRequestParameters parameters, Action<Exception> errorCallback)
        {
            return (data, continuation) =>
            {
                if (continuation == null)
                {
                    // If continuation is null then we must use sync and return false
                    parameters.Body.Write(data.Array, data.Offset, data.Count);
                    return false;
                }

                // Otherwise use begin/end (which may be blocking anyway)
                // and return true.
                // No need to do any locking because the spec states we can't be called again
                // until we call the continuation.
                var asyncState = new AsyncBuilderState(parameters.Body, continuation, errorCallback);
                parameters.Body.BeginWrite(
                    data.Array,
                    data.Offset,
                    data.Count,
                    ar =>
                    {
                        var state = (AsyncBuilderState)ar.AsyncState;

                        try
                        {
                            state.Stream.EndWrite(ar);

                            state.OnComplete.Invoke();
                        }
                        catch (Exception e)
                        {
                            state.OnError.Invoke(e);
                        }
                    },
                    asyncState);

                return true;
            };
        }
Esempio n. 4
0
        private void InvokeNancy(NancyRequestParameters parameters, ResponseCallBack responseCallBack, Action<Exception> errorCallback)
        {
            try
            {
                parameters.Body.Seek(0, SeekOrigin.Begin);

                var request = new Request(parameters.Method, parameters.Url, parameters.Body, parameters.Headers);

                // Execute the nancy async request handler
                this.engine.HandleRequest(
                    request,
                    (result) =>
                    {
                        var returnCode = GetReturnCode(result);
                        var headers = result.Response.Headers;

                        responseCallBack.Invoke(returnCode, headers, GetResponseBodyBuilder(result));
                    },
                    errorCallback);
            }
            catch (Exception e)
            {
                errorCallback.Invoke(e);
            }
        }