Development of sites (web applications) in C ++ (and not only) in the form of plug-in libraries (* .so, * .dll)

Introduction (lyrics)

About two years I was engaged in web development, creating sites and web applications in PHP. That's just in web development, I got a combination of very strange life circumstances. Not to say that it was not interesting to me - it was just very interesting for me to find out how websites are created and how the Internet generally works.

But at the same time, I was always attracted to low-level programming. Even during my studies, I really liked the C ++ programming language. Only there was nowhere to use it, except for its entertainment. Then I went down - I studied Assembler. I understood how the processor works (albeit too superficially) and how the programs actually run.

With all this set of knowledge and experience, I got into web development. At first everything seemed very, very good, it turned out to be much simpler than I thought. And over time, it became boring, it became too simple, uninteresting, there is no room for optimizations and interesting solutions. You generate web pages, write and connect js-scripts, arrange pages using css. I felt that I was no longer developing as a programmer.

At that time, questions began to torment me:

  1. Why can't I create high-performance web applications in a language like C ++ (not CGI)? After all, I like this language more than anyone else. I have never heard that there are sites written in C ++. Why?
  2. Why did scripting (interpreted) programming languages ​​capture web development?

With interpreted languages, of course, everything is clear. They are very convenient - no need to reassemble the entire project after making changes. In this case, all changes are applied on the fly, without rebooting (stopping) the web application.

I was looking for the answer to the first question - how to write sites in C ++. I didn’t find anything sensible on the Internet (only through CGI). And horrified: how so? I want to be free to choose a development tool, I want to use the language that I like. And so far no one has done anything? Or did, but uses only at home?

This made me start developing my own web server, where web applications (sites) would connect as libraries (* .so, * .dll).

(During development, I became aware and understood how to use the HTTP protocol correctly - I learned what RESTful is. I learned how to build the architecture of web applications correctly. I was surprised how many sites do not meet RESTful requirements, that is, in fact, they work incorrectly) .


For a year of working on my own web server (which I write in C ++ of the most fashionable new standards), I managed to implement all the basic necessary functions:

  • Sending files (by setting the X-Sendfile header);
  • sending generated by the application HTML pages;
  • understanding and processing of partial GET requests;
  • receiving data from forms (files and text) in various forms (application / x-www-form-urlencoded, multipart / form-data) and transferring them to the application in a finished form;
  • support for Keep-Alive connections;
  • Support for Upgrade connections (WebSocket).

Supported Operating Systems: Linux, Windows

How does my web server work

The web server at startup connects to itself the libraries specified in the configuration files. Example:

server {
	listen 2280;
	server_name servertest www.servertest;
	server_module /media/projects/sites/servertest/module/;
	server_module_update /media/projects/httpserverapp/httpserverapp/bin/Release/;
	root_dir /media/projects/sites/servertest/www/;
	request_max_size 10485760;

  • listen - port for receiving requests to the application;
  • server_name - names of the web application (site);
  • server_module - the path to the library that implements the functionality of the web application;
  • server_module_update - path to the web application library of the new version, used to update the web application without restarting the web server itself (almost on the fly);
  • root_dir - the root directory of the site files (web applications);
  • request_max_size - the maximum size of the request data (in bytes), if the data size in the request is larger, the request is ignored.

When connecting a library with a web application, it searches for functions in it:

  • application_call - a function that is called to process each request;
  • application_clear - the function is called to free the memory allocated for the headers that come from the web application to the web server;
  • application_init - the function is executed when the library is loaded successfully, [optional];
  • application_final - executed when the web server shuts down, [optional];

The description shows that application_call is the main function of the web application, the entry point. All headers (disassembled separately), request data (URI - resource identifier (query string), request parameters "? Var1 = sample & var2 = 1000" in an unassembled form), file links (if transferred), web application data are transferred to it (root directory). Each request is processed and executed in a separate thread.

The peculiarity is that the client socket is also transmitted, with which you can (need) to work directly.

The main work on the development of the web server can be said to be completed. Already, the developer can create his own web applications in any programming language, the compilers of which allow you to assemble the project in the form of a library. For example, C and C ++.

Of course, you will need your own server (computer) to run such a web server (program) on it and enjoy the speed of web applications. Just remember that using a database dramatically reduces the performance of any application. But not so much as to completely abandon the use of compiled languages.

Want to try?

The source code of the web server and an example application are attached to it (used by the IDE - Monodevelop).

The web server is licensed under the AGPL.

What's next?

The web server is operational, but there is still work to do (bring the code of some functions in order, write comments on the code).

Further, I would like to analyze and describe the structure of the web application in more detail, describe where to start development, for those who may be interested.

Now I am writing a framework for the convenient development of C ++ web applications for my web server (not yet available). At the very same framework, I’m developing a website for one company. Normal site - nothing remarkable, but with optimizations. I use a DBMS MariaDB.

Also popular now: