From 21a066b4b972bd055b424a946ff1f80a939443c3 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 3 May 2020 12:01:13 +0200 Subject: Added redirect plugin, documentation --- plugins/redirect/Makefile | 124 ++++++++++++++++++++++++++++++++++++++++++ plugins/redirect/redirect.cpp | 64 ++++++++++++++++++++++ plugins/redirect/redirect.h | 21 +++++++ 3 files changed, 209 insertions(+) create mode 100644 plugins/redirect/Makefile create mode 100644 plugins/redirect/redirect.cpp create mode 100644 plugins/redirect/redirect.h (limited to 'plugins/redirect') diff --git a/plugins/redirect/Makefile b/plugins/redirect/Makefile new file mode 100644 index 0000000..aea8106 --- /dev/null +++ b/plugins/redirect/Makefile @@ -0,0 +1,124 @@ +DISTROS=debian10 +VERSION=$(shell dpkg-parsechangelog --show-field Version) +PROJECTNAME=redirect + +CXX=clang++-10 + +ifeq ($(shell which $(CXX)),) +CXX=clang++ +endif + +ifeq ($(shell which $(CXX)),) +CXX=g++-9 +endif + +ifeq ($(CXXFLAGS),) +#CXXFLAGS=-O2 -DNDEBUG +CXXFLAGS=-O0 -g -D_DEBUG +endif +# -fprofile-instr-generate -fcoverage-mapping +# gcc:--coverage + +CXXFLAGS+= -Wall -I. + +CXXFLAGS+= -pthread -fvisibility=hidden -fPIC +ifeq ($(CXX),clang++-10) +CXXFLAGS+=-std=c++20 #-stdlib=libc++ +else +CXXFLAGS+=-std=c++17 +endif + +CXXTESTFLAGS=-Igoogletest/include -Igooglemock/include/ -Igoogletest -Igooglemock + +LIBS=\ +-lboost_context \ +-lboost_coroutine \ +-lboost_program_options \ +-lboost_system \ +-lboost_thread \ +-lboost_filesystem \ +-lboost_regex \ +-lpthread \ +-lssl -lcrypto \ +-ldl + +ifeq ($(CXX),clang++-10) +LIBS+= \ +-fuse-ld=lld-10 \ +-lstdc++ +#-lc++ \ +#-lc++abi +#-lc++fs +#-lstdc++fs +else +LIBS+= \ +-lstdc++ \ +-lstdc++fs +endif + +PROGSRC=\ + redirect.cpp + +TESTSRC=\ + test-webserver.cpp \ + googlemock/src/gmock-all.cpp \ + googletest/src/gtest-all.cpp \ + $(PROGSRC) + +SRC=$(PROGSRC) + +all: $(PROJECTNAME).so + +# testsuite ---------------------------------------------- +test-$(PROJECTNAME): $(TESTSRC:.cpp=.o) + $(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@ + +$(PROJECTNAME).so: $(SRC:.cpp=.o) + $(CXX) -shared $(CXXFLAGS) $^ $(LIBS) -o $@ + +dep: $(TESTSRC:.cpp=.d) + +%.d: %.cpp + $(CXX) $(CXXFLAGS) $(CXXTESTFLAGS) -MM -MP -MF $@ -c $< + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) $(CXXTESTFLAGS) -c $< -o $@ + +googletest/src/%.o: googletest/src/%.cc + $(CXX) $(CXXFLAGS) $(CXXTESTFLAGS) -c $< -o $@ + +# dependencies + +ADD_DEP=Makefile + +install: + mkdir -p $(DESTDIR)/usr/lib/webserver/plugins + cp $(PROJECTNAME).so $(DESTDIR)/usr/lib/webserver/plugins + +# misc --------------------------------------------------- +deb: + # build binary deb package + dpkg-buildpackage -us -uc -rfakeroot + +deb-src: + dpkg-source -b . + +$(DISTROS): deb-src + sudo pbuilder build --basetgz /var/cache/pbuilder/$@.tgz --buildresult result/$@ ../webserver_$(VERSION).dsc ; \ + +debs: $(DISTROS) + +clean: + -rm -f test-$(PROJECTNAME) $(PROJECTNAME) + -find . -name '*.o' -o -name '*.so' -o -name '*.d' -o -name '*.gcno' -o -name '*.gcda' | xargs rm -f + +zip: clean + -rm -f ../$(PROJECTNAME).zip + zip -r ../$(PROJECTNAME).zip * + ls -l ../$(PROJECTNAME).zip + + + +.PHONY: clean all zip install deb deb-src debs all $(DISTROS) + +-include $(wildcard $(SRC:.cpp=.d)) diff --git a/plugins/redirect/redirect.cpp b/plugins/redirect/redirect.cpp new file mode 100644 index 0000000..91d5f64 --- /dev/null +++ b/plugins/redirect/redirect.cpp @@ -0,0 +1,64 @@ +#include "redirect.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace std::string_literals; +namespace bp = boost::process; +namespace fs = std::filesystem; + +namespace { + + // Used to return errors by generating response page and HTTP status code + std::string HttpStatus(std::string status, std::string message, std::function& SetResponseHeader) + { + SetResponseHeader("status", status); + SetResponseHeader("content_type", "text/html"); + return status + " " + message; + } + +} // anonymous namespace + +std::string redirect_plugin::name() +{ + return "redirect"; +} + +redirect_plugin::redirect_plugin() +{ + //std::cout << "Plugin constructor" << std::endl; +} + +redirect_plugin::~redirect_plugin() +{ + //std::cout << "Plugin destructor" << std::endl; +} + +std::string redirect_plugin::generate_page( + std::function& GetServerParam, + std::function& GetRequestParam, // request including body (POST...) + std::function& SetResponseHeader // to be added to result string +) +{ + try { + std::string new_location{GetRequestParam("doc_root")}; + + std::string status_code{GetRequestParam("STATUS_CODE")}; + std::string message{GetRequestParam("MESSAGE")}; + + SetResponseHeader("location", new_location); + return HttpStatus(status_code, message, SetResponseHeader); + + } catch (const std::exception& ex) { + return HttpStatus("500", "Unknown Error: "s + ex.what(), SetResponseHeader); + } +} + diff --git a/plugins/redirect/redirect.h b/plugins/redirect/redirect.h new file mode 100644 index 0000000..403cc16 --- /dev/null +++ b/plugins/redirect/redirect.h @@ -0,0 +1,21 @@ +#pragma once + +#include "../../plugin_interface.h" + +class redirect_plugin: public webserver_plugin_interface +{ +public: + redirect_plugin(); + ~redirect_plugin(); + + std::string name(); + std::string generate_page( + std::function& GetServerParam, + std::function& GetRequestParam, // request including body (POST...) + std::function& SetResponseHeader // to be added to result string + ); + +}; + +extern "C" BOOST_SYMBOL_EXPORT redirect_plugin webserver_plugin; +redirect_plugin webserver_plugin; -- cgit v1.2.3