httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From humbed...@apache.org
Subject svn commit: r1519977 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml modules/lua/lua_request.c
Date Wed, 04 Sep 2013 10:47:46 GMT
Author: humbedooh
Date: Wed Sep  4 10:47:46 2013
New Revision: 1519977

URL: http://svn.apache.org/r1519977
Log:
Add r:setcookie(key, val, secure, expires) and r:getcookie(key) to the request_rec table.

Modified:
    httpd/httpd/trunk/CHANGES
    httpd/httpd/trunk/docs/manual/mod/mod_lua.xml
    httpd/httpd/trunk/modules/lua/lua_request.c

Modified: httpd/httpd/trunk/CHANGES
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/CHANGES?rev=1519977&r1=1519976&r2=1519977&view=diff
==============================================================================
--- httpd/httpd/trunk/CHANGES [utf-8] (original)
+++ httpd/httpd/trunk/CHANGES [utf-8] Wed Sep  4 10:47:46 2013
@@ -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>]

Modified: httpd/httpd/trunk/docs/manual/mod/mod_lua.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/mod_lua.xml?rev=1519977&r1=1519976&r2=1519977&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/mod/mod_lua.xml (original)
+++ httpd/httpd/trunk/docs/manual/mod/mod_lua.xml Wed Sep  4 10:47:46 2013
@@ -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>

Modified: httpd/httpd/trunk/modules/lua/lua_request.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/lua_request.c?rev=1519977&r1=1519976&r2=1519977&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/lua_request.c (original)
+++ httpd/httpd/trunk/modules/lua/lua_request.c Wed Sep  4 10:47:46 2013
@@ -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_nam
 
 
 
+
 static const struct luaL_Reg server_methods[] = {
     {NULL, NULL}
 };
@@ -2236,6 +2283,10 @@ void ap_lua_load_request_lmodule(lua_Sta
                  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");



Mime
View raw message