private static BodyDelegate GetResponseBodyBuilder(NancyContext result) { return((next, error, complete) => { // Wrap the completion delegate so the context is disposed on completion. // Technically we could just do this after the .Invoke below, but doing it // here gives scope for supporting async response body generation in the future. Action onComplete = () => { complete.Invoke(); result.Dispose(); }; using (var stream = new ResponseStream(next, onComplete)) { try { result.Response.Contents.Invoke(stream); } catch (Exception e) { error.Invoke(e); result.Dispose(); } } // Don't currently support cancelling, but if it gets called then dispose the context return result.Dispose; }); }
private void ConvertNancyResponseToResponse(NancyContext nancyRequest, Response nancyResponse, HttpListenerResponse response) { foreach (KeyValuePair <string, string> keyValuePair in nancyResponse.Headers) { response.AddHeader(keyValuePair.Key, keyValuePair.Value); } using (IEnumerator <INancyCookie> enumerator = nancyResponse.Cookies.GetEnumerator()) { while (enumerator.MoveNext()) { INancyCookie current = enumerator.Current; response.Headers.Add(HttpResponseHeader.SetCookie, current.ToString()); } } if (nancyResponse.ReasonPhrase != null) { response.StatusDescription = nancyResponse.ReasonPhrase; } if (nancyResponse.ContentType != null) { response.ContentType = nancyResponse.ContentType; } response.StatusCode = (int)nancyResponse.StatusCode; if (_configuration.AllowChunkedEncoding && !nancyResponse.Headers.ContainsKey("Content-Length")) { OutputWithDefaultTransferEncoding(nancyResponse, response); } else { OutputWithContentLength(nancyResponse, response); } nancyRequest.Dispose(); }
/// <summary> /// Gets a delegate to handle converting a nancy response /// to the format required by OWIN and signals that the we are /// now complete. /// </summary> /// <param name="context">The Nancy Context.</param> /// <param name="environment">OWIN environment.</param> /// <param name="next">The next stage in the OWIN pipeline.</param> /// <param name="performPassThrough">A predicate that will allow the caller to determine if the request passes through to the /// next stage in the owin pipeline.</param> /// <returns>Delegate</returns> private static Task RequestComplete( NancyContext context, IDictionary <string, object> environment, Func <NancyContext, bool> performPassThrough, AppFunc next) { var owinResponseHeaders = Get <IDictionary <string, string[]> >(environment, "owin.ResponseHeaders"); var owinResponseBody = Get <Stream>(environment, "owin.ResponseBody"); var nancyResponse = context.Response; if (!performPassThrough(context)) { environment["owin.ResponseStatusCode"] = (int)nancyResponse.StatusCode; if (nancyResponse.ReasonPhrase != null) { environment["owin.ResponseReasonPhrase"] = nancyResponse.ReasonPhrase; } foreach (var responseHeader in nancyResponse.Headers) { owinResponseHeaders[responseHeader.Key] = new[] { responseHeader.Value }; } if (!string.IsNullOrWhiteSpace(nancyResponse.ContentType)) { owinResponseHeaders["Content-Type"] = new[] { nancyResponse.ContentType }; } if (nancyResponse.Cookies != null && nancyResponse.Cookies.Count != 0) { const string setCookieHeaderKey = "Set-Cookie"; string[] cookieHeader; string[] setCookieHeader = owinResponseHeaders.TryGetValue(setCookieHeaderKey, out cookieHeader) ? cookieHeader : ArrayCache.Empty <string>(); owinResponseHeaders[setCookieHeaderKey] = setCookieHeader .Concat(nancyResponse.Cookies.Select(cookie => cookie.ToString())) .ToArray(); } nancyResponse.Contents(owinResponseBody); } else { return(next(environment)); } context.Dispose(); return(TaskHelpers.CompletedTask); }
private static Task RequestComplete( NancyContext context, HttpContext environment, Func <NancyContext, bool> performPassThrough, RequestDelegate next) { var aspnetCoreResponseHeaders = environment.Response.Headers; var aspnetCoreResponseBody = environment.Response.Body; var nancyResponse = context.Response; if (!performPassThrough(context)) { environment.Response.StatusCode = (int)nancyResponse.StatusCode; if (nancyResponse.ReasonPhrase != null) { environment.Response.Headers.Add("ReasonPhrase", nancyResponse.ReasonPhrase); } foreach (var responseHeader in nancyResponse.Headers) { aspnetCoreResponseHeaders[responseHeader.Key] = new[] { responseHeader.Value }; } if (!string.IsNullOrWhiteSpace(nancyResponse.ContentType)) { aspnetCoreResponseHeaders["Content-Type"] = new[] { nancyResponse.ContentType }; } if (nancyResponse.Cookies != null && nancyResponse.Cookies.Count != 0) { const string setCookieHeaderKey = "Set-Cookie"; string[] setCookieHeader = aspnetCoreResponseHeaders.ContainsKey(setCookieHeaderKey) ? aspnetCoreResponseHeaders[setCookieHeaderKey].ToArray() : ArrayCache.Empty <string>(); aspnetCoreResponseHeaders[setCookieHeaderKey] = setCookieHeader .Concat(nancyResponse.Cookies.Select(cookie => cookie.ToString())) .ToArray(); } nancyResponse.Contents(aspnetCoreResponseBody); } else { return(next(environment)); } context.Dispose(); return(TaskHelpers.CompletedTask); }
private static BodyDelegate GetResponseBodyBuilder(NancyContext result) { return (next, error, complete) => { // Wrap the completion delegate so the context is disposed on completion. // Technically we could just do this after the .Invoke below, but doing it // here gives scope for supporting async response body generation in the future. Action onComplete = () => { complete.Invoke(); result.Dispose(); }; using (var stream = new ResponseStream(next, onComplete)) { try { result.Response.Contents.Invoke(stream); } catch (Exception e) { error.Invoke(e); result.Dispose(); } } // Don't currently support cancelling, but if it gets called then dispose the context return result.Dispose; }; }