Ejemplo n.º 1
0
        private void Init()
        {
            if (_sslStream != null)
            {
                //ssl_stream.AuthenticateAsServer(client_cert, true, (SslProtocols)ServicePointManager.SecurityProtocol, false);
            }

            _contextBound = false;
            _iStream      = null;
            _oStream      = null;
            _prefix       = null;
            _chunked      = false;
            _ms           = new MemoryStream();
            _position     = 0;
            _inputState   = InputState.RequestLine;
            _lineState    = LineState.None;
            _context      = new HttpListenerContext(this, _logger);
        }
Ejemplo n.º 2
0
        internal void FinishInitialization()
        {
            string host = UserHostName;

            if (_version > HttpVersion.Version10 && (host == null || host.Length == 0))
            {
                _context.ErrorMessage = "Invalid host name";
                return;
            }

            string path;
            Uri    rawUri = null;

            if (MaybeUri(_rawUrl.ToLowerInvariant()) && Uri.TryCreate(_rawUrl, UriKind.Absolute, out rawUri))
            {
                path = rawUri.PathAndQuery;
            }
            else
            {
                path = _rawUrl;
            }

            if ((host == null || host.Length == 0))
            {
                host = UserHostAddress;
            }

            if (rawUri != null)
            {
                host = rawUri.Host;
            }

            int colon = host.LastIndexOf(':');

            if (colon >= 0)
            {
                host = host.Substring(0, colon);
            }

            string baseUri = String.Format("{0}://{1}:{2}",
                                           (IsSecureConnection) ? (IsWebSocketRequest ? "wss" : "https") : (IsWebSocketRequest ? "ws" : "http"),
                                           host, LocalEndPoint.Port);

            if (!Uri.TryCreate(baseUri + path, UriKind.Absolute, out _url))
            {
                _context.ErrorMessage = WebUtility.HtmlEncode("Invalid url: " + baseUri + path);
                return;
            }

            CreateQueryString(_url.Query);

            if (_version >= HttpVersion.Version11)
            {
                string tEncoding = Headers["Transfer-Encoding"];
                _isChunked = (tEncoding != null && String.Compare(tEncoding, "chunked", StringComparison.OrdinalIgnoreCase) == 0);
                // 'identity' is not valid!
                if (tEncoding != null && !_isChunked)
                {
                    _context.Connection.SendError(null, 501);
                    return;
                }
            }

            if (!_isChunked && !_clSet)
            {
                if (String.Compare(_method, "POST", StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(_method, "PUT", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    _context.Connection.SendError(null, 411);
                    return;
                }
            }

            if (String.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ResponseStream output = _context.Connection.GetResponseStream();
                output.InternalWrite(_100Continue, 0, _100Continue.Length);
            }
        }