about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-12-15 20:52:41 +0000
committerEric Wong <mwrap-perl@80x24.org>2022-12-16 09:27:40 +0000
commit695bd5b9876ab31a063228390b7bf8acb32f3a1a (patch)
treec09ab7a102aae49c41c236b69606dbc769e9aaf0
parent4c4fc4891e88b29243b0ea80277c5f8edef531f5 (diff)
downloadmwrap-695bd5b9876ab31a063228390b7bf8acb32f3a1a.tar.gz
I won't be developing Devel::Mwrap::PSGI further since
mwrap_httpd is less intrusive and faster.
-rw-r--r--lib/Devel/Mwrap/PSGI.pm2
-rw-r--r--mwrap_httpd.h31
-rw-r--r--t/mwrap-httpd.t11
3 files changed, 44 insertions, 0 deletions
diff --git a/lib/Devel/Mwrap/PSGI.pm b/lib/Devel/Mwrap/PSGI.pm
index 5d143cd..3e8795e 100644
--- a/lib/Devel/Mwrap/PSGI.pm
+++ b/lib/Devel/Mwrap/PSGI.pm
@@ -1,5 +1,7 @@
 # Copyright (C) all contributors <mwrap@80x24.org>
 # License: GPL-2.0+ <https://www.gnu.org/licenses/gpl-2.0.txt>
+#
+# Note: this is deprecated, use mwrap_httpd.h instead
 package Devel::Mwrap::PSGI;
 use v5.12; # strict
 use warnings;
diff --git a/mwrap_httpd.h b/mwrap_httpd.h
index 1ef21e0..f5c3e1b 100644
--- a/mwrap_httpd.h
+++ b/mwrap_httpd.h
@@ -29,6 +29,7 @@
 #include "picohttpparser_c.h"
 #include <pthread.h>
 #include <stdbool.h>
+#define URL "https://80x24.org/mwrap-perl.git/about"
 
 enum mw_qev {
         MW_QEV_IGNORE = 0,
@@ -321,6 +322,15 @@ static enum mw_qev h1_do_reset(struct mw_h1 *h1)
         return h1_res_oneshot(h1, r200, sizeof(r200) - 1);
 }
 
+static enum mw_qev h1_do_trim(struct mw_h1 *h1)
+{
+        static const char r200[] = "HTTP/1.1 200 OK\r\n"
+                "Content-Type: text/plain\r\n"
+                "Content-Length: 9\r\n\r\n" "trimming\n";
+        malloc_trim(0);
+        return h1_res_oneshot(h1, r200, sizeof(r200) - 1);
+}
+
 #define PATH_SKIP(h1r, pfx) path_skip(h1r, pfx, sizeof(pfx) - 1)
 static const char *path_skip(struct mw_h1req *h1r, const char *pfx, size_t len)
 {
@@ -633,6 +643,23 @@ static enum mw_qev each_gt(struct mw_h1 *h1, struct mw_h1req *h1r,
         return h1_200(h1, &html);
 }
 
+/* /$PID/ root endpoint */
+static enum mw_qev pid_root(struct mw_h1 *h1, struct mw_h1req *h1r)
+{
+        struct mw_fbuf html;
+        FILE *fp = wbuf_init(&html);
+        if (!fp) return h1_close(h1);
+#define default_min "2000"
+
+        FPUTS("<html><head><title>mwrap demo"
+                "</title></head><body><p><a\n"
+                "href=\"each/" default_min "\">allocations &gt;"
+                default_min " bytes</a><p><a\nhref=\""
+                URL "\">" URL "</a></body></html>", fp);
+        return h1_200(h1, &html);
+#undef default_min
+}
+
 static enum mw_qev h1_dispatch(struct mw_h1 *h1, struct mw_h1req *h1r)
 {
         if (h1r->method_len == 3 && !memcmp(h1r->method, "GET", 3)) {
@@ -646,10 +673,14 @@ static enum mw_qev h1_dispatch(struct mw_h1 *h1, struct mw_h1req *h1r)
                                 return each_gt(h1, h1r, min);
                 } else if ((PATH_SKIP(h1r, "/at/"))) {
                         return each_at(h1, h1r);
+                } else if (h1r->path_len == 1 && h1r->path[0] == '/') {
+                        return pid_root(h1, h1r);
                 }
         } else if (h1r->method_len == 4 && !memcmp(h1r->method, "POST", 4)) {
                 if (h1r->path_len == 6 && !memcmp(h1r->path, "/reset", 6))
                         return h1_do_reset(h1);
+                if (h1r->path_len == 5 && !memcmp(h1r->path, "/trim", 5))
+                        return h1_do_trim(h1);
         }
         return h1_404(h1);
 }
diff --git a/t/mwrap-httpd.t b/t/mwrap-httpd.t
index aec1780..f300eae 100644
--- a/t/mwrap-httpd.t
+++ b/t/mwrap-httpd.t
@@ -65,6 +65,17 @@ SKIP: {
                 "http://0/$pid/each/2000");
         is($rc, 0, 'curl /each');
         unlink($cout);
+
+        $rc = system(qw(curl -vsSf --unix-socket), $sock, '-o', $cout,
+                "http://0/$pid/");
+        is($rc, 0, 'curl / (PID root)');
+        like(slurp($cout), qr/<html>/, 'root shown');
+
+        $rc = system(qw(curl -vsSf -XPOST --unix-socket), $sock, '-o', $cout,
+                "http://0/$pid/trim");
+        is($rc, 0, 'curl / (PID root)');
+        like(slurp($cout), qr/trimming/, 'trim started');
+        unlink($cout);
 };
 
 {