상속: System.Collections.Specialized.NameValueCollection, ISerializable
 internal HttpListenerResponse(HttpListenerContext context)
 {
     _context = context;
       _headers = new WebHeaderCollection ();
       _keepAlive = true;
       _statusCode = 200;
       _statusDescription = "OK";
       _version = HttpVersion.Version11;
 }
 internal HttpListenerRequest (HttpListenerContext context)
 {
   _context = context;
   _contentLength = -1;
   _headers = new WebHeaderCollection ();
   _identifier = Guid.NewGuid ();
 }
예제 #3
0
        public static ResponseHandshake Parse(string[] response)
        {
            var statusLine = response[0].Split(' ');
              if (statusLine.Length < 3)
            throw new ArgumentException("Invalid status line.");

              var reason = new StringBuilder(statusLine[2]);
              for (int i = 3; i < statusLine.Length; i++)
            reason.AppendFormat(" {0}", statusLine[i]);

              var headers = new WebHeaderCollection();
              for (int i = 1; i < response.Length; i++)
            headers.Add(response[i]);

              return new ResponseHandshake {
            Headers         = headers,
            Reason          = reason.ToString(),
            StatusCode      = statusLine[1],
            ProtocolVersion = new Version(statusLine[0].Substring(5))
              };
        }
예제 #4
0
        public static HandshakeResponse Parse(string [] headerParts)
        {
            var statusLine = headerParts [0].Split (new char [] { ' ' }, 3);
              if (statusLine.Length != 3)
            throw new ArgumentException ("Invalid status line: " + headerParts [0]);

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

              return new HandshakeResponse {
            Headers = headers,
            ProtocolVersion = new Version (statusLine [0].Substring (5)),
            Reason = statusLine [2],
            StatusCode = statusLine [1]
              };
        }
예제 #5
0
    internal static HttpResponse Parse (string[] headerParts)
    {
      var statusLine = headerParts[0].Split (new[] { ' ' }, 3);
      if (statusLine.Length != 3)
        throw new ArgumentException ("Invalid status line: " + headerParts[0]);

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

      return new HttpResponse (
        statusLine[1], statusLine[2], new Version (statusLine[0].Substring (5)), headers);
    }
        public static RequestHandshake Parse(string[] request)
        {
            var requestLine = request[0].Split(' ');
              if (requestLine.Length != 3)
              {
            var msg = "Invalid HTTP Request-Line: " + request[0];
            throw new ArgumentException(msg, "request");
              }

              var headers = new WebHeaderCollection();
              for (int i = 1; i < request.Length; i++)
            headers.Add(request[i]);

              return new RequestHandshake {
            Headers         = headers,
            HttpMethod      = requestLine[0],
            RequestUri      = requestLine[1].ToUri(),
            ProtocolVersion = new Version(requestLine[2].Substring(5))
              };
        }
예제 #7
0
        public static HandshakeRequest Parse(string [] headerParts)
        {
            var requestLine = headerParts [0].Split (new char [] { ' ' }, 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.SetInternal (headerParts [i], false);

              return new HandshakeRequest {
            Headers = headers,
            HttpMethod = requestLine [0],
            ProtocolVersion = new Version (requestLine [2].Substring (5)),
            RawUrl = requestLine [1],
            RequestUri = requestLine [1].ToUri ()
              };
        }
예제 #8
0
    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);
    }
예제 #9
0
    public static HandshakeRequest Parse (string [] request)
    {
      var requestLine = request [0].Split (' ');
      if (requestLine.Length != 3)
        throw new ArgumentException (
          "Invalid HTTP Request-Line: " + request [0], "request");

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

      return new HandshakeRequest {
        Headers = headers,
        HttpMethod = requestLine [0],
        ProtocolVersion = new Version (requestLine [2].Substring (5)),
        RawUrl = requestLine [1],
        RequestUri = requestLine [1].ToUri ()
      };
    }
    /// <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", "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;
    }
예제 #12
0
    internal static HandshakeResponse Parse (string[] headerParts)
    {
      var statusLine = headerParts[0].Split (new[] { ' ' }, 3);
      if (statusLine.Length != 3)
        throw new ArgumentException ("Invalid status line: " + headerParts[0]);

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

      var res = new HandshakeResponse (new Version (statusLine[0].Substring (5)), headers);
      res._code = statusLine[1];
      res._reason = statusLine[2];

      return res;
    }
예제 #13
0
    internal static HandshakeRequest 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.SetInternally (headerParts[i], false);

      var req = new HandshakeRequest (new Version (requestLine[2].Substring (5)), headers);
      req._method = requestLine[0];
      req._uri = requestLine[1];

      return req;
    }
예제 #14
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);
        }
예제 #15
0
 public ChunkStream(byte[] buffer, int offset, int count, WebHeaderCollection headers)
     : this(headers)
 {
     Write(buffer, offset, count);
 }