Skip to content
Advertisement

How to handle 0 Content length in POST request in CGI?

In my CGI application something happens when I pass a 0 content length POST request. Here is my code:

JavaScript

I test my application using the Http-Requester plugin for FireFox, when I pass a POST request with no data, the application seems like it enters a loop and no response. If I pass a GET request, the code works fine because len_ becomes NULL and it exits the if statement. If I pass a POST request with data, it works fine and receives the data well and saves it.

The case I can’t figure out is POST with CONTENT_LENGTH = 0. How to figure this case ? I tried strlen(len_) but it did not work. Thanks for the help.

Advertisement

Answer

Check if getenv returns NULL, e.g.:

JavaScript

Note that (as pointed out by @Deduplicator) is better to declare len as unsigned long or size_t because CONTENT_LENGTH (the number of bytes being sent by the client) is always positive.

Advertisement