Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding apache request to a c++ program

I am basically looking for tips and tricks on how to approach that problem.

I have a Server Software (Linux), which is written in C++. What I need to do is to provide some information, that is generated inside that software, via a http call to the apache webserver.

Of course it would be possible to store the data in the database and write a servlet for it, or use IPC to get the data, but I want to keep it simple and bundled in one file. And also I want to know if the idea would work.

So the workflow would look like: Client -> Apache Webserver -> C++ Software

Is there any existing library or something that could handle the Apache -> C++ connection? Or would it be something like just redirecting the request to a socket in c++ and do it manually? Basically something like the tomcat connector.

EDIT

Please note that the server software is running permanently in the background, and should not be "started" by the call

like image 690
Lukas Obermann Avatar asked Feb 15 '13 10:02

Lukas Obermann


1 Answers

There are a few options.

CGI is simple and has been around forever. Under CGI, the web server would spawn a separate process for every web request. As you mentioned in your comment, you could write a CGI script that makes RPC calls to your C++ program.

FastCGI is an alternative to CGI; instead of spawning a separate process for every web request, it defines a protocol for letting the web server dispatch multiple web requests to a single long-running process. It works quite well for web applications. However, for your scenario, where you have a preexisting server process that needs to add a web interface, it may not work as well; based on my limited understanding, web servers typically expect to start and stop the long-running FastCGI processes themselves (in response to incoming requests, server load, idle time, etc.) instead of connecting to preexisting FastCGI processes. (Most servers would let you reconfigure this, I think, but it's not the default.)

You can also always embed a web server like Mongoose or cpp-netlib in your C++ process and set up Apache to proxy requests to it. This might be your best approach. (Mongoose, for example, is extremely easy to embed.)

Finally, you can use a full-fledged Apache module (either redesign your C++ server as an Apache module or have an Apache module to communicate with your C++ service). This is probably more complicated than you want to do, although there are existing projects like CPPSERV that take this approach.

like image 179
Josh Kelley Avatar answered Nov 09 '22 08:11

Josh Kelley