Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/httpd add Lua scripting support to bozohttpd, see ht...



details:   https://anonhg.NetBSD.org/src/rev/7a44dd023061
branches:  trunk
changeset: 790455:7a44dd023061
user:      mbalmer <mbalmer%NetBSD.org@localhost>
date:      Sat Oct 12 17:24:06 2013 +0000

description:
add Lua scripting support to bozohttpd, see httpd(8) for details

diffstat:

 libexec/httpd/Makefile     |    7 +-
 libexec/httpd/bozohttpd.8  |   30 ++-
 libexec/httpd/bozohttpd.c  |    8 +-
 libexec/httpd/bozohttpd.h  |   34 +++-
 libexec/httpd/lua-bozo.c   |  437 +++++++++++++++++++++++++++++++++++++++++++++
 libexec/httpd/main.c       |   20 +-
 libexec/httpd/printenv.lua |   83 ++++++++
 7 files changed, 610 insertions(+), 9 deletions(-)

diffs (truncated from 764 to 300 lines):

diff -r ef023cf5737e -r 7a44dd023061 libexec/httpd/Makefile
--- a/libexec/httpd/Makefile    Sat Oct 12 16:52:21 2013 +0000
+++ b/libexec/httpd/Makefile    Sat Oct 12 17:24:06 2013 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.15 2013/10/12 07:49:40 mbalmer Exp $
+#      $NetBSD: Makefile,v 1.16 2013/10/12 17:24:06 mbalmer Exp $
 #
 #      $eterna: Makefile,v 1.30 2010/07/11 00:34:27 mrg Exp $
 #
@@ -13,6 +13,7 @@
 #      NO_DYNAMIC_CONTENT      /* don't support dynamic content updates */
 #      NO_SSL_SUPPORT          /* don't support ssl (https) */
 #      DO_HTPASSWD             /* support .htpasswd files */
+#      NO_LUA_SUPPORT          /* don't support Lua for dynamic content */
 #
 # these are usually set via the "COPTS" variable, or some other method
 # for setting CFLAGS relevant to your make, eg
@@ -23,10 +24,10 @@
 MAN=   httpd.8
 BUILDSYMLINKS+=bozohttpd.8 httpd.8
 SRCS=  bozohttpd.c ssl-bozo.c auth-bozo.c cgi-bozo.c daemon-bozo.c \
-       tilde-luzah-bozo.c dir-index-bozo.c content-bozo.c
+       tilde-luzah-bozo.c dir-index-bozo.c content-bozo.c lua-bozo.c
 SRCS+= main.c
 
-LDADD= -lcrypt
+LDADD= -lcrypt -llua
 DPADD= ${LIBCRYPT}
 
 WARNS?=        4
diff -r ef023cf5737e -r 7a44dd023061 libexec/httpd/bozohttpd.8
--- a/libexec/httpd/bozohttpd.8 Sat Oct 12 16:52:21 2013 +0000
+++ b/libexec/httpd/bozohttpd.8 Sat Oct 12 17:24:06 2013 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: bozohttpd.8,v 1.38 2013/07/11 08:19:56 wiz Exp $
+.\"    $NetBSD: bozohttpd.8,v 1.39 2013/10/12 17:24:06 mbalmer Exp $
 .\"
 .\"    $eterna: bozohttpd.8,v 1.101 2011/11/18 01:25:11 mrg Exp $
 .\"
@@ -26,7 +26,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd June 11, 2013
+.Dd October 12, 2013
 .Dt HTTPD 8
 .Os
 .Sh NAME
@@ -36,6 +36,7 @@
 .Nm
 .Op Fl CIMPSZciptvx
 .Op Fl C Ar suffix cgihandler
+.Op Fl L Ar prefix script
 .Op Fl I Ar port
 .Op Fl M Ar suffix type encoding encoding11
 .Op Fl P Ar pidfile
@@ -95,6 +96,27 @@
 Multiple
 .Fl C
 options may be passed.
+.It Fl L Ar prefix script
+Adds a new Lua script for a particular prefix.
+The
+.Ar prefix
+should be an arbitrary text, and the
+.Ar script
+should be a full path to a Lua script.
+Multiple
+.Fl L
+options may be passed.
+A separate Lua state is created for each prefix.
+The Lua script can register callbacks using the
+httpd.register_handler('<name>', function) Lua function,
+which will trigger the execution of the Lua function
+.Em function
+when a URL in the form
+.Em http://<hostname>/<prefix>/<name>
+is being accessed.
+The function is passed three tables as arguments, the server
+environment, the request headers, and the decoded query string
+plus any data that was send as application/x-www-form-urlencoded.
 .It Fl c Ar cgibin
 Enables the CGI/1.1 interface.
 The
@@ -494,6 +516,10 @@
 The large list of contributors includes:
 .Bl -dash
 .It
+Marc Balmer
+.Aq mbalmer%NetBSD.org@localhost
+added Lua support for dynamic content creation
+.It
 Arnaud Lacombe
 .Aq alc%NetBSD.org@localhost
 provided some clean up for memory leaks
diff -r ef023cf5737e -r 7a44dd023061 libexec/httpd/bozohttpd.c
--- a/libexec/httpd/bozohttpd.c Sat Oct 12 16:52:21 2013 +0000
+++ b/libexec/httpd/bozohttpd.c Sat Oct 12 17:24:06 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.c,v 1.42 2013/10/12 07:49:40 mbalmer Exp $   */
+/*     $NetBSD: bozohttpd.c,v 1.43 2013/10/12 17:24:06 mbalmer Exp $   */
 
 /*     $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $      */
 
@@ -1425,6 +1425,9 @@
        if (bozo_process_cgi(request))
                return 0;
 
+       if (bozo_process_lua(request))
+               return 0;
+
        debug((httpd, DEBUG_FAT, "transform_request set: %s", newfile));
        return 1;
 bad_done:
@@ -2086,6 +2089,9 @@
                        "bozohttpd: memory_allocation failure\n");
                return 0;
        }
+#ifndef NO_LUA_SUPPORT
+       SIMPLEQ_INIT(&httpd->lua_states);
+#endif
        return 1;
 }
 
diff -r ef023cf5737e -r 7a44dd023061 libexec/httpd/bozohttpd.h
--- a/libexec/httpd/bozohttpd.h Sat Oct 12 16:52:21 2013 +0000
+++ b/libexec/httpd/bozohttpd.h Sat Oct 12 17:24:06 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.h,v 1.29 2013/10/12 07:49:40 mbalmer Exp $   */
+/*     $NetBSD: bozohttpd.h,v 1.30 2013/10/12 17:24:07 mbalmer Exp $   */
 
 /*     $eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $       */
 
@@ -36,6 +36,9 @@
 
 #include <sys/stat.h>
 
+#ifndef NO_LUA_SUPPORT
+#include <lua.h>
+#endif
 #include <stdio.h>
 
 /* lots of "const" but gets free()'ed etc at times, sigh */
@@ -47,6 +50,22 @@
        SIMPLEQ_ENTRY(bozoheaders)      h_next;
 } bozoheaders_t;
 
+#ifndef NO_LUA_SUPPORT
+typedef struct lua_handler {
+       const char      *name;
+       int              ref;
+       SIMPLEQ_ENTRY(lua_handler)      h_next;
+} lua_handler_t;
+
+typedef struct lua_state_map {
+       const char      *script;
+       const char      *prefix;
+       lua_State       *L;
+       SIMPLEQ_HEAD(, lua_handler)     handlers;
+       SIMPLEQ_ENTRY(lua_state_map)    s_next;
+} lua_state_map_t;
+#endif
+
 typedef struct bozo_content_map_t {
        const char      *name;          /* postfix of file */
        size_t           namelen;       /* length of postfix */
@@ -94,6 +113,10 @@
        int              hide_dots;     /* hide .* */
        int              process_cgi;   /* use the cgi handler */
        char            *cgibin;        /* cgi-bin directory */
+#ifndef NO_LUA_SUPPORT
+       int              process_lua;   /* use the Lua handler */
+       SIMPLEQ_HEAD(, lua_state_map)   lua_states;
+#endif
        void            *sslinfo;       /* pointer to ssl struct */
        int             dynamic_content_map_size;/* size of dyn cont map */
        bozo_content_map_t      *dynamic_content_map;/* dynamic content map */
@@ -252,6 +275,15 @@
 #endif /* NO_CGIBIN_SUPPORT */
 
 
+/* lua-bozo.c */
+#ifdef NO_LUA_SUPPORT
+#define bozo_process_lua(h)                            0
+#else
+void   bozo_add_lua_map(bozohttpd_t *, const char *, const char *);
+int    bozo_process_lua(bozo_httpreq_t *);
+#endif /* NO_LUA_SUPPORT */
+
+
 /* daemon-bozo.c */
 #ifdef NO_DAEMON_MODE
 #define bozo_daemon_init(x)                            do { /* nothing */ } while (0)
diff -r ef023cf5737e -r 7a44dd023061 libexec/httpd/lua-bozo.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/libexec/httpd/lua-bozo.c  Sat Oct 12 17:24:06 2013 +0000
@@ -0,0 +1,437 @@
+/*     $NetBSD: lua-bozo.c,v 1.1 2013/10/12 17:24:07 mbalmer Exp $     */
+
+/*
+ * Copyright (c) 2013 Marc Balmer <marc%msys.ch@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer and
+ *    dedication in the documentation and/or other materials provided
+ *    with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/* this code implements dynamic content generation using Lua for bozohttpd */
+
+#ifndef NO_LUA_SUPPORT
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "bozohttpd.h"
+
+/* Lua binding for bozohttp */
+
+#if LUA_VERSION_NUM < 502
+#define LUA_HTTPDLIBNAME "httpd"
+#endif
+
+#define FORM   "application/x-www-form-urlencoded"
+
+static int
+lua_flush(lua_State *L)
+{
+       bozohttpd_t *httpd;
+
+       lua_pushstring(L, "bozohttpd");
+       lua_gettable(L, LUA_REGISTRYINDEX);
+       httpd = lua_touserdata(L, -1);
+       lua_pop(L, 1);
+
+       bozo_flush(httpd, stdout);
+       return 0;
+}
+
+static int
+lua_print(lua_State *L)
+{
+       bozohttpd_t *httpd;
+
+       lua_pushstring(L, "bozohttpd");
+       lua_gettable(L, LUA_REGISTRYINDEX);
+       httpd = lua_touserdata(L, -1);
+       lua_pop(L, 1);
+
+       bozo_printf(httpd, "%s\r\n", lua_tostring(L, -1));
+       return 0;
+}
+
+static int
+lua_read(lua_State *L)
+{
+       bozohttpd_t *httpd;
+       int len;
+       char *data;
+
+       lua_pushstring(L, "bozohttpd");
+       lua_gettable(L, LUA_REGISTRYINDEX);
+       httpd = lua_touserdata(L, -1);
+       lua_pop(L, 1);
+
+       len = luaL_checkinteger(L, -1);
+       data = bozomalloc(httpd, len + 1);
+       bozo_read(httpd, STDIN_FILENO, data, len);
+       lua_pushstring(L, data);
+       free(data);
+       return 1;
+}
+
+static int
+lua_register_handler(lua_State *L)
+{
+       lua_state_map_t *map;



Home | Main Index | Thread Index | Old Index