Exemplo n.º 1
0
        // When this returns zero, the entire entity has been read.
        public override int EndRead(IAsyncResult asyncResult)
        {
            HttpWorkerRequest wr = _context.WorkerRequest;

            if (wr != null && wr.SupportsAsyncRead && !_context.IsInCancellablePeriod)
            {
                int totalBytesRead = _preloadedBytesRead;
                if (_preloadedBytesRead > 0)
                {
                    _preloadedBytesRead = 0;
                }
                int bytesRead = 0;
                try {
                    bytesRead = wr.EndRead(asyncResult);
                }
                catch (HttpException) {
                    if (_persistEntityBody)
                    {
                        SetRawContentOnce();
                    }
                    throw;
                }
                totalBytesRead += bytesRead;
                if (bytesRead > 0)
                {
                    if (_persistEntityBody)
                    {
                        if (_rawContent != null)
                        {
                            _rawContent.AddBytes(_buffer, _offset, bytesRead);
                        }
                        _buffer = null;
                        _offset = 0;
                        _count  = 0;
                    }
                    int dummy1 = 0, dummy2 = 0, dummy3 = 0;
                    UpdateCounters(bytesRead, ref dummy1, ref dummy2, ref dummy3);
                }
                if (_persistEntityBody
                    // we might attempt a read with count == 0, in which case bytesRead will
                    // be zero but we may not be done reading the entity body. Don't set raw
                    // content until bytesRead is 0 and count is not 0 or _remainingBytes is 0
                    && ((bytesRead == 0 && _count != 0) || _remainingBytes == 0))
                {
                    SetRawContentOnce();
                }
                return(totalBytesRead);
            }
            else
            {
                return(base.EndRead(asyncResult));
            }
        }
Exemplo n.º 2
0
        /*
         * Read entire raw content as byte array
         */
        private HttpRawUploadedContent GetEntireRawContent() {
            if (_wr == null)
                return null;

            if (_rawContent != null) {
                // if _rawContent was set by HttpBufferlessInputStream, then we will apply the filter here
                if (_installedFilter != null && !_filterApplied) {
                    ApplyFilter(ref _rawContent, RuntimeConfig.GetConfig(_context).HttpRuntime.RequestLengthDiskThresholdBytes);
                }
                return _rawContent;
            }

            if (_readEntityBodyMode == ReadEntityBodyMode.None) {
                _readEntityBodyMode = ReadEntityBodyMode.Classic;
            }
            else if (_readEntityBodyMode == ReadEntityBodyMode.Buffered) {
                // _rawContent should have been set already
                throw new InvalidOperationException(SR.GetString(SR.Invalid_operation_with_get_buffered_input_stream));
            }
            else if (_readEntityBodyMode == ReadEntityBodyMode.Bufferless) {
                throw new HttpException(SR.GetString(SR.Incompatible_with_get_bufferless_input_stream));
            }

            // enforce the limit
            HttpRuntimeSection cfg = RuntimeConfig.GetConfig(_context).HttpRuntime;
            int limit = cfg.MaxRequestLengthBytes;
            if (ContentLength > limit) {
                if ( !(_wr is IIS7WorkerRequest) ) {
                    Response.CloseConnectionAfterError();
                }
                throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                        null, WebEventCodes.RuntimeErrorPostTooLarge);
            }

            // threshold to go to file

            int fileThreshold = cfg.RequestLengthDiskThresholdBytes;

            // read the preloaded content

            HttpRawUploadedContent rawContent = new HttpRawUploadedContent(fileThreshold, ContentLength);

            byte[] preloadedContent = _wr.GetPreloadedEntityBody();

            if (preloadedContent != null) {
                _wr.UpdateRequestCounters(preloadedContent.Length);
                rawContent.AddBytes(preloadedContent, 0, preloadedContent.Length);
            }

            // read the remaing content

            if (!_wr.IsEntireEntityBodyIsPreloaded()) {
                int remainingBytes = (ContentLength > 0) ? ContentLength - rawContent.Length : Int32.MaxValue;

                HttpApplication app = _context.ApplicationInstance;
                byte[] buf = (app != null) ? app.EntityBuffer : new byte[8 * 1024];
                int numBytesRead = rawContent.Length;

                while (remainingBytes > 0) {
                    int bytesToRead = buf.Length;
                    if (bytesToRead > remainingBytes)
                        bytesToRead = remainingBytes;

                    int bytesRead = _wr.ReadEntityBody(buf, bytesToRead);
                    if (bytesRead <= 0)
                        break;

                    _wr.UpdateRequestCounters(bytesRead);

                    rawContent.AddBytes(buf, 0, bytesRead);

                    remainingBytes -= bytesRead;
                    numBytesRead += bytesRead;

                    if (numBytesRead > limit) {
                        throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                    null, WebEventCodes.RuntimeErrorPostTooLarge);
                    }

                    // Fail synchrously if receiving the request content takes too long
                    // RequestTimeoutManager is not efficient in case of ThreadPool starvation
                    // as the timer callback doing Thread.Abort may not trigger for a long time
                    if (remainingBytes > 0 && _context.HasTimeoutExpired) {
                        throw new HttpException(SR.GetString(SR.Request_timed_out));
                    }
                }
            }

            rawContent.DoneAddingBytes();

            // filter content
            if (_installedFilter != null) {
                ApplyFilter(ref rawContent, fileThreshold);
            }

            SetRawContent(rawContent);
            return _rawContent;
        }
Exemplo n.º 3
0
 private void ApplyFilter(ref HttpRawUploadedContent rawContent, int fileThreshold) {
     if (_installedFilter != null) {
         _filterApplied = true;
         if (rawContent.Length > 0) {
             try {
                 try {
                     _filterSource.SetContent(rawContent);
                     
                     HttpRawUploadedContent filteredRawContent = new HttpRawUploadedContent(fileThreshold, rawContent.Length);
                     HttpApplication app = _context.ApplicationInstance;
                     byte[] buf = (app != null) ? app.EntityBuffer : new byte[8 * 1024];
                     
                     for (;;) {
                         int bytesRead = _installedFilter.Read(buf, 0, buf.Length);
                         if (bytesRead == 0)
                             break;
                         filteredRawContent.AddBytes(buf, 0, bytesRead);
                     }
                     
                     filteredRawContent.DoneAddingBytes();
                     rawContent = filteredRawContent;
                 }
                 finally {
                     _filterSource.SetContent(null);
                 }
             }
             catch { // Protect against exception filters
                 throw;
             }
         }
     }
 }
Exemplo n.º 4
0
        /*
         * Read entire raw content as byte array 
         */
        private HttpRawUploadedContent GetEntireRawContent()
        {
            if (_wr == null)
                return null;

            if (_rawContent != null)
                return _rawContent;

            // enforce the limit 
            HttpRuntimeSection cfg = RuntimeConfig.GetConfig(_context).HttpRuntime;
            int limit = cfg.MaxRequestLengthBytes;
            if (ContentLength > limit)
            {
                if (!(_wr is IIS7WorkerRequest))
                {
                    Response.CloseConnectionAfterError();
                }
                throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                        null, WebEventCodes.RuntimeErrorPostTooLarge);
            }

            // threshold to go to file

            int fileThreshold = cfg.RequestLengthDiskThresholdBytes;

            // read the preloaded content 

            HttpRawUploadedContent rawContent = new HttpRawUploadedContent(fileThreshold, ContentLength);

            byte[] preloadedContent = _wr.GetPreloadedEntityBody();

            if (preloadedContent != null)
            {
                _wr.UpdateRequestCounters(preloadedContent.Length);
                rawContent.AddBytes(preloadedContent, 0, preloadedContent.Length);
            }

            // read the remaing content

            if (!_wr.IsEntireEntityBodyIsPreloaded())
            {
                int remainingBytes = (ContentLength > 0) ? ContentLength - rawContent.Length : Int32.MaxValue;

                HttpApplication app = _context.ApplicationInstance;
                byte[] buf = (app != null) ? app.EntityBuffer : new byte[8 * 1024];
                int numBytesRead = rawContent.Length;

                while (remainingBytes > 0)
                {
                    int bytesToRead = buf.Length;
                    if (bytesToRead > remainingBytes)
                        bytesToRead = remainingBytes;

                    int bytesRead = _wr.ReadEntityBody(buf, bytesToRead);
                    if (bytesRead <= 0)
                        break;

                    _wr.UpdateRequestCounters(bytesRead);

                    _readEntityBody = true;
                    rawContent.AddBytes(buf, 0, bytesRead);

                    remainingBytes -= bytesRead;
                    numBytesRead += bytesRead;

                    if (numBytesRead > limit)
                    {
                        throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                    null, WebEventCodes.RuntimeErrorPostTooLarge);
                    }
                }
            }

            rawContent.DoneAddingBytes();

            // filter content 

            if (_installedFilter != null)
            {
                if (rawContent.Length > 0)
                {
                    try
                    {
                        try
                        {
                            _filterSource.SetContent(rawContent);

                            HttpRawUploadedContent filteredRawContent = new HttpRawUploadedContent(fileThreshold, rawContent.Length);
                            HttpApplication app = _context.ApplicationInstance;
                            byte[] buf = (app != null) ? app.EntityBuffer : new byte[8 * 1024];

                            for (; ; )
                            {
                                int bytesRead = _installedFilter.Read(buf, 0, buf.Length);
                                if (bytesRead == 0)
                                    break;
                                filteredRawContent.AddBytes(buf, 0, bytesRead);
                            }

                            filteredRawContent.DoneAddingBytes();
                            rawContent = filteredRawContent;
                        }
                        finally
                        {
                            _filterSource.SetContent(null);
                        }
                    }
                    catch
                    { // Protect against exception filters 
                        throw;
                    }
                }
            }

            _rawContent = rawContent;
            return _rawContent;
        }
 private HttpRawUploadedContent GetEntireRawContent()
 {
     if (this._wr == null)
     {
         return null;
     }
     if (this._rawContent == null)
     {
         HttpRuntimeSection httpRuntime = RuntimeConfig.GetConfig(this._context).HttpRuntime;
         int maxRequestLengthBytes = httpRuntime.MaxRequestLengthBytes;
         if (this.ContentLength > maxRequestLengthBytes)
         {
             if (!(this._wr is IIS7WorkerRequest))
             {
                 this.Response.CloseConnectionAfterError();
             }
             throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
         }
         int requestLengthDiskThresholdBytes = httpRuntime.RequestLengthDiskThresholdBytes;
         HttpRawUploadedContent data = new HttpRawUploadedContent(requestLengthDiskThresholdBytes, this.ContentLength);
         byte[] preloadedEntityBody = this._wr.GetPreloadedEntityBody();
         if (preloadedEntityBody != null)
         {
             this._wr.UpdateRequestCounters(preloadedEntityBody.Length);
             data.AddBytes(preloadedEntityBody, 0, preloadedEntityBody.Length);
         }
         if (!this._wr.IsEntireEntityBodyIsPreloaded())
         {
             int num3 = (this.ContentLength > 0) ? (this.ContentLength - data.Length) : 0x7fffffff;
             HttpApplication applicationInstance = this._context.ApplicationInstance;
             byte[] buffer = (applicationInstance != null) ? applicationInstance.EntityBuffer : new byte[0x2000];
             int length = data.Length;
             while (num3 > 0)
             {
                 int size = buffer.Length;
                 if (size > num3)
                 {
                     size = num3;
                 }
                 int bytesIn = this._wr.ReadEntityBody(buffer, size);
                 if (bytesIn <= 0)
                 {
                     break;
                 }
                 this._wr.UpdateRequestCounters(bytesIn);
                 this.NeedToInsertEntityBody = true;
                 data.AddBytes(buffer, 0, bytesIn);
                 num3 -= bytesIn;
                 length += bytesIn;
                 if (length > maxRequestLengthBytes)
                 {
                     throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
                 }
             }
         }
         data.DoneAddingBytes();
         if ((this._installedFilter != null) && (data.Length > 0))
         {
             try
             {
                 try
                 {
                     this._filterSource.SetContent(data);
                     HttpRawUploadedContent content2 = new HttpRawUploadedContent(requestLengthDiskThresholdBytes, data.Length);
                     HttpApplication application2 = this._context.ApplicationInstance;
                     byte[] buffer3 = (application2 != null) ? application2.EntityBuffer : new byte[0x2000];
                     while (true)
                     {
                         int num7 = this._installedFilter.Read(buffer3, 0, buffer3.Length);
                         if (num7 == 0)
                         {
                             break;
                         }
                         content2.AddBytes(buffer3, 0, num7);
                     }
                     content2.DoneAddingBytes();
                     data = content2;
                 }
                 finally
                 {
                     this._filterSource.SetContent(null);
                 }
             }
             catch
             {
                 throw;
             }
         }
         this._rawContent = data;
     }
     return this._rawContent;
 }