Getting all raw http request headers and values in lua

  • I want to get all http request headers and values in lua context.
    But I find that there is no connection header and HaProxy skips to save the connection header.
    Is there a way to get all intact http request headers and values?

  • haproxy version: 2.4.1

  • haproxy.conf

global
   user test_user
   group test_user

   log /dev/log local0 debug

   lua-load /path/check_http_hdr.lua

defaults
   mode http
   balance random
   log global
   option httplog

frontend nid.naver.com
   bind *:80
   default_backend backend_servers

   http-request lua.check_http_hdr

   default_backend back-server

backend back-server
   balance roundrobin
   server localhost-1 127.0.0.1:8001
  • check_http_hdr.lua
core.register_action("check_http_hdr", { "http-req" }, function(txn)
    txn:log(core.info, "hdr: " .. txn.f:req_hdr_names(","))
    txn:log(core.info, "hdr: " .. txn.f:req_hdr("connection"))
end)

  • Step 1

    • connect to the HaProxy with browser.
      I test the chrome browser, and the browser sends the connection header.
  • Step 2

    • check the HaProxy error log
    • There is no “Connection” http request header, and lua code makes error, when it accesses to the connection header.
hdr: host,pragma,cache-control,referer,accept-encoding,accept-language,cookie
[ALERT]    (21765) : Lua function 'check_http_hdr': [state-id 0] runtime error: ...path/check_http_hdr.lua:2: attempt to concatenate a nil value from ...path/check_http_hdr.lua:2: in function line 1.
  • src/h1.c (haproxy source code internal)
 304 int h1_headers_to_hdr_list(char *start, const char *stop,
 305                            struct http_hdr *hdr, unsigned int hdr_num,
 306                            struct h1m *h1m, union h1_sl *slp)
...
...
...
 861                 else if (isteqi(n, ist("connection"))) {
 862                     h1_parse_connection_header(h1m, &v); 
 863                     if (!v.len) {
 864                         /* skip it */  // I find this line.                                            
                                               // haproxy skips to save the connection header                                              
 865                         break;
 866                     }
 867                 }