【问题标题】:maxRequestLength - behind the scene details needed!maxRequestLength - 需要幕后细节!
【发布时间】:2011-01-26 08:06:47
【问题描述】:

在文件上传期间,当上传长度超过配置的 maxRequestLength 时,IIS 中当前正在执行的请求究竟会发生什么。 努力找到一篇谈论这个的像样的文章,但没有!

谢谢

【问题讨论】:

  • 什么意思?到底发生了什么?抛出异常就是发生的事情。

标签: asp.net iis


【解决方案1】:

这正是发生的事情:

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

【讨论】:

  • 为什么它使用 applicationInstance.EntityBuffer 来缓冲 ReadEntityBody 的读取。这不会导致线程问题吗?还是 IIS 会串行处理这些请求?
猜你喜欢
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多