Author: gonzalo
Date: 2005-06-10 01:14:06 -0400 (Fri, 10 Jun 2005)
New Revision: 45733

Modified:
   trunk/mcs/class/System.Web/System.Web/ChangeLog
   trunk/mcs/class/System.Web/System.Web/HttpRequest.cs
Log:
2005-06-10 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>

        * HttpRequest.cs: avoid one intermediate buffer when reading the request
        body and fail for over limit content length when the content type is
        not handled as form or multipart data.



Modified: trunk/mcs/class/System.Web/System.Web/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web/ChangeLog     2005-06-10 04:57:38 UTC 
(rev 45732)
+++ trunk/mcs/class/System.Web/System.Web/ChangeLog     2005-06-10 05:14:06 UTC 
(rev 45733)
@@ -1,3 +1,9 @@
+2005-06-10 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
+
+       * HttpRequest.cs: avoid one intermediate buffer when reading the request
+       body and fail for over limit content length when the content type is
+       not handled as form or multipart data.
+
 2005-06-09 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
 
        * HttpResponse.cs: fix the check in Close() so that CloseConnection is

Modified: trunk/mcs/class/System.Web/System.Web/HttpRequest.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web/HttpRequest.cs        2005-06-10 
04:57:38 UTC (rev 45732)
+++ trunk/mcs/class/System.Web/System.Web/HttpRequest.cs        2005-06-10 
05:14:06 UTC (rev 45733)
@@ -224,9 +224,12 @@
                                _oFormData = new HttpValueCollection (data, 
true, enc);
                                return;
                        }
+
                        if (!ContentType.StartsWith ("multipart/form-data")) {
                                if (contentType.Length > 0)
                                        Console.WriteLine ("Content-Type -> {0} 
not supported", contentType);
+
+                               GetRawContent (); // at least, read the data 
and check length validity
                                _oFormData = new HttpValueCollection ();
                                return;
                        }
@@ -268,37 +271,38 @@
                        int length = ContentLength;
                        HttpRuntimeConfig cfg = (HttpRuntimeConfig) 
_oContext.GetConfig ("system.web/httpRuntime");
                        int maxRequestLength = cfg.MaxRequestLength * 1024;
-                       if (ContentLength > maxRequestLength)
+                       if (length > maxRequestLength) {
                                throw new HttpException (400, "Maximum request 
length exceeded.");
+                       }
                                
                        if (_WorkerRequest.IsEntireEntityBodyIsPreloaded () || 
length <= _arrRawContent.Length)
                                return _arrRawContent;
 
+                       byte [] result = new byte [length];
+                       int offset = _arrRawContent.Length;
+                       Buffer.BlockCopy (_arrRawContent, 0, result, 0, offset);
+
+                       int read = 0;
                        byte [] arrBuffer = new byte [Math.Min (16384, length)];
-                       MemoryStream ms = new MemoryStream (arrBuffer.Length);
-                       ms.Write (_arrRawContent, 0, _arrRawContent.Length);
-                       int read = 0;
                        int bufLength = arrBuffer.Length;
-                       for (int loaded = _arrRawContent.Length; loaded < 
length; loaded += read) {
-                               if (length - loaded < bufLength)
-                                       bufLength = length - loaded;
+                       for (; offset < length; offset += read) {
+                               if (length - offset < bufLength)
+                                       bufLength = length - offset;
 
                                read = _WorkerRequest.ReadEntityBody 
(arrBuffer, bufLength);
                                if (read == 0 ||read == -1 )
                                        break;
 
-                               if (ContentLength > maxRequestLength || 
ms.Length + read > maxRequestLength)
+                               if (length > maxRequestLength || offset + read 
> maxRequestLength)
                                        throw new HttpException (400, "Maximum 
request length exceeded.");
 
-                               ms.Write (arrBuffer, 0, read);
+                               Buffer.BlockCopy (arrBuffer, 0, result, offset, 
read);
                        }
 
-                       byte [] msBuffer = ms.GetBuffer ();
-                       if (msBuffer.Length == length)
-                               _arrRawContent = msBuffer;
-                       else
-                               _arrRawContent = ms.ToArray ();
+                       if (offset < length)
+                               throw new HttpException (400, "Data length is 
shorter than Content-Length.");
 
+                       _arrRawContent = result;
                        if (userFilter != null) {
                                requestFilter.Set (_arrRawContent, 0, 
_arrRawContent.Length);
                                int userLength = Convert.ToInt32 
(userFilter.Length - userFilter.Position);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to