Hi,

With Lua integration in HAProxy 1.6, one can change the request method,
path, uri, header, response header etc except response line.
I'd like to contribute the following methods to allow modification of the
response line.

diff --git a/haproxy-ss-20150711/src/hlua.c b/haproxy-ss-20150711/src/hlua.c
index a5df204..c56de6e 100644
--- a/haproxy-ss-20150711/src/hlua.c
+++ b/haproxy-ss-20150711/src/hlua.c
@@ -3305,6 +3305,26 @@ static int hlua_http_req_set_uri(lua_State *L)
        return 1;
 }

+static int hlua_http_res_set_code(lua_State *L)
+{
+       struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
+       size_t name_len;
+       const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
+
+       lua_pushboolean(L, http_replace_res_line(0, name, name_len, htxn->p,
htxn->s) != -1);
+       return 1;
+}
+
+static int hlua_http_res_set_reason(lua_State *L)
+{
+       struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
+       size_t name_len;
+       const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
+
+       lua_pushboolean(L, http_replace_res_line(1, name, name_len, htxn->p,
htxn->s) != -1);
+       return 1;
+}
+
 /*
  *
  *
@@ -4969,6 +4989,8 @@ void hlua_init(void)
        hlua_class_function(gL.T, "res_rep_value",  hlua_http_res_rep_val);
        hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
        hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
+       hlua_class_function(gL.T, "res_set_code",   hlua_http_res_set_code);
+       hlua_class_function(gL.T, "res_set_reason", hlua_http_res_set_reason);

        lua_settable(gL.T, -3);

diff --git a/haproxy-ss-20150711/src/proto_http.c
b/haproxy-ss-20150711/src/proto_http.c
index 0ebc2c4..6075f83 100644
--- a/haproxy-ss-20150711/src/proto_http.c
+++ b/haproxy-ss-20150711/src/proto_http.c
@@ -12463,6 +12463,45 @@ int http_replace_req_line(int action, const
char *replace, int len,
        return 0;
 }

+int http_replace_res_line(int action, const char *replace, int len,
struct proxy *px, struct stream *s)
+{
+       struct http_txn *txn = s->txn;
+       char *cur_ptr, *cur_end;
+       int offset = 0;
+       int delta;
+
+       switch (action) {
+       case 0: // code
+               cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
+               cur_end = cur_ptr + txn->rsp.sl.st.c_l;
+
+               /* adjust res line offsets and lengths */
+               delta = len - offset - (cur_end - cur_ptr);
+               txn->rsp.sl.st.c_l += delta;
+               txn->rsp.sl.st.r   += delta;
+               break;
+
+       case 1: // reason
+               cur_ptr = s->res.buf->p + txn->rsp.sl.st.r;
+               cur_end = cur_ptr + txn->rsp.sl.st.r_l;
+
+               /* adjust res line offsets and lengths */
+               delta = len - offset - (cur_end - cur_ptr);
+               txn->rsp.sl.st.r_l += delta;
+               break;
+
+       default:
+               return -1;
+       }
+
+       /* commit changes and adjust end of message */
+       delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, replace +
offset, len - offset);
+       txn->rsp.sl.st.l += delta;
+       txn->hdr_idx.v[0].len += delta;
+       http_msg_move_end(&txn->rsp, delta);
+       return 0;
+}
+

Best,

Bowen

Reply via email to