trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpe...@apache.org
Subject [2/50] git commit: Add Lua example
Date Tue, 12 Jun 2012 15:51:36 GMT
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/03eedc13
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/03eedc13
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/03eedc13

Branch: refs/heads/jpeach/lua
Commit: 03eedc13429dd0f9df00d77604ea0a7a462e8a2f
Parents: f015b96
Author: James Peach <jpeach@apache.org>
Authored: Thu May 10 20:55:29 2012 -0700
Committer: James Peach <jpeach@apache.org>
Committed: Tue Jun 12 08:48:39 2012 -0700

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


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/03eedc13/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


Mime
View raw message