From 52f6309844bcc13a1c9a29f7ab63e22f6ebed5fe Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Tue, 5 Dec 2017 23:01:44 +0800 Subject: [PATCH] Lua template: Add the headers variable Example: <%for k, v in pairs(_UHTTP["HEADERS"]) do%>

<%=k%>: <%=v%>

<%end%> Signed-off-by: Jianhui Zhao --- example/test.html | 3 +++ src/template.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/example/test.html b/example/test.html index 01996d0..61bd4c6 100644 --- a/example/test.html +++ b/example/test.html @@ -4,3 +4,6 @@

Remote Host: <%=_UHTTP["REMOTE_HOST"]%>

Url: <%=_UHTTP["HTTP_URL"]%>

Path: <%=_UHTTP["HTTP_PATH"]%>

+<%for k, v in pairs(_UHTTP["HEADERS"]) do%> +

<%=k%>: <%=v%>

+<%end%> diff --git a/src/template.c b/src/template.c index 1d4300f..5b71758 100755 --- a/src/template.c +++ b/src/template.c @@ -576,6 +576,7 @@ void uh_template(struct uh_connection *con) struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); struct uh_str *ustr; + int i; strcpy(path, con->srv->docroot); strncat(path, con->req.path.at, con->req.path.len); @@ -621,6 +622,19 @@ void uh_template(struct uh_connection *con) lua_pushlstring(L, ustr->at, ustr->len); lua_setfield(L, -2, "HTTP_PATH"); + lua_newtable(L); + + for (i = 0; i < con->req.header_num; i ++) { + struct uh_header *h = &con->req.header[i]; + char buf[128] = ""; + + snprintf(buf, sizeof(buf), "%.*s", (int)h->field.len, h->field.at); + lua_pushlstring(L, h->value.at, h->value.len); + lua_setfield(L, -2, buf); + } + + lua_setfield(L, -2, "HEADERS"); + lua_setglobal(L, "_UHTTP"); if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) {