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

[6/50] git commit: Switch to userdata for headers, and implement key indexing

Switch to userdata for headers, and implement key indexing


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

Branch: refs/heads/jpeach/lua
Commit: 4322f25057381681de826feff9358f75c25bf16f
Parents: 1d380fb
Author: James Peach <jp...@apache.org>
Authored: Sat Apr 21 21:30:55 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jun 12 08:48:38 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lapi.cc |   46 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 39 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/4322f250/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index 3ae2645..86ea7a3 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -24,6 +24,26 @@
 // Return the type name string for the given index.
 #define LTYPEOF(L, index) lua_typename(L, lua_type(L, index))
 
+struct LuaRemapHeaders
+{
+  TSMBuffer buffer;
+  TSMLoc    headers;
+
+  static LuaRemapHeaders * get(lua_State * lua, int index) {
+    return (LuaRemapHeaders *)luaL_checkudata(lua, index, "ts.meta.rri.headers");
+  }
+
+  static LuaRemapHeaders * alloc(lua_State * lua) {
+    LuaRemapHeaders * hdrs;
+
+    hdrs = (LuaRemapHeaders *)lua_newuserdata(lua, sizeof(LuaRemapHeaders));
+    luaL_getmetatable(lua, "ts.meta.rri.headers");
+    lua_setmetatable(lua, -2);
+
+    return hdrs;
+  }
+};
+
 LuaRemapRequest *
 LuaRemapRequest::get(lua_State * lua, int index)
 {
@@ -259,13 +279,13 @@ LuaRemapIndex(lua_State * lua)
   // Get the requested field from the environment table.
   lua_getfield(lua, -1, index);
   if (lua_isnoneornil(lua, -1)) {
+    LuaRemapHeaders * hdrs;
     TSDebug("lua", "populating '%s' field", index);
     lua_pop(lua, 1);
 
-    // Make a new header table.
-    lua_newtable(lua);
-    luaL_getmetatable(lua, "ts.meta.headers");
-    lua_setmetatable(lua, -2);
+    hdrs = LuaRemapHeaders::alloc(lua);
+    hdrs->buffer = rq->rri->requestBufp;
+    hdrs->headers = rq->rri->requestHdrp;
 
     // Set it for the 'headers' index and push it on the stack.
     lua_setfield(lua, -2, index);
@@ -290,14 +310,26 @@ static const luaL_Reg RRI[] =
 static int
 LuaRemapHeaderIndex(lua_State * lua)
 {
-  const char * index;
+  LuaRemapHeaders * hdrs;
+  const char *      index;
+  const char *      value;
+  int               vlen;
+  TSMLoc            field;
 
-  TSAssert(lua_istable(lua, 1));
+
+  hdrs = LuaRemapHeaders::get(lua, 1);;
   index = luaL_checkstring(lua, 2);
 
   TSDebug("lua", "%s[%s]", __func__, index);
 
-  lua_pushboolean(lua, 1);
+  field = TSMimeHdrFieldFind(hdrs->buffer, hdrs->headers, index, -1);
+  if (field == TS_NULL_MLOC) {
+    lua_pushnil(lua);
+    return 1;
+  }
+
+  value = TSMimeHdrFieldValueStringGet(hdrs->buffer, hdrs->headers, field, 0, &vlen);
+  lua_pushlstring(lua, value, vlen);
   return 1;
 }