How can I host a C program in Apache?

I have a C program that is called with a system call in a Perl script. I was wondering: is there a way that I can actually place the C program inside Apache so that it can be controlled using the same rules that Apache set, such as timeout and memory?

+4
source share
4 answers

What have you tried and what does not work?

If it starts with:

printf("Content-type: text/html\r\n\r\n"); /* Or whatever the content type is */ 

... and then produces some output, and in your cgi-bin, it should work.

+4
source

I think you are looking for how to write an Apache module . They are limited by Apache settings, while, for example, CGI can do something. However, I think Apache can actually limit the use of CGI memory, for example.

(Not necessarily something bad, but you wanted to limit it in the Apache configuration?)

+2
source

I would recommend using the FastCGI protocol between your C program and Apache. The fastcgi development kit has an easy-to-use C API.

Here is an example FastCGI C program from the document:

 #include "fcgi_stdio.h" /* fcgi library; put it first*/ #include <stdlib.h> int count; void initialize(void) { count=0; } void main(void) { /* Initialization. */ initialize(); /* Response loop. */ while (FCGI_Accept() >= 0) { printf("Content-type: text/html\r\n" "\r\n" "<title>FastCGI Hello! (C, fcgi_stdio library)</title>" "<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>" "Request number %d running on host <i>%s</i>\n", ++count, getenv("SERVER_HOSTNAME")); } } 
+2
source

If you can turn your program into a library, you can use Inline :: C to connect it directly to your perl code. This will allow you to replace your system() call with a normal perl function call. You may have to deal with some sorting problems, but Inline :: C is a lot easier to work with XS.

+2
source

All Articles