예제 #1
0
        private async Task <MemoryStream> ReceiveZipBody(bool chunked)
        {
            MemoryStream outputStream = new MemoryStream();

            using (StreamWrapper streamWrapper = new StreamWrapper(_request.CommonStream, _content))
            {
                using (Stream gZipStream = GetZipStream(streamWrapper))
                {
                    byte[] buffer = new byte[_request.Connection.ReceiveBufferSize];

                    while (streamWrapper.TotalBytesRead != ContentLength)
                    {
                        if (!chunked)
                        {
                            int readBytes = await gZipStream.ReadAsync(buffer, 0, buffer.Length);

                            if (readBytes == 0)
                            {
                                if (streamWrapper.TotalBytesRead != ContentLength)
                                {
                                    await WaitStream();

                                    continue;
                                }

                                break;
                            }

                            await outputStream.WriteAsync(buffer, 0, readBytes);
                        }
                        else
                        {
                            string getLine = _content.Get(true);

                            if (getLine == "\r\n")
                            {
                                continue;
                            }

                            getLine = getLine.Trim(' ', '\r', '\n');

                            if (getLine == string.Empty)
                            {
                                break;
                            }

                            int blockLength = Convert.ToInt32(getLine, 16);

                            if (blockLength == 0)
                            {
                                break;
                            }

                            streamWrapper.TotalBytesRead = 0;
                            streamWrapper.LimitBytesRead = blockLength;

                            while (true)
                            {
                                int readBytes = gZipStream.Read(buffer, 0, buffer.Length);

                                if (readBytes == 0)
                                {
                                    if (streamWrapper.TotalBytesRead != blockLength)
                                    {
                                        await WaitStream();

                                        continue;
                                    }

                                    break;
                                }

                                await outputStream.WriteAsync(buffer, 0, readBytes);
                            }
                        }
                    }
                }
            }

            return(outputStream);
        }