From b3e6012ad9f138125baeb565631cceb8ccbc0eb8 Mon Sep 17 00:00:00 2001
From: Ivo Brunnbauer <ivobrunnbauer@gmail.com>
Date: Thu, 17 May 2012 20:13:11 +0200
Subject: [PATCH] Add deflate plugin for compressing files in selected
 folders.

---
 plugins/deflate/ABOUT             |    3 +
 plugins/deflate/Makefile.in       |   17 ++
 plugins/deflate/conf/deflate.conf |   12 +
 plugins/deflate/deflate.c         |  408 +++++++++++++++++++++++++++++++++++++
 plugins/deflate/deflate.h         |   83 ++++++++
 5 files changed, 523 insertions(+), 0 deletions(-)
 create mode 100644 plugins/deflate/ABOUT
 create mode 100644 plugins/deflate/Makefile.in
 create mode 100644 plugins/deflate/OPTIONAL
 create mode 100644 plugins/deflate/conf/deflate.conf
 create mode 100644 plugins/deflate/deflate.c
 create mode 100644 plugins/deflate/deflate.h

diff --git a/plugins/deflate/ABOUT b/plugins/deflate/ABOUT
new file mode 100644
index 0000000..575020e
--- /dev/null
+++ b/plugins/deflate/ABOUT
@@ -0,0 +1,3 @@
+Deflate Plugin
+===============
+This plugin lets you add folders of files that are going to be served in raw deflated form. (=better than gzip)
diff --git a/plugins/deflate/Makefile.in b/plugins/deflate/Makefile.in
new file mode 100644
index 0000000..4d18852
--- /dev/null
+++ b/plugins/deflate/Makefile.in
@@ -0,0 +1,17 @@
+CC=gcc -D_BSD_SOURCE
+CFLAGS=-Wall -ggdb -fPIC -I../../src/include
+LDFLAGS=-shared -lc -ldl -lz -pthread
+SOURCES=deflate.c
+OBJECTS=$(SOURCES:.c=.o)
+EXECUTABLE=monkey-deflate.so
+
+all: $(SOURCES) $(EXECUTABLE)
+	
+$(EXECUTABLE): $(OBJECTS) 
+	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
+
+.cpp.o:
+	$(CC) $(CFLAGS) $< -o $@
+
+clean:
+	rm -rf *.o *.*~ *.*so*
diff --git a/plugins/deflate/OPTIONAL b/plugins/deflate/OPTIONAL
new file mode 100644
index 0000000..e69de29
diff --git a/plugins/deflate/conf/deflate.conf b/plugins/deflate/conf/deflate.conf
new file mode 100644
index 0000000..81a4cbe
--- /dev/null
+++ b/plugins/deflate/conf/deflate.conf
@@ -0,0 +1,12 @@
+# DEFLATE:
+# -------
+# This plugin allows the configuration of directories that will create be used for compressed
+# file transfer to the client. Only css and js files in those directories will be compressed.
+# all paths relative to /htdocs
+
+[DEFLATE]
+    # CompressedDirs
+    # --------------
+
+    CompressedDirs css css/js
+
diff --git a/plugins/deflate/deflate.c b/plugins/deflate/deflate.c
new file mode 100644
index 0000000..77f3b20
--- /dev/null
+++ b/plugins/deflate/deflate.c
@@ -0,0 +1,408 @@
+#include "deflate.h"
+
+
+MONKEY_PLUGIN("deflate",     /* shortname */
+              "Deflate",     /* name */
+              "1.0",              /* version */
+              MK_PLUGIN_CORE_PRCTX | MK_PLUGIN_STAGE_30); /* hooks */
+
+void _mkp_core_prctx()
+{
+}
+
+int _mkp_init(void **api, char *confdir)
+{
+    mk_api = *api;
+
+    mk_header_content_encoding_deflate.data = MK_HEADER_CONTENT_ENCODING_DEFLATE;
+    mk_header_content_encoding_deflate.len = strlen(MK_HEADER_CONTENT_ENCODING_DEFLATE);
+
+    /* Initiate event loop */
+    inotify_fd = inotify_init1(IN_NONBLOCK);
+    if (inotify_fd < 0) {
+        handle_error (errno);
+        return 1;
+    }
+
+    /* Read config */
+    struct mk_config *conf;
+    struct mk_config_section *section;
+
+    char default_file[512];
+    sprintf(default_file, "%sdeflate.conf", confdir);
+
+    conf = mk_api->config_create(default_file);
+    section = mk_api->config_section_get(conf, "DEFLATE");
+
+    char* line = mk_api->config_section_getval(section, "CompressedDirs", MK_CONFIG_VAL_STR);
+
+    int counter = 0;
+    char** res = splitstring(&line, " ", &counter, 10);
+
+    watched_dirs = (dirlist**)malloc(counter * sizeof(dirlist*));
+    watched_arrlength = counter;
+
+    if (watched_arrlength > 0)
+    {
+        char* fullpath = malloc(512);
+        readlink("/proc/self/exe", fullpath, 512);
+        char* test = dirname(dirname(fullpath));
+        sprintf(fullpath, "%s%s", test, "/htdocs/");
+        int i = 0;
+        for (i = 0; i < counter; i++)
+        {
+            char* dir = malloc(512);
+            sprintf(dir, "%s%s", fullpath, res[i]);
+
+            dirlist* temp = malloc(sizeof(dirlist));
+            temp->dirname = dir;
+            temp->wd = compressAndWatch(dir);
+            watched_dirs[i] = temp;
+        }
+        fflush(0);
+
+        free(fullpath);
+        free(res);
+    }
+
+    return 0;
+}
+
+int compressAndWatch(char* directory_path)
+{
+    /* Open directory for reading */
+    DIR *dir;
+    struct dirent *ent;
+    int wd = inotify_add_watch (inotify_fd, directory_path, IN_CLOSE_WRITE);
+    if (wd < 0) {
+        handle_error (errno);
+        return 1;
+    }
+    dir = opendir (directory_path);
+    if (dir != NULL)
+    {
+        /* Read each file in the directory */
+        while ((ent = readdir (dir)) != NULL)
+        {
+            /* Ignore trash files, and also the built-in directory navigation files . and .. */
+            int dotIndex = findIndex(ent->d_name, ".");
+            if (strchr(ent->d_name, '~') != NULL || dotIndex < 2)
+                continue;
+
+            char* directoryDup = malloc(256);
+            memset(directoryDup, '\0', 256);
+            strcat(directoryDup, directory_path);
+            strcat(directoryDup, "/");
+            strcat(directoryDup, ent->d_name);
+
+            char* hiddenname= malloc(256);
+            memset(hiddenname, '\0', 256);
+            strcat(hiddenname, directory_path);
+            strcat(hiddenname, "/.");
+            strcat(hiddenname, ent->d_name);
+
+            FILE* source = fopen(directoryDup, "r");
+            FILE* dest = fopen(hiddenname, "w+");
+            /* We compress the file from it's fd into a new fd */
+            zerr(def(source, dest, 8));
+            fclose(source);
+            fclose(dest);
+
+            free(directoryDup);
+            free(hiddenname);
+        }
+        closedir (dir);
+    } else
+    {
+        return EXIT_FAILURE;
+    }
+
+    return wd;
+}
+
+void _mkp_exit()
+{
+    close(inotify_fd);
+    int i = 0;
+    for (i = 0; i < watched_arrlength; i++)
+    {
+        free(watched_dirs[i]->dirname);
+        free(watched_dirs[i]);
+    }
+    free(watched_dirs);
+}
+
+int _mkp_stage_30(struct plugin *plugin, struct client_session *cs,
+                  struct session_request *sr)
+{
+    if (watched_arrlength > 0)
+        handle_events();
+
+    if (sr->file_info.size > 0) {
+        int i = 0;
+        for (i = 0; i < watched_arrlength; i++)
+        {
+            /* Determine if the requested file is inside on of the folders
+             * we specified in the conf file that should be compressed. */
+            int watchedlen = strlen(watched_dirs[i]->dirname);
+            if (strncmp(watched_dirs[i]->dirname, sr->real_path.data, watchedlen) == 0)
+            {
+                /* Now assemble a new path that is identical to the old one except
+                 * for the fact that it has a '.' prepended to hide it. */
+                char* p = malloc(sr->real_path.len + 1);
+                char* pos = strrchr(sr->real_path.data, '/');
+                int len = pos ? pos - sr->real_path.data : -1;
+
+                memcpy(p, sr->real_path.data, len+1);
+                p[len+1] = '.';
+                memcpy(p + len + 2, sr->real_path.data + len +1, strlen(sr->real_path.data+len+1));
+
+                /* Check if we really do have a compressed file like that on our harddrive. */
+                struct stat info;
+                if (lstat(p, &info) == 0)
+                {
+                    if (sr->real_path.data != sr->real_path_static) {
+                        free(sr->real_path.data);
+                    }
+                    sr->real_path.data = p;
+                    sr->file_info.size = info.st_size;
+                    sr->headers.content_length = info.st_size;
+                    sr->headers.content_encoding = mk_header_content_encoding_deflate;
+                }
+                else
+                {
+                    free(p);
+                }
+                break;
+            }
+        }
+
+
+    }
+    return MK_PLUGIN_RET_NOT_ME;
+}
+
+/* Allow for 256 simultanious events */
+#define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*256)
+
+/* This function works through gathered events on watched dirs, and
+   if an event occured on a file, we recompress it. (because we only
+   watch dirs with all files wanting to be served compressed) */
+void handle_events()
+{
+    ssize_t len, i = 0;
+
+    char buff[BUFF_SIZE] = {0};
+
+    len = read (inotify_fd, buff, BUFF_SIZE);
+
+    while (i < len) {
+        struct inotify_event *pevent = (struct inotify_event *)&buff[i];
+
+        /* We don't do recompression just because the compressed file got opened,
+         * read and sent to client, so we exclude events on our hidden, compressed files.*/
+        int j = 0;
+        if (pevent->name[0] != '.')
+            for (j = 0; j < watched_arrlength; j++)
+            {
+                if (watched_dirs[j]->wd == pevent->wd)
+                {
+                    char nonhidden[256] = {0};
+                    sprintf(nonhidden, "%s/%s", watched_dirs[j]->dirname, pevent->name);
+
+                    char hidden[256] = {0};
+                    sprintf(hidden, "%s/.%s", watched_dirs[j]->dirname, pevent->name);
+
+                    FILE* source = fopen(nonhidden, "r");
+                    FILE* dest = fopen(hidden, "w+");
+
+                    zerr(def(source, dest, 8));
+
+                    fclose(source);
+                    fclose(dest);
+                }
+            }
+
+        i += sizeof(struct inotify_event) + pevent->len;
+
+    }
+
+}
+
+void handle_error (int error)
+{
+    fprintf (stderr, "Error: %s\n", strerror(error));
+}
+
+#define CHUNK 16384
+
+/* Compress from file source to file dest until EOF on source.
+   def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
+   allocated for processing, Z_STREAM_ERROR if an invalid compression
+   level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
+   version of the library linked do not match, or Z_ERRNO if there is
+   an error reading or writing the files. */
+int def(FILE *source, FILE *dest, int level)
+{
+    int ret, flush;
+    unsigned have;
+    z_stream strm;
+    unsigned char in[CHUNK];
+    unsigned char out[CHUNK];
+
+    /* allocate deflate state */
+    strm.zalloc = Z_NULL;
+    strm.zfree = Z_NULL;
+    strm.opaque = Z_NULL;
+    ret = deflateInit2(&strm, level, Z_DEFLATED, -15,MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
+    if (ret != Z_OK)
+        return ret;
+
+    /* compress until end of file */
+    do {
+        strm.avail_in = fread(in, 1, CHUNK, source);
+        if (ferror(source)) {
+            (void)deflateEnd(&strm);
+            return Z_ERRNO;
+        }
+        flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
+        strm.next_in = in;
+
+        /* run deflate() on input until output buffer not full, finish
+           compression if all of source has been read in */
+        do {
+            strm.avail_out = CHUNK;
+            strm.next_out = out;
+            ret = deflate(&strm, flush);    /* no bad return value */
+            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+            have = CHUNK - strm.avail_out;
+            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
+                (void)deflateEnd(&strm);
+                return Z_ERRNO;
+            }
+        } while (strm.avail_out == 0);
+        assert(strm.avail_in == 0);     /* all input will be used */
+
+        /* done when last data in file processed */
+    } while (flush != Z_FINISH);
+    assert(ret == Z_STREAM_END);        /* stream will be complete */
+
+    /* clean up and return */
+    (void)deflateEnd(&strm);
+    return Z_OK;
+}
+
+/* Decompress from file source to file dest until stream ends or EOF.
+   inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
+   allocated for processing, Z_DATA_ERROR if the deflate data is
+   invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
+   the version of the library linked do not match, or Z_ERRNO if there
+   is an error reading or writing the files. */
+int inf(FILE *source, FILE *dest)
+{
+    int ret;
+    unsigned have;
+    z_stream strm;
+    unsigned char in[CHUNK];
+    unsigned char out[CHUNK];
+
+    /* allocate inflate state */
+    strm.zalloc = Z_NULL;
+    strm.zfree = Z_NULL;
+    strm.opaque = Z_NULL;
+    strm.avail_in = 0;
+    strm.next_in = Z_NULL;
+    ret = inflateInit2(&strm, -15);
+    if (ret != Z_OK)
+        return ret;
+
+    /* decompress until deflate stream ends or end of file */
+    do {
+        strm.avail_in = fread(in, 1, CHUNK, source);
+        if (ferror(source)) {
+            (void)inflateEnd(&strm);
+            return Z_ERRNO;
+        }
+        if (strm.avail_in == 0)
+            break;
+        strm.next_in = in;
+
+        /* run inflate() on input until output buffer not full */
+        do {
+            strm.avail_out = CHUNK;
+            strm.next_out = out;
+            ret = inflate(&strm, Z_NO_FLUSH);
+            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+            switch (ret) {
+            case Z_NEED_DICT:
+                ret = Z_DATA_ERROR;     /* and fall through */
+            case Z_DATA_ERROR:
+            case Z_MEM_ERROR:
+                (void)inflateEnd(&strm);
+                return ret;
+            }
+            have = CHUNK - strm.avail_out;
+            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
+                (void)inflateEnd(&strm);
+                return Z_ERRNO;
+            }
+        } while (strm.avail_out == 0);
+
+        /* done when inflate() says it's done */
+    } while (ret != Z_STREAM_END);
+
+    /* clean up and return */
+    (void)inflateEnd(&strm);
+    return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+}
+
+/* report a zlib or i/o error */
+void zerr(int ret)
+{
+    /* fputs("zpipe: ", stderr); */
+    switch (ret) {
+    case Z_ERRNO:
+        if (ferror(stdin))
+            fputs("error reading stdin\n", stderr);
+        if (ferror(stdout))
+            fputs("error writing stdout\n", stderr);
+        break;
+    case Z_STREAM_ERROR:
+        fputs("invalid compression level\n", stderr);
+        break;
+    case Z_DATA_ERROR:
+        fputs("invalid or incomplete deflate data\n", stderr);
+        break;
+    case Z_MEM_ERROR:
+        fputs("out of memory\n", stderr);
+        break;
+    case Z_VERSION_ERROR:
+        fputs("zlib version mismatch!\n", stderr);
+    }
+}
+
+char** splitstring(char** line, char* delim, int* counter, int max)
+{
+    char** ret = (char**)malloc(max * sizeof(char*));
+    while (1)
+    {
+        ret[*counter] = strsep(line, delim);
+        if (ret[*counter] == NULL)
+            break;
+        *counter += 1;
+        if (*counter >= max)
+            break;
+    }
+    return ret;
+}
+
+int findIndex(char* haystack, char* needle)
+{
+    char* pos = strstr(haystack, needle);
+    int index = pos ? pos - haystack : -1;
+    if (index == -1)
+    {
+        /* printf("Didn't find the needle.");*/
+    }
+    return index;
+}
diff --git a/plugins/deflate/deflate.h b/plugins/deflate/deflate.h
new file mode 100644
index 0000000..e37d162
--- /dev/null
+++ b/plugins/deflate/deflate.h
@@ -0,0 +1,83 @@
+/*  Monkey HTTP Daemon
+ *  ------------------
+ *  Copyright (C) 2011-2012 Ivo Brunnbauer
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+
+#ifndef DEFLATE_H
+#define DEFLATE_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <limits.h>
+#include <time.h>
+#include <dirent.h>
+#include <sys/inotify.h>
+#include <zlib.h>
+#include <assert.h>
+#include <libgen.h>
+
+#include "MKPlugin.h"
+#include "mk_string.h"
+#include "zlib.h"
+#include "memory.h"
+
+#define MK_CRLF "\r\n"
+#define MK_HEADER_CONTENT_ENCODING_DEFLATE "deflate" MK_CRLF
+
+/* ---------------------- */
+
+int inotify_fd;
+
+struct dir_list{
+    char* dirname;
+    int wd;
+};
+
+typedef struct dir_list dirlist;
+
+dirlist** watched_dirs;
+int watched_arrlength;
+
+mk_pointer mk_header_content_encoding_deflate;
+
+/* ---------------------- */
+
+int def(FILE *source, FILE *dest, int level);
+
+int inf(FILE *source, FILE *dest);
+
+void zerr(int ret);
+
+int compressAndWatch(char* directory_path);
+
+void handle_error (int error);
+
+void handle_events();
+
+int findIndex(char* haystack, char* needle);
+
+char** splitstring(char** line, char* delim, int* counter, int max);
+
+
+
+#endif
-- 
1.7.7

