internal void SendHeaders(MemoryStream stream, bool closing)
        {
            if (_contentType != null)
            {
                string value = ((_contentType.IndexOf("charset=", StringComparison.Ordinal) != -1 || _contentEncoding == null) ? _contentType : $"{_contentType}; charset={_contentEncoding.WebName}");
                _headers.InternalSet("Content-Type", value, response: true);
            }
            if (_headers["Server"] == null)
            {
                _headers.InternalSet("Server", "websocket-sharp/1.0", response: true);
            }
            CultureInfo invariantCulture = CultureInfo.InvariantCulture;

            if (_headers["Date"] == null)
            {
                _headers.InternalSet("Date", DateTime.UtcNow.ToString("r", invariantCulture), response: true);
            }
            if (!_chunked)
            {
                if (!_contentLengthWasSet && closing)
                {
                    _contentLengthWasSet = true;
                    _contentLength       = 0L;
                }
                if (_contentLengthWasSet)
                {
                    _headers.InternalSet("Content-Length", _contentLength.ToString(invariantCulture), response: true);
                }
            }
            Version protocolVersion = _context.Request.ProtocolVersion;

            if (!_contentLengthWasSet && !_chunked && protocolVersion > HttpVersion.Version10)
            {
                _chunked = true;
            }
            bool flag = _statusCode == 400 || _statusCode == 408 || _statusCode == 411 || _statusCode == 413 || _statusCode == 414 || _statusCode == 500 || _statusCode == 503;

            if (!flag)
            {
                flag = !_context.Request.KeepAlive;
            }
            if (!_keepAlive || flag)
            {
                _headers.InternalSet("Connection", "close", response: true);
                flag = true;
            }
            if (_chunked)
            {
                _headers.InternalSet("Transfer-Encoding", "chunked", response: true);
            }
            int reuses = _context.Connection.Reuses;

            if (reuses >= 100)
            {
                _forceCloseChunked = true;
                if (!flag)
                {
                    _headers.InternalSet("Connection", "close", response: true);
                    flag = true;
                }
            }
            if (!flag)
            {
                _headers.InternalSet("Keep-Alive", $"timeout=15,max={100 - reuses}", response: true);
                if (protocolVersion < HttpVersion.Version11)
                {
                    _headers.InternalSet("Connection", "keep-alive", response: true);
                }
            }
            if (_location != null)
            {
                _headers.InternalSet("Location", _location, response: true);
            }
            if (_cookies != null)
            {
                IEnumerator enumerator = _cookies.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        Cookie cookie = (Cookie)enumerator.Current;
                        _headers.InternalSet("Set-Cookie", cookie.ToResponseString(), response: true);
                    }
                }
                finally
                {
                    IDisposable disposable;
                    if ((disposable = enumerator as IDisposable) != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            Encoding     encoding     = _contentEncoding ?? Encoding.Default;
            StreamWriter streamWriter = new StreamWriter(stream, encoding, 256);

            streamWriter.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
            streamWriter.Write(_headers.ToStringMultiValue(response: true));
            streamWriter.Flush();
            stream.Position = ((encoding.CodePage != 65001) ? encoding.GetPreamble().Length : 3);
            if (_outputStream == null)
            {
                _outputStream = _context.Connection.GetResponseStream();
            }
            _headersWereSent = true;
        }
Exemplo n.º 2
0
        internal void SendHeaders(bool closing, MemoryStream stream)
        {
            if (this._contentType != null)
            {
                if (this._contentEncoding != null && this._contentType.IndexOf("charset=", StringComparison.Ordinal) == -1)
                {
                    string webName = this._contentEncoding.WebName;
                    this._headers.SetInternal("Content-Type", this._contentType + "; charset=" + webName, true);
                }
                else
                {
                    this._headers.SetInternal("Content-Type", this._contentType, true);
                }
            }
            if (this._headers["Server"] == null)
            {
                this._headers.SetInternal("Server", "websocket-sharp/1.0", true);
            }
            CultureInfo invariantCulture = CultureInfo.InvariantCulture;

            if (this._headers["Date"] == null)
            {
                this._headers.SetInternal("Date", DateTime.UtcNow.ToString("r", invariantCulture), true);
            }
            if (!this._chunked)
            {
                if (!this._contentLengthSet && closing)
                {
                    this._contentLengthSet = true;
                    this._contentLength    = 0L;
                }
                if (this._contentLengthSet)
                {
                    this._headers.SetInternal("Content-Length", this._contentLength.ToString(invariantCulture), true);
                }
            }
            Version protocolVersion = this._context.Request.ProtocolVersion;

            if (!this._contentLengthSet && !this._chunked && protocolVersion >= HttpVersion.Version11)
            {
                this._chunked = true;
            }
            bool flag = this._statusCode == 400 || this._statusCode == 408 || this._statusCode == 411 || this._statusCode == 413 || this._statusCode == 414 || this._statusCode == 500 || this._statusCode == 503;

            if (!flag)
            {
                flag = !this._context.Request.KeepAlive;
            }
            if (!this._keepAlive || flag)
            {
                this._headers.SetInternal("Connection", "close", true);
                flag = true;
            }
            if (this._chunked)
            {
                this._headers.SetInternal("Transfer-Encoding", "chunked", true);
            }
            int reuses = this._context.Connection.Reuses;

            if (reuses >= 100)
            {
                this._forceCloseChunked = true;
                if (!flag)
                {
                    this._headers.SetInternal("Connection", "close", true);
                    flag = true;
                }
            }
            if (!flag)
            {
                this._headers.SetInternal("Keep-Alive", string.Format("timeout=15,max={0}", 100 - reuses), true);
                if (this._context.Request.ProtocolVersion <= HttpVersion.Version10)
                {
                    this._headers.SetInternal("Connection", "keep-alive", true);
                }
            }
            if (this._location != null)
            {
                this._headers.SetInternal("Location", this._location, true);
            }
            if (this._cookies != null)
            {
                IEnumerator enumerator = this._cookies.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        object obj    = enumerator.Current;
                        Cookie cookie = (Cookie)obj;
                        this._headers.SetInternal("Set-Cookie", cookie.ToResponseString(), true);
                    }
                }
                finally
                {
                    IDisposable disposable;
                    if ((disposable = (enumerator as IDisposable)) != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            Encoding     encoding     = this._contentEncoding ?? Encoding.Default;
            StreamWriter streamWriter = new StreamWriter(stream, encoding, 256);

            streamWriter.Write("HTTP/{0} {1} {2}\r\n", this._version, this._statusCode, this._statusDescription);
            string value = this._headers.ToStringMultiValue(true);

            streamWriter.Write(value);
            streamWriter.Flush();
            int num = (encoding.CodePage != 65001) ? encoding.GetPreamble().Length : 3;

            if (this._outputStream == null)
            {
                this._outputStream = this._context.Connection.GetResponseStream();
            }
            stream.Position  = (long)num;
            this.HeadersSent = true;
        }