Lua template: Add Http Variables

<h1>------------Http Variables-------------</h1>
<%for k, v in pairs(_UHTTP["VARIABLES"]) do%>
<h1><%=k%>: <%=v%></h1>
<%end%>

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2017-12-09 23:28:32 +08:00
parent 38688ef5f6
commit 6f0f62faa8
2 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,13 @@
<h1>Remote Host: <%=_UHTTP["REMOTE_HOST"]%></h1>
<h1>Url: <%=_UHTTP["HTTP_URL"]%></h1>
<h1>Path: <%=_UHTTP["HTTP_PATH"]%></h1>
<h1>------------Http Headers-------------</h1>
<%for k, v in pairs(_UHTTP["HEADERS"]) do%>
<h1><%=k%>: <%=v%></h1>
<%end%>
<h1>------------Http Variables-------------</h1>
<%for k, v in pairs(_UHTTP["VARIABLES"]) do%>
<h1><%=k%>: <%=v%></h1>
<%end%>

View File

@ -565,6 +565,17 @@ static void child_cb(struct ev_loop *loop, ev_child *w, int revents)
ev_child_stop(con->srv->loop, w);
}
static bool found_var(struct uh_str *key, struct uh_str *val, void *udata)
{
lua_State *L = (lua_State *)udata;
lua_pushlstring(L, key->at, key->len);
lua_pushlstring(L, val->at, val->len);
lua_settable(L, -3);
return false;
}
void uh_template(struct uh_connection *con)
{
struct template_parser *parser;
@ -638,6 +649,10 @@ void uh_template(struct uh_connection *con)
lua_setfield(L, -2, "HEADERS");
lua_newtable(L);
uh_foreach_var(con, found_var, L);
lua_setfield(L, -2, "VARIABLES");
lua_setglobal(L, "_UHTTP");
if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) {