domenica 18 ottobre 2009

How to retrieve data passed inside the request header

Usually the browser provide a lot of information inside the header of a http request: not only the requested page, but also cookies, POST or GET variables, the user-agent etc.etc. All this data are stored in an array of strings and are accessible trough an external variable: environ.

Taking inspiration from the echo.c example of the development kit I made a simple application that gives back all the information stored inside the header and the body of a request, similar to the phpinfo() function present in PHP.

If you want to test the program you can download a zip file here. With the C source there is also a HTML file useful to test different types of GET or POST requests, otherwise you can download the full FLUS environment here where you can test all the example of my blog connecting to the homepage of the server.

Note: under some conditions some data are passed also in the body section of the request.

References:
echo.c example of the Development Kit
URIs, Addressability, and the use of HTTP GET and POSTURIs, Addressability, and the use of HTTP GET and POST
Forms (W3C Reccomendation page)

venerdì 9 ottobre 2009

How to share data between different HTTP requests

An easy way to share data between different HTTP requests of the same FastCGI is declaring variables outside the main loop of the FastCGI. The following FastCGI, a simple web counter, use this technique to maintain into an integer the count of accesses:

#include "fcgi_stdio.h"
int main( int argc, char *argv[] )
{
int c = 1;
while( FCGI_Accept() >= 0 )
{
printf( "Content-Type: text/plain\r\n\r\n" );
printf( "%d\n", c );
}
return 0;
}

Assuming that the source has been named webcounter.c and application has been compiled (see this post) as webcounter into the /var/fastcgi directory, it’s necessary to add the following lines to the /etc/lighttpd/conf-available/10-fastcgi.conf file:

fastcgi.server += ( "/fastcgi/webcounter" =>;
((
"bin-path" => "/var/fastcgi/webcounter",
"max-procs" => 1,
"socket" => "/tmp/webcounter.socket",
"check-local" => "disable"
)))

then, after restarting lighttpd, we can try the counter at the URL http://ip-of-your-server/fastcgi/webcounter.

Only two notes about the configuration file: the “+” in the first added line before the “=” and the line “max-procs” => “1”, that force lighttpd to execute only one instance of the FastCGI to avoid malfunctions.

References: Structure and options of the FastCGIs’ configuration file