Skip to content

Instantly share code, notes, and snippets.

@jainvishal
Last active November 3, 2023 23:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainvishal/64efa5534fd93fad504495304168ffd3 to your computer and use it in GitHub Desktop.
Save jainvishal/64efa5534fd93fad504495304168ffd3 to your computer and use it in GitHub Desktop.
First C++ Apache Module
#pragma once
#include <string>
using std::string;
#include "http_protocol.h"
class Foo {
public:
int RunHandler(request_rec *req) {
if (!req->handler || module_name != req->handler) return DECLINED;
ap_rputs(message.c_str(), req);
return OK;
}
private:
string module_name = "foo";
string message = "Hello world from " + module_name;
};
LoadFile /usr/lib/x86_64-linux-gnu/libstdc++.so.6
LoadModule foo_module modules/mod_foo.so
<Location /foo>
SetHandler foo
</Location>
vishalj@dreamland:~/src/foo$ make reload
make[1]: Entering directory '/home/vishalj/src/foo'
/home/vishalj/www/build/libtool --silent --mode=compile g++ -std=c++14 -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/home/vishalj/www/include -I. -I/home/vishalj/src/httpd/mybuild/srclib/apr/include -I/home/vishalj/src/httpd/srclib/apr/include -prefer-pic -c mod_foo.cpp && touch mod_foo.slo
/home/vishalj/www/build/libtool --silent --mode=link gcc -std=gnu99 -g -O2 -pthread -o mod_foo.la -rpath /home/vishalj/www/modules -module -avoid-version mod_foo.lo
/home/vishalj/www/build/libtool --silent --mode=install install mod_foo.la /home/vishalj/www/modules/
make[1]: Leaving directory '/home/vishalj/src/foo'
/home/vishalj/www/build/libtool --silent --mode=install install mod_foo.la /home/vishalj/www/modules/
~/www/bin/apachectl restart
vishalj@dreamland:~/src/foo$
##
## Makefile -- Build procedure for sample foo Apache module
## Autogenerated via ``apxs -n foo -g''.
##
builddir=.
top_srcdir=/home/vishalj/www
top_builddir=/home/vishalj/www
include /home/vishalj/www/build/special.mk
CXX=g++
CXXFLAGS=-std=c++14
# the used tools
APACHECTL=~/www/bin/apachectl
# additional defines, includes and libraries
#DEFS=-Dmy_define=my_value
#INCLUDES=-Imy/include/dir
#LIBS=-Lmy/lib/dir -lmylib
# the default target
all: local-shared-build
# install the shared object file into Apache
install: install-modules-yes
# cleanup
clean:
-rm -f mod_foo.o mod_foo.lo mod_foo.slo mod_foo.la
# simple test
test: reload
lynx -mime_header http://localhost/foo
# install and activate shared object by reloading Apache to
# force a reload of the shared object file
reload: install restart
# the general Apache start/restart/stop
# procedures
start:
$(APACHECTL) start
restart:
$(APACHECTL) restart
stop:
$(APACHECTL) stop
#include <memory>
#include "Foo.h"
#include "http_config.h"
auto foo = std::make_unique<Foo>();
extern "C" {
void foo_hooks(apr_pool_t* pool);
module AP_MODULE_DECLARE_DATA foo_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
foo_hooks
};
};
// Apache callback to register our hooks
void foo_hooks(apr_pool_t* pool) {
ap_hook_handler(
[](request_rec *req) {
return foo->RunHandler(req);
},
NULL, NULL, APR_HOOK_MIDDLE);
}
@farhangamary
Copy link

Thanks!

I just needed to adjust the httpd.conf to add the complete path of the generated shred object, which was /usr/lib/apache2/modules/mod_foo.so , I also moved the httpd.conf to /etc/apache2/conf-available/foo.conf and then enabled it sudo a2enconf foo.
All worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment