From 38024c1665f7d78c6854a52204361dbeb64bed14 Mon Sep 17 00:00:00 2001 From: Daniel Gruno Date: Wed, 4 Sep 2013 10:47:46 +0000 Subject: [PATCH] Add r:setcookie(key, val, secure, expires) and r:getcookie(key) to the request_rec table. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1519977 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 3 +++ docs/manual/mod/mod_lua.xml | 9 +++++++ modules/lua/lua_request.c | 51 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/CHANGES b/CHANGES index 0b1418cb6f..f142a7d0dc 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,9 @@ -*- coding: utf-8 -*- Changes with Apache 2.5.0 + *) mod_lua: add setcookie and getcookie functions to the request_rec struct. + [Daniel Gruno] + *) mod_status, mod_echo: Fix the display of client addresses. They were truncated to 31 characters which is not enough for IPv6 addresses. PR 54848 [Bernhard Schmidt ] diff --git a/docs/manual/mod/mod_lua.xml b/docs/manual/mod/mod_lua.xml index a3f1e5e7f5..3488d417ac 100644 --- a/docs/manual/mod/mod_lua.xml +++ b/docs/manual/mod/mod_lua.xml @@ -967,6 +967,15 @@ end r.date_parse_rfc(string) -- Parses a date/time string and returns seconds since epoche. + +r:getcookie(key) -- Gets a HTTP cookie + + + +r:setcookie(key, value, secure, expires) -- Sets a HTTP cookie, for instance: +r:setcookie("foo", "bar and stuff", false, os.time() + 86400) + +
Logging Functions diff --git a/modules/lua/lua_request.c b/modules/lua/lua_request.c index eb6aef1d16..220bfbaf35 100644 --- a/modules/lua/lua_request.c +++ b/modules/lua/lua_request.c @@ -26,6 +26,7 @@ #include "apr_date.h" #include "apr_pools.h" #include "apr_thread_mutex.h" +#include "apr_tables.h" #include @@ -839,6 +840,7 @@ static int lua_apr_sha1(lua_State *L) apr_sha1_init(&sha1); apr_sha1_update(&sha1, buffer, len); apr_sha1_final(digest, &sha1); + ap_bin2hex(digest, sizeof(digest), result); lua_pushstring(L, result); return 1; @@ -1887,6 +1889,50 @@ static int lua_ivm_set(lua_State *L) return 0; } +static int lua_get_cookie(lua_State *L) +{ + const char *key, *cookies, *pattern; + char *cookie; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + cookie = apr_pcalloc(r->pool, 256); + cookies = apr_table_get(r->headers_in, "Cookie"); + pattern = apr_psprintf(r->pool, "%s=%%255[^;]", key); + sscanf(cookies, pattern, cookie); + if (strlen(cookie) > 0) { + lua_pushstring(L, cookie); + return 1; + } + return 0; +} + +static int lua_set_cookie(lua_State *L) +{ + const char *key, *value, *out, *strexpires; + int secure, expires; + char cdate[APR_RFC822_DATE_LEN+1]; + apr_status_t rv; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + value = luaL_checkstring(L, 3); + secure = 0; + if (lua_isboolean(L, 4)) { + secure = lua_toboolean(L, 4); + } + expires = luaL_optinteger(L, 5, 0); + strexpires = ""; + if (expires > 0) { + rv = apr_rfc822_date(cdate, apr_time_from_sec(expires)); + if (rv == APR_SUCCESS) { + strexpires = apr_psprintf(r->pool, "Expires=%s", cdate); + } + } + out = apr_psprintf(r->pool, "%s=%s; %s %s", key, value, secure ? "Secure;" : "", expires ? strexpires : ""); + apr_table_set(r->headers_out, "Set-Cookie", out); + return 0; +} + + #define APLUA_REQ_TRACE(lev) static int req_trace##lev(lua_State *L) \ { \ return req_log_at(L, APLOG_TRACE##lev); \ @@ -1994,6 +2040,7 @@ static const char* lua_ap_get_server_name(request_rec* r) + static const struct luaL_Reg server_methods[] = { {NULL, NULL} }; @@ -2236,6 +2283,10 @@ void ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p) makefun(&lua_ivm_get, APL_REQ_FUNTYPE_LUACFUN, p)); apr_hash_set(dispatch, "ivm_set", APR_HASH_KEY_STRING, makefun(&lua_ivm_set, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "getcookie", APR_HASH_KEY_STRING, + makefun(&lua_get_cookie, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "setcookie", APR_HASH_KEY_STRING, + makefun(&lua_set_cookie, APL_REQ_FUNTYPE_LUACFUN, p)); lua_pushlightuserdata(L, dispatch); lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch"); -- 2.40.0