Beispiel #1
0
        // Generate:
        //   Content-Length
        //   Content-Type
        //   Transfer-Encoding (chunked)
        //   Cache-Control
        //   X-AspNet-Version
        void AddHeadersNoCache(NameValueCollection write_headers, bool final_flush)
        {
            //
            // Transfer-Encoding
            //
            if (use_chunked)
            {
                write_headers.Add("Transfer-Encoding", "chunked");
            }
            else if (transfer_encoding != null)
            {
                write_headers.Add("Transfer-Encoding", transfer_encoding);
            }
            if (redirect_location != null)
            {
                write_headers.Add("Location", redirect_location);
            }

            string vh = VersionHeader;

            if (vh != null)
            {
                write_headers.Add("X-AspNet-Version", vh);
            }

            //
            // If Content-Length is set.
            //
            if (content_length >= 0)
            {
                write_headers.Add(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderContentLength),
                                  content_length.ToString(Helpers.InvariantCulture));
            }
            else if (BufferOutput)
            {
                if (final_flush)
                {
                    //
                    // If we are buffering and this is the last flush, not a middle-flush,
                    // we know the content-length.
                    //
                    content_length = output_stream.total;
                    write_headers.Add(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderContentLength),
                                      content_length.ToString(Helpers.InvariantCulture));
                }
                else
                {
                    //
                    // We are buffering, and this is a flush in the middle.
                    // If we are not chunked, we need to set "Connection: close".
                    //
                    if (use_chunked)
                    {
                        write_headers.Add(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderConnection), "close");
                    }
                }
            }
            else
            {
                //
                // If the content-length is not set, and we are not buffering, we must
                // close at the end.
                //
                if (use_chunked)
                {
                    write_headers.Add(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderConnection), "close");
                }
            }

            //
            // Cache Control, the cache policy takes precedence over the cache_control property.
            //
            if (cache_policy != null)
            {
                cache_policy.SetHeaders(this, headers);
            }
            else
            {
                write_headers.Add("Cache-Control", CacheControl);
            }

            //
            // Content-Type
            //
            if (content_type != null)
            {
                string header = content_type;

                if (charset_set || header == "text/plain" || header == "text/html")
                {
                    if (header.IndexOf("charset=") == -1 && !string.IsNullOrEmpty(charset))
                    {
                        header += "; charset=" + charset;
                    }
                }

                write_headers.Add("Content-Type", header);
            }

            if (cookies != null && cookies.Count != 0)
            {
                int n = cookies.Count;
                for (int i = 0; i < n; i++)
                {
                    write_headers.Add("Set-Cookie", cookies.Get(i).GetCookieHeaderValue());
                }
            }
        }