writing a web server embedded in an application

Started by
4 comments, last by Nercury 11 years, 1 month ago

Hey, for my job I need to write a C++ module that contains, basically, a web server. This module will be used by a couple of different processes in a networked application environment to communicate with each other. Basically, the module needs to

  1. Provide a general toolbox-style library for various processes that compose the application to communicate with each other using HTTP.
  2. The top-level of the application needs to actually serve web pages meaning handle GET requests coming from a browser.

I'm pretty new to this sort of thing and basically just signed up for this task because, for me, it is something totally different. I've been looking at what exists in open source -- I can use open source stuff with permissive licenses -- and so far have found Mongoose and libCurl but am looking for general guidance about how to approach this project.

Should I use Mongoose or just look at Mongoose's implementation? Has anyone worked on this sort of thing before? Any help or comments would be appreciated.

Advertisement

Is using C++ mandatory?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

For messaging between processes, ZeroMQ is pretty great! It doesn't do much HTTP, though.
For doing HTTP in C++, either find a library (like Mongoose,) or implement what you need on top of something like boost::asio which does things like line terminated reads already.
Note that the full HTTP protocol is very complex! Chunked transfer encoding, content transfer encoding, content type negotiation, cache headers, HEAD requests, OPTIONS requests, the list goes on and on. Even if you think *you* won't be sending those things, whoever is talking to you on the other end probably will, and you'd better be prepared for it.
If you control both the sender and receiver, and don't need to interoperate with other HTTP clients, then I highly recommend using ZeroMQ, or perhaps something like Thrift instead.
enum Bool { True, False, FileNotFound };

Is using C++ mandatory?

Yeah, I think it will be a requirement and I don't have control over that.

hplus0603, thanks I'll check out ZeroMQ which seems like it would work for the "messaging" portion of this but not the portion that needs to feed HTML to the UI which will be in a browser. I will control sender and receiver -- no one else will be talking to the components that I will implement a messaging system for. Within the distributed application the only communication will be communcation completely defined by me (and colleagues) but we won't control the top-level receiver in the sense that we won't implement it; it will be a browser. Basically this thing is a distributed application that a customer will install on an intranet that has a browser-based user interface.

Has anyone here used Mongoose? because looking at it briefly it seems to work as web server but I am not seeing anyway to make requests using Mongoose.

So, the system is a distributed system, and there is a user interface in HTML/JavaScript?
Couldn't you use whatever HTTP front-end you want (Apache, nginx, Python, J2EE, ...) and call out to the C++ from the front-end HTTP server when needed?
You can also serve a bit of HTML and JS from static files on disk, and serve some data-driven sub-path from the C++ part. There's some code in "istatd" that does just this to expose a UI to web browsers for the large database of time series counters it keeps. Check it out on https://github.com/imvu-open/istatd in the daemon/HttpServer and daemon/RequestInFlight pieces.

If this is your use case, you don't need to *make* HTTP requests at all from the C++ -- you could use ZeroMQ or Thrift or whatever for the internal communications. If you still have that requirement, libcurl is a solid, well-tested HTTP client library for C/C++.
enum Bool { True, False, FileNotFound };

Mongoose for http server looks the easiest option by far. Two files!

Has anyone here used Mongoose? because looking at it briefly it seems to work as web server but I am not seeing anyway to make requests using Mongoose.

Looks like you may plug PHP into Mongoose and then you can do whatever you like with it, have local DB in a file with SQLite or execute other remote requests, or even call localhost process written in another language. On the plus side PHP may be easier to understand to other developers who will maintain this code than a custom web server written with C++.

I am going to try to give some approximate idea of the performance versus time. If performance is super-critical - you need your fastest response to be quicker than 10 ms (without a DB), then you would write your web server with some socket library in C++, like mentioned boost::asio. But you are looking at more than 10 times longer development time compared to running a PHP installation, and a maintenance hell later. That is my humble estimation. Multiply it by Pi.

Of course I am pulling these numbers out of whatever. It depends on your task.

This topic is closed to new replies.

Advertisement