开发者

How to FastCGI in C?

I have a website where each webpage is compiled into a binary (I have 100 webpages, therefore I have 100 binaries). Apache's .htaccess contains the line "SetHandler cgi-script" which instructs apache to use CGI when a binary (webpage) is requested.

How can I modify this website to use FastCGI instead of CGI ?

Do I just have to include this header and use this while loop (FastCGI.com) in each of the 100 binaries and modify .htaccess to "SetHandler fastcgi-script" ?

#include "fcgi_stdio.h" // instead of stdio.h  
while(FCGI_Accept() >= 0)  

So how will FastCGI work exactly ? Apache will dispatch 开发者_如何学JAVAwebpages using 1 persistent process for the entire website or will there be 1 persistent process for each of the 100 binaries ?


A FastCGI script is a network server that listens for connections in a loop. The web server forward requests to the FCGI server which sends back some dynamically generated content - all over a socket connection. Thus a FCGI script is faster than CGI as it is not re-spawned for each request.

I don't understand why you need 100 binaries for 100 pages. A single script is enough to generate content for 100 pages, based on some request parameter. The FCGI server should also scale pretty well for multiple connections as it is usually made to poll on the socket file descriptor. (Look at the code of the implementation to make sure of this).

To generate 100 pages you don't necessarily need 100 if statements. Consider this pseudo-code:

hash_table page_generators; // map page types to function objects (or function pointers)
page_generators["login_page"] = handle_login_page_fn; 
page_generators["contact_page"] = handle_contact_page_fn; 
// ... and so on

// request handler
page_type = request.get("page_type");
fn = page_generators[page_type];
if (fn == NULL)
    return "<html><body>Invalid request</body></html>";
else
    return fn(request);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜