Example #1
0
        public int Read(out int statusCode)
        {
            var read   = _client.Read(_buffer, 0, _buffer.Length);
            var length = read;

            statusCode    = HttpHelper.GetStatusCode(_buffer, 0, read);
            _responseType = HttpHelper.GetResponseType(_buffer, 0, read);

            if (_responseType == ResponseType.ContentLength)
            {
                var responseLength = HttpHelperContentLength.GetResponseLength(_buffer, 0, read);

                while (length < responseLength)
                {
                    length += _client.Read(_buffer, 0, _buffer.Length);
                }

                return(length);
            }

            if (_responseType == ResponseType.Chunked)
            {
                while (!HttpHelperChunked.IsEndOfChunkedStream(_buffer, read))
                {
                    read    = _client.Read(_buffer, 0, _buffer.Length);
                    length += read;
                }

                return(length);
            }

            throw new UnknownResponseTypeException();
        }
Example #2
0
        public (int length, int statusCode) Send()
        {
            _client.Write(_request.Span);
            var read = _client.Read(_buffer);

            if (read == 0)
            {
                _client.Reset();
                throw new ConnectionClosedException();
            }

            var length       = read;
            var responseSpan = _buffer.Span.Slice(0, read);
            var statusCode   = HttpHelper.GetStatusCode(responseSpan);
            var responseType = HttpHelper.GetResponseType(responseSpan);

            if (responseType == ResponseType.ContentLength)
            {
                var responseLength = HttpHelperContentLength.GetResponseLength(responseSpan);

                while (length < responseLength)
                {
                    length += _client.Read(_buffer);
                }

                return(length, statusCode);
            }

            if (responseType == ResponseType.Chunked)
            {
                while (!HttpHelperChunked.IsEndOfChunkedStream(_buffer.Span.Slice(0, read)))
                {
                    read    = _client.Read(_buffer);
                    length += read;
                }

                return(length, statusCode);
            }

            throw new UnknownResponseTypeException();
        }