* allow default path expanding in path/cpath and added tests

* added revim script to automatically insert vim modeline to sources
* added retab script to reexpand tabs for given files
* format sources and tests
This commit is contained in:
Wang Xiaozhe 2010-09-03 22:42:42 +08:00
Родитель 9f206095f0
Коммит 75b3362d5a
35 изменённых файлов: 461 добавлений и 245 удалений

1
TODO
Просмотреть файл

@ -1,5 +1,4 @@
Current: Current:
* include original path/cpath instead of override them all for lua_package_path/cpath
* add directives to run lua codes when nginx stops/reloads * add directives to run lua codes when nginx stops/reloads
* implement ngx.exec() functionality * implement ngx.exec() functionality
* deal with TCP 3-second delay problem under great connection harness * deal with TCP 3-second delay problem under great connection harness

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef DDEBUG_H #ifndef DDEBUG_H
#define DDEBUG_H #define DDEBUG_H

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#define DDEBUG 0 #define DDEBUG 0
#include "ddebug.h" #include "ddebug.h"

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_CACHE_H #ifndef NGX_HTTP_LUA_CACHE_H
#define NGX_HTTP_LUA_CACHE_H #define NGX_HTTP_LUA_CACHE_H

Просмотреть файл

@ -1,18 +1,19 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#include "ngx_http_lua_clfactory.h" #include "ngx_http_lua_clfactory.h"
typedef struct { typedef struct {
int sent_begin; int sent_begin;
int sent_end; int sent_end;
int extraline; int extraline;
FILE *f; FILE *f;
char buff[LUAL_BUFFERSIZE]; char buff[LUAL_BUFFERSIZE];
} clfactory_file_ctx_t; } clfactory_file_ctx_t;
typedef struct { typedef struct {
int sent_begin; int sent_begin;
int sent_end; int sent_end;
const char *s; const char *s;
size_t size; size_t size;
} clfactory_buffer_ctx_t; } clfactory_buffer_ctx_t;
static const char* clfactory_getF(lua_State *l, void *ud, size_t *size); static const char* clfactory_getF(lua_State *l, void *ud, size_t *size);
@ -22,129 +23,129 @@ static const char* clfactory_getS(lua_State *l, void *ud, size_t *size);
int int
ngx_http_lua_clfactory_loadfile(lua_State *l, const char *filename) ngx_http_lua_clfactory_loadfile(lua_State *l, const char *filename)
{ {
clfactory_file_ctx_t lf; clfactory_file_ctx_t lf;
int status, readstatus; int status, readstatus;
int c; int c;
int fnameindex = lua_gettop(l) + 1; /* index of filename on the stack */ int fnameindex = lua_gettop(l) + 1; /* index of filename on the stack */
lf.extraline = 0; lf.extraline = 0;
if (filename == NULL) { if (filename == NULL) {
lua_pushliteral(l, "=stdin"); lua_pushliteral(l, "=stdin");
lf.f = stdin; lf.f = stdin;
} else { } else {
lua_pushfstring(l, "@%s", filename); lua_pushfstring(l, "@%s", filename);
lf.f = fopen(filename, "r"); lf.f = fopen(filename, "r");
if (lf.f == NULL) if (lf.f == NULL)
return clfactory_errfile(l, "open", fnameindex); return clfactory_errfile(l, "open", fnameindex);
} }
c = getc(lf.f); c = getc(lf.f);
if (c == '#') { /* Unix exec. file? */ if (c == '#') { /* Unix exec. file? */
lf.extraline = 1; lf.extraline = 1;
while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
if (c == '\n') if (c == '\n')
c = getc(lf.f); c = getc(lf.f);
} }
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
// no binary file supported as closure factory code needs to be // no binary file supported as closure factory code needs to be
// compiled to bytecode along with user code // compiled to bytecode along with user code
return clfactory_errfile(l, "load binary file", fnameindex); return clfactory_errfile(l, "load binary file", fnameindex);
} }
ungetc(c, lf.f); ungetc(c, lf.f);
lf.sent_begin = lf.sent_end = 0; lf.sent_begin = lf.sent_end = 0;
status = lua_load(l, clfactory_getF, &lf, lua_tostring(l, -1)); status = lua_load(l, clfactory_getF, &lf, lua_tostring(l, -1));
readstatus = ferror(lf.f); readstatus = ferror(lf.f);
if (filename) if (filename)
fclose(lf.f); /* close file (even in case of errors) */ fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) { if (readstatus) {
lua_settop(l, fnameindex); /* ignore results from `lua_load' */ lua_settop(l, fnameindex); /* ignore results from `lua_load' */
return clfactory_errfile(l, "read", fnameindex); return clfactory_errfile(l, "read", fnameindex);
} }
lua_remove(l, fnameindex); lua_remove(l, fnameindex);
return status; return status;
} }
int ngx_http_lua_clfactory_loadstring(lua_State *l, const char *s) int ngx_http_lua_clfactory_loadstring(lua_State *l, const char *s)
{ {
return ngx_http_lua_clfactory_loadbuffer(l, s, strlen(s), s); return ngx_http_lua_clfactory_loadbuffer(l, s, strlen(s), s);
} }
int ngx_http_lua_clfactory_loadbuffer(lua_State *l, const char *buff, size_t size, const char *name) int ngx_http_lua_clfactory_loadbuffer(lua_State *l, const char *buff, size_t size, const char *name)
{ {
clfactory_buffer_ctx_t ls; clfactory_buffer_ctx_t ls;
ls.s = buff; ls.s = buff;
ls.size = size; ls.size = size;
ls.sent_begin = ls.sent_end = 0; ls.sent_begin = ls.sent_end = 0;
return lua_load(l, clfactory_getS, &ls, name); return lua_load(l, clfactory_getS, &ls, name);
} }
static const char* static const char*
clfactory_getF(lua_State *l, void *ud, size_t *size) clfactory_getF(lua_State *l, void *ud, size_t *size)
{ {
clfactory_file_ctx_t *lf = (clfactory_file_ctx_t *)ud; clfactory_file_ctx_t *lf = (clfactory_file_ctx_t *)ud;
(void)l; (void)l;
if(lf->sent_begin == 0) { if(lf->sent_begin == 0) {
lf->sent_begin = 1; lf->sent_begin = 1;
*size = CLFACTORY_BEGIN_SIZE; *size = CLFACTORY_BEGIN_SIZE;
return CLFACTORY_BEGIN_CODE; return CLFACTORY_BEGIN_CODE;
} }
if (lf->extraline) { if (lf->extraline) {
lf->extraline = 0; lf->extraline = 0;
*size = 1; *size = 1;
return "\n"; return "\n";
} }
if (feof(lf->f)) { if (feof(lf->f)) {
if(lf->sent_end == 0) { if(lf->sent_end == 0) {
lf->sent_end = 1; lf->sent_end = 1;
*size = CLFACTORY_END_SIZE; *size = CLFACTORY_END_SIZE;
return CLFACTORY_END_CODE; return CLFACTORY_END_CODE;
} }
return NULL; return NULL;
} }
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
return (*size > 0) ? lf->buff : NULL; return (*size > 0) ? lf->buff : NULL;
} }
static int static int
clfactory_errfile(lua_State *l, const char *what, int fnameindex) clfactory_errfile(lua_State *l, const char *what, int fnameindex)
{ {
const char *serr = strerror(errno); const char *serr = strerror(errno);
const char *filename = lua_tostring(l, fnameindex) + 1; const char *filename = lua_tostring(l, fnameindex) + 1;
lua_pushfstring(l, "cannot %s %s: %s", what, filename, serr); lua_pushfstring(l, "cannot %s %s: %s", what, filename, serr);
lua_remove(l, fnameindex); lua_remove(l, fnameindex);
return LUA_ERRFILE; return LUA_ERRFILE;
} }
static const char* static const char*
clfactory_getS(lua_State *l, void *ud, size_t *size) clfactory_getS(lua_State *l, void *ud, size_t *size)
{ {
clfactory_buffer_ctx_t *ls = (clfactory_buffer_ctx_t *)ud; clfactory_buffer_ctx_t *ls = (clfactory_buffer_ctx_t *)ud;
(void)l; (void)l;
if(ls->sent_begin == 0) { if(ls->sent_begin == 0) {
ls->sent_begin = 1; ls->sent_begin = 1;
*size = CLFACTORY_BEGIN_SIZE; *size = CLFACTORY_BEGIN_SIZE;
return CLFACTORY_BEGIN_CODE; return CLFACTORY_BEGIN_CODE;
} }
if(ls->size == 0) { if(ls->size == 0) {
if(ls->sent_end == 0) { if(ls->sent_end == 0) {
ls->sent_end = 1; ls->sent_end = 1;
*size = CLFACTORY_END_SIZE; *size = CLFACTORY_END_SIZE;
return CLFACTORY_END_CODE; return CLFACTORY_END_CODE;
} }
return NULL; return NULL;
} }
*size = ls->size; *size = ls->size;
ls->size = 0; ls->size = 0;
return ls->s; return ls->s;
} }

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_CLFACTORY_H__ #ifndef NGX_HTTP_LUA_CLFACTORY_H__
#define NGX_HTTP_LUA_CLFACTORY_H__ #define NGX_HTTP_LUA_CLFACTORY_H__

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_COMMON_H #ifndef NGX_HTTP_LUA_COMMON_H
#define NGX_HTTP_LUA_COMMON_H #define NGX_HTTP_LUA_COMMON_H

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#include "ngx_http_lua_conf.h" #include "ngx_http_lua_conf.h"
#include "ngx_http_lua_util.h" #include "ngx_http_lua_util.h"
@ -108,5 +109,4 @@ ngx_http_lua_init_vm(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
return NGX_CONF_OK; return NGX_CONF_OK;
} }
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_CONF_H__ #ifndef NGX_HTTP_LUA_CONF_H__
#define NGX_HTTP_LUA_CONF_H__ #define NGX_HTTP_LUA_CONF_H__
@ -11,5 +12,4 @@ extern char* ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *chi
#endif #endif
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#define DDEBUG 0 #define DDEBUG 0
#include "ngx_http_lua_contentby.h" #include "ngx_http_lua_contentby.h"
@ -325,6 +326,3 @@ error:
return; return;
} }
/* vi:ts=4 sw=4 fdm=marker
*/

Просмотреть файл

@ -1,15 +1,15 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_CONTENT_BY_H__ #ifndef NGX_HTTP_LUA_CONTENT_BY_H__
#define NGX_HTTP_LUA_CONTENT_BY_H__ #define NGX_HTTP_LUA_CONTENT_BY_H__
#include "ngx_http_lua_common.h" #include "ngx_http_lua_common.h"
extern ngx_int_t ngx_http_lua_content_by_chunk( extern ngx_int_t ngx_http_lua_content_by_chunk(
lua_State *l, lua_State *l,
ngx_http_request_t *r ngx_http_request_t *r
); );
extern void ngx_http_lua_wev_handler(ngx_http_request_t *r); extern void ngx_http_lua_wev_handler(ngx_http_request_t *r);
#endif #endif
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#define DDEBUG 0 #define DDEBUG 0
#include "ngx_http_lua_directive.h" #include "ngx_http_lua_directive.h"

Просмотреть файл

@ -1,19 +1,20 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_DIRECTIVE_H #ifndef NGX_HTTP_LUA_DIRECTIVE_H
#define NGX_HTTP_LUA_DIRECTIVE_H #define NGX_HTTP_LUA_DIRECTIVE_H
#include "ngx_http_lua_common.h" #include "ngx_http_lua_common.h"
char * ngx_http_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd, char * ngx_http_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
char * ngx_http_lua_package_path(ngx_conf_t *cf, ngx_command_t *cmd, char * ngx_http_lua_package_path(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
char * ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, char * ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
char * ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, char * ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
ngx_int_t ngx_http_lua_filter_set_by_lua_inline(ngx_http_request_t *r, ngx_int_t ngx_http_lua_filter_set_by_lua_inline(ngx_http_request_t *r,
ngx_str_t *val, ngx_http_variable_value_t *v, void *data); ngx_str_t *val, ngx_http_variable_value_t *v, void *data);

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#include "ngx_http_lua_filter.h" #include "ngx_http_lua_filter.h"
#include "ngx_http_lua_util.h" #include "ngx_http_lua_util.h"
@ -10,51 +11,50 @@ static ngx_int_t ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in
ngx_int_t ngx_int_t
ngx_http_lua_filter_init(ngx_conf_t *cf) ngx_http_lua_filter_init(ngx_conf_t *cf)
{ {
ngx_http_lua_next_header_filter = ngx_http_top_header_filter; ngx_http_lua_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_lua_header_filter; ngx_http_top_header_filter = ngx_http_lua_header_filter;
ngx_http_lua_next_body_filter = ngx_http_top_body_filter; ngx_http_lua_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_lua_body_filter; ngx_http_top_body_filter = ngx_http_lua_body_filter;
return NGX_OK; return NGX_OK;
} }
static ngx_int_t static ngx_int_t
ngx_http_lua_header_filter(ngx_http_request_t *r) ngx_http_lua_header_filter(ngx_http_request_t *r)
{ {
ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module); ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if(ctx && ctx->capture) { if(ctx && ctx->capture) {
// force subrequest response body buffer in memory // force subrequest response body buffer in memory
r->filter_need_in_memory = 1; r->filter_need_in_memory = 1;
return NGX_OK; return NGX_OK;
} }
return ngx_http_lua_next_header_filter(r); return ngx_http_lua_next_header_filter(r);
} }
static ngx_int_t static ngx_int_t
ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in) ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{ {
int rc; int rc;
ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module); ngx_http_lua_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if(!ctx || !ctx->capture) { if(!ctx || !ctx->capture) {
return ngx_http_lua_next_body_filter(r, in); return ngx_http_lua_next_body_filter(r, in);
} }
rc = ngx_http_lua_add_copy_chain(r->pool, rc = ngx_http_lua_add_copy_chain(r->pool,
&ctx->body, in); &ctx->body, in);
if (rc != NGX_OK) { if (rc != NGX_OK) {
return NGX_ERROR; return NGX_ERROR;
} }
ngx_http_lua_discard_bufs(r->pool, in); ngx_http_lua_discard_bufs(r->pool, in);
return NGX_OK; return NGX_OK;
} }
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_FILTER_H__ #ifndef NGX_HTTP_LUA_FILTER_H__
#define NGX_HTTP_LUA_FILTER_H__ #define NGX_HTTP_LUA_FILTER_H__
@ -7,5 +8,4 @@ extern ngx_int_t ngx_http_lua_filter_init(ngx_conf_t *cf);
#endif #endif
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#include "ngx_http_lua_hook.h" #include "ngx_http_lua_hook.h"
#include "ngx_http_lua_util.h" #include "ngx_http_lua_util.h"
#include "ngx_http_lua_contentby.h" #include "ngx_http_lua_contentby.h"
@ -44,19 +45,19 @@ ngx_http_lua_atpanic(lua_State *L)
} }
/* restore nginx execution */ /* restore nginx execution */
NGX_LUA_EXCEPTION_THROW(1); NGX_LUA_EXCEPTION_THROW(1);
} }
static void static void
log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L) log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
{ {
const char *s; const char *s;
/* XXX: easy way to support multiple args, any serious performance penalties? */ /* XXX: easy way to support multiple args, any serious performance penalties? */
lua_concat(L, lua_gettop(L)); lua_concat(L, lua_gettop(L));
s = lua_tostring(L, -1); s = lua_tostring(L, -1);
ngx_log_error(level, r->connection->log, 0, "(%s) %s", ident, (s == NULL) ? "(null)" : s); ngx_log_error(level, r->connection->log, 0, "(%s) %s", ident, (s == NULL) ? "(null)" : s);
} }
/** /**
@ -76,12 +77,12 @@ ngx_http_lua_ngx_log(lua_State *L)
lua_pop(L, 1); lua_pop(L, 1);
if(r && r->connection && r->connection->log) { if(r && r->connection && r->connection->log) {
int level = luaL_checkint(L, 1); int level = luaL_checkint(L, 1);
// remove log-level param from stack // remove log-level param from stack
lua_remove(L, 1); lua_remove(L, 1);
log_wrapper(r, "lua-log", level, L); log_wrapper(r, "lua-log", level, L);
} else { } else {
dd("(lua-log) can't output log due to invalid logging context!"); dd("(lua-log) can't output log due to invalid logging context!");
} }
@ -106,7 +107,7 @@ ngx_http_lua_print(lua_State *L)
lua_pop(L, 1); lua_pop(L, 1);
if(r && r->connection && r->connection->log) { if(r && r->connection && r->connection->log) {
log_wrapper(r, "lua-print", NGX_LOG_ERR, L); log_wrapper(r, "lua-print", NGX_LOG_ERR, L);
} else { } else {
dd("(lua-print) can't output print content to error log due to invalid logging context!"); dd("(lua-print) can't output print content to error log due to invalid logging context!");
} }

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_HOOK_H #ifndef NGX_HTTP_LUA_HOOK_H
#define NGX_HTTP_LUA_HOOK_H #define NGX_HTTP_LUA_HOOK_H

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#include "ngx_http_lua_directive.h" #include "ngx_http_lua_directive.h"
#include "ngx_http_lua_conf.h" #include "ngx_http_lua_conf.h"
#include "ngx_http_lua_filter.h" #include "ngx_http_lua_filter.h"

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#define DDEBUG 0 #define DDEBUG 0
#include "ngx_http_lua_setby.h" #include "ngx_http_lua_setby.h"
#include "ngx_http_lua_hook.h" #include "ngx_http_lua_hook.h"
@ -144,7 +145,7 @@ ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r, ngx_str_t *val,
return NGX_ERROR; return NGX_ERROR;
} }
NGX_LUA_EXCEPTION_TRY { NGX_LUA_EXCEPTION_TRY {
size_t rlen; size_t rlen;
const char *rdata = lua_tolstring(L, -1, &rlen); const char *rdata = lua_tolstring(L, -1, &rlen);
@ -162,9 +163,9 @@ ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r, ngx_str_t *val,
val->data = NULL; val->data = NULL;
val->len = 0; val->len = 0;
} }
} NGX_LUA_EXCEPTION_CATCH { } NGX_LUA_EXCEPTION_CATCH {
dd("NginX execution restored"); dd("NginX execution restored");
} }
/* clear Lua stack */ /* clear Lua stack */
lua_settop(L, 0); lua_settop(L, 0);

Просмотреть файл

@ -1,17 +1,17 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_SET_BY_H__ #ifndef NGX_HTTP_LUA_SET_BY_H__
#define NGX_HTTP_LUA_SET_BY_H__ #define NGX_HTTP_LUA_SET_BY_H__
#include "ngx_http_lua_common.h" #include "ngx_http_lua_common.h"
extern ngx_int_t ngx_http_lua_set_by_chunk( extern ngx_int_t ngx_http_lua_set_by_chunk(
lua_State *l, lua_State *l,
ngx_http_request_t *r, ngx_http_request_t *r,
ngx_str_t *val, ngx_str_t *val,
ngx_http_variable_value_t *args, ngx_http_variable_value_t *args,
size_t nargs size_t nargs
); );
#endif #endif
// vi:ts=4 sw=4 fdm=marker

Просмотреть файл

@ -1,13 +1,30 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#define DDEBUG 0 #define DDEBUG 0
#include "ngx_http_lua_util.h" #include "ngx_http_lua_util.h"
#include "ngx_http_lua_hook.h" #include "ngx_http_lua_hook.h"
#define LUA_PATH_SEP ";"
#define AUX_MARK "\1"
static void init_ngx_lua_registry(lua_State *L); static void init_ngx_lua_registry(lua_State *L);
static void init_ngx_lua_globals(lua_State *L); static void init_ngx_lua_globals(lua_State *L);
static void inject_http_consts(lua_State *L); static void inject_http_consts(lua_State *L);
static void inject_log_consts(lua_State *L); static void inject_log_consts(lua_State *L);
static void setpath(lua_State *L, int tab_idx, const char *fieldname, const char *path, const char *def);
static void
setpath(lua_State *L, int tab_idx, const char *fieldname, const char *path, const char *def)
{
const char *tmp_path;
tmp_path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, LUA_PATH_SEP AUX_MARK LUA_PATH_SEP);
luaL_gsub(L, tmp_path, AUX_MARK, def);
lua_remove(L, -2);
/* fix negative index as there's new data on stack */
tab_idx = (tab_idx < 0) ? (tab_idx - 1) : tab_idx;
lua_setfield(L, tab_idx, fieldname);
}
lua_State * lua_State *
ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf) ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
@ -28,15 +45,33 @@ ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
} }
if (lmcf->lua_path.len != 0) { if (lmcf->lua_path.len != 0) {
lua_pushlstring(L, (char *) lmcf->lua_path.data, lmcf->lua_path.len); const char *old_path;
const char *new_path;
lua_setfield(L, -2, "path"); lua_getfield(L, -1, "path"); /* get original package.path */
old_path = lua_tostring(L, -1);
lua_pushlstring(L, (char *) lmcf->lua_path.data, lmcf->lua_path.len);
new_path = lua_tostring(L, -1);
setpath(L, -3, "path", new_path, old_path);
lua_pop(L, 2);
} }
if (lmcf->lua_cpath.len != 0) { if (lmcf->lua_cpath.len != 0) {
lua_pushlstring(L, (char *) lmcf->lua_cpath.data, lmcf->lua_cpath.len); const char *old_cpath;
const char *new_cpath;
lua_setfield(L, -2, "cpath"); lua_getfield(L, -1, "cpath"); /* get original package.cpath */
old_cpath = lua_tostring(L, -1);
lua_pushlstring(L, (char *) lmcf->lua_cpath.data, lmcf->lua_cpath.len);
new_cpath = lua_tostring(L, -1);
setpath(L, -3, "cpath", new_cpath, old_cpath);
lua_pop(L, 2);
} }
@ -316,32 +351,32 @@ inject_http_consts(lua_State *L)
static void static void
inject_log_consts(lua_State *L) inject_log_consts(lua_State *L)
{ {
lua_pushinteger(L, NGX_LOG_STDERR); lua_pushinteger(L, NGX_LOG_STDERR);
lua_setfield(L, -2, "STDERR"); lua_setfield(L, -2, "STDERR");
lua_pushinteger(L, NGX_LOG_EMERG); lua_pushinteger(L, NGX_LOG_EMERG);
lua_setfield(L, -2, "EMERG"); lua_setfield(L, -2, "EMERG");
lua_pushinteger(L, NGX_LOG_ALERT); lua_pushinteger(L, NGX_LOG_ALERT);
lua_setfield(L, -2, "ALERT"); lua_setfield(L, -2, "ALERT");
lua_pushinteger(L, NGX_LOG_CRIT); lua_pushinteger(L, NGX_LOG_CRIT);
lua_setfield(L, -2, "CRIT"); lua_setfield(L, -2, "CRIT");
lua_pushinteger(L, NGX_LOG_ERR); lua_pushinteger(L, NGX_LOG_ERR);
lua_setfield(L, -2, "ERR"); lua_setfield(L, -2, "ERR");
lua_pushinteger(L, NGX_LOG_WARN); lua_pushinteger(L, NGX_LOG_WARN);
lua_setfield(L, -2, "WARN"); lua_setfield(L, -2, "WARN");
lua_pushinteger(L, NGX_LOG_NOTICE); lua_pushinteger(L, NGX_LOG_NOTICE);
lua_setfield(L, -2, "NOTICE"); lua_setfield(L, -2, "NOTICE");
lua_pushinteger(L, NGX_LOG_INFO); lua_pushinteger(L, NGX_LOG_INFO);
lua_setfield(L, -2, "INFO"); lua_setfield(L, -2, "INFO");
lua_pushinteger(L, NGX_LOG_DEBUG); lua_pushinteger(L, NGX_LOG_DEBUG);
lua_setfield(L, -2, "DEBUG"); lua_setfield(L, -2, "DEBUG");
} }
static void static void
@ -369,8 +404,8 @@ init_ngx_lua_globals(lua_State *L)
lua_pushcfunction(L, ngx_http_lua_ngx_say); lua_pushcfunction(L, ngx_http_lua_ngx_say);
lua_setfield(L, -2, "say"); lua_setfield(L, -2, "say");
lua_pushcfunction(L, ngx_http_lua_ngx_log); lua_pushcfunction(L, ngx_http_lua_ngx_log);
lua_setfield(L, -2, "log"); lua_setfield(L, -2, "log");
lua_pushcfunction(L, ngx_http_lua_ngx_throw_error); lua_pushcfunction(L, ngx_http_lua_ngx_throw_error);
lua_setfield(L, -2, "throw_error"); lua_setfield(L, -2, "throw_error");
@ -402,8 +437,8 @@ init_ngx_lua_globals(lua_State *L)
lua_setfield(L, -2, "location"); lua_setfield(L, -2, "location");
/* }}} */ /* }}} */
inject_http_consts(L); inject_http_consts(L);
inject_log_consts(L); inject_log_consts(L);
/* {{{ register reference maps */ /* {{{ register reference maps */
lua_newtable(L); /* .var */ lua_newtable(L); /* .var */

Просмотреть файл

@ -1,3 +1,4 @@
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef NGX_HTTP_LUA_UTIL_H #ifndef NGX_HTTP_LUA_UTIL_H
#define NGX_HTTP_LUA_UTIL_H #define NGX_HTTP_LUA_UTIL_H

Просмотреть файл

@ -1,4 +1,4 @@
# vi:filetype=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;
@ -12,9 +12,9 @@ __DATA__
=== TEST 1: sanity (integer) === TEST 1: sanity (integer)
--- config --- config
location /lua { location /lua {
echo 2; echo 2;
} }
--- request --- request
GET /lua GET /lua
--- response_body --- response_body
@ -24,9 +24,9 @@ GET /lua
=== TEST 2: sanity (string) === TEST 2: sanity (string)
--- config --- config
location /lua { location /lua {
echo "helloworld"; echo "helloworld";
} }
--- request --- request
GET /lua GET /lua
--- response_body --- response_body

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft= # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft= # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft= # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;
@ -34,9 +34,10 @@ __DATA__
location /load { location /load {
content_by_lua ' content_by_lua '
package.loaded.foo = null; package.loaded.foo = nil;
local foo = require "foo"; local foo = require "foo";
foo.hi()'; foo.hi()
';
} }
location /check { location /check {
@ -85,3 +86,63 @@ GET /main
--- user_files --- user_files
--- response_body_like: ^[^;]+/servroot/html/\?.so$ --- response_body_like: ^[^;]+/servroot/html/\?.so$
=== TEST 3: expand default path (after)
--- http_config eval
"lua_package_path '$::HtmlDir/?.lua;;';"
--- config
location /main {
content_by_lua '
ngx.print(package.path);
';
}
--- request
GET /main
--- response_body_like: ^[^;]+/servroot/html/\?.lua;.+\.lua;$
=== TEST 4: expand default cpath (after)
--- http_config eval
"lua_package_cpath '$::HtmlDir/?.so;;';"
--- config
location /main {
content_by_lua '
ngx.print(package.cpath);
';
}
--- request
GET /main
--- response_body_like: ^[^;]+/servroot/html/\?.so;.+\.so;$
=== TEST 5: expand default path (before)
--- http_config eval
"lua_package_path ';;$::HtmlDir/?.lua';"
--- config
location /main {
content_by_lua '
ngx.print(package.path);
';
}
--- request
GET /main
--- response_body_like: ^.+\.lua;[^;]+/servroot/html/\?.lua$
=== TEST 6: expand default cpath (before)
--- http_config eval
"lua_package_cpath ';;$::HtmlDir/?.so';"
--- config
location /main {
content_by_lua '
ngx.print(package.cpath);
';
}
--- request
GET /main
--- response_body_like: ^.+\.so;[^;]+/servroot/html/\?.so$

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,4 +1,4 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;

Просмотреть файл

@ -1,10 +1,10 @@
# vi:ft=perl # vim:set ft=perl ts=4 sw=4 et fdm=marker:
use lib 'lib'; use lib 'lib';
use Test::Nginx::Socket; use Test::Nginx::Socket;
#worker_connections(1014); #worker_connections(1014);
#master_process_enabled(1); #master_process_enabled(1);
log_level('debug'); # to ensure any log-level can be outputed log_level('debug'); # to ensure any log-level can be outputed
repeat_each(1); repeat_each(1);
@ -20,10 +20,10 @@ __DATA__
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.STDERR, "hello, log", 1234, 3.14159) ngx.log(ngx.STDERR, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -37,10 +37,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.EMERG, "hello, log", 1234, 3.14159) ngx.log(ngx.EMERG, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -54,10 +54,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.ALERT, "hello, log", 1234, 3.14159) ngx.log(ngx.ALERT, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -71,10 +71,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.CRIT, "hello, log", 1234, 3.14159) ngx.log(ngx.CRIT, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -88,10 +88,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.ERR, "hello, log", 1234, 3.14159) ngx.log(ngx.ERR, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -105,10 +105,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.WARN, "hello, log", 1234, 3.14159) ngx.log(ngx.WARN, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -122,10 +122,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.NOTICE, "hello, log", 1234, 3.14159) ngx.log(ngx.NOTICE, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -139,10 +139,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.INFO, "hello, log", 1234, 3.14159) ngx.log(ngx.INFO, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -156,10 +156,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
ngx.log(ngx.DEBUG, "hello, log", 1234, 3.14159) ngx.log(ngx.DEBUG, "hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log
@ -173,10 +173,10 @@ after log
--- config --- config
location /log { location /log {
content_by_lua ' content_by_lua '
ngx.say("before log") ngx.say("before log")
print("hello, log", 1234, 3.14159) print("hello, log", 1234, 3.14159)
ngx.say("after log") ngx.say("after log")
'; ';
} }
--- request --- request
GET /log GET /log

8
util/retab Executable file
Просмотреть файл

@ -0,0 +1,8 @@
#!/bin/bash
for f in $*; do
if [ -f "$f" ]; then
vim -c retab -c x $f
fi
done

102
util/revim Executable file
Просмотреть файл

@ -0,0 +1,102 @@
#!/usr/bin/perl
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
#
# revim - add customized vim modeline for given files
# Copyright (c) 2010 chaoslawful
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopts('hm:', \%opts);
if($opts{h} or !@ARGV) {
die <<EOT;
Usage: revim [-m <vim modeline>] src/*
In <vim modeline>, the following placeholder(s) can be used:
* %t - Expands to vim file type. E.g. 'c' for .c files, 'perl' for .pl files.
EOT
}
my $vim_ml = $opts{m} || "vim:set ft=%t ts=4 sw=4 et fdm=marker:";
my @files = map glob, @ARGV;
for my $file (@files) {
next if -d $file;
my ($ft, $ml) = detect_filetype($file, $vim_ml);
next if !defined($ft);
revim($file, $ml);
}
sub detect_filetype
{
my ($f, $tmpl) = @_;
my ($ft, $lcmt, $rcmt);
my %phs;
if($f =~ /.([cC]|[hH])$/) {
$ft = "c";
($lcmt, $rcmt) = ("/* ", " */");
} elsif($f =~ /.(pl|pm)$/) {
$ft = "perl";
($lcmt, $rcmt) = ("# ", "");
} elsif($f =~ /.t_?$/) {
# assuming tests are written in perl
$ft = "perl";
($lcmt, $rcmt) = ("# ", "");
} else {
$ft = undef;
}
if(defined($ft)) {
%phs = (
"%t" => $ft,
);
$tmpl =~ s/(%[a-z])/$phs{$1}/ge;
$tmpl =~ s/^/$lcmt/;
$tmpl =~ s/$/$rcmt/;
return ($ft, $tmpl);
}
return (undef, undef);
}
sub revim
{
my ($f, $ml) = @_;
my @lines;
open my $in, $f
or die "Can't open $f for reading: $!";
while(<$in>) {
push(@lines, $_);
}
close $in;
my @nlines = grep {!/\bvim?:/} @lines;
warn "revim: $f:\tremoved existing vim modeline.\n"
if(@nlines != @lines);
if($nlines[0] =~ /^#!/) { # has shebang line
my $shebang = shift @nlines;
unshift(@nlines, $shebang, "$ml\n");
} else {
unshift(@nlines, "$ml\n");
}
my $text = join '', @nlines;
open my $out, "> $f"
or die "Can't open $f for writing: $!";
binmode $out;
print $out $text;
close $out;
warn "revim: $f:\tdone.\n";
}