Ejemplo n.º 1
0
        public async Task TransferEncodingTest2()
        {
            const string expectedData = "7\r\nMozilla\r\n9\r\nDeveloper\r\n7\r\nNetwork\r\n0\r\n\r\n";
            var          memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(expectedData));

            var encodingService = new TransferEncodingService();


            var remaining = await encodingService.TransferEncoding(memoryStream, 65536);

            var actualData = Encoding.UTF8.GetString(remaining);

            Assert.Equal(expectedData, actualData);
        }
Ejemplo n.º 2
0
        public async Task <byte[]> HandleSend(string requestText)
        {
            await _remoteSslStream.WriteAsync(Encoding.UTF8.GetBytes(requestText));

            var headresService = new HeadersService();
            var buffer         = await headresService.ReceiveUpToHeaders(_remoteSslStream, _clientReceiveBufferSize, CancellationToken.None);

            var initialRawHttpResponse = Encoding.UTF8.GetString(buffer);
            var httpData = HttpData.ParseRawHttp(initialRawHttpResponse);

            if (httpData.StatusCode == (int)HttpStatusCode.NotModified)
            {
                return(buffer);
            }


            if (httpData.StatusCode == (int)HttpStatusCode.Continue)
            {
                return(buffer);
            }

            byte[] rawResponse;
            if (int.TryParse(httpData.HeadersList.SingleOrDefault(h => h.Key == "Content-Length").Value, out int contentLength))
            {
                var encodingService = new ContentLengthService();
                rawResponse = await encodingService.ParseByContentLength(_remoteSslStream, _clientReceiveBufferSize, contentLength);
            }
            else
            {
                if (!initialRawHttpResponse.Contains("Transfer-Encoding: chunked"))
                {
                    throw new Exception("invalid encoding");
                }

                var contentSlice    = buffer.AsSpan(buffer.Length - httpData.ContentLength).ToArray();
                var encodingService = new TransferEncodingService();
                rawResponse = await encodingService.TransferEncoding(_remoteSslStream, _clientReceiveBufferSize, contentSlice);
            }

            var headersSlice = buffer.AsSpan(0, buffer.Length - httpData.ContentLength).ToArray();

            var fullResponse = headersSlice.Concat(rawResponse).ToArray();
            var fullResText  = Encoding.UTF8.GetString(fullResponse);

            return(fullResponse);
        }