internal static void ContentsFromString(this Response resp, string text) { resp.Contents = stream => { using (var writer = new NonClosingStreamWriter(stream)) { writer.Write(text); writer.Flush(); } stream.Flush(); Thread.MemoryBarrier(); }; }
/// <summary> /// Process the response /// </summary> /// <param name="requestedMediaRange">Content type requested by the client</param> /// <param name="model">The model for the given media range</param> /// <param name="context">The nancy context</param> /// <returns>A response</returns> public virtual Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var pomonaResponse = (PomonaResponse)model; if (pomonaResponse.Entity == PomonaResponse.NoBodyEntity) return new Response { StatusCode = pomonaResponse.StatusCode }; var serializer = GetSerializer(context); var serializeOptions = new SerializeOptions { ExpandedPaths = pomonaResponse.ExpandedPaths, ExpectedBaseType = pomonaResponse.ResultType }; if (IsTextHtmlContentType(requestedMediaRange)) { // Wrap in html var response = new Response(); var htmlLinks = GetHtmlLinks(context); var jsonString = serializer.SerializeToString(pomonaResponse.Entity, serializeOptions); HtmlJsonPrettifier.CreatePrettifiedHtmlJsonResponse(response, htmlLinks, jsonString, "http://failfailtodo"); return response; } else { var response = new Response { //Headers = {{"Content-Length", bytes.Length.ToString()}}, Contents = stream => { using (var streamWriter = new NonClosingStreamWriter(stream)) { serializer.Serialize(streamWriter, pomonaResponse.Entity, serializeOptions); } }, ContentType = ContentType, StatusCode = pomonaResponse.StatusCode }; if (pomonaResponse.ResponseHeaders != null) { foreach (var kvp in pomonaResponse.ResponseHeaders) response.Headers.Add(kvp); } // Add etag header var resourceType = pomonaResponse.ResultType as ResourceType; if (resourceType == null) return response; var etagProperty = resourceType.ETagProperty; if (pomonaResponse.Entity == null || etagProperty == null) return response; var etagValue = (string)etagProperty.GetValue(pomonaResponse.Entity); if (etagValue != null) { // I think defining this as a weak etag will be correct, since we can specify $expand which change data (byte-by-byte). response.Headers["ETag"] = $"W/\"{etagValue}\""; } return response; } }
/// <summary> /// Process the response /// </summary> /// <param name="requestedMediaRange">Content type requested by the client</param> /// <param name="model">The model for the given media range</param> /// <param name="context">The nancy context</param> /// <returns>A response</returns> public virtual Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var pomonaResponse = (PomonaResponse)model; if (pomonaResponse.Entity == PomonaResponse.NoBodyEntity) { return new Response { StatusCode = pomonaResponse.StatusCode } } ; var serializer = GetSerializer(context); var serializeOptions = new SerializeOptions { ExpandedPaths = pomonaResponse.ExpandedPaths, ExpectedBaseType = pomonaResponse.ResultType }; if (IsTextHtmlContentType(requestedMediaRange)) { // Wrap in html var response = new Response(); var htmlLinks = GetHtmlLinks(context); var jsonString = serializer.SerializeToString(pomonaResponse.Entity, serializeOptions); HtmlJsonPrettifier.CreatePrettifiedHtmlJsonResponse(response, htmlLinks, jsonString, "http://failfailtodo"); return(response); } else { var response = new Response { //Headers = {{"Content-Length", bytes.Length.ToString()}}, Contents = stream => { using (var streamWriter = new NonClosingStreamWriter(stream)) { serializer.Serialize(streamWriter, pomonaResponse.Entity, serializeOptions); } }, ContentType = ContentType, StatusCode = pomonaResponse.StatusCode }; if (pomonaResponse.ResponseHeaders != null) { foreach (var kvp in pomonaResponse.ResponseHeaders) { response.Headers.Add(kvp); } } // Add etag header var resourceType = pomonaResponse.ResultType as ResourceType; if (resourceType == null) { return(response); } var etagProperty = resourceType.ETagProperty; if (pomonaResponse.Entity == null || etagProperty == null) { return(response); } var etagValue = (string)etagProperty.GetValue(pomonaResponse.Entity); if (etagValue != null) { // I think defining this as a weak etag will be correct, since we can specify $expand which change data (byte-by-byte). response.Headers["ETag"] = $"W/\"{etagValue}\""; } return(response); } } }