FastCGI Server C++ - c++

I am developing a server in C++, and I wanted to implement a FastCGI application that goes with it and handles the HTTP requests. I have looked at several examples of applications, but haven't seen anything about what I am supposed to do on the server side in order to communicate with the app.
Could anyone give me some insight on this? I have searched everywhere, even FastCGI website is down.

You can try fcgid, a very simple FastCGI Server implementation in C++. It's based on flup, a fastcgi server written in python.
Additionally, maybe digging into fastcgipp project can bring you some light. It's a FastCGI Client implementation. There are some good examples of use:
HelloWorld
Echo
Hope it helps.

Related

Deploy Go/Golang REST Web API on production

I am writing a backend web api for a mobile app. It should support HTTPS. Most of my experience in .NET, but for this one I want to use Go/Golang. I have a sample service ready, now I need to make sure that it is production ready.
In .NET I will just use IIS, but I have no clue what would be a good approach for Go.
Should I have nginx as reverse proxy, or I better use FastCGI ? And how to make sure that my go app is up and will run on system reboot ? should I use upstart or something similar ?
I've been using Nginx FastCGI with a Go webservice - they work well together. It's no harder to set up than HTTP reverse proxying - except for having to learn how to do it. The performance ought in principle to be a lot better, but I have no measurements to justify that hunch. My web service can work in both HTTP mode and FastCGI mode (one or other at a time), so I suppose I ought to do some benchmarking (note to self!).
If you want proper system startup (and you should), you need to learn how init scripts work. I sometimes cheat and start with an existing working script someone else wrote for a similar application and customise it to work with mine.
I've used nginx as a reverse proxy for my Go projects. I've found that it's a lot easier to set up useful server settings such as TLS, compression, etc., in nginx rather than as a pure Go server.
Keeping it alive on server reboot is a more complicated question. I would suggest learning how to write a script/whatever for your server's init daemon and just doing it that way.

Two way communication using AJAX from an HTML page to a C++ application running in same server

Is it possible to communicate from a web browser(Loaded an HTM page from server) to an application running in the same server using AJAX. Need to send the request from browser using a button click and update the page with responses received from one another application running in the same server machine?
I am using HTML pages to create website and not using any PHP or ASP like server side scripting. In server machine data are manipulated using a C++ application.
I think you can use any sort of Javascript functions to do that. But you might need to use jQuery or similar frameworks to make your live easier. You might need to search for "Comet Programming" to know exactly how to do 2-way communication between client and server
Updated:
Well, this kind of stuff requires you to read a lot (if you have not already known). Basically, what you need is a server that can do long-polling (or eventsource, websockets). There are many open-source ones that might help you to get started. I can list a several good ones here. There are a lot more
http://www.ape-project.org/
http://cometd.org/
http://socket.io/
http://code.google.com/p/erlycomet/
http://faye.jcoglan.com/
So after you have the comet server up and running you will need to setup the client side (probably Javascript). For those listed projects, most of them come with the client side code to interact with the server (Except for erlycomet). Therefore, you can just use the examples provided and run a quick prototype. If you want to use your raspberry pi, you can use nodejs which provide a lot of ease for dealing with real-time communication (socket.io, faye). And lately, http://www.meteor.com/
I would think of the problem this way: you want to provide a web front end to an existing c++ application. To achieve this you need to think about how your web server communicates with your c++ application. Communication between the browser and web server can be thought of as a separate problem - as you say AJAX calls can be used, or maybe have a look at websockets.
Once you have your request in the web server you need to communicate it to the C++ application (and/or visa versa). This can be done a number of ways, e.g. sockets or RPC. I found this question here which has some good advice.

How do i call/send messages to a fastcgi app?

I created a fastcgi app using this (threaded) example. I like it and the interface. I have the app running and have nginx using it with no problem. Now i would like to communicate with the fastcgi applications from my own C/C++ applications. What library may I use to communicate with the app?
When i google fastcgi and C++ i get results on writing fastcgi apps in C. I dont see any code to call apps using the fastcgi interface in C. My fastcgi app is running, theres no server on this machine and i'd like to talk to it as something like nginx does.
You basically need to implement Web server side of the FastCGI interface defined in this specification. It is biased towards writing a FastCGI application, but offers enough detail about the protocol to aid Web server side implementation, too.
See also this documentation and mod_fastcgi sources.
Note however, that depending on your end goal, it may be a better and considerably easier solution to use a lightweight HTTP server with FastCGI support like lighttpd (see this list for more) and issue ordinary HTTP requests to the server. The latter can easily be accomplished by using an HTTP client library (see for example this site).

communication between javascript and C++ code through web sockets?

I have javascript (client - executed through node.js) and C++ (server) code running on Ubuntu (Linux) and I want this client-server to communicate with each other. Can somebody tell me how I can make C++ code work like a server or client using web socket? Basically, I want javascript code to send some data to C++ code, the C++ code will process on the data and return the result back to javascript code. I'm not sure if I this communication between javascript and C++ code can happen with out web socket. Any pointers in this direction would be of great help!
Thanks,
pats
I very recently started working on a C++ websocket library: https://github.com/szmoore/foxbox
An example of a websocket server is: https://github.com/szmoore/foxbox/blob/master/examples/wsserver.cpp
I also have an example of a JavaScript client.
Warnings: The library doesn't support TLS, is based around POSIX sockets, and is still in development and probably horribly insecure.
So, whilst shamelessly promoting my own library I will also point you at libwebsockets, a C library suggested in answer to this question: https://stackoverflow.com/questions/3916217/standalone-c-or-c-websocket-server-library?lq=1
You have several choices. I am assuming your C++ server already has a websocket server running on it but if not, get Mongoose or the non-GPL fork Civetweb. Both are tiny bits of c code you build into your C++ program to add webserver,including websockets, functionality.
In fact, civetweb comes with a websocket example.
However, you do not need a websocket, just an ordinary socket should do. You'll probably want to send the data in JSON format to makeit easy to consume by the javascript code.
I know this question was asked a LOOONG time ago, but here is a gist of my set up that allows to have a communication between my c++ and Javascript apps; if anyone stumbles on this question:
I use TCP/IP connections to set up communication between a C++ app and a Javascript (Typescript) app. ZeroMQ (0mq) library is perfect for that. On c++ side you have libs like zmqpp and on the JS side you have zeromq.js.
After the data is received in JS land, you can use Socket.IO if you would like to have that streaming data available in a browser. Generally, you would have to forward the data stream off 0mq through Socket.IO and then access it on the browser side.
Bonus: make the data format using Protobuf.
On the Javascript side you will probably want to use an XmlHttpRequest. This will cause the javascript to post an HTTP(s) request to your server. For the C++ server side, you can look at something like Pion for an embeddable HTTP server or if you want to link into a full web server like Apache, you can use Fast CGI to plug your server code into Apache.

Lightweight HTTP/HTTPS server in C++ (not C)

I need to build a lightweight http server for my application basically it's a server which listen to a port and outputs a status information on requests, https, other functionality. But I would like to know first if something like this existe in C++, for linux and open source.
Does anyone know a program like that?
Thanks.
EDIT: It should be able to support high load.
If you can use boost, the asio library provides an http example. It does not use SSL, but asio can use OpenSSL very easily.
If you want to handle high loads I would suggest following:
Use proper web server with all goodies it comes with like Lighttpd, Nginx or Apache (in that order).
It would do great job in serving static files and handle your application. And they are very lightweight.
Write an Application in C++ using proper web framework - CppCMS - that is designed for high loads
Connect Web Application to the server via FastCGI or SCGI protocol (in this order).
Disclaimer: I'm the author of CppCMS
A quick google search for "C++ web application framework" shows things called CppCMS and something else called WT. That might get you started.
Or, as Sam already answered: boost.asio comes with a HTTP example that may be sufficient if your needs are simple. (Real HTTP request handling is actually surprisingly complex: http://webmachine.basho.com/diagram.html )
See thttpd. Supposibly the fastest open source file server on all machines with a single CPU.
If not using HTTPS, it's about a two hour exercise to write a static file server.