uhttpd: fix building without TLS and Lua support
[project/uhttpd.git] / main.c
diff --git a/main.c b/main.c
index ae54b69d468fae99cbb7ca6222c50658bdb13215..6c29afe0d06facf7d885921250fc09e48b131afa 100644 (file)
--- a/main.c
+++ b/main.c
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+
 #define _BSD_SOURCE
 #define _GNU_SOURCE
 #define _XOPEN_SOURCE  700
@@ -30,6 +34,7 @@
 #include <signal.h>
 
 #include <libubox/usock.h>
+#include <libubox/utils.h>
 
 #include "uhttpd.h"
 #include "tls.h"
@@ -160,6 +165,7 @@ static int usage(const char *name)
                "       -t seconds      CGI, Lua and UBUS script timeout in seconds, default is 60\n"
                "       -T seconds      Network timeout in seconds, default is 30\n"
                "       -k seconds      HTTP keepalive timeout\n"
+               "       -A seconds      TCP keepalive timeout, default is unset\n"
                "       -d string       URL decode given string\n"
                "       -r string       Specify basic auth realm\n"
                "       -m string       MD5 crypt given string\n"
@@ -179,6 +185,7 @@ static void init_defaults_pre(void)
        conf.cgi_prefix = "/cgi-bin";
        conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
        INIT_LIST_HEAD(&conf.cgi_alias);
+       INIT_LIST_HEAD(&conf.lua_prefix);
 }
 
 static void init_defaults_post(void)
@@ -213,6 +220,25 @@ static void fixup_prefix(char *str)
        str[len + 1] = 0;
 }
 
+#ifdef HAVE_LUA
+static void add_lua_prefix(const char *prefix, const char *handler) {
+       struct lua_prefix *p;
+       char *pprefix, *phandler;
+
+       p = calloc_a(sizeof(*p),
+                    &pprefix, strlen(prefix) + 1,
+                    &phandler, strlen(handler) + 1);
+
+       if (!p)
+               return;
+
+       p->prefix = strcpy(pprefix, prefix);
+       p->handler = strcpy(phandler, handler);
+
+       list_add_tail(&p->list, &conf.lua_prefix);
+}
+#endif
+
 int main(int argc, char **argv)
 {
        struct alias *alias;
@@ -225,6 +251,9 @@ int main(int argc, char **argv)
        int n_tls = 0;
        const char *tls_key = NULL, *tls_crt = NULL;
 #endif
+#ifdef HAVE_LUA
+       const char *lua_prefix = NULL, *lua_handler = NULL;
+#endif
 
        BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
 
@@ -232,7 +261,7 @@ int main(int argc, char **argv)
        init_defaults_pre();
        signal(SIGPIPE, SIG_IGN);
 
-       while ((ch = getopt(argc, argv, "afqSDRXC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:y:i:t:k:T:A:u:U:")) != -1) {
+       while ((ch = getopt(argc, argv, "A:aC:c:Dd:E:fh:H:I:i:K:k:L:l:m:N:n:p:qRr:Ss:T:t:U:u:Xx:y:")) != -1) {
                switch(ch) {
 #ifdef HAVE_TLS
                case 'C':
@@ -273,6 +302,14 @@ int main(int argc, char **argv)
                        conf.docroot = strdup(uh_buf);
                        break;
 
+               case 'H':
+                       if (uh_handler_add(optarg)) {
+                               fprintf(stderr, "Error: Failed to load handler script %s\n",
+                                       optarg);
+                               exit(1);
+                       }
+                       break;
+
                case 'E':
                        if (optarg[0] != '/') {
                                fprintf(stderr, "Error: Invalid error handler: %s\n",
@@ -401,11 +438,28 @@ int main(int argc, char **argv)
 
 #ifdef HAVE_LUA
                case 'l':
-                       conf.lua_prefix = optarg;
-                       break;
-
                case 'L':
-                       conf.lua_handler = optarg;
+                       if (ch == 'l') {
+                               if (lua_prefix)
+                                       fprintf(stderr, "uhttpd: Ignoring previous -%c %s\n",
+                                               ch, lua_prefix);
+
+                               lua_prefix = optarg;
+                       }
+                       else {
+                               if (lua_handler)
+                                       fprintf(stderr, "uhttpd: Ignoring previous -%c %s\n",
+                                               ch, lua_handler);
+
+                               lua_handler = optarg;
+                       }
+
+                       if (lua_prefix && lua_handler) {
+                               add_lua_prefix(lua_prefix, lua_handler);
+                               lua_prefix = NULL;
+                               lua_handler = NULL;
+                       }
+
                        break;
 #else
                case 'l':
@@ -475,14 +529,13 @@ int main(int argc, char **argv)
 #endif
 
 #ifdef HAVE_LUA
-       if (conf.lua_handler || conf.lua_prefix) {
-               if (!conf.lua_handler || !conf.lua_prefix) {
-                       fprintf(stderr, "Need handler and prefix to enable Lua support\n");
-                       return 1;
-               }
-               if (uh_plugin_init("uhttpd_lua.so"))
-                       return 1;
+       if (lua_handler || lua_prefix) {
+               fprintf(stderr, "Need handler and prefix to enable Lua support\n");
+               return 1;
        }
+
+       if (!list_empty(&conf.lua_prefix) && uh_plugin_init("uhttpd_lua.so"))
+               return 1;
 #endif
 #ifdef HAVE_UBUS
        if (conf.ubus_prefix && uh_plugin_init("uhttpd_ubus.so"))