]> granicus.if.org Git - apache/commitdiff
Add r:setcookie(key, val, secure, expires) and r:getcookie(key) to the request_rec...
authorDaniel Gruno <humbedooh@apache.org>
Wed, 4 Sep 2013 10:47:46 +0000 (10:47 +0000)
committerDaniel Gruno <humbedooh@apache.org>
Wed, 4 Sep 2013 10:47:46 +0000 (10:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1519977 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
docs/manual/mod/mod_lua.xml
modules/lua/lua_request.c

diff --git a/CHANGES b/CHANGES
index 0b1418cb6f42c165a92d02610882a115b1c00aa6..f142a7d0dc2a4237c63815bb835ac5021908e3ec 100644 (file)
--- 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 <berni birkenwald de>]
index a3f1e5e7f544f8f81d17cdac0cd85b195025cd59..3488d417ac56d3f9241dc5a9c74edf16200f9f99 100644 (file)
@@ -967,6 +967,15 @@ end
 r.date_parse_rfc(string) -- Parses a date/time string and returns seconds since epoche.
 </highlight>
 
+<highlight language="lua">
+r:getcookie(key) -- Gets a HTTP cookie
+</highlight>
+
+<highlight language="lua">
+r:setcookie(key, value, secure, expires) -- Sets a HTTP cookie, for instance:
+r:setcookie("foo", "bar and stuff", false, os.time() + 86400)
+</highlight>
+
 </section>
 
 <section id="logging"><title>Logging Functions</title>
index eb6aef1d164a4a8a39de00f977ef5208dc622f6f..220bfbaf35dc1e27b143984b2ee96238a5728bae 100644 (file)
@@ -26,6 +26,7 @@
 #include "apr_date.h"
 #include "apr_pools.h"
 #include "apr_thread_mutex.h"
+#include "apr_tables.h"
 
 #include <lua.h>
 
@@ -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");