aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--Makefile24
-rw-r--r--endpoints.c38
-rw-r--r--main.c50
4 files changed, 116 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3a1a458
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.so
+*.o
+rename.ld
+main
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..44869e6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,24 @@
+# Makefile
+
+all: main endpoints.so
+
+.PHONY: all clean
+
+endpoints.o: endpoints.c
+ $(CC) -c $< -o $@
+
+rename.ld: endpoints.o
+ # Looks terrible
+ # All it does it generates renaming
+ echo SECTIONS { > $@
+ @readelf --syms endpoints.o | awk '/FUNC/ && /GLOBAL/ {printf "\t%s = %s;\n", gensub(/_/, "/", "g", $$8), $$8;}' >> $@
+ echo } >> $@
+
+endpoints.so: endpoints.o rename.ld
+ $(CC) -shared -Wl,rename.ld $< -fPIE -o $@
+
+main: main.c
+ $(CC) $< -ldl -lmicrohttpd -o $@
+
+clean:
+ $(RM) main endpoints.o endpoints.so rename.ld
diff --git a/endpoints.c b/endpoints.c
new file mode 100644
index 0000000..9a02098
--- /dev/null
+++ b/endpoints.c
@@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "microhttpd.h"
+
+// not accessible in browser because ALL URLs start with / (or with _ in dylib)
+char* duplicate(const char* x) {
+ size_t len = strlen(x);
+ char* ret = malloc(len+1);
+ memcpy(ret, x, len);
+ ret[len] = '\0';
+ return ret;
+}
+
+// index, path: /
+char* _(struct MHD_Connection* connection) {
+ const char* resp = "<html><body>This is an index page!</body></html>";
+ return duplicate(resp);
+}
+
+// home, path: /home
+char* _home(struct MHD_Connection* connection) {
+ const char* resp = "<html><body>"
+ "<h1>This is a HOME page!</h1>"
+ "<div style=\"height:10000px;\"></div>"
+ "<a href=\"/hidden/hentai\">Don't click me! :0</a>"
+ "</body></html>";
+ return duplicate(resp);
+}
+
+// hentai, path: /hidden/hentai
+char* _hidden_hentai(struct MHD_Connection* connection) {
+ const char* resp = "<html><style>a{text-decoration:none;}</style><body>"
+ "<p>what did you expect to see...</p>"
+ "<a href=\"https://pornhub.com\">&nbsp;</a>"
+ "</body></html>";
+ return duplicate(resp);
+}
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..3d8f57d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#include "microhttpd.h"
+
+typedef char*(*endpoint)(struct MHD_Connection*);
+
+enum MHD_Result process_connection(void *dylib, struct MHD_Connection *connection, const char *url,
+ const char *method, const char *version, const char *upload_data,
+ size_t *upload_data_size, void **req_cls)
+{
+ printf("\nurl: %s\nmethod:%s\nversion:%s\nupload_data:%s\nsize:%zu\n",
+ url, method, version, upload_data, *upload_data_size);
+ /* Load function with the name of the url from shared library */
+ endpoint handler = dlsym(dylib, url);
+ struct MHD_Response *res; int status;
+ if (handler != NULL) {
+ fprintf(stderr, "Accessed at url\t%s\n", url);
+ char* response = handler(connection);
+ res = MHD_create_response_from_buffer(strlen(response), response, MHD_RESPMEM_MUST_FREE);
+ status = MHD_HTTP_OK;
+ } else {
+ fprintf(stderr, "Failed to access at url\t%s\n", url);
+ const char* error = "<html><body>Error 404: Page does not exist</body></html>";
+ res = MHD_create_response_from_buffer(strlen(error), (void*)error, MHD_RESPMEM_PERSISTENT);
+ status = MHD_HTTP_NOT_FOUND;
+ }
+ MHD_queue_response(connection, status, res);
+ MHD_destroy_response(res);
+ return MHD_YES;
+}
+
+
+int main(){
+ void* dylib = dlopen("./endpoints.so", RTLD_NOW);
+ if (!dylib) {
+ fprintf(stderr, "Could not open dynamic library: %s\n", dlerror());
+ return 1;
+ }
+ struct MHD_Daemon *daemon = MHD_start_daemon(MHD_USE_POLL | MHD_USE_INTERNAL_POLLING_THREAD, 8080, NULL, NULL, process_connection, dylib, MHD_OPTION_END);
+ if (daemon == NULL) {
+ perror("daemon");
+ fprintf(stderr, "could not initialize daemon\n");
+ return 1;
+ }
+ getchar();
+ MHD_stop_daemon(daemon);
+ return 0;
+}