views:

1185

answers:

9

Hello Stackoverflow,

I want to create a web application that runs with very little RAM and I think C++ can help me achieve that.

Now, many people say C++ is unsuited for web development because:

  • there is no easy string manipulation
  • is an unsafe language (overflows, etc)
  • long change/build/test cycles
  • etc

But I'm sure the C++ community have found ways to alleviate all those (maybe not the compile time) however since I'm not a regular is hard for me to put a value on what I find in google.

So I'm asking for some guidance. I would appreciate if you share what works, what tools/libs are current and alive. What strategies can help with web dev in C++? FastCGI or embedded server (Asio/POCO/Pion/etc) ? How do you address security concerns?

Thanks a lot for any help

--jbar.

+1  A: 

Give us some more hints about what you're trying to do.

You can write a good old-fashioned cgi program in C++ easily enough, and run it with FastCGI. We used to do that all the time.

You could write a C++ program embedding a lightweight HTTP server as well.

Both of them are much bigger PITAs than using something like perl or ruby.

So for why C++?

Update

Okay, got it. The main thing about FastCGI is that it avoids a fork-exec to run your CGI program, but it is a little bit different API. That's good, but you still have the problem of handling the HTTP stuff.

There are, however, several very lightweight HTTP servers, like Cherokee and Lighttpd. In similar situations (building web interfaces for appliances) I've seen people use one of these and run their C/C++ programs under them as a CGI. Lighttpd in particular seems to concentrate on making CGI-like stuff fast and efficient.

Another update. I just had cgicc pointed out to me: http://www.gnu.org/software/cgicc/

That might solve some problems.

Charlie Martin
Charlie, I'm trying to create an application that "scales down" as much as possible. That's why C++ and not Perl or Ruby. I understand that's not a mainstream thing, and a PITA. I'm not targeting an embedded system -- no Arduino for example -- but underpowered / virtual machines facing the public web -- so I need reasonable security. I though you could write FastCGI apps and run them as CGI but not the other way around.
As a matter of fact, the PITA part is the meat of this question. What are the pains and how people are handling them inside C++.
The biggest thing is that you haev to do something to write out your HTML. If it's all static, that's easy enough: you copy to stdout, badaboom. If you're generating a lot of dynamic output, you have to write code to generate it in HTML. There are probably class libraries to help do that, but I've never seen one.
Charlie Martin
+11  A: 

Have you looked at http://www.tntnet.org/. They have created a... well let me cut and paste from their website:

Tntnet is a modular, multithreaded, high performance webapplicationserver for C++. To create webapplications Tntnet has a template-language called ecpp similar to php, jsp or mason, where you can embed c++-code inside a html-page to generate active content. The ecpp-files are precompiled to c++-classes called components and compiled and linked into a shared library. This process is done at compiletime.

I've used it and it has quite a small overhead plus it has screamingly fast dynamic page generation. Makes PHP, Ruby etc snails in comparison because with tntnet you are running compiled C/C++ code.

gbrandt
Hello gbrandt, Yes I have looked at it, I followed the quick start guide and it was clear. How is it as far as security? -- my app would be facing the web, no embedded scenario. Since the web server is embedded I have to run it as root to grab port 80, then I would need a proxy. How that has worked for you? Is it actively maintained?
It has not been updated since I used it about 6 months ago, so not sure about the maintenance. It is full source code, but I suspect it would take a lot of work to figure out what was going on. It seems secure as long as you code securely, it does allow you to shoot yourself in your foot.
gbrandt
"...no embedded scenario. Since the web server is embedded..." - Huh?
Ed Swangren
+5  A: 

There's the Wt Project. It uses a paradigm similar to Qt's signals/slots.

Grant Limberg
Wt looks awesome, very complete, great work. However I'm trying to look for non GPL solutions first. Do you have experience with it?
Not much. I was looking into it for a contract project for a family member's company, but after we ran all the numbers on cost/profit margins, etc, we decided not to do the project.
Grant Limberg
+1  A: 

ATL Server is a library of C++ classes that allow developers to build internet based applications.

Pavel Chuchuva
+2  A: 

There is nothing wrong with trying to build a web app in C++. It's actually a lot of fun. What you need is a:

  • Templating system
  • A CGI lib
  • A database API wrapper, most likely, to avoid dealing with something like the low-level MySQL API
  • A logger

The ONLY problem is persistent database connections, or persistent anything, so your app would probably have to be the app server as well, so it's a daemon instead of a script that runs under Apache (for example).

Andrei Taranchenko
Thanks for the support! :)I was thinking that instead of storing anything locally I could just use Amazon S3. I don't know yet.How is your experience with cgicc? Did you choose it above other options for some reason or maybe you just needed to be consistent with existing code base?
I am not sure if CGICC is *the* answer but you definitely need a library to deal with the boring HTTP stuff, like cookies, headers, etc. It's been done before, so no need to re-invent the wheel. What I like about CGICC is that the interface is very nice, and very C++, the one that actually uses the STL :)
Andrei Taranchenko
+1  A: 

ATL Server. It's open source too! And of course there is always ISAPI. Ah, the bad old days. :)

JP Alioto
Yes but (from the site) : "The use of the code is limited to the Windows platform. "
+1  A: 

In your other question you mention that your embedded system is openwrt. As this router firmware already comes with a embedded web server (for it's administration UI), why don't you use that for you app as well?

lothar
Hello lothar, That other question is excellent, but not mine :)
They seem to be using BOA, I wonder is someone has some experience that can be shared about using it in a regular Linux distro facing the public web.
+1  A: 

Our web app backend is in C++ via CGI and we use Clearsilver templates along with the HDF that comes with it.

apphacker
I have found only good things said about Clearsilver, and only bad ones about CGI :)How did you find the -- allegedly terrible -- process creation overhead to be in practice?
Well the project started as CGI many years ago and we haven't gotten around to transitioning to something like FastCGI, but although we have a heavy load on our servers, our response is still very fast.
apphacker
A: 

libapache2-mod-raii is a complete framework with builtin database connexion pools, sessions and all the stuffs that makes developping web applications enjoying !

Guillaume Gimenez