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/05/11 06:01:16 UTC

[1/32] git commit: Add Lua example

Updated Branches:
  refs/heads/jpeach/lua [created] c2e9b63f7


Add Lua example


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

Branch: refs/heads/jpeach/lua
Commit: c2e9b63f7de4fdf7001477bbb651c5ce3d752173
Parents: 6cf9235
Author: James Peach <jp...@apache.org>
Authored: Thu May 10 20:55:29 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Thu May 10 21:00:31 2012 -0700

----------------------------------------------------------------------
 plugins/lua/example.lua |   63 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/c2e9b63f/plugins/lua/example.lua
----------------------------------------------------------------------
diff --git a/plugins/lua/example.lua b/plugins/lua/example.lua
new file mode 100644
index 0000000..a57c601
--- /dev/null
+++ b/plugins/lua/example.lua
@@ -0,0 +1,63 @@
+-- Example Lua remap plugin. Load this with the following remap.comfig line:
+--
+-- map http://test.foo.com http://foo.foo.com @plugin=lua.so @pparam=/path/to/example.lua
+
+-- Pull in the Traffic Server API.
+local TS = require 'ts'
+
+require 'string'
+require 'math'
+
+-- Compulsory remap hook. We are given a request object that we can modify if necessary.
+function remap(request)
+  -- Get a copy of the current URL.
+  url = request:url()
+
+  TS.debug('example', string.format('remapping %s://%s', url.scheme, url.host))
+
+  -- Do some header manipulation, just to mess with the origin.
+  request.headers['added-bool'] = true
+  request.headers['added-int'] = 1
+  request.headers['added-string'] = 'ttt'
+  request.headers['added-table'] = {}
+  request.headers['deleted'] = nil
+
+  -- We can also print to stdout using Lua standard library.
+  print(string.format('request URL is %s://%s:%d/%s',
+        url.scheme, url.host, url.port, url.path and url.path or ''))
+
+  -- Modify components of the URL ... everybody loves slashdot.
+  url.host = 'www.slashdot.org'
+  url.port = 80
+  url.method = 'POST'
+
+  -- Plugin chain evaluation rules:
+  --    redirect: plugin chain terminates
+  --    reject: plugin chain terminates
+  --    rewrite: plugin chain continues
+
+  chance = math.random(4)
+  if chance == 1 then
+    -- Send a 301 redirect to the new URL.
+    request:redirect(url)
+  elseif chance == 2 then
+    -- Reject the request with an optional message.
+    request:reject(400, "Denied")
+  elseif chance == 3 then
+    -- Reject the request with a response body. We sniff the body to set the content type.
+    request:reject(500, [[
+      <HEAD></TITLE></HEAD>
+      <BODY>Internal error, sorry</BODY>
+      ]])
+  else
+    -- Rewrite the request URL. The remap plugin chain continues and other plugins
+    request:rewrite(url)
+  end
+
+end
+
+-- Optional module initialization hook.
+function init()
+    TS.debug("example", string.format('init called by Traffic Server %s', TS.VERSION));
+    return true
+end