Inheritance: System.Net.DelegatedStream
Exemplo n.º 1
0
 internal ReadAsyncResult(BufferedReadStream parent, AsyncCallback callback, object state) : base(null,state,callback)
 {
     this.parent = parent;
 }
Exemplo n.º 2
0
 internal ReadAsyncResult(BufferedReadStream parent, AsyncCallback callback, object state) : base(null, state, callback)
 {
     _parent = parent;
 }
Exemplo n.º 3
0
        void Initialize(BufferOffsetSize buffer)
        {
            WebConnection.Debug($"{ME} INIT: status={(int)StatusCode} bos={buffer.Offset}/{buffer.Size}");

            long   contentLength;
            string contentType = Headers["Transfer-Encoding"];
            bool   chunkedRead = (contentType != null && contentType.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);
            string clength     = Headers["Content-Length"];

            if (!chunkedRead && !string.IsNullOrEmpty(clength))
            {
                if (!long.TryParse(clength, out contentLength))
                {
                    contentLength = Int64.MaxValue;
                }
            }
            else
            {
                contentLength = Int64.MaxValue;
            }

            string tencoding = null;

            if (ExpectContent)
            {
                tencoding = Headers["Transfer-Encoding"];
            }

            ChunkedRead = (tencoding != null && tencoding.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);

            if (Version == HttpVersion.Version11 && RequestStream.KeepAlive)
            {
                KeepAlive = true;
                var cncHeader = Headers[ServicePoint.UsesProxy ? "Proxy-Connection" : "Connection"];
                if (cncHeader != null)
                {
                    cncHeader = cncHeader.ToLower();
                    KeepAlive = cncHeader.IndexOf("keep-alive", StringComparison.Ordinal) != -1;
                    if (cncHeader.IndexOf("close", StringComparison.Ordinal) != -1)
                    {
                        KeepAlive = false;
                    }
                }

                if (!ChunkedRead && contentLength == Int64.MaxValue)
                {
                    /*
                     * This is a violation of the HTTP Spec - the server neither send a
                     * "Content-Length:" nor a "Transfer-Encoding: chunked" header.
                     * The only way to recover from this is to keep reading until the
                     * remote closes the connection, so we can't reuse it.
                     */
                    KeepAlive = false;
                }
            }

            /*
             * Inner layer:
             * We may have read a few extra bytes while parsing the headers, these will be
             * passed to us in the @buffer parameter and we need to read these before
             * reading from the `InnerStream`.
             */
            Stream networkStream;

            if (!ExpectContent || (!ChunkedRead && buffer.Size >= contentLength))
            {
                bufferedEntireContent = true;
                innerStream           = new BufferedReadStream(Operation, null, buffer);
                networkStream         = innerStream;
            }
            else if (buffer.Size > 0)
            {
                networkStream = new BufferedReadStream(Operation, RequestStream.InnerStream, buffer);
            }
            else
            {
                networkStream = RequestStream.InnerStream;
            }

            /*
             * Intermediate layer:
             * - Wrap with MonoChunkStream when using chunked encoding.
             * - Otherwise, we should have a Content-Length, wrap with
             *   FixedSizeReadStream to read exactly that many bytes.
             */
            if (ChunkedRead)
            {
                innerStream = new MonoChunkStream(Operation, networkStream, Headers);
            }
            else if (bufferedEntireContent)
            {
                // 'innerStream' has already been set above.
            }
            else if (contentLength != Int64.MaxValue)
            {
                innerStream = new FixedSizeReadStream(Operation, networkStream, contentLength);
            }
            else
            {
                // Violation of the HTTP Spec - neither chunked nor length.
                innerStream = new BufferedReadStream(Operation, networkStream, null);
            }

            /*
             * Outer layer:
             * - Decode gzip/deflate if requested.
             */
            string content_encoding = Headers["Content-Encoding"];

            if (content_encoding == "gzip" && (Request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
            {
                innerStream = ContentDecodeStream.Create(Operation, innerStream, ContentDecodeStream.Mode.GZip);
                Headers.Remove(HttpRequestHeader.ContentEncoding);
            }
            else if (content_encoding == "deflate" && (Request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
            {
                innerStream = ContentDecodeStream.Create(Operation, innerStream, ContentDecodeStream.Mode.Deflate);
                Headers.Remove(HttpRequestHeader.ContentEncoding);
            }

            WebConnection.Debug($"{ME} INIT #1: - {ExpectContent} {closed} {nextReadCalled}");

            if (!ExpectContent)
            {
                nextReadCalled = true;
                Operation.Finish(true);
            }
        }
Exemplo n.º 4
0
        public virtual void OnTcpClientConnect(IAsyncResult asyn)
        {
            if (TcpClientConnected != null)
                TcpClientConnected(this, new TWSTcpClientConnectedEventArgs(this, null));
            try
            {
                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                TcpClient tc = _listener.EndAcceptTcpClient(asyn);
                _clientCount++;
                var s = new BufferedReadStream(tc.GetStream(), DEFAULT_BUFFER_SIZE);
                TWSServerClientState connection = new TWSServerClientState(this, s);

                lock (_clients) {
                    _clients.Add(connection);
                }

                connection.Start();

                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                _listener.BeginAcceptTcpClient(_connectCallback, null);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                OnError(new TWSError(TWSErrors.NO_VALID_CODE, se.Message));
            }
        }