Skip to content
Browse files

hahaha

  • Loading branch information...
1 parent e937fd5 commit f346373104706a4b1497f5a0b03b25c078862f33 @alacner committed Apr 28, 2011
Showing with 268 additions and 12 deletions.
  1. +73 −0 ddebug.h
  2. +195 −12 ngx_http_lua_module.c
View
73 ddebug.h
@@ -0,0 +1,73 @@
+/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
+
+#ifndef DDEBUG_H
+#define DDEBUG_H
+
+#include <ngx_core.h>
+
+#if defined(DDEBUG) && (DDEBUG)
+
+# if (NGX_HAVE_VARIADIC_MACROS)
+
+# define dd(...) fprintf(stderr, "lua *** %s: ", __func__); \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__)
+
+# else
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <stdarg.h>
+
+static void dd(const char *fmt, ...) {
+}
+
+# endif
+
+#else
+
+# if (NGX_HAVE_VARIADIC_MACROS)
+
+# define dd(...)
+
+# else
+
+#include <stdarg.h>
+
+static void dd(const char *fmt, ...) {
+}
+
+# endif
+
+#endif
+
+#if defined(DDEBUG) && (DDEBUG)
+
+#define dd_check_read_event_handler(r) \
+ dd("r->read_event_handler = %s", \
+ r->read_event_handler == ngx_http_block_reading ? \
+ "ngx_http_block_reading" : \
+ r->read_event_handler == ngx_http_test_reading ? \
+ "ngx_http_test_reading" : \
+ r->read_event_handler == ngx_http_request_empty_handler ? \
+ "ngx_http_request_empty_handler" : "UNKNOWN")
+
+#define dd_check_write_event_handler(r) \
+ dd("r->write_event_handler = %s", \
+ r->write_event_handler == ngx_http_handler ? \
+ "ngx_http_handler" : \
+ r->write_event_handler == ngx_http_core_run_phases ? \
+ "ngx_http_core_run_phases" : \
+ r->write_event_handler == ngx_http_request_empty_handler ? \
+ "ngx_http_request_empty_handler" : "UNKNOWN")
+
+#else
+
+#define dd_check_read_event_handler(r)
+#define dd_check_write_event_handler(r)
+
+#endif
+
+#endif /* DDEBUG_H */
+
View
207 ngx_http_lua_module.c
@@ -1,3 +1,6 @@
+#define DDEBUG 1
+#include "ddebug.h"
+
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
@@ -17,6 +20,21 @@
#define safe_emalloc(nmemb, size, offset) malloc((nmemb) * (size) + (offset))
+#define CLFACTORY_BEGIN_CODE "return function() "
+#define CLFACTORY_BEGIN_SIZE (sizeof(CLFACTORY_BEGIN_CODE)-1)
+
+#define CLFACTORY_END_CODE " end"
+#define CLFACTORY_END_SIZE (sizeof(CLFACTORY_END_CODE)-1)
+
+typedef struct {
+ int sent_begin;
+ int sent_end;
+ int extraline;
+ FILE *f;
+ char buff[LUAL_BUFFERSIZE];
+} clfactory_file_ctx_t;
+
+
//Global Setting
lua_State *L; /* lua state handle */
@@ -29,6 +47,10 @@ static void ngx_http_lua_process_exit(ngx_cycle_t *cycle);
static ngx_int_t make_http_header(ngx_http_request_t *r);
static ngx_int_t make_http_get_body(ngx_http_request_t *r, char *out_buf);
+int ngx_http_lua_clfactory_loadfile(lua_State *L, const char *filename);
+static int clfactory_errfile(lua_State *L, const char *what, int fname_index);
+static const char * clfactory_getF(lua_State *L, void *ud, size_t *size);
+
static char g_foo_settings[64] = {0};
/* Commands */
@@ -143,15 +165,7 @@ static ngx_int_t make_http_get_body(ngx_http_request_t *r, char *out_buf){
return NGX_HTTP_BAD_REQUEST;
}
snprintf(uri, r->uri.len + 1, "%s", r->uri.data);
- sprintf(out_buf, "Author: http://timyang.net/ nconfig=%snid=%snuri=%snret=%lxn", g_foo_settings, id, uri, ngx_random());
-
-
- if (luaL_dofile(L, "/usr/local/nginx/conf/test.lua")) {
- ;
- //if (luaL_loadfile(L, "/usr/local/nginx/conf/test.lua") || lua_pcall(L, 0, 0, 0)) { /* load the compile template functions */
- sprintf(out_buf, "cannot run lua: %s", lua_tostring(L, -1));
- //return NGX_HTTP_BAD_REQUEST;
- }
+ sprintf(out_buf, "nconfig=%snid=%snuri=%snret=%lxn", g_foo_settings, id, uri, ngx_random());
return NGX_OK;
}
@@ -195,6 +209,55 @@ ngx_http_lua_handler(ngx_http_request_t *r)
return NGX_HTTP_NOT_ALLOWED;
}
+
+ lua_State *cr = lua_newthread(L);
+#if 0
+ if (cr) {
+ /* new globals table for coroutine */
+ lua_newtable(cr);
+
+ /* {{{ inherit coroutine's globals to main thread's globals table
+ * for print() function will try to find tostring() in current
+ * globals * table. */
+ lua_createtable(cr, 0, 1);
+ lua_pushvalue(cr, LUA_GLOBALSINDEX);
+ lua_setfield(cr, -2, "__index");
+ lua_setmetatable(cr, -2);
+ /* }}} */
+
+ lua_replace(cr, LUA_GLOBALSINDEX);
+ }
+
+ if (cr == NULL) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "(lua-content-by-chunk) failed to create new coroutine "
+ "to handle request");
+
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ /* move code closure to new coroutine */
+ lua_xmove(L, cr, 1);
+
+ /* set closure's env table to new coroutine's globals table */
+ lua_pushvalue(cr, LUA_GLOBALSINDEX);
+ lua_setfenv(cr, -2);
+#endif
+ if (0!=ngx_http_lua_clfactory_loadfile(cr, "/usr/local/nginx/conf/test.lua")) { /* load the compile template functions */
+ //if (luaL_loadfile(cr, "/usr/local/nginx/conf/test.lua") || lua_pcall(cr, 0, 0, 0)) { /* load the compile template functions */
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "runtime error: %s", lua_tostring(cr, -1));
+ lua_pop(cr, 1);
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ //sprintf(out_buf, "%s", lua_tostring(cr, -1));
+ //lua_pop(cr, 1);
+
+ //lua_close(cr);
+
+
+
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -245,13 +308,17 @@ ngx_http_lua_process_init(ngx_cycle_t *cycle)
{
L = luaL_newstate();
if (L == NULL) {
- ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "Failed to initialize Lua VM");
- //ngx_log_error(NGX_ERROR, cycle->log, 0, "Failed to initialize Lua VM");
+ ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Failed to initialize Lua VM");
+ dd("Failed to initialize Lua VM");
return NGX_ERROR;
}
luaL_openlibs(L);
- ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "Lua VM initialized!");
+ //lua_pushcfunction(L, ngx_http_lua_print);
+ //lua_setglobal(L, "print");
+
+ dd("Lua VM initialized!");
+ //ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "Lua VM initialized!");
return NGX_OK;
}
@@ -262,3 +329,119 @@ ngx_http_lua_process_exit(ngx_cycle_t *cycle)
lua_close(L);
return;
}
+
+int
+ngx_http_lua_clfactory_loadfile(lua_State *L, const char *filename)
+{
+ clfactory_file_ctx_t lf;
+ int status, readstatus;
+ int c;
+
+ /* index of filename on the stack */
+ int fname_index;
+
+ fname_index = lua_gettop(L) + 1;
+
+ lf.extraline = 0;
+
+ if (filename == NULL) {
+ lua_pushliteral(L, "=stdin");
+ lf.f = stdin;
+
+ } else {
+ lua_pushfstring(L, "@%s", filename);
+ lf.f = fopen(filename, "r");
+
+ if (lf.f == NULL)
+ return clfactory_errfile(L, "open", fname_index);
+ }
+
+ c = getc(lf.f);
+
+ if (c == '#') { /* Unix exec. file? */
+ lf.extraline = 1;
+
+ while ((c = getc(lf.f)) != EOF && c != '\n') {
+ /* skip first line */
+ }
+
+ if (c == '\n') {
+ c = getc(lf.f);
+ }
+ }
+
+ if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
+ /* no binary file supported as closure factory code needs to be */
+ /* compiled to bytecode along with user code */
+ return clfactory_errfile(L, "load binary file", fname_index);
+ }
+
+ ungetc(c, lf.f);
+
+ lf.sent_begin = lf.sent_end = 0;
+ status = lua_load(L, clfactory_getF, &lf, lua_tostring(L, -1));
+
+ readstatus = ferror(lf.f);
+
+ if (filename)
+ fclose(lf.f); /* close file (even in case of errors) */
+
+ if (readstatus) {
+ lua_settop(L, fname_index); /* ignore results from `lua_load' */
+ return clfactory_errfile(L, "read", fname_index);
+ }
+
+ lua_remove(L, fname_index);
+
+ return status;
+}
+
+static int
+clfactory_errfile(lua_State *L, const char *what, int fname_index)
+{
+ const char *serr;
+ const char *filename;
+
+ serr = strerror(errno);
+ filename = lua_tostring(L, fname_index) + 1;
+
+ lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
+ lua_remove(L, fname_index);
+
+ return LUA_ERRFILE;
+}
+
+
+static const char *
+clfactory_getF(lua_State *L, void *ud, size_t *size)
+{
+ clfactory_file_ctx_t *lf;
+
+ lf = (clfactory_file_ctx_t *) ud;
+
+ if (lf->sent_begin == 0) {
+ lf->sent_begin = 1;
+ *size = CLFACTORY_BEGIN_SIZE;
+ return CLFACTORY_BEGIN_CODE;
+ }
+
+ if (lf->extraline) {
+ lf->extraline = 0;
+ *size = 1;
+ return "\n";
+ }
+
+ if (feof(lf->f)) {
+ if (lf->sent_end == 0) {
+ lf->sent_end = 1;
+ *size = CLFACTORY_END_SIZE;
+ return CLFACTORY_END_CODE;
+ }
+
+ return NULL;
+ }
+
+ *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
+
+ return (*size > 0) ? lf->buff : NULL;
+}

0 comments on commit f346373

Please sign in to comment.
Something went wrong with that request. Please try again.