Last active
November 3, 2023 23:20
-
-
Save jainvishal/64efa5534fd93fad504495304168ffd3 to your computer and use it in GitHub Desktop.
First C++ Apache Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LoadFile /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | |
LoadModule foo_module modules/mod_foo.so | |
<Location /foo> | |
SetHandler foo | |
</Location> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 itsudo a2enconf foo
.All worked perfectly!