|
fastcgi++
A C++ FastCGI/Web API
|
Our goal here will be to walk through the helloworld.fcgi application that responds to clients with a "Hello World" in five different languages. It covers the following topics:
This example can be built with a simple
make helloworld.fcgi
The first step is to make our request handling class. It must at minimum:
First things first let's decide on what kind of character set we will use. Obviously with all these different languages we can't use something like ISO-8859-1. Our only option is unicode and in particular utf-8. The way this library handles unicode might be different than some are used to. All internal characters are wide. This way we don't have to mess around with variable size characters in our program. A string with 10 wchar_ts is ten characters long. Not up in the air as it is with utf-8. Anyway, moving right along, the streams will code convert all the wide characters to utf-8 before it is sent out to the client. This way we get the best of both worlds.
So, whenever we are going to use utf-8, our template parameter for Fastcgipp::Request<charT> should be wchar_t. Keep in mind that this suddenly makes everything wide character and unicode compatible. Including HTTP header data (cookies, urls, yada-yada).
Now we need to write the code that actually handles the request. Quite simply, all we need to do is derive from Fastcgipp::Request and define the Fastcgipp::Request::response() function. Since we've decided to use wide Unicode characters, we need to pass wchar_t as the template parameter to the Request class as opposed to char.
Now we can define our response function. It is this function that is called to generate a response for the client.
Any data we want to send to the client just get's inserted into the requests "out" stream. It works just the same as std::cout in almost every way. We'll start by outputting an HTTP header to the client. Note the "charset=utf-8" and keep in mind that proper HTTP headers are to be terminated with "\r\n\r\n"; not just "\n\n".
Now we're ready to insert all the HTML data into the stream.
And we're basically done defining our response! All we need to do is return a boolean value. Always return true if you are done. This will let apache and the manager know we are done so they can destroy the request and free it's resources. Return false if you are not finished but want to relinquish control and allow other requests to operate. You would do this if the request needed to wait for a message to be passed back to it through the task manager.
Now we need to make our main() function. First we need to create a Fastcgipp::Manager object with the new class we made as a template parameter.
We need to do the following to ensure the manager is tied to signals that the web server may send.
The default Manager::listen() call makes the manager listen for incoming connections on the default FastCGI socket.
This tells the manager to start it's worker threads.
Now we just pause execution until the manager threads have completed.
1.8.14