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:37 UTC

[23/50] git commit: Make to and from URLs to pass to the remap

Make to and from URLs to pass to the remap


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

Branch: refs/heads/jpeach/lua
Commit: 279040c76a7beb93c5d2125fe1e110b329bdde5e
Parents: 4df83da
Author: James Peach <jp...@apache.org>
Authored: Thu Apr 12 21:42:22 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jun 12 08:48:35 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lapi.cc |   59 ++++++++++++++++++++++++++++++++++++++++++++++
 plugins/lua/lua.cc  |   10 +++++++-
 2 files changed, 68 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/279040c7/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index 8ecdf14..e0848b5 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -27,6 +27,52 @@ extern "C" {
 
 #include <unistd.h>
 
+int
+LuaPushUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url)
+{
+  int len;
+  const char * str;
+
+#define PUSH_URL_COMPONENT(accessor, name) do { \
+  str = accessor(buffer, url, &len); \
+  if (str) { \
+    lua_pushlstring(lua, str, len); \
+  }  else { \
+    lua_pushnil(lua); \
+  } \
+  lua_setfield(lua, -2, name); \
+} while (0)
+
+  lua_newtable(lua);
+
+  // Set fundamental URL fields.
+  // XXX should we be luvit-compatible with these names?
+  PUSH_URL_COMPONENT(TSUrlSchemeGet, "scheme");       // luvit: protocol
+  PUSH_URL_COMPONENT(TSUrlUserGet, "user");
+  PUSH_URL_COMPONENT(TSUrlPasswordGet, "password");
+  PUSH_URL_COMPONENT(TSUrlHostGet, "host");
+  lua_pushinteger(lua, TSUrlPortGet(buffer, url));
+  lua_setfield(lua, -2, "port");
+  PUSH_URL_COMPONENT(TSUrlPathGet, "path");           // luvit: pathname
+  PUSH_URL_COMPONENT(TSUrlHttpQueryGet, "query");     // luvit: search
+  PUSH_URL_COMPONENT(TSUrlHttpFragmentGet, "fragment");
+
+  // It would be cleaner to add a __tostring metamethod, but to do that we would have to keep the
+  // buffer and url around indefinitely. Better to make a straight copy now; use the 'href' key
+  // just like luvit does.
+  str = TSUrlStringGet(buffer, url, &len);
+  if (str) {
+    lua_pushlstring(lua, str, len);
+    lua_setfield(lua, -2, "href");
+    TSfree((void *)str);
+  }
+
+  TSReleaseAssert(lua_istable(lua, -1) == 1);
+  return true;
+
+#undef PUSH_URL_COMPONENT
+}
+
 static int
 TSLuaDebug(lua_State * lua)
 {
@@ -54,6 +100,19 @@ LuaApiInit(lua_State * lua)
   luaL_register(lua, NULL, LUAEXPORTS);
 
   // Push constants into the "ts" module.
+
+  lua_pushstring(lua, TSTrafficServerVersionGet());
+  lua_setfield(lua, -2, "VERSION");
+
+  lua_pushinteger(lua, TSTrafficServerVersionGetMajor());
+  lua_setfield(lua, -2, "MAJOR_VERSION");
+
+  lua_pushinteger(lua, TSTrafficServerVersionGetMinor());
+  lua_setfield(lua, -2, "MINOR_VERSION");
+
+  lua_pushinteger(lua, TSTrafficServerVersionGetPatch());
+  lua_setfield(lua, -2, "PATCH_VERSION");
+
   lua_pushinteger(lua, TSREMAP_DID_REMAP_STOP);
   lua_setfield(lua, -2, "REMAP_COMPLETE");
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/279040c7/plugins/lua/lua.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lua.cc b/plugins/lua/lua.cc
index 37ed384..3d63cfd 100644
--- a/plugins/lua/lua.cc
+++ b/plugins/lua/lua.cc
@@ -27,8 +27,12 @@ extern "C" {
 
 #include <unistd.h>
 
+// Initialize the 'ts' module.
 extern int LuaApiInit(lua_State *);
 
+// Push a copy of the given URL.
+extern int LuaPushUrl(lua_State*, TSMBuffer, TSMLoc);
+
 static void *
 LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize)
 {
@@ -96,7 +100,11 @@ LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
     return TSREMAP_NO_REMAP;
   }
 
-  if (lua_pcall(lua, 0, 1, 0) != 0) {
+  // XXX we should cache these ...
+  LuaPushUrl(lua, rri->requestBufp, rri->mapFromUrl);
+  LuaPushUrl(lua, rri->requestBufp, rri->mapToUrl);
+
+  if (lua_pcall(lua, 2, 1, 0) != 0) {
     TSDebug("lua", "remap failed: %s", lua_tostring(lua, -1));
     lua_pop(lua, 1);
     return TSREMAP_ERROR;