trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpe...@apache.org
Subject [6/50] git commit: Switch to userdata for headers, and implement key indexing
Date Tue, 12 Jun 2012 15:51:36 GMT
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 <jpeach@apache.org>
Authored: Sat Apr 21 21:30:55 2012 -0700
Committer: James Peach <jpeach@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;
 }
 


Mime
View raw message