Esempio n. 1
0
        /// <summary>
        /// Send headers and body to the browser.
        /// </summary>
        /// <exception cref="InvalidOperationException">If content have already been sent.</exception>
        public void SendOri()
        {
            if (Sent)
            {
                throw new InvalidOperationException("Everything have already been sent.");
            }

            m_context.ReqResponseAboutToSend(requestID);
            if (m_context.MAXRequests == 0 || _keepAlive == 0)
            {
                Connection = ConnectionType.Close;
                m_context.TimeoutKeepAlive = 0;
            }
            else
            {
                if (_keepAlive > 0)
                {
                    m_context.TimeoutKeepAlive = _keepAlive * 1000;
                }
            }

            if (!HeadersSent)
            {
                if (!SendHeaders())
                {
                    _body.Dispose();
                    Sent = true;
                    return;
                }
            }

            if (RawBuffer != null)
            {
                if (RawBufferStart >= 0 && RawBufferLen > 0)
                {
                    if (RawBufferStart > RawBuffer.Length)
                    {
                        RawBufferStart = 0;
                    }

                    if (RawBufferLen + RawBufferStart > RawBuffer.Length)
                    {
                        RawBufferLen = RawBuffer.Length - RawBufferStart;
                    }

                    /*
                     * int curlen;
                     * while(RawBufferLen > 0)
                     * {
                     *  curlen = RawBufferLen;
                     *  if(curlen > 8192)
                     *      curlen = 8192;
                     *  if (!_context.Send(RawBuffer, RawBufferStart, curlen))
                     *  {
                     *      RawBuffer = null;
                     *      RawBufferStart = -1;
                     *      RawBufferLen = -1;
                     *      Body.Dispose();
                     *      return;
                     *  }
                     *  RawBufferLen -= curlen;
                     *  RawBufferStart += curlen;
                     * }
                     */
                    if (RawBufferLen > 0)
                    {
                        if (!m_context.Send(RawBuffer, RawBufferStart, RawBufferLen))
                        {
                            RawBuffer      = null;
                            RawBufferStart = -1;
                            RawBufferLen   = -1;
                            if (_body != null)
                            {
                                _body.Dispose();
                            }
                            Sent = true;
                            return;
                        }
                    }
                }

                RawBuffer      = null;
                RawBufferStart = -1;
                RawBufferLen   = -1;
            }

            if (_body != null && _body.Length > 0)
            {
                _body.Flush();
                _body.Seek(0, SeekOrigin.Begin);

                var buffer    = new byte[8192];
                int bytesRead = _body.Read(buffer, 0, 8192);
                while (bytesRead > 0)
                {
                    if (!m_context.Send(buffer, 0, bytesRead))
                    {
                        break;
                    }
                    bytesRead = _body.Read(buffer, 0, 8192);
                }

                _body.Dispose();
            }
            Sent = true;
            m_context.ReqResponseSent(requestID, Connection);
        }