Esempio n. 1
0
        private void CloseChunkGzip()
        {
            var crlfBuffer = this.Encoding.GetBytes("\r\n");
            var buffers    = new ArraySegment <byte> [4];

            //
            //gzip
            this.chunkGzipStream.Close();
            var contentLength = (int)this.chunkStream.Length;

            this.chunkStream.Position = 0;
            contentBuffer             = StreamHelper.Receive(this.chunkStream, contentLength);
            this.chunkStream.SetLength(0);
            //len
            var lenBuffer = this.Encoding.GetBytes(contentLength.ToString("x"));

            buffers[0] = new ArraySegment <byte>(lenBuffer);
            buffers[1] = new ArraySegment <byte>(crlfBuffer);
            //content
            buffers[2] = new ArraySegment <byte>(contentBuffer);
            buffers[3] = new ArraySegment <byte>(crlfBuffer);
            //
            try
            {
                this.socket.Send(buffers);
            }
            catch
            {
                SocketHelper.TryClose(this.socket);
                StreamHelper.TryClose(this.chunkGzipStream);
                StreamHelper.TryClose(this.chunkStream);
                this.chunkWriteEvent.Set();
                throw;
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 进行必要的资源清理,此方法仅供系统调用, 不允许使用者直接调用
 /// </summary>
 internal void Clear()
 {
     if (this.files != null)
     {
         foreach (var file in this.files)
         {
             StreamHelper.TryClose(file.Stream);
         }
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Write Chunk Content
 /// </summary>
 /// <param name="contentBuffer"></param>
 private void Chunk(byte[] contentBuffer)
 {
     lock (this.chunkWriteEvent)
     {
         var crlfBuffer = this.Encoding.GetBytes("\r\n");
         var buffers    = new ArraySegment <byte> [4];
         //
         int contentLength = contentBuffer.Length;
         //gzip
         if (this.acceptGzip && this.gzipThreshold != 0)
         {
             this.chunkGzipStream.Write(contentBuffer, 0, contentBuffer.Length);
             contentLength = (int)this.chunkStream.Length;
             if (contentLength < 256)
             {
                 //小字节退出,待下次压缩或关闭压缩
                 return;
             }
             this.chunkStream.Position = 0;
             contentBuffer             = StreamHelper.Receive(this.chunkStream, contentLength);
             this.chunkStream.SetLength(0);
         }
         //len
         var lenBuffer = this.Encoding.GetBytes(contentLength.ToString("x"));
         buffers[0] = new ArraySegment <byte>(lenBuffer);
         buffers[1] = new ArraySegment <byte>(crlfBuffer);
         //content
         buffers[2] = new ArraySegment <byte>(contentBuffer);
         buffers[3] = new ArraySegment <byte>(crlfBuffer);
         //
         try
         {
             this.socket.Send(buffers);
         }
         catch
         {
             SocketHelper.TryClose(this.socket);
             StreamHelper.TryClose(this.chunkGzipStream);
             StreamHelper.TryClose(this.chunkStream);
             this.chunkWriteEvent.Set();
             throw;
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 响应客户端
        /// </summary>
        /// <param name="build"></param>
        /// <param name="status"></param>
        protected override void Response(StringBuilder build, HttpStatusCode status)
        {
            byte[] contentBuffer = null;
            byte[] headerBuffer  = null;

            //header
            if (this.chunkWriteStatus == HttpServerChunkStatus.NoBegin)
            {
                if (this.contentBuffer != null)
                {
                    contentBuffer = this.contentBuffer;
                }
                else if (!string.IsNullOrEmpty(this.content))
                {
                    contentBuffer = this.Encoding.GetBytes(this.content);
                }
                else
                {
                    contentBuffer = new byte[0];
                }

                //gzip
                if (this.acceptGzip && this.gzipThreshold != 0 && contentBuffer.Length > this.gzipThreshold)
                {
                    build.AppendLine("Content-Encoding: gzip");

                    using (var m = new MemoryStream())
                        using (var stream = new GZipStream(m, CompressionMode.Compress, true))
                        {
                            stream.Write(contentBuffer, 0, contentBuffer.Length);
                            stream.Close();
                            //content
                            contentBuffer = m.ToArray();
                        }
                }

                build.AppendLine(string.Concat("Content-Length: ", contentBuffer.Length));
                build.AppendLine();
            }
            else
            {
                if (this.acceptGzip && this.gzipThreshold != 0)
                {
                    build.AppendLine("Content-Encoding: gzip");
                }
                build.AppendLine("Transfer-Encoding: chunked");
                build.AppendLine();
            }
            headerBuffer = Encoding.ASCII.GetBytes(build.ToString());

            //
            if (this.chunkWriteStatus == HttpServerChunkStatus.NoBegin)
            {
                if (this.isRequestHead == false && contentBuffer != null && contentBuffer.Length > 0)
                {
                    var buffers = new ArraySegment <byte> [2];
                    //header
                    buffers[0] = new ArraySegment <byte>(headerBuffer);
                    //content
                    buffers[1] = new ArraySegment <byte>(contentBuffer);
                    //send
                    this.socket.Send(buffers);
                }
                else
                {
                    this.socket.Send(headerBuffer);
                }
            }
            else
            {
                try
                {
                    this.socket.Send(headerBuffer);
                }
                catch
                {
                    SocketHelper.TryClose(this.socket);
                    StreamHelper.TryClose(this.chunkGzipStream);
                    StreamHelper.TryClose(this.chunkStream);
                    throw;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// End Write
        /// </summary>
        /// <exception cref="InvalidOperationException">No Invoke BeginWrite / Has Been Invoked EndWrite</exception>
        public void EndWrite()
        {
            lock (this.chunkWriteEvent)
            {
                if (this.chunkWriteStatus == HttpServerChunkStatus.NoBegin)
                {
                    throw new InvalidOperationException("No Invoke BeginWrite");
                }

                if (this.chunkWriteStatus == HttpServerChunkStatus.End)
                {
                    throw new InvalidOperationException("Has Been Invoked EndWrite");
                }

                if (this.chunkWriteStatus == HttpServerChunkStatus.Writing)
                {
                    this.chunkWriteStatus = HttpServerChunkStatus.End;

                    try
                    {
                        //gzip
                        if (this.acceptGzip && this.gzipThreshold != 0)
                        {
                            this.CloseChunkGzip();
                        }

                        if (this.isRequestHead == false)
                        {
                            var crlfBuffer = this.Encoding.GetBytes("\r\n");

                            var buffers = new ArraySegment <byte> [3];
                            using (var m = new MemoryStream(512))
                            {
                                //len
                                var lenBuffer = this.Encoding.GetBytes("0");
                                buffers[0] = new ArraySegment <byte>(lenBuffer);
                                buffers[1] = new ArraySegment <byte>(crlfBuffer);
                                //content
                                buffers[2] = new ArraySegment <byte>(crlfBuffer);
                            }
                            //
                            try
                            {
                                this.socket.Send(buffers);
                            }
                            catch
                            {
                                SocketHelper.TryClose(this.socket);
                                throw;
                            }
                            finally
                            {
                                StreamHelper.TryClose(this.chunkGzipStream);
                                StreamHelper.TryClose(this.chunkStream);
                                this.chunkWriteEvent.Set();
                            }
                        }
                        else
                        {
                            this.chunkWriteEvent.Set();
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new InvalidOperationException("End check write failure", exception);
                    }
                }
            }
        }