[OpenWrt-Devel] uhttpd: chunked output from cgi script

Luigi Tarenga luigi.tarenga at gmail.com
Wed Oct 7 09:46:17 EDT 2015

Hello list,
I'm working on a CGI written in lua and I'm using uhttpd as webserver on 
a tp-link AP.
I'm using external CGI interpreter, not embedded lua.

I need that the output from my CGI is chunked since I would like to 
avoid to buffer the
whole response and calculate the "Content-Length".

I discovered that uhttpd try to send chunked response if the response is 
a normal 200 OK
and the method is a GET. This is my case but unfortunately I ended with 
a strange behavior
where sometimes the GET return with chunked content and sometimes not.

I have an html page that call the CGI with a jquery method:

the board.lua script return  a piece of html code (without headers and 
The output of the CGI is always the same between test.

Debugging the http traffic with firefox I see that the GET of board.lua 
take about 33ms
when the output is chunked and 20s (the full keep-alive timeout) when 
the output is not
chunked. I see the response headers and it contain Transfer-Encoding: 
"chunked" directive
in one case on not in the other. From client side everything seems 
correctly following what
the websever gives back.

If I directly call cgi-bin/board.lua it seems to always return in 33ms. 
Debugging the code I came up
with the idea that where there are multiple GET on one http session if 
one GET is solved with
a 204 or 304 that all other GET can't be chunked anymore. In fact my 
html page load other pages
(css, js, etc about 5 GET in one page refresh).

I tried with a simple patch (I'm sure this is not the best solution 
though) that seems to correct
my problem.

--- file.c    2015-10-07 15:39:45.169056639 +0200
+++ file.c    2015-10-07 15:39:55.859056443 +0200
@@ -807,9 +807,10 @@
          return true;

      d = dispatch_find(url, pi);
-    if (d)
+    if (d) {
+        cl->request.respond_chunked = true;
          uh_invoke_handler(cl, d, url, pi);
-    else
+    } else
          uh_file_request(cl, url, pi, tb);

      return true;

I would like to know if this have sense, if it has been corrected in 
other uhttpd version or if my patch
broke with some RFC recommendation.

my openwrt version is:
DISTRIB_DESCRIPTION='OpenWrt Chaos Calmer 15.05'
DISTRIB_TAINTS='no-all no-ipv6'

thank you in advance
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org

More information about the openwrt-devel mailing list