Small http server c++

Hi all,

I want to add a small http server to my project.

I am looking for the smallest possible **working** C++ code for an http server. My requirements:

1. Source code must have no external dependencies dependencies that do not come pre-installed on ubuntu
2. Must work on Ubuntu.
3. As small and simple as possible. I will be modifying it heavily. No need for security, encryption, no fancy stuff.


What would you recommend? Also, I know next to nothing of how http servers work, would you recommend a quick tutorial (hopefully less-than-an-hour-one)?

***
At the moment I am using an apache webserver, calling my application using CGI. However, I need a more detailed communication with the web browser: I am doing a large computation, and need to display various counters and progress reports. The computation may take upwards of a day, with multiple phases and a number of progress reports. I need to make those run smoothly (without any of the built-in timeouts and safeguards of the apache server).

Last but not least, maintaining the apache configuration has been a royal pain in the butt: every Ubuntu update and every new machine breaks my configuration. Getting my own http server will save me from all that trouble.

Thanks for your advice!
Cheers,
tition

Last edited on
Small != Fast. Try the Nginx server.
Sounds like investing some time in system and server administration would solve your problems faster, but as far as the smallest C++ HTTP servers are concerned, there are five in the examples section of boost.asio (google tells me on Ubuntu, boost is in libboost-all-dev): four in the C++03 section http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.http_server
and one more in
http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp11_examples.html#boost_asio.examples.cpp11_examples.http_server
Last edited on
An alternative to using a tiny webserver so you can customize it, is to go the other way--use conventional (defacto) standards.

In your case, you want something that runs in the background doing the calculations, but an HTTP interface that provides access to counters.

You can do this using custom pages that query the running "thing" and displays the results, or you could provide a REST interface to allow programmatic access to the counters.
Last edited on
I have to agree with Cubbi. What you're asking sounds like the job for a script to me.

However, if you are going to use C++, I would look into libmicrohttpd [ https://www.gnu.org/software/libmicrohttpd/ ]. I'm not sure if it's pre-installed in Ubuntu now, but I know there are a lot of commonly-installed applications that use it as a dependency. It's a lot faster and lighter than non-C implementations I've used before. I'd give it a good look.

I don't know what your end goal consists of really, but any incarnation of libwebkitgtk may also be more what you're looking for - instead of an actual server (i.e. you are reading the reports on the machine rather than serving them to read elsewhere). It is installed by default on some flavors of Ubuntu and Debian, but it's not preinstalled on every implementation (I don't even know about the default Unity-using Ubuntu). It lets you create a fully-featured browser with a couple lines of code, and it is still relatively fast. I've used this before to make reports, and it's very flexible. I know that Linux Mint (an Ubuntu derivative) uses libwebkitgtk inside it's login screen manager, MDM. It lets your reports be made in an HTML format and styled using CSS. Events can be handled in either C or JavaScript (or even other languages if needed).

I don't _think_ libwebkitgtk is what your looking for, but I'd definitely give libmicrohttpd a good look.
how about cgi-bin binary with apache.
Write code in any language c or c++ and let apache call it as cgi-binary

following is sample c-code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <string.h>
#include <stdlib.h>     /* atexit() getenv() setenv() */
#include <unistd.h>

extern char** environ;

int main(int argc, char* argv[])
{
    char *query_string;
    char *remote_addr;
    unsigned int i;
    char buf[BUFSIZ];

    printf("Content-type: text/html\r\n\r\n");

    printf("number of args are: %d<br>\n", argc);
    printf("reading from stdin<br>\n");
    i = 0;
    while (fgets(buf, BUFSIZ, stdin))
    {
        printf("stdin %02d: %s<br>\n", ++i, buf);
    }

    if (!(query_string = getenv("QUERY_STRING")))
    {
        printf("QUERY_STRING not found<br>\n");
        /*return 1;*/
    }

    if (!(remote_addr = getenv("REMOTE_ADDR")))
    {
        printf("REMOTE_ADDR not found<br>\n");
        /*return 1;*/
    }

    while (*++environ != NULL)
    {
        printf("variable: %s<br>\n", *environ);
    }

    return 0;
}
Last edited on
there is also pythons SimpleHTTPServer and CGIHTTPServer if you want to use pythons cgi
Civetweb (based on Mongoose) is a C (with C++ bindings), cross-platform, embeddable or standalone webserver implementation. It supports HTTPS, WebSockets, CGI, WebDAV, etc. It can also function as an HTTP client. The interface is pretty comprehensive and easy to use (adding request handlers, rewriting URLs, etc). Comes complete with a Makefile for building standalone, static library, shared library, docs, etc. It's MIT licensed, so you can use it in commercial projects.

https://github.com/bel2125/civetweb

PS: I'm not a marketer (or even a developer) of Civetweb, I just really like the project.
Last edited on
Topic archived. No new replies allowed.