Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching "Maximum request length exceeded"

I'm writing an upload function, and have problems catching "System.Web.HttpException: Maximum request length exceeded" with files larger than the specified max size in httpRuntimein web.config (max size set to 5120). I'm using a simple <input> for the file.

The problem is that the exception is thrown before the upload button's click-event, and the exception happens before my code is run. So how do I catch and handle the exception?

EDIT: The exception is thrown instantly, so I'm pretty sure it's not a timeout issue due to slow connections.

like image 818
Marcus L Avatar asked Mar 20 '09 09:03

Marcus L


People also ask

What does Maximum request length exceeded mean?

Large file uploads in ASP.NET The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."

What is MaxRequestLength?

The MaxRequestLength property specifies the limit for the buffering threshold of the input stream. For example, this limit can be used to prevent denial of service attacks that are caused by users who post large files to the server.


2 Answers

There is no easy way to catch such exception unfortunately. What I do is either override the OnError method at the page level or the Application_Error in global.asax, then check if it was a Max Request failure and, if so, transfer to an error page.

protected override void OnError(EventArgs e) .....   private void Application_Error(object sender, EventArgs e) {     if (GlobalHelper.IsMaxRequestExceededException(this.Server.GetLastError()))     {         this.Server.ClearError();         this.Server.Transfer("~/error/UploadTooLarge.aspx");     } } 

It's a hack but the code below works for me

const int TimedOutExceptionCode = -2147467259; public static bool IsMaxRequestExceededException(Exception e) {     // unhandled errors = caught at global.ascx level     // http exception = caught at page level      Exception main;     var unhandled = e as HttpUnhandledException;      if (unhandled != null && unhandled.ErrorCode == TimedOutExceptionCode)     {         main = unhandled.InnerException;     }     else     {         main = e;     }       var http = main as HttpException;      if (http != null && http.ErrorCode == TimedOutExceptionCode)     {         // hack: no real method of identifying if the error is max request exceeded as          // it is treated as a timeout exception         if (http.StackTrace.Contains("GetEntireRawContent"))         {             // MAX REQUEST HAS BEEN EXCEEDED             return true;         }     }      return false; } 
like image 180
Damien McGivern Avatar answered Oct 10 '22 09:10

Damien McGivern


As GateKiller said you need to change the maxRequestLength. You may also need to change the executionTimeout in case the upload speed is too slow. Note that you don't want either of these settings to be too big otherwise you'll be open to DOS attacks.

The default for the executionTimeout is 360 seconds or 6 minutes.

You can change the maxRequestLength and executionTimeout with the httpRuntime Element.

<?xml version="1.0" encoding="utf-8"?> <configuration>     <system.web>         <httpRuntime maxRequestLength="102400" executionTimeout="1200" />     </system.web> </configuration> 

EDIT:

If you want to handle the exception regardless then as has been stated already you'll need to handle it in Global.asax. Here's a link to a code example.

like image 29
Jonathan Parker Avatar answered Oct 10 '22 09:10

Jonathan Parker