RFC 3875 defines the CONTENT_LENGTH in this way:
The CONTENT_LENGTH variable contains the size of the message-body attached to the request, if any, in decimal number of octets. If no data is attached, then NULL (or unset).
CONTENT_LENGTH = "" | 1*digit
The server MUST set this meta-variable if and only if the request is accompanied by a message-body entity. The CONTENT_LENGTH value must reflect the length of the message-body after the server has removed any transfer-codings or content-codings.
I am not sure which size this is.
Is it the size of the data the clients wants to send, which means the value of the HTTP request header "Content-Length"?
Or is it the size of the data the HTTP server read from the request before the data is send to the CGI?
I ask because I am not sure who is responsible to check, that the amount of data, the client wants to send matches the amount of data actually arrived. Is the HTTP server responsible to check that the size matches the value in the request header, or is the CGI responsible to count the bytes, which arrive on STDIN? And if so how gets the CGI the value of the request header?
Right now I do this in my CGI:
cat > $TMP/upload.csv
size=$(stat -c %s "$TMP/upload.csv")
if [ "$CONTENT_LENGTH" != "$size" ]; then
echo "Size missmatch"
return -1
fi
But is it the right thing to do?