maxRequestLength - 幕后需要的细节!

时间:2011-01-25 23:38:48

标签: asp.net iis

当文件上传时,当上传长度超过配置的maxRequestLength时,IIS中当前正在执行的请求会发生什么。 很难找到一篇体面的文章谈论这个,但没有!!!

由于

1 个答案:

答案 0 :(得分:2)

这是到底发生了什么:

在课程HttpRequest和方法GetEntireRawContent中检查此条件并引发异常:

if (length > maxRequestLengthBytes)
{
    throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
}

如果您觉得有用,这是整个方法:

    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;
    }