I would like to know how should I handle multiple requests with one instance of a program, by that I mean, a fcgi program is supposed to continue running after one request has been answered, the problem is, how do I know that the current request data inside the environment variables is not the one from the last request.
My idea is to use setenv
to set the environment variables to NULL
after parsing them so when they are not NULL
it means that the server has set them to the values of the new request but I'm not sure if this is the way it is supposed to be done.
I know that there are libraries that handle this stuff and that it is safer to use those, but right now my objective is just to learn how fcgi works behind the libraries
It's not clear what do you mean by 'multiple requests' in your question.
Based on your description I assume that you expect that your FastCgi app is still alive after processing the first request and can handle another request. But that's the nature of FastCgi: a single program/service is running in an 'infinite loop' and handle all incoming requests. It's guaranteed by the FastCGI design that the Request object (including all environment variables) are properly set.
The old CGI works in an opposite way: a new process (i.e. instance) of the CGI program is spawned on each request.
It's highly likely that you keen on concurrent requests. But that's still possible. Unfortunately you haven't mentioned neither server type nor programming language nor OS which you work with.
It's really easy to find examples for handling concurrent requests on Unix systems in C/C++.
You've mentioned that you wouldn't like to use any libraries but I believe you have to use at least one which implements the FastCGI interface. The most commonly used is fcgiapp by Open Market. Handling concurrent requests is achieved by the multi-threading technique which is called Locks. I'm a 'Windows guy' so this is my example for WINAPI and C:
All the magic is around accept_mutex.
Hope that will help even if you use a different OS or a programming language