Complex Case (Large Amount of Variable Data)
To avoid the problem situation described in the previous topic, use a more complex, but very reliable way of handling large HTTP variable data.
The on_sock_postdata event is generated when there is data in the VAR buffer. In this regard, the event is similar in nature to the on_sock_data_arrival event, which is generated when the RX buffer of the socket has data. Unlike the on_sock_data_arrival event, on_sock_postdata will first be generated only after the VAR buffer becomes full. That is, you won't be bothered by this event unless the HTTP request processing simply can't continue without it.
OK, so now you have a chance to access and process the HTTP variable data as it arrives and before the reply phase even starts. The sock.gethttprqstring method, unlike the sock.httprqstring read-only property, actually removes the data from the VAR buffer, thus freeing up the buffer space. The diagram below illustrates the process:
Here is a modified login example:
In the event handler for the on_sock_postdata event we extract available HTTP variable data and process it. The event will be called as many times as necessary. For example, we may save the data into a file:
Sub On_sock_postdata()
fd.setdata(sock.gethttprqstring(255)) 'very simplified, but illustrates the point
End Sub
Then, in the HTML file, it is necessary to remember that a portion of the HTTP variable data may still be unhandled by the time you get here:
<html><body>
<?
While sock.httprqstring<>"" 'extract the remaining part of the variable data
fd.setdata(sock.gethttprqstring(255))
Wend
?>
</body></html>
It is unlikely that user login will require such careful handling. A user name and password will comfortably fit in 255 bytes, unless your users are paranoid and have humongous passwords. Still, there are many situations in which you need to send large variable data. For example, HTTP POST methods are routinely used to upload files to the web server and your device will be able to handle this, too.