Which C++ Library for CGI Programming? - c++

I'm looking at doing some work (for fun) in a compiled language to run some simple tests and benchmarks against php.
Basically I'd like to see what other people use for C++ CGI programming. (Including backend database, like mysql++ or something else)

I'm not sure exactly what you're looking for, but there is a C++ web framework called wt (pronounced "witty"). It's been kept pretty much up to date and if you want robust C++ server-side code, this is probably what you're looking for.
You can check it out and read more at the wt homepage.
P.S. You may have some trouble installing wt if you don't have experience with *nix or C++ libraries. There are walkthroughs but since frameworks like these are the road less traveled, expect to hit a few bumps.

Another option is the Cgicc library which appears to be mature (currently at version 3.x):
http://www.gnu.org/software/cgicc/

If I were thinking of working at that level, I'd probably just write a straight-up Apache or IIS module instead of a CGI.
That said, if you do want to go with CGI, I'd suggest using the venerable cgic from Thomas Boutell. It's a "plain" C library, but it's been in constant use since the mid '90s so it's thoroughly tested and solid as a rock.

Check out Boost's C++ CGI class, which is not a part of boost yet.

In short, I don't think there is such a thing for generic server CGI programming (happy to be proven wrong of course).
Instead you'll probably have to target the server APIs, such as Apache's. This looks like a reasonable introduction to request processing, which will be a big part of what you're doing.
As an alternative, Lighttpd may be even more developer-friendly, and (particularly if you're looking at performance) faster.
I note there's a cpp-netlib under development but it seems to be HTTP client only.

I've found very pleasant to use CppCMS to develop a Fast CGI app to deploy in a nginx server - although it never went in production =( . The CppCMS project also includes some libraries for SQL connectivity.

Related

Web App and c++

It's something totally new for me, and since it seems to be something trivial, I can't find any response with our beloved friend ggle...
My question is : I have to create a webapp that can work if the client have nothing on his computer but a browser. My question is : is it possible to include to my project an external library (here I want to use PCL (Point Cloud library), a C++ library), and make it works even if the client have nothing installed? If I use QtWebKit for example, will I be able to create this kind of webapp?
Thanks a lot, and sorry for the (maybe) stupid question, I never did web dev before, it's my first time. ...
Have a nice day!
Wow, that question is as broad as the day is long, but here goes (you don't give too much about your background so I apologise if I am going too basic).
A web browser is just a piece of software that knows how to process HTML, CSS and Javascript passed over HTTP from a TCP socket.
So, on a most basic level, to get something to work in a browser all you need is to write a program that listens on a TCP socket and passes headers and HTML in the body of the response as per the HTTP protocol.
So, leaving aside the socket part, your method may look something like this:
std::cout << "<h1>Hello, world!</h1>" << std::endl;
In fact, the vast majority of the original dynamic web sites worked just like this: they were just C programs that just happened to produce HTML as output in the old days (cgi-bin).
The main issue with this approach is it's very tedious and time consuming to write code to that level of detail to generate HTML, especially when you factor in CSS and JavaScript.
This is one of the key reasons that most web applications these days are implemented in Java, .Net, Ruby, PHP etc as there is a wealth of fully-featured, stable and mature frameworks for web development that take the grunt work out of creating dynamic web sites (and C/C++ is much rarer).
However there are a couple of good frameworks out there for C++ if that is definitely the language you want to use.
Wikipedia has a limited section on C++ frameworks but it covers the 2 I would suggest investigating (CppCMS and Wt):
https://en.wikipedia.org/wiki/Comparison_of_web_frameworks#C.2B.2B
Of these, I've only had direct experience of CppCMS but I can recommend it - it's approach is idiomatic C++ (to my eyes at least, but I must confess I am more often a Java web developer than C++)
Also, there is quite a useful if slightly dogmatic SO discussion on this thread that may be relevant:
How popular is C++ for making websites/web applications?

Web Services using C++

I am building a server-client application that involves heavy signal processing (e.g. FFT). I have a working application written in C++/Qt, where everything (signal processing and other calculations) is done in client and server just sends raw data. Now I feel it would be easier to implement these features on the server. So, that maintenance becomes easier.
As I am doing signal processing, I think I should stick to C++ for performance. But I am open to new ideas.
Constraints:
I need type checking so javascript is out of discussion.
Scaling includes adding more server and each server will have at the max
10-12 users. So, Hardware cost is important. I cannot use x number of
i7 processors.
No option of using cloud services.
So, right now my question is as follows:
How can I create web services using C++ for Linux server? (Although cross platform is not important, I would appreciate if I can achieve it.)
EDIT [02:09:2015]
Right now, I think the choice is between poco and C++ Rest SDK. I feel I should go for C++ Rest SDK. Mainly because it has only those features that I need. And Also it is supported by microsoft and uses boost internally. So, I feel in future, this might be well integreated with standard.
You could use cross-platform Poco library to implement HTTP server, it is really straightforward with this framework, and they have a lot of examples. You can also use JSON serialization (like rapidjson library) to implement REST service on top of HTTP - this way your Web service will be accesable by most of the modern Web frameworks.
You might want to take a look at the C++ Rest SDK, an open source, cross platform API from Microsoft.
Like #nogard suggested, I also recommend POCO for now. It's the most serious and feature-full solution. Given you mentioned Qt, I suggest you to take a look at Tufão.
EDIT:
I forgot to mention one comparison of mine on the C++ HTTP server frameworks.
If you directly handle HTTP requests, you might loose the functionality what Web Servers does well what it was build to do. I had a similar issue, what I did was wrap up my Qt c++ code inside a PHP extension. In your case you can do the same. Wrap your logic inside what ever technology you are about to use, doesn't matter it's PHP, net , Java or anything else.

Web Application Frameworks: C++ vs Python

I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.
Some things I'm looking for:
It has to be intuitive. I recognize that Wt exists and it follows the model of Qt. The one thing I hate about Qt is that they encourage strange syntax through obfuscated means (e.g. the "public slots:" idiom). If I'm going to write C++, I need it to be standard, recognizable, clean code. No fancy shmancy silliness that Qt provides.
The less non-C++ or Python code I have to write, the better. The thing about Django (Python web framework) is that it requires you pretty much write the HTML by hand. I think it would be great if HTML forms took more of a wxWidgets approach. Wt is close to this but follows the Qt model instead of wxWidgets.
I'm typically writing video games with C++ and I have no experience in web development. I want to write a nice web site for many reasons. I want it to be a learning experience, I want it to be fun, and I want to easily be able to concentrate on "fun stuff" (e.g. less boilerplate, more meat of the app).
Any tips for a newbie web developer? I'm guessing web app frameworks are the way to go, but it's just a matter of picking one.
I would go with Wt because:
You already know C++
It has a nice layout system, so you don't need to know lots of HTML
It is very well written and a pleasure to code in
Your deployed apps will handle 50 times the load of the python app on less hardware (from experience with pylons apps, 10,000 times the load of a plone app :P)
It has all the libraries that the guy in the first question says it doesn't and more
In built development webserver
Templating language
ORM
unit testing help
open-id and user+password authentication
A brilliant widget library
Web 2.0 isn't an after thought; it wasn't designed on a Request+Response model like all the python frameworks (as far as I know), but on an event driven interactive model.
It uses WebSockets if available
Falls back to normal ajax gracefully if not
Falls back to http for browsers like linx
It is more like coding a gui app than a web app, which is probably what you're used to
It is statically typed and therefore less error prone. Does def delete(id): take an int or a string ?
The unit tests (on my apps at least) take 10-100 times less time than my python app unit tests to run (including the compile time)
It has a strong and friendly community. All my email list posts are answered in 0-3 days.
If you'd like to avoid writing HTML, you could try GWT. However, in my experience, using an intermediate framework to generate HTML and ECMAScript never works anywhere near as well as hand-writing the pages.
[edit] nikow mentions in the comments that Pyjamas is a port of GWT to Python.
Regarding the language, if given the choice between C++ and Python I would pick Python 100% of the time. Even ignoring the obvious difference in abstraction between those languages, Python simply has more useful libraries than C++. You don't have to write your own development-oriented web server -- Django comes with one. You don't need to write a custom template library -- Python has Genshi. Django comes with a capable ORM layer, or for even more control you can use SQLAlchemy. It's barely a contest.
Django is good point to start web development it is great framework
If you look for C++ take a look on CppCMS, it is much more close to Django, it is not like Wt that mimics Qt.
In any case, it is really depends on your needs. C++ can be used for embedded or high performance web applications, but for medium range web sites Django would be better. (and I'm developer of CppCMS)
I think you better go firt python in your case, meanwhile you can extend cppCMS functionalities and write your own framework arround it.
wt was a good idea design, but somehow not that suitable.
If you are exploring Python frameworks (based on the excepted answer I think you are) I think you really owe it to yourself to check out CherryPy. When you write CherryPy apps, you really are just writing Python apps. The framework gets out of your way in a real hurry. Your free to choose your own templating, ORM (if you choose to use ORM), etc. Seriously, take 10 or 20 minutes and give it a look.
The only reason you might want to use C++ over Python is when speed is paramount.
If this is going to be your first web-app, you'll probably be ok with just Python, and your development speed will be orders of magnitude better than with CPP.
Django's templating language is far from powerless, to me it actually seems very pythonic. You actually can write pure python in a template(although this is generally not recommended).
Even better, it's possible to replace Django's templating system with the one you like.
My personal favourite language for this is HAML.
Here's some data on this:
Is there a HAML implementation for use with Python and Django
Having looked several ones, like django, pylos, web2py, wt. My recommendation is web2py. It's a python version of "ruby on rails" and easy to learn.

What works for web dev in C++

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 so it 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
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.
There's the Wt Project. It uses a paradigm similar to Qt's signals/slots.
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
ATL Server is a library of C++ classes that allow developers to build internet based applications.
ATL Server. It's open source too! And of course there is always ISAPI. Ah, the bad old days. :)
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?
Our web app backend is in C++ via CGI and we use Clearsilver templates along with the HDF that comes with it.
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.
You can try Cutelyst a C++11 built with Qt, with one of the best positions on TechEmpower Benchmarks.
Even though it requires Qt 5.6+ a full CMS (CMlyst) uses around 6MB of RAM while serving around 3000 requests per seconds on a single core.
And for your string manipulation issue QString is just an amazing class for that.

What tools do you use to create prototypes or mockups of webservices?

Ideally, and you will think I am crazy, I can code some basic logic into a bash or korn script and open that functionality up to clients hitting them. There is a lot of plumbing involved in web services and I was wondering what tools and techniques more experienced developers have been using to prototype systems where a backend webservice may not be yet available. Do such tools exist for bash and ksh? What languages are the easiest to develop mockups in. BTW I am staring at an ecplipse IDE feeling a bit disgusted by what I am looking at. I just now got JBOSS installed and running... at its heart all these services are are socket connections to the client through a port ... or am I mistaken?
BTW: I am currently reading through this. And my disgust is increasing.
I share your disgust with eclipse. A language like Python is perfect for rapid prototyping. If you combine it with one of the many web frameworks (Pylons or Django would be my recommendation for Python), the amount of work you can accomplish quickly is astounding.
Seriously consider some other scripting language. I've been using Python and Ruby to build quick prototypes, and been very happy with them.
If you're free to make this choice, consider using a REST architecture instead of a WSDL and SOAP solution. RPC has its place, but if you can live with the restrictions of REST, life will be much easier. Even if you can't, it's a lot quicker for prototyping to use a dynamic language.