Le 13/02/2019 à 09:34, Laurent Penot a écrit :
Hi Thierry, guys,

When receiving a POST request on haproxy, I use lua to compute some values, and modify the body of the request before forwarding to the backend, so my backend can get these variables from the POST and use them.

Here is a sample cfg, and lua code to reproduce this.

##### Conf (I removed all defauts, timeout and co ..) :

frontend front-nodes

bind :80

# option to wait for the body before processing (mandatory for POST requests)

option http-buffer-request

# default backend

default_backend be_test

http-request lua.manageRequests

##### Lua :

*function */manageRequests/(txn)

-- create new postdata

*local *newPostData = /core/./concat/()

newPostData:add('POST /test.php HTTP/1.1\r\n')

newPostData:add('Host: test1\r\n')

newPostData:add('content-type: application/x-www-form-urlencoded\r\n')

*local *newBodyStr = 'var1=valueA&var2=valueB'

*local *newBodyLen = string.len(newBodyStr)

newPostData:add('content-length: ' .. tostring(newBodyLen) .. '\r\n')

newPostData:add('\r\n')

newPostData:add(newBodyStr)

*local *newPostDataStr = tostring(newPostData:dump())

txn.req:send(newPostDataStr)

*end*

/core/./register_action/("manageRequests", { "http-req" }, /manageRequests/)

This is working well in haproxy 1.8.x (x : 14 to 18) but I get the following error with 1.9.4 (same error with 1.9.2, others 1.9.x versions not tested) :

Lua function 'manageRequests': runtime error: 0 from [C] method 'send', /etc/haproxy/lua/bench.lua:97 C function line 80.

Line 97 of my lua file is txn.req:send(newPostDataStr)

Maybe I’m missing something on 1.9.x but cant find what, or maybe it’s a bug, I can’t say.

Hi Laurent,

It is not supported to modify an HTTP request/response calling Channel functions. It means calling following functions within an HTTP proxy is forbidden: Channel.get, Channel.dup, Channel.getline, Channel.set, Channel.append, Channel.send, Channel.forward.

Since HAProxy 1.9, a runtime error is triggered (because there is no way to do it during the configuration parsing, AFAIK). You may see this as a regression, but in fact, it was never really supported. But because of a lack check, no error was triggered. Because these functions totally hijacked the HTTP parser, if used, the result is undefined. There are many ways to crash HAProxy. Unfortunately, for now, there is no way to rewrite the HTTP messages in Lua.

--
Christopher Faulet

Reply via email to