Exemplo n.º 1
0
        protected static async Task <HttpResponse> ReadAsync(
            Stream stream,
            int millisecondsTimeout = 90000,
            CancellationToken ct    = default)
        {
            var timeout = false;
            var timer   = new Timer(
                state =>
            {
                timeout = true;
#if NET452
                stream.Close();
#else
                stream.Dispose();
#endif
            },
                null,
                millisecondsTimeout,
                -1);

            try
            {
                var http       = HttpResponse.Parse(ReadHeaders(stream));
                var contentLen = http.Headers["Content-Length"];

                if (!string.IsNullOrEmpty(contentLen))
                {
                    http._entityBodyData = await ReadEntityBodyAsync(stream, contentLen, ct).ConfigureAwait(false);
                }

                return(http);
            }
            catch (Exception ex)
            {
                throw new WebSocketException(timeout
                    ? "A timeout has occurred while reading an HTTP request/response."
                    : "An exception has occurred while reading an HTTP request/response.", ex);
            }
            finally
            {
                timer.Change(-1, -1);
                timer.Dispose();
            }
        }