You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2012/06/12 17:51:36 UTC

[18/50] git commit: Keep the remap status with the Lua userdata.

Keep the remap status with the Lua userdata.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/c55141ab
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/c55141ab
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/c55141ab

Branch: refs/heads/jpeach/lua
Commit: c55141ab1453303e6bcd871532dcd7a5a3dd2c68
Parents: 9174b44
Author: James Peach <jp...@apache.org>
Authored: Mon Apr 16 20:59:30 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jun 12 08:48:36 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lapi.cc |   42 ++++++++++++++++++++++++++++++------------
 plugins/lua/lapi.h  |   17 ++++++++++++++++-
 plugins/lua/lua.cc  |   34 ++++++++++------------------------
 3 files changed, 56 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/c55141ab/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index 947f81d..dcaa6ae 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -20,6 +20,24 @@
 #include <ts/remap.h>
 #include "lapi.h"
 
+LuaRemapRequest *
+LuaRemapRequest::get(lua_State * lua, int index)
+{
+  return (LuaRemapRequest *)luaL_checkudata(lua, index, "ts.meta.rri");
+}
+
+LuaRemapRequest *
+LuaRemapRequest::alloc(lua_State * lua)
+{
+  LuaRemapRequest * rq;
+
+  rq = (LuaRemapRequest *)lua_newuserdata(lua, sizeof(LuaRemapRequest));
+  luaL_getmetatable(lua, "ts.meta.rri");
+  lua_setmetatable(lua, -2);
+
+    return rq;
+  }
+
 bool
 LuaPushUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url)
 {
@@ -69,16 +87,18 @@ LuaPushUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url)
 static int
 LuaRemapRedirect(lua_State * lua)
 {
-  TSRemapRequestInfo * rri;
+  LuaRemapRequest * rq;
 
-  rri = *(TSRemapRequestInfo **)luaL_checkudata(lua, 1, "ts.meta.rri");
+  rq = LuaRemapRequest::get(lua, 1);
   luaL_checktype(lua, 2, LUA_TTABLE);
 
-  TSDebug("lua", "redirecting request %p", rri);
+  TSDebug("lua", "redirecting request %p", rq->rri);
 
   // XXX take the URL table argument and use it to rewrite rri->requestUrl ...
 
-  rri->redirect = 1;
+  // A redirect always terminates plugin chain evaluation.
+  rq->rri->redirect = 1;
+  rq->status = TSREMAP_DID_REMAP_STOP;
 
   // Return true back to Lua-space.
   lua_pushboolean(lua, 1);
@@ -91,19 +111,17 @@ static const luaL_Reg RRI[] =
   { NULL, NULL}
 };
 
-bool
+LuaRemapRequest *
 LuaPushRemapRequestInfo(lua_State * lua, TSRemapRequestInfo * rri)
 {
-  TSRemapRequestInfo ** ptr;
+  LuaRemapRequest * rq;
 
-  ptr = (TSRemapRequestInfo **)lua_newuserdata(lua, sizeof(TSRemapRequestInfo *));
-  *ptr = rri;
-
-  luaL_getmetatable(lua, "ts.meta.rri");
-  lua_setmetatable(lua, -2);
+  rq = LuaRemapRequest::alloc(lua);
+  rq->rri = rri;
+  rq->status = TSREMAP_NO_REMAP;
 
   TSReleaseAssert(lua_isuserdata(lua, -1) == 1);
-  return true;
+  return rq;
 }
 
 static int

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/c55141ab/plugins/lua/lapi.h
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.h b/plugins/lua/lapi.h
index aff426c..8ad0495 100644
--- a/plugins/lua/lapi.h
+++ b/plugins/lua/lapi.h
@@ -16,12 +16,24 @@
   limitations under the License.
 */
 
+#ifndef LUA_LAPI_H_
+#define LUA_LAPI_H_
+
 extern "C" {
 #include <lua.h>
 #include <lualib.h>
 #include <lauxlib.h>
 }
 
+struct LuaRemapRequest
+{
+  TSRemapRequestInfo * rri;
+  TSRemapStatus status;
+
+  static LuaRemapRequest * get(lua_State * lua, int index);
+  static LuaRemapRequest * alloc(lua_State * lua);
+};
+
 // Initialize the 'ts' module.
 int LuaApiInit(lua_State * lua);
 
@@ -29,4 +41,7 @@ int LuaApiInit(lua_State * lua);
 bool LuaPushUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url);
 
 // Push a wrapper object for the given TSRemapRequestInfo.
-bool LuaPushRemapRequestInfo(lua_State * lua, TSRemapRequestInfo * rri);
+LuaRemapRequest *
+LuaPushRemapRequestInfo(lua_State * lua, TSRemapRequestInfo * rri);
+
+#endif // LUA_LAPI_H_

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/c55141ab/plugins/lua/lua.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lua.cc b/plugins/lua/lua.cc
index 4758763..a4d969e 100644
--- a/plugins/lua/lua.cc
+++ b/plugins/lua/lua.cc
@@ -80,7 +80,7 @@ LuaPluginRelease(lua_State * lua)
 static TSRemapStatus
 LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
 {
-  lua_Integer status = TSREMAP_ERROR;
+  LuaRemapRequest * rq;
 
   lua_getglobal(lua, "remap");
   if (lua_isnil(lua, -1)) {
@@ -88,37 +88,23 @@ LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
     return TSREMAP_NO_REMAP;
   }
 
-  // XXX we should cache these ...
+  // XXX The 'to' and 'from' URLs are supposed to be static so we can cache them somewhere
+  // in the Lua state.
   LuaPushUrl(lua, rri->requestBufp, rri->mapFromUrl);
   LuaPushUrl(lua, rri->requestBufp, rri->mapToUrl);
-  LuaPushRemapRequestInfo(lua, rri);
 
-  if (lua_pcall(lua, 3, 1, 0) != 0) {
-    TSDebug("lua", "remap failed: %s", lua_tostring(lua, -1));
-    lua_pop(lua, 1);
-    return TSREMAP_ERROR;
-  }
+  // XXX We can also cache the RemapRequestInfo in the Lua state. We we just need to reset
+  // the rri pointer and status.
+  rq = LuaPushRemapRequestInfo(lua, rri);
 
-  // Return type is integer. It must be one of the REMAP constants.
-  if (!lua_isnumber(lua, 1)) {
+  if (lua_pcall(lua, 3, 0, 0) != 0) {
+    TSDebug("lua", "remap failed: %s", lua_tostring(lua, -1));
     lua_pop(lua, 1);
     return TSREMAP_ERROR;
   }
 
-  status = lua_tointeger(lua, 1);
-  lua_pop(lua, 1);
-
-  // Lua remap plugins only get to say whether to continue the remap chain or to stop.
-  switch (status) {
-  case TSREMAP_DID_REMAP:
-  case TSREMAP_NO_REMAP_STOP:
-    return (TSRemapStatus)status;
-  case TSREMAP_NO_REMAP:
-  case TSREMAP_DID_REMAP_STOP:
-  case TSREMAP_ERROR:
-  default:
-    return TSREMAP_ERROR;
-  }
+  // XXX can we guarantee that rq has not been garbage collected?
+  return rq->status;
 }
 
 TSReturnCode