You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by rr...@apache.org on 2019/11/11 03:56:33 UTC

[trafficserver] branch master updated: tslua: Exposes set/get method for server request objects

This is an automated email from the ASF dual-hosted git repository.

rrm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 97692a5  tslua: Exposes set/get method for server request objects
97692a5 is described below

commit 97692a552c8ad72a1cc8a890cdf11e53d3a6a1bc
Author: Randall Meyer <rr...@apache.org>
AuthorDate: Thu Nov 7 16:06:39 2019 -0800

    tslua: Exposes set/get method for server request objects
---
 doc/admin-guide/plugins/lua.en.rst  | 42 ++++++++++++++++++++++++++++
 plugins/lua/ts_lua_server_request.c | 55 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/doc/admin-guide/plugins/lua.en.rst b/doc/admin-guide/plugins/lua.en.rst
index dd0d197..5362435 100644
--- a/doc/admin-guide/plugins/lua.en.rst
+++ b/doc/admin-guide/plugins/lua.en.rst
@@ -1903,6 +1903,48 @@ ts.server_request.set_url_scheme
 
 :ref:`TOP <admin-plugins-ts-lua>`
 
+ts.server_request.get_method
+----------------------------
+**syntax:** *ts.server_request.get_method()*
+
+**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later
+
+**description:** This function can be used to retrieve the current server request's method name. String like "GET" or "POST" is returned.
+
+Here is an example:
+
+::
+
+    function send_request()
+        local method = ts.server_request.get_method()
+        ts.debug(method)
+    end
+
+    function do_remap()
+        ts.hook(TS_LUA_HOOK_SEND_REQUEST_HDR, send_request)
+        return 0
+    end
+
+Then ``HEAD /`` will yield the output:
+
+``HEAD``
+
+:ref:`TOP <admin-plugins-ts-lua>`
+
+ts.server_request.set_method
+----------------------------
+**syntax:** *ts.server_request.set_method()*
+
+**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later
+
+**description:** This function can be used to override the current server request's method with METHOD_NAME.
+
+::
+
+    ts.server_request.set_method('HEAD')
+
+:ref:`TOP <admin-plugins-ts-lua>`
+
 ts.server_response.get_status
 -----------------------------
 **syntax:** *status = ts.server_response.get_status()*
diff --git a/plugins/lua/ts_lua_server_request.c b/plugins/lua/ts_lua_server_request.c
index c927217..2aa7e45 100644
--- a/plugins/lua/ts_lua_server_request.c
+++ b/plugins/lua/ts_lua_server_request.c
@@ -51,6 +51,7 @@ static void ts_lua_inject_server_request_uri_api(lua_State *L);
 static void ts_lua_inject_server_request_uri_args_api(lua_State *L);
 static void ts_lua_inject_server_request_uri_params_api(lua_State *L);
 static void ts_lua_inject_server_request_url_api(lua_State *L);
+static void ts_lua_inject_server_request_method_api(lua_State *L);
 
 static int ts_lua_server_request_header_get(lua_State *L);
 static int ts_lua_server_request_header_set(lua_State *L);
@@ -63,6 +64,8 @@ static int ts_lua_server_request_set_uri_args(lua_State *L);
 static int ts_lua_server_request_get_uri_args(lua_State *L);
 static int ts_lua_server_request_set_uri_params(lua_State *L);
 static int ts_lua_server_request_get_uri_params(lua_State *L);
+static int ts_lua_server_request_get_method(lua_State *L);
+static int ts_lua_server_request_set_method(lua_State *L);
 static int ts_lua_server_request_get_url_host(lua_State *L);
 static int ts_lua_server_request_set_url_host(lua_State *L);
 static int ts_lua_server_request_get_url_scheme(lua_State *L);
@@ -87,7 +90,7 @@ ts_lua_inject_server_request_api(lua_State *L)
   ts_lua_inject_server_request_headers_api(L);
   ts_lua_inject_server_request_get_header_size_api(L);
   ts_lua_inject_server_request_get_body_size_api(L);
-
+  ts_lua_inject_server_request_method_api(L);
   ts_lua_inject_server_request_uri_api(L);
   ts_lua_inject_server_request_uri_args_api(L);
   ts_lua_inject_server_request_uri_params_api(L);
@@ -924,3 +927,53 @@ ts_lua_server_request_server_addr_set_outgoing_addr(lua_State *L)
 
   return 0;
 }
+
+static void
+ts_lua_inject_server_request_method_api(lua_State *L)
+{
+  lua_pushcfunction(L, ts_lua_server_request_get_method);
+  lua_setfield(L, -2, "get_method");
+
+  lua_pushcfunction(L, ts_lua_server_request_set_method);
+  lua_setfield(L, -2, "set_method");
+}
+
+static int
+ts_lua_server_request_get_method(lua_State *L)
+{
+  const char *method;
+  int method_len;
+
+  ts_lua_http_ctx *http_ctx;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  method = TSHttpHdrMethodGet(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, &method_len);
+
+  if (method && method_len) {
+    lua_pushlstring(L, method, method_len);
+  } else {
+    lua_pushnil(L);
+  }
+
+  return 1;
+}
+
+static int
+ts_lua_server_request_set_method(lua_State *L)
+{
+  const char *method;
+  size_t method_len;
+
+  ts_lua_http_ctx *http_ctx;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  method = luaL_checklstring(L, 1, &method_len);
+
+  if (method) {
+    TSHttpHdrMethodSet(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, method, method_len);
+  }
+
+  return 0;
+}