89160e4212d6aff6ea0a681ac4289d055c03c7ab
[openwrt-packages.git] /
1 commit ff96b8bd3f85155f65b2b9c9f046fe3e40f630a4
2 Author: Christopher Faulet <cfaulet@haproxy.com>
3 Date:   Fri Jul 26 15:09:53 2019 +0200
4
5     MINOR: hlua: Add a flag on the lua txn to know in which context it can be used
6     
7     When a lua action or a lua sample fetch is called, a lua transaction is
8     created. It is an entry in the stack containing the class TXN. Thanks to it, we
9     can know the direction (request or response) of the call. But, for some
10     functions, it is also necessary to know if the buffer is "HTTP ready" for the
11     given direction. "HTTP ready" means there is a valid HTTP message in the
12     channel's buffer. So, when a lua action or a lua sample fetch is called, the
13     flag HLUA_TXN_HTTP_RDY is set if it is appropriate.
14     
15     (cherry picked from commit bfab2dddad3ded87617d1e2db54761943d1eb32d)
16     Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
17
18 diff --git a/include/types/hlua.h b/include/types/hlua.h
19 index 70c76852..2f4e38be 100644
20 --- a/include/types/hlua.h
21 +++ b/include/types/hlua.h
22 @@ -43,7 +43,8 @@ struct stream;
23  #define HLUA_F_AS_STRING    0x01
24  #define HLUA_F_MAY_USE_HTTP 0x02
25  
26 -#define HLUA_TXN_NOTERM 0x00000001
27 +#define HLUA_TXN_NOTERM   0x00000001
28 +#define HLUA_TXN_HTTP_RDY 0x00000002 /* Set if the txn is HTTP ready for the defined direction */
29  
30  #define HLUA_CONCAT_BLOCSZ 2048
31  
32 diff --git a/src/hlua.c b/src/hlua.c
33 index 36454cdc..d37e3c61 100644
34 --- a/src/hlua.c
35 +++ b/src/hlua.c
36 @@ -6494,6 +6494,7 @@ static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp
37         struct stream *stream = smp->strm;
38         const char *error;
39         const struct buffer msg = { };
40 +       unsigned int hflags = HLUA_TXN_NOTERM;
41  
42         if (!stream)
43                 return 0;
44 @@ -6517,6 +6518,13 @@ static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp
45  
46         consistency_set(stream, smp->opt, &stream->hlua->cons);
47  
48 +       if (stream->be->mode == PR_MODE_HTTP) {
49 +               if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
50 +                       hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
51 +               else
52 +                       hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
53 +       }
54 +
55         /* If it is the first run, initialize the data for the call. */
56         if (!HLUA_IS_RUNNING(stream->hlua)) {
57  
58 @@ -6541,8 +6549,7 @@ static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp
59                 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
60  
61                 /* push arguments in the stack. */
62 -               if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
63 -                                 HLUA_TXN_NOTERM)) {
64 +               if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
65                         SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
66                         RESET_SAFE_LJMP(stream->hlua->T);
67                         return 0;
68 @@ -6759,16 +6766,16 @@ static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
69                                     struct session *sess, struct stream *s, int flags)
70  {
71         char **arg;
72 -       unsigned int analyzer;
73 +       unsigned int hflags = 0;
74         int dir;
75         const char *error;
76         const struct buffer msg = { };
77  
78         switch (rule->from) {
79 -       case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE     ; dir = SMP_OPT_DIR_REQ; break;
80 -       case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT        ; dir = SMP_OPT_DIR_RES; break;
81 -       case ACT_F_HTTP_REQ:    analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
82 -       case ACT_F_HTTP_RES:    analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
83 +       case ACT_F_TCP_REQ_CNT:                            ; dir = SMP_OPT_DIR_REQ; break;
84 +       case ACT_F_TCP_RES_CNT:                            ; dir = SMP_OPT_DIR_RES; break;
85 +       case ACT_F_HTTP_REQ:    hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
86 +       case ACT_F_HTTP_RES:    hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
87         default:
88                 SEND_ERR(px, "Lua: internal error while execute action.\n");
89                 return ACT_RET_CONT;
90 @@ -6821,7 +6828,7 @@ static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
91                 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
92  
93                 /* Create and and push object stream in the stack. */
94 -               if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
95 +               if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
96                         SEND_ERR(px, "Lua function '%s': full stack.\n",
97                                  rule->arg.hlua_rule->fcn.name);
98                         RESET_SAFE_LJMP(s->hlua->T);
99 @@ -6864,9 +6871,9 @@ static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
100         case HLUA_E_AGAIN:
101                 /* Set timeout in the required channel. */
102                 if (s->hlua->wake_time != TICK_ETERNITY) {
103 -                       if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
104 +                       if (dir & SMP_OPT_DIR_REQ)
105                                 s->req.analyse_exp = s->hlua->wake_time;
106 -                       else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
107 +                       else
108                                 s->res.analyse_exp = s->hlua->wake_time;
109                 }
110                 /* Some actions can be wake up when a "write" event
git clone https://git.99rst.org/PROJECT