Error when trying to read txn.res channel

Hi,

I am trying to implement a simple action in Lua that is going to save the http response body to a file on disk. Unfortunately I get "unknown runtime error from [C] method 'dup'" everytime I try to read from the txn:res channel/buffer via dup() get() getline(). I get the error when running the latest version of HAProxy via Docker and also when trying with a pre-packaged version of HAProxy 1.9 in Ubuntu 14.04.

I have pasted my play code below - I would really appreciate any pointers that can help me move forward.

haproxy.cfg

global
  maxconn 10000
  daemon
  lua-load /usr/local/etc/haproxy/hello.lua

defaults
  mode http
  log stdout format raw local0 info

frontend fe-http
  bind :80
  http-response lua.save_response_action
  default_backend be-default

hello.lua

local function save_response(txn)
  core.Debug("save_response start!")
  if txn.res:get_in_len() > 0 then
    core.Debug(txn.res:get_in_len())
    core.Debug(txn.res:dup())
  end
  core.Debug("save_response complete!")
end

core.register_action("save_response_action", { 'http-res' }, save_response, 0)

log output

$ docker run --rm --name haproxy-lua -p 9090:80 -v /tmp/haproxy-docker/conf:/usr/local/etc/haproxy:ro haproxy -d -- /usr/local/etc/haproxy/haproxy.cfg
[WARNING] 273/101818 (1) : config : missing timeouts for frontend 'fe-http'.
| While not properly invalid, you will certainly encounter various problems
| with such a configuration. To fix this, please ensure that all following
| timeouts are set to a non-zero value: 'client', 'connect', 'server'.
[WARNING] 273/101818 (1) : config : missing timeouts for backend 'be-default'.
| While not properly invalid, you will certainly encounter various problems
| with such a configuration. To fix this, please ensure that all following
| timeouts are set to a non-zero value: 'client', 'connect', 'server'.
Available polling systems :
    epoll : pref=300,  test result OK
    poll : pref=200,  test result OK
    select : pref=150,  test result FAILED
Total: 3 (2 usable), will use epoll.

Available filters :
        [SPOE] spoe
        [COMP] compression
        [CACHE] cache
        [TRACE] trace
Using epoll() as the polling mechanism.
[NOTICE] 273/101818 (1) : New worker #1 (6) forked
Proxy fe-http started.
Proxy be-default started.
Connect from 192.168.50.1:60192 to 172.17.0.2:80 (fe-http/HTTP)
00000000:fe-http.accept(0006)=000c from [192.168.50.1:60192] ALPN=<none>
00000000:fe-http.clireq[000c:ffffffff]: GET /index.html HTTP/1.1
00000000:fe-http.clihdr[000c:ffffffff]: host: docker-slave:9090
00000000:fe-http.clihdr[000c:ffffffff]: user-agent: curl/7.58.0
00000000:fe-http.clihdr[000c:ffffffff]: accept: */*
00000000:be-default.srvrep[000c:000d]: HTTP/1.1 200 OK
00000000:be-default.srvhdr[000c:000d]: date: Tue, 01 Oct 2019 10:18:22 GMT
00000000:be-default.srvhdr[000c:000d]: server: Apache
00000000:be-default.srvhdr[000c:000d]: vary: X-B3-TraceId,X-B3-SpanId,X-B3-Sampled
00000000:be-default.srvhdr[000c:000d]: last-modified: Mon, 09 Sep 2019 06:35:39 GMT
00000000:be-default.srvhdr[000c:000d]: etag: "1005-59218ff06e2a7"
00000000:be-default.srvhdr[000c:000d]: accept-ranges: bytes
00000000:be-default.srvhdr[000c:000d]: content-length: 4101
Lua function 'save_response_action': unknown runtime error from [C] method 'dup', /usr/local/etc/haproxy/hello.lua:5 C function line 1.
[debug] 273/101822 (6) : save_response start!
[debug] 273/101822 (6) : 4328
[ALERT] 273/101822 (6) : Lua function 'save_response_action': unknown runtime error from [C] method 'dup', /usr/local/etc/haproxy/hello.lua:5 C function line 1.
00000000:be-default.srvcls[000c:000d]
00000000:be-default.clicls[000c:000d]
00000000:be-default.closed[000c:000d]
Connect from 192.168.50.1:60192 to 172.17.0.2:80 (fe-http/HTTP)
00000001:fe-http.accept(0006)=000c from [192.168.50.1:60192] ALPN=<none>
00000001:fe-http.clicls[000c:ffffffff]
00000001:fe-http.closed[000c:ffffffff]
^C[WARNING] 273/101825 (1) : Exiting Master process...
[ALERT] 273/101825 (1) : Current worker #1 (6) exited with code 130 (Interrupt)
[WARNING] 273/101825 (1) : All workers exited. Exiting... (130)

/Andreas

1 Like

Hi,

It seems that since HAProxy 1.9 certain Channel functions are no longer permitted to access from Lua-scripts running in http-mode. A more detailed explanation can be found in the following mail conversation: https://www.mail-archive.com/haproxy@formilux.org/msg33388.html

I will see if I can get it to work via a sample fetch instead and if so I will mark this question as solved.

/Andreas

Hi,

I am still stuck with how to reading the response body. I have scanned trough various QA posts on this forum, stackoverflow etc but I cannot find any ideas on how to do it.

Can anyone clarify if it is possible in any way (captures/fetches or Lua) to read the response body in HAProxy 2.0+ ?

/Andreas

Hi,
Did you find the solution to get response body ?