Пример #1
0
        /// <summary>
        /// Copies the streams chunked
        /// </summary>
        /// <param name="inStreamReader"></param>
        /// <returns></returns>
        internal async Task WriteResponseBodyChunkedAsync(CustomBinaryReader inStreamReader)
        {
            while (true)
            {
                string chunkHead = await inStreamReader.ReadLineAsync();

                int chunkSize = int.Parse(chunkHead, NumberStyles.HexNumber);

                if (chunkSize != 0)
                {
                    var chunkHeadBytes = Encoding.ASCII.GetBytes(chunkSize.ToString("x2"));
                    await BaseStream.WriteAsync(chunkHeadBytes, 0, chunkHeadBytes.Length);

                    await BaseStream.WriteAsync(ProxyConstants.NewLineBytes, 0, ProxyConstants.NewLineBytes.Length);

                    await inStreamReader.CopyBytesToStream(BaseStream, chunkSize);

                    await BaseStream.WriteAsync(ProxyConstants.NewLineBytes, 0, ProxyConstants.NewLineBytes.Length);

                    await inStreamReader.ReadLineAsync();
                }
                else
                {
                    await inStreamReader.ReadLineAsync();

                    await BaseStream.WriteAsync(ProxyConstants.ChunkEnd, 0, ProxyConstants.ChunkEnd.Length);

                    break;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Copies the specified content length number of bytes to the output stream from the given inputs stream
        /// optionally chunked
        /// </summary>
        /// <param name="bufferSize"></param>
        /// <param name="inStreamReader"></param>
        /// <param name="isChunked"></param>
        /// <param name="contentLength"></param>
        /// <returns></returns>
        internal async Task WriteResponseBodyAsync(int bufferSize, CustomBinaryReader inStreamReader, bool isChunked,
                                                   long contentLength)
        {
            if (!isChunked)
            {
                //http 1.0
                if (contentLength == -1)
                {
                    contentLength = long.MaxValue;
                }

                await inStreamReader.CopyBytesToStream(BaseStream, contentLength);
            }
            else
            {
                await WriteResponseBodyChunkedAsync(inStreamReader);
            }
        }