I am trying to understand a particular web architecture.
There is a cgi web application which works this way.
The request from web browser comes to the apache web server which is then routed to an application dispatcher.
Application dispatcher has a config file which has all the possibles commands and its corresponding handler methods.
Say, if there is a cmd=_login request from the browser, dispatcher looks for config file which may have this entry.
Command: _login
Handler: handle_login()
Header: login.h
Binary: Exe_name
So, what are the ways by which, application dispatcher can fork the cgi binary (Exe_name) and directly invoke handle_login() function?
Can a particular method be directly invoked?
There are basically two approaches to doing symbol resolution.
Built-in symbol resolution.
Depending on your system, you may be able to look up regular functions by name in a hidden symbol table used at run-time. For example, the
dlopen()anddlsym()on UNIX-like systems allow you to load a shared object library and resolve functions in that library by name. Windows has similarly behaving functionsLoadLibrary()andGetProcAddress()for dynamically linked libraries (DLLs). You have to pay attention to the concept of visibility. Functions must be exported by the library in order to be resolved using this mechanism. I believe GCC creates exports all functions by default (visibility is public) and Windows compilers export nothing by default. Also note that on Windows, you can resolve functions in the host program without loading an external library because the null library handle designates the host process’s executable image.In general, this is full of subtleties and not portable. I don’t recomment you build a system based on this mechanism.
Manual symbol resolution: routes
The modern way of mapping URLs (or parts of URLs) to functions is to build URL routes. This approached is adopted by most modern web frameworks based on the MVC paradigm (Ruby on Rails, Django and ASP.NET MVC to name a few — there are many others).
This basically consists in building a map of names (or regular expressions) to callbacks (in C++, this would be a pointer to function or a
std::function<>object). If your compiler supports the up and coming standard, you should be able to populate astd::map< std::regex,std::function<void()>>object to implement the look-up.Here is a simple (untested) example: