internal HttpListenerRequest(HttpListenerContext context)
 {
     _context = context;
       _contentLength = -1;
       _headers = new WebHeaderCollection ();
       _identifier = Guid.NewGuid ();
 }
        /// <summary>
        /// Copies some properties from the specified <see cref="HttpListenerResponse"/> to
        /// this response.
        /// </summary>
        /// <param name="templateResponse">
        /// A <see cref="HttpListenerResponse"/> to copy.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="templateResponse"/> is <see langword="null"/>.
        /// </exception>
        public void CopyFrom(HttpListenerResponse templateResponse)
        {
            if (templateResponse == null)
            throw new ArgumentNullException ("templateResponse");

              if (templateResponse._headers != null) {
            if (_headers != null)
              _headers.Clear ();

            Headers.Add (templateResponse._headers);
              }
              else if (_headers != null) {
            _headers = null;
              }

              _contentLength = templateResponse._contentLength;
              _statusCode = templateResponse._statusCode;
              _statusDescription = templateResponse._statusDescription;
              _keepAlive = templateResponse._keepAlive;
              _version = templateResponse._version;
        }
        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", "DiPS Service/1.00", 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.CodePage == 65001 ? 3 : enc.GetPreamble ().Length;

              return headers;
        }
        internal static HttpRequest Parse(string[] headerParts)
        {
            var requestLine = headerParts[0].Split (new[] { ' ' }, 3);
              if (requestLine.Length != 3)
            throw new ArgumentException ("Invalid request line: " + headerParts[0]);

              var headers = new WebHeaderCollection ();
              for (int i = 1; i < headerParts.Length; i++)
            headers.InternalSet (headerParts[i], false);

              return new HttpRequest (
            requestLine[0], requestLine[1], new Version (requestLine[2].Substring (5)), headers);
        }