Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.BinaryRead(Request.TotalBytes) throws error for large files

I have code that accepts binary data via POST and reads in an array of bytes. For files larger than 200 Kb, the operation fails. I've checked with my sysadmin (we're running IIS 7) to see if there was a limit in our configuration and he says there is none, and suspects it is a problem with the code. Does anybody here see any potential problems? Here is my code:

Public Sub Initialize
  If Request.TotalBytes > 0 Then
    Dim binData
      binData = Request.BinaryRead(Request.TotalBytes) ' This line fails'
      getData binData
  End If
End Sub


 Private Sub getData(rawData)
    Dim separator 
      separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)

    Dim lenSeparator
      lenSeparator = LenB(separator)

    Dim currentPos
      currentPos = 1
    Dim inStrByte
      inStrByte = 1
    Dim value, mValue
    Dim tempValue
      tempValue = ""

    While inStrByte > 0
      inStrByte = InStrB(currentPos, rawData, separator)
      mValue = inStrByte - currentPos

      If mValue > 1 Then
        value = MidB(rawData, currentPos, mValue)

        Dim begPos, endPos, midValue, nValue
        Dim intDict
          Set intDict = Server.CreateObject("Scripting.Dictionary")

          begPos = 1 + InStrB(1, value, ChrB(34))
          endPos = InStrB(begPos + 1, value, ChrB(34))
          nValue = endPos

        Dim nameN
          nameN = MidB(value, begPos, endPos - begPos)

        Dim nameValue, isValid
          isValid = True

          If InStrB(1, value, stringToByte("Content-Type")) > 1 Then

            begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
            endPos = InStrB(begPos + 1, value, ChrB(34))

            If endPos = 0 Then
              endPos = begPos + 1
              isValid = False
            End If

            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add "FileName", trim(byteToString(midValue))

          begPos = 14 + InStrB(endPos + 1, value, stringToByte("Content-Type:"))
          endPos = InStrB(begPos, value, ChrB(13))

            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add "ContentType", trim(byteToString(midValue))

            begPos = endPos + 4
            endPos = LenB(value)

            nameValue = MidB(value, begPos, ((endPos - begPos) - 1))
          Else
            nameValue = trim(byteToString(MidB(value, nValue + 5)))
          End If

          If isValid = True Then

            intDict.Add "Value", nameValue
            intDict.Add "Name", nameN

            dict.Add byteToString(nameN), intDict
          End If
      End If

      currentPos = lenSeparator + inStrByte
    Wend
  End Sub

Here is the error that appears in the logs:

Log Name:      Application    
Source:        Active Server Pages    
Date:          11/11/2010 2:15:35 PM    
Event ID:      5    
Task Category: None    
Level:         Error    
Keywords:      Classic    
User:          N/A    
Computer:      xxxxx.xxxxx.xxx    
Description:    
Error: File /path-to-file/loader.asp Line 36 Operation not Allowed. .    
Event Xml:    
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">    
  <System>    
    <Provider Name="Active Server Pages" />    
    <EventID Qualifiers="49152">5</EventID>    
    <Level>2</Level>    
    <Task>0</Task>    
    <Keywords>0x80000000000000</Keywords>    
    <TimeCreated SystemTime="2010-11-11T19:15:35.000Z" />    
    <EventRecordID>19323</EventRecordID>    
    <Channel>Application</Channel>    
    <Computer>PHSWEB524.partners.org</Computer>    
    <Security />    
  </System>    
  <EventData>    
    <Data>File /mghdev/loader.asp Line 36 Operation not Allowed. </Data>    
  </EventData>    
</Event>
like image 980
Adam Avatar asked Nov 17 '10 21:11

Adam


3 Answers

By default the limit for the entity size in a POST request is 200K, hence your error.

You can increase that limit open IIS Manager and navigate the tree to your application. Double click the "ASP" icon in the main panel. Expand the "Limits" category. Modify the "Maximum Requesting Entity Body Limit" to a larger value.

If this is for a public web-site be careful as to the limit you set, the purpose of the limit is to prevent malicious POSTs overwhelming the site.

like image 111
AnthonyWJones Avatar answered Sep 28 '22 20:09

AnthonyWJones


If you read the specifications of the BinaryRead method, you will see that the parameter is actually an out parameter as well. The BinaryRead method is trying to change the value of Request.TotalBytes which it can't do. TotalBytes is read-only.

You can easily fix this by assigning TotalBytes to a variable and passing that in instead. This is what the example code shows in the MSDN documentation.

If the BinaryRead read a different amount of data, the variable will reflect the size of the read.

like image 31
Adam Dymitruk Avatar answered Sep 28 '22 19:09

Adam Dymitruk


Two Settings are required in IIS under the "Limit Properties" section

1- Maximum Requesting Entity Body Limit (please not that it is in bytes). You have to set the value according to your maximum file size e-g- 40MB(40000000 bytes).

2)- Script Time-out . Its default value is "00:01:30: which is 90 seconds. Increase it according to the time required by your code to run. I set it to 5 minutes and it solved the problem.

like image 41
Ulfat Hussain Avatar answered Sep 28 '22 19:09

Ulfat Hussain