A version of StreamWriter that doesn't close the underlying Stream when Disposed. Writes to the underlying Stream with a BOM-less UTF-8 encoding.
상속: System.IO.StreamWriter
예제 #1
0
        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();
            };
        }
예제 #2
0
        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();
            };
        }
예제 #3
0
        /// <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;
            }
        }
예제 #4
0
        /// <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);
            }
        }
    }