trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpe...@apache.org
Subject [19/50] git commit: Implement LuaRemapReject
Date Tue, 12 Jun 2012 15:51:36 GMT
Implement LuaRemapReject


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

Branch: refs/heads/jpeach/lua
Commit: ee6acbadfcf06b23798e597c8aaa4ef81577a14d
Parents: 488a4ed
Author: James Peach <jpeach@apache.org>
Authored: Tue Apr 17 21:38:56 2012 -0700
Committer: James Peach <jpeach@apache.org>
Committed: Tue Jun 12 08:48:36 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lapi.cc |   35 ++++++++++++++++++++++++++++++++++-
 plugins/lua/lapi.h  |    7 ++++---
 plugins/lua/lua.cc  |    5 ++++-
 3 files changed, 42 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ee6acbad/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index 2dcc1b2..90eb64d 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -131,6 +131,7 @@ LuaRemapRedirect(lua_State * lua)
   luaL_checktype(lua, 2, LUA_TTABLE);
 
   TSDebug("lua", "redirecting request %p", rq->rri);
+
   lua_pushvalue(lua, 2);
   LuaPopUrl(lua, rq->rri->requestBufp, rq->rri->requestUrl);
   lua_pop(lua, 1);
@@ -153,6 +154,7 @@ LuaRemapRewrite(lua_State * lua)
   luaL_checktype(lua, 2, LUA_TTABLE);
 
   TSDebug("lua", "rewriting request %p", rq->rri);
+
   lua_pushvalue(lua, 2);
   LuaPopUrl(lua, rq->rri->requestBufp, rq->rri->requestUrl);
   lua_pop(lua, 1);
@@ -165,20 +167,51 @@ LuaRemapRewrite(lua_State * lua)
   return 1;
 }
 
+static int
+LuaRemapReject(lua_State * lua)
+{
+  LuaRemapRequest * rq;
+  int status;
+  const char * body = NULL;
+
+  rq = LuaRemapRequest::get(lua, 1);
+  status = luaL_checkint(lua, 2);
+  if (!lua_isnoneornil(lua, 3)) {
+    body = luaL_checkstring(lua, 3);
+  }
+
+  TSDebug("lua", "rejecting request %p with status %d", rq->rri, status);
+
+  TSHttpTxnSetHttpRetStatus(rq->txn, (TSHttpStatus)status);
+  if (body && *body) {
+    // XXX Dubiously guess the content type from the body. This doesn't actually seem to
work
+    // so it doesn't matter that our guess is pretty bad.
+    int isplain = (*body != '<');
+    TSHttpTxnSetHttpRetBody(rq->txn, body, isplain);
+  }
+
+  // A reject terminates plugin chain evaluation but does not update the request URL.
+  rq->status = TSREMAP_NO_REMAP_STOP;
+
+  return 1;
+}
+
 static const luaL_Reg RRI[] =
 {
   { "redirect", LuaRemapRedirect },
   { "rewrite", LuaRemapRewrite },
+  { "reject", LuaRemapReject },
   { NULL, NULL}
 };
 
 LuaRemapRequest *
-LuaPushRemapRequestInfo(lua_State * lua, TSRemapRequestInfo * rri)
+LuaPushRemapRequestInfo(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
 {
   LuaRemapRequest * rq;
 
   rq = LuaRemapRequest::alloc(lua);
   rq->rri = rri;
+  rq->txn = txn;
   rq->status = TSREMAP_NO_REMAP;
 
   TSReleaseAssert(lua_isuserdata(lua, -1) == 1);

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ee6acbad/plugins/lua/lapi.h
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.h b/plugins/lua/lapi.h
index 8ad0495..9dc8534 100644
--- a/plugins/lua/lapi.h
+++ b/plugins/lua/lapi.h
@@ -27,8 +27,9 @@ extern "C" {
 
 struct LuaRemapRequest
 {
-  TSRemapRequestInfo * rri;
-  TSRemapStatus status;
+  TSRemapRequestInfo *  rri;
+  TSHttpTxn             txn;
+  TSRemapStatus         status;
 
   static LuaRemapRequest * get(lua_State * lua, int index);
   static LuaRemapRequest * alloc(lua_State * lua);
@@ -42,6 +43,6 @@ bool LuaPushUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url);
 
 // Push a wrapper object for the given TSRemapRequestInfo.
 LuaRemapRequest *
-LuaPushRemapRequestInfo(lua_State * lua, TSRemapRequestInfo * rri);
+LuaPushRemapRequestInfo(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri);
 
 #endif // LUA_LAPI_H_

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/ee6acbad/plugins/lua/lua.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lua.cc b/plugins/lua/lua.cc
index a4d969e..f308524 100644
--- a/plugins/lua/lua.cc
+++ b/plugins/lua/lua.cc
@@ -19,6 +19,7 @@
 #include <ts/ts.h>
 #include <ts/remap.h>
 #include <unistd.h>
+#include <pthread.h>
 #include "lapi.h"
 
 static void *
@@ -88,6 +89,8 @@ LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
     return TSREMAP_NO_REMAP;
   }
 
+  TSDebug("lua", "handling request %p on thread 0x%llx", rri, (unsigned long long)pthread_self());
+
   // 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);
@@ -95,7 +98,7 @@ LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
 
   // 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);
+  rq = LuaPushRemapRequestInfo(lua, txn, rri);
 
   if (lua_pcall(lua, 3, 0, 0) != 0) {
     TSDebug("lua", "remap failed: %s", lua_tostring(lua, -1));


Mime
View raw message