Search code examples
htmlbashcgishopenwrt

How do I serve HTML from cgi-bin?


I am running uhttpd on OpenWrt 12.09. I have a shell script at /www/cgi-bin/test that looks like this:

#!/bin/sh
echo "Content-type: text/html"
echo "<p>It works!</p>"

I gave the file execute permissions by doing chmod +x on it. Owner is root, and uhttpd is running as root.

Now when I go to http://192.168.1.1/cgi-bin/test I get an error:

The CGI process did not produce any response

There are no errors in the system or kernel logs.

I can go to http://192.168.1.1/cgi-bin/luci and get the LuCI login page, so other CGI scripts are working. That file has a different shebang (#!/usr/bin/lua) but both scripts have the same permissions and owner.

What am I doing wrong?


Solution

  • There must be a blank line between the HTTP header and the body. Also, you might want to serve up a complete html document.

    #!/bin/sh
    echo "Content-type: text/html"
    echo 
    echo "<html><head><title>hello world</title></head><body><p>It works!</p></body></html>"