]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: filters/lua: Add request and response HTTP messages in the lua TXN
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 26 Feb 2020 16:14:08 +0000 (17:14 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 12 Aug 2021 06:57:07 +0000 (08:57 +0200)
When a lua TXN is created from a filter context, the request and the response
HTTP message objects are accessible from ".http_req" and ".http_res" fields. For
an HTTP proxy, these objects are always defined. Otherwise, for a TCP proxy, no
object is created and nil is used instead. From any other context (action or
sample fetch), these fields don't exist.

src/hlua.c

index c1a578ed4e3ef0dfa2b4bf269ada2d58572d5639..cd539d05f4aa8ca84afb42140ef3b17de758e4c4 100644 (file)
@@ -5915,7 +5915,7 @@ __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud)
 
 /* Creates and pushes on the stack a HTTP object according with a current TXN.
  */
-static __maybe_unused int hlua_http_msg_new(lua_State *L, struct http_msg *msg)
+static int hlua_http_msg_new(lua_State *L, struct http_msg *msg)
 {
        /* Check stack size. */
        if (!lua_checkstack(L, 3))
@@ -7086,6 +7086,32 @@ static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir
                lua_pushnil(L);
        lua_rawset(L, -3);
 
+       if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) {
+               /* HTTPMessage object are created when a lua TXN is created from
+                * a filter context only
+                */
+
+               /* Creates the HTTP-Request object is the current proxy allows http. */
+               lua_pushstring(L, "http_req");
+               if (p->mode == PR_MODE_HTTP) {
+                       if (!hlua_http_msg_new(L, &s->txn->req))
+                               return 0;
+               }
+               else
+                       lua_pushnil(L);
+               lua_rawset(L, -3);
+
+               /* Creates the HTTP-Response object is the current proxy allows http. */
+               lua_pushstring(L, "http_res");
+               if (p->mode == PR_MODE_HTTP) {
+                       if (!hlua_http_msg_new(L, &s->txn->rsp))
+                               return 0;
+               }
+               else
+                       lua_pushnil(L);
+               lua_rawset(L, -3);
+       }
+
        /* Pop a class sesison metatable and affect it to the userdata. */
        lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
        lua_setmetatable(L, -2);