Example #1
0
        protected override void Dispose(bool disposing)
        {
            if (disposed == false)
            {
                disposed = true;
                byte[]       bytes   = null;
                MemoryStream ms      = GetHeaders(response, _memoryStreamFactory, false);
                bool         chunked = response.SendChunked;
                if (stream.CanWrite)
                {
                    try
                    {
                        if (ms != null)
                        {
                            long start = ms.Position;
                            if (chunked && !trailer_sent)
                            {
                                bytes       = GetChunkSizeBytes(0, true);
                                ms.Position = ms.Length;
                                ms.Write(bytes, 0, bytes.Length);
                            }
                            byte[] msBuffer;
                            _memoryStreamFactory.TryGetBuffer(ms, out msBuffer);
                            InternalWrite(msBuffer, (int)start, (int)(ms.Length - start));
                            trailer_sent = true;
                        }
                        else if (chunked && !trailer_sent)
                        {
                            bytes = GetChunkSizeBytes(0, true);
                            InternalWrite(bytes, 0, bytes.Length);
                            trailer_sent = true;
                        }
                    }
                    catch (IOException ex)
                    {
                        // Ignore error due to connection reset by peer
                    }
                }
                response.Close();
            }

            base.Dispose(disposing);
        }
Example #2
0
        public RequestStream GetRequestStream(bool chunked, long contentlength)
        {
            if (i_stream == null)
            {
                byte[] buffer;
                _memoryStreamFactory.TryGetBuffer(ms, out buffer);

                int length = (int)ms.Length;
                ms = null;
                if (chunked)
                {
                    this.chunked = true;
                    //context.Response.SendChunked = true;
                    i_stream = new ChunkedInputStream(context, stream, buffer, position, length - position);
                }
                else
                {
                    i_stream = new RequestStream(stream, buffer, position, length - position, contentlength);
                }
            }
            return(i_stream);
        }
Example #3
0
        static Stream GetSubStream(Stream stream, IMemoryStreamFactory streamProvider)
        {
            if (stream is MemoryStream)
            {
                var other = (MemoryStream)stream;

                byte[] buffer;
                if (streamProvider.TryGetBuffer(other, out buffer))
                {
                    return(streamProvider.CreateNew(buffer));
                }
                return(streamProvider.CreateNew(other.ToArray()));
            }

            return(stream);
        }