ToStringMultiValue() private method

private ToStringMultiValue ( bool response ) : string
response bool
return string
Ejemplo n.º 1
0
        internal WebHeaderCollection WriteHeadersTo(MemoryStream destination)
        {
            var headers = new WebHeaderCollection(HttpHeaderType.Response, true);

            if (_headers != null)
            {
                headers.Add(_headers);
            }

            if (_contentType != null)
            {
                var type = _contentType.IndexOf("charset=", StringComparison.Ordinal) == -1 &&
                           _contentEncoding != null
                   ? String.Format("{0}; charset={1}", _contentType, _contentEncoding.WebName)
                   : _contentType;

                headers.InternalSet("Content-Type", type, true);
            }

            if (headers["Server"] == null)
            {
                headers.InternalSet("Server", "websocket-sharp/1.0", true);
            }

            var prov = CultureInfo.InvariantCulture;

            if (headers["Date"] == null)
            {
                headers.InternalSet("Date", DateTime.UtcNow.ToString("r", prov), true);
            }

            if (!_sendChunked)
            {
                headers.InternalSet("Content-Length", _contentLength.ToString(prov), true);
            }
            else
            {
                headers.InternalSet("Transfer-Encoding", "chunked", true);
            }

            /*
             * Apache forces closing the connection for these status codes:
             * - 400 Bad Request
             * - 408 Request Timeout
             * - 411 Length Required
             * - 413 Request Entity Too Large
             * - 414 Request-Uri Too Long
             * - 500 Internal Server Error
             * - 503 Service Unavailable
             */
            var closeConn = !_context.Request.KeepAlive ||
                            !_keepAlive ||
                            _statusCode == 400 ||
                            _statusCode == 408 ||
                            _statusCode == 411 ||
                            _statusCode == 413 ||
                            _statusCode == 414 ||
                            _statusCode == 500 ||
                            _statusCode == 503;

            var reuses = _context.Connection.Reuses;

            if (closeConn || reuses >= 100)
            {
                headers.InternalSet("Connection", "close", true);
            }
            else
            {
                headers.InternalSet(
                    "Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses), true);

                if (_context.Request.ProtocolVersion < HttpVersion.Version11)
                {
                    headers.InternalSet("Connection", "keep-alive", true);
                }
            }

            if (_location != null)
            {
                headers.InternalSet("Location", _location, true);
            }

            if (_cookies != null)
            {
                foreach (Cookie cookie in _cookies)
                {
                    headers.InternalSet("Set-Cookie", cookie.ToResponseString(), true);
                }
            }

            var enc    = _contentEncoding ?? Encoding.Default;
            var writer = new StreamWriter(destination, enc, 256);

            writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
            writer.Write(headers.ToStringMultiValue(true));
            writer.Flush();

            // Assumes that the destination was at position 0.
            destination.Position = enc.GetPreamble().Length;

            return(headers);
        }
Ejemplo n.º 2
0
        internal void WriteHeadersTo(MemoryStream destination, bool closing)
        {
            if (_contentType != null)
            {
                var type = _contentType.IndexOf("charset=", StringComparison.Ordinal) == -1 &&
                           _contentEncoding != null
                   ? String.Format("{0}; charset={1}", _contentType, _contentEncoding.WebName)
                   : _contentType;

                _headers.InternalSet("Content-Type", type, true);
            }

            if (_headers["Server"] == null)
            {
                _headers.InternalSet("Server", "websocket-sharp/1.0", true);
            }

            var prov = CultureInfo.InvariantCulture;

            if (_headers["Date"] == null)
            {
                _headers.InternalSet("Date", DateTime.UtcNow.ToString("r", prov), true);
            }

            if (!_chunked)
            {
                if (!_contentLengthSet && closing)
                {
                    _contentLengthSet = true;
                    _contentLength    = 0;
                }

                if (_contentLengthSet)
                {
                    _headers.InternalSet("Content-Length", _contentLength.ToString(prov), true);
                }
                else if (_context.Request.ProtocolVersion > HttpVersion.Version10)
                {
                    _chunked = true;
                }
            }

            if (_chunked)
            {
                _headers.InternalSet("Transfer-Encoding", "chunked", true);
            }

            /*
             * Apache forces closing the connection for these status codes:
             * - HttpStatusCode.BadRequest            400
             * - HttpStatusCode.RequestTimeout        408
             * - HttpStatusCode.LengthRequired        411
             * - HttpStatusCode.RequestEntityTooLarge 413
             * - HttpStatusCode.RequestUriTooLong     414
             * - HttpStatusCode.InternalServerError   500
             * - HttpStatusCode.ServiceUnavailable    503
             */
            var closeConn = _statusCode == 400 ||
                            _statusCode == 408 ||
                            _statusCode == 411 ||
                            _statusCode == 413 ||
                            _statusCode == 414 ||
                            _statusCode == 500 ||
                            _statusCode == 503 ||
                            !_context.Request.KeepAlive ||
                            !_keepAlive;

            var reuses = _context.Connection.Reuses;

            if (closeConn || reuses >= 100)
            {
                _headers.InternalSet("Connection", "close", true);
            }
            else
            {
                _headers.InternalSet(
                    "Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses), true);

                if (_context.Request.ProtocolVersion < HttpVersion.Version11)
                {
                    _headers.InternalSet("Connection", "keep-alive", true);
                }
            }

            if (_location != null)
            {
                _headers.InternalSet("Location", _location, true);
            }

            if (_cookies != null)
            {
                foreach (Cookie cookie in _cookies)
                {
                    _headers.InternalSet("Set-Cookie", cookie.ToResponseString(), true);
                }
            }

            var enc    = _contentEncoding ?? Encoding.Default;
            var writer = new StreamWriter(destination, enc, 256);

            writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
            writer.Write(_headers.ToStringMultiValue(true));
            writer.Flush();

            // Assumes that the destination was at position 0.
            destination.Position = enc.CodePage == 65001 ? 3 : enc.GetPreamble().Length;
        }
Ejemplo n.º 3
0
        internal void SendHeaders(bool closing, MemoryStream ms)
        {
            Encoding encoding = content_encoding;

            if (encoding == null)
            {
                encoding = Encoding.Default;
            }

            if (content_type != null)
            {
                if (content_encoding != null && content_type.IndexOf("charset=", StringComparison.Ordinal) == -1)
                {
                    string enc_name = content_encoding.WebName;
                    headers.SetInternal("Content-Type", content_type + "; charset=" + enc_name);
                }
                else
                {
                    headers.SetInternal("Content-Type", content_type);
                }
            }

            if (headers ["Server"] == null)
            {
                headers.SetInternal("Server", "Mono-HTTPAPI/1.0");
            }

            CultureInfo inv = CultureInfo.InvariantCulture;

            if (headers ["Date"] == null)
            {
                headers.SetInternal("Date", DateTime.UtcNow.ToString("r", inv));
            }

            if (!chunked)
            {
                if (!cl_set && closing)
                {
                    cl_set         = true;
                    content_length = 0;
                }

                if (cl_set)
                {
                    headers.SetInternal("Content-Length", content_length.ToString(inv));
                }
            }

            Version v = context.Request.ProtocolVersion;

            if (!cl_set && !chunked && v >= HttpVersion.Version11)
            {
                chunked = true;
            }

            /* Apache forces closing the connection for these status codes:
             *	HttpStatusCode.BadRequest               400
             *	HttpStatusCode.RequestTimeout           408
             *	HttpStatusCode.LengthRequired           411
             *	HttpStatusCode.RequestEntityTooLarge    413
             *	HttpStatusCode.RequestUriTooLong        414
             *	HttpStatusCode.InternalServerError      500
             *	HttpStatusCode.ServiceUnavailable       503
             */
            bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                               status_code == 413 || status_code == 414 || status_code == 500 ||
                               status_code == 503);

            if (conn_close == false)
            {
                conn_close = !context.Request.KeepAlive;
            }

            // They sent both KeepAlive: true and Connection: close!?
            if (!keep_alive || conn_close)
            {
                headers.SetInternal("Connection", "close");
                conn_close = true;
            }

            if (chunked)
            {
                headers.SetInternal("Transfer-Encoding", "chunked");
            }

            int reuses = context.Connection.Reuses;

            if (reuses >= 100)
            {
                force_close_chunked = true;
                if (!conn_close)
                {
                    headers.SetInternal("Connection", "close");
                    conn_close = true;
                }
            }

            if (!conn_close)
            {
                headers.SetInternal("Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses));
                if (context.Request.ProtocolVersion <= HttpVersion.Version10)
                {
                    headers.SetInternal("Connection", "keep-alive");
                }
            }

            if (location != null)
            {
                headers.SetInternal("Location", location);
            }

            if (cookies != null)
            {
                foreach (Cookie cookie in cookies)
                {
                    headers.SetInternal("Set-Cookie", cookie.ToClientString());
                }
            }

            StreamWriter writer = new StreamWriter(ms, encoding, 256);

            writer.Write("HTTP/{0} {1} {2}\r\n", version, status_code, status_description);
            string headers_str = headers.ToStringMultiValue();

            writer.Write(headers_str);
            writer.Flush();
            int preamble = (encoding.CodePage == 65001) ? 3 : encoding.GetPreamble().Length;

            if (output_stream == null)
            {
                output_stream = context.Connection.GetResponseStream();
            }

            /* Assumes that the ms was at position 0 */
            ms.Position = preamble;
            HeadersSent = true;
        }
Ejemplo n.º 4
0
        internal void SendHeaders(bool closing, MemoryStream stream)
        {
            if (_contentType != null)
            {
                var contentType = _contentEncoding != null &&
                                  _contentType.IndexOf("charset=", StringComparison.Ordinal) == -1
                          ? _contentType + "; charset=" + _contentEncoding.WebName
                          : _contentType;

                _headers.SetInternally("Content-Type", contentType, true);
            }

            if (_headers ["Server"] == null)
            {
                _headers.SetInternally("Server", "websocket-sharp/1.0", true);
            }

            var provider = CultureInfo.InvariantCulture;

            if (_headers ["Date"] == null)
            {
                _headers.SetInternally("Date", DateTime.UtcNow.ToString("r", provider), true);
            }

            if (!_chunked)
            {
                if (!_contentLengthSet && closing)
                {
                    _contentLengthSet = true;
                    _contentLength    = 0;
                }

                if (_contentLengthSet)
                {
                    _headers.SetInternally("Content-Length", _contentLength.ToString(provider), true);
                }
            }

            var version = _context.Request.ProtocolVersion;

            if (!_contentLengthSet && !_chunked && version >= HttpVersion.Version11)
            {
                _chunked = true;
            }

            /* Apache forces closing the connection for these status codes:
             * - HttpStatusCode.BadRequest            400
             * - HttpStatusCode.RequestTimeout        408
             * - HttpStatusCode.LengthRequired        411
             * - HttpStatusCode.RequestEntityTooLarge 413
             * - HttpStatusCode.RequestUriTooLong     414
             * - HttpStatusCode.InternalServerError   500
             * - HttpStatusCode.ServiceUnavailable    503
             */
            var connClose = _statusCode == 400 ||
                            _statusCode == 408 ||
                            _statusCode == 411 ||
                            _statusCode == 413 ||
                            _statusCode == 414 ||
                            _statusCode == 500 ||
                            _statusCode == 503;

            if (!connClose)
            {
                connClose = !_context.Request.KeepAlive;
            }

            // They sent both KeepAlive: true and Connection: close!?
            if (!_keepAlive || connClose)
            {
                _headers.SetInternally("Connection", "close", true);
                connClose = true;
            }

            if (_chunked)
            {
                _headers.SetInternally("Transfer-Encoding", "chunked", true);
            }

            int reuses = _context.Connection.Reuses;

            if (reuses >= 100)
            {
                _forceCloseChunked = true;
                if (!connClose)
                {
                    _headers.SetInternally("Connection", "close", true);
                    connClose = true;
                }
            }

            if (!connClose)
            {
                _headers.SetInternally(
                    "Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses), true);

                if (_context.Request.ProtocolVersion <= HttpVersion.Version10)
                {
                    _headers.SetInternally("Connection", "keep-alive", true);
                }
            }

            if (_location != null)
            {
                _headers.SetInternally("Location", _location, true);
            }

            if (_cookies != null)
            {
                foreach (Cookie cookie in _cookies)
                {
                    _headers.SetInternally("Set-Cookie", cookie.ToResponseString(), true);
                }
            }

            var encoding = _contentEncoding ?? Encoding.Default;
            var writer   = new StreamWriter(stream, encoding, 256);

            writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);

            var headers = _headers.ToStringMultiValue(true);

            writer.Write(headers);
            writer.Flush();

            var preamble = encoding.CodePage == 65001 ? 3 : encoding.GetPreamble().Length;

            if (_outputStream == null)
            {
                _outputStream = _context.Connection.GetResponseStream();
            }

            // Assumes that the stream was at position 0.
            stream.Position = preamble;
            _headersSent    = true;
        }
Ejemplo n.º 5
0
        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;
        }
    internal WebHeaderCollection WriteHeadersTo (MemoryStream destination)
    {
      var headers = new WebHeaderCollection (HttpHeaderType.Response, true);
      if (_headers != null)
        headers.Add (_headers);

      if (_contentType != null) {
        var type = _contentType.IndexOf ("charset=", StringComparison.Ordinal) == -1 &&
                   _contentEncoding != null
                   ? String.Format ("{0}; charset={1}", _contentType, _contentEncoding.WebName)
                   : _contentType;

        headers.InternalSet ("Content-Type", type, true);
      }

      if (headers["Server"] == null)
        headers.InternalSet ("Server", "websocket-sharp/1.0", true);

      var prov = CultureInfo.InvariantCulture;
      if (headers["Date"] == null)
        headers.InternalSet ("Date", DateTime.UtcNow.ToString ("r", prov), true);

      if (!_sendChunked)
        headers.InternalSet ("Content-Length", _contentLength.ToString (prov), true);
      else
        headers.InternalSet ("Transfer-Encoding", "chunked", true);

      /*
       * Apache forces closing the connection for these status codes:
       * - 400 Bad Request
       * - 408 Request Timeout
       * - 411 Length Required
       * - 413 Request Entity Too Large
       * - 414 Request-Uri Too Long
       * - 500 Internal Server Error
       * - 503 Service Unavailable
       */
      var closeConn = !_context.Request.KeepAlive ||
                      !_keepAlive ||
                      _statusCode == 400 ||
                      _statusCode == 408 ||
                      _statusCode == 411 ||
                      _statusCode == 413 ||
                      _statusCode == 414 ||
                      _statusCode == 500 ||
                      _statusCode == 503;

      var reuses = _context.Connection.Reuses;
      if (closeConn || reuses >= 100) {
        headers.InternalSet ("Connection", "close", true);
      }
      else {
        headers.InternalSet (
          "Keep-Alive", String.Format ("timeout=15,max={0}", 100 - reuses), true);

        if (_context.Request.ProtocolVersion < HttpVersion.Version11)
          headers.InternalSet ("Connection", "keep-alive", true);
      }

      if (_location != null)
        headers.InternalSet ("Location", _location, true);

      if (_cookies != null)
        foreach (Cookie cookie in _cookies)
          headers.InternalSet ("Set-Cookie", cookie.ToResponseString (), true);

      var enc = _contentEncoding ?? Encoding.Default;
      var writer = new StreamWriter (destination, enc, 256);
      writer.Write ("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
      writer.Write (headers.ToStringMultiValue (true));
      writer.Flush ();

      // Assumes that the destination was at position 0.
      destination.Position = enc.GetPreamble ().Length;

      return headers;
    }