CGI program doesn't return anything

I am running thttpd on a (very) cut down version of Red Hat 7. I've written the following C program:

#include <stdio.h>
int main(void)
{
   printf("Content-Type:text/html;charset=iso-8859-1\n\n");
   printf("Hello world...\n");

   return 0;
}


The program works fine when run from the shell and does what it should. I've put it under my www root as /cgi-bin/test.cgi
When I try to access it via my web browser, it is found (no 'document does not exist' error) and is being run as CGI (no 'not a cgi program' error), but just doesn't return anything. I've tried replacing the "\n\n" with "\r\n\r\n" to see if that was the problem but just can't get anything out of it. No errors, nothing.

Does anyone know why this might not work?
Cheers,
--Rob
LVL 6
boycyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITcrowCommented:
Try:

#include <stdio.h>
int main(void)
{
   printf("Content-Type:text/html\n\n");
   printf("Hello world...\n");

   return 0;
}

May be your header is not completely understood.
0
gripeCommented:
It sounds like you haven't configured your cgi directory properly. How are you executing thttpd?

The CGI_PATTERN define or the argument to the '-c' option is relative to the root directory thttpd runs in. By default this directory is the same one you execute it from or you can specify it by using the '-d' option.

As an example, let's say I have my web server at: /home/http
and my cgi directory is at: /home/http/cgi-bin

I will want to start thttpd with the following command to set up the cgi-bin directory:

thttpd -d /home/http -c '/cgi-bin/*'

Note the single quotes (') around cgi-bin to avoid interpolation of the '*' wildcard by the shell.

Could you post the command you're using to start thttpd if this doesn't help?

Thanks
0
gripeCommented:
As a side note, there is no problem with your program. I compiled it exactly as you posted and ran it as a CGI with thttpd v2.25b on FreeBSD 4.9. I would suggest a space between 'Content-type:' and your content type information though, as I think this is what the spec calls for. IE:

printf("Content-Type: text/html;charset=iso-8859-1\n\n");
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

boycyAuthor Commented:
ITcrow: nope, that program does exactly the same thing - runs but no output from web server.

gripe: cheers for the spec pointer.
thttpd is being run with the following command:

thttpd -C /etc/thttpd.conf

I'm sure that the configuration for CGI_PATTERN is correct. It's defined in thttpd.conf as follows:
cgipat=/cgi-bin/*

I say I'm sure it's correct because if I change it eg to *.x then I get an error accessing test.cgi saying that it's a cgi program but not allowed to run as cgi.
...

--Rob
0
boycyAuthor Commented:
It's definitely a configuration problem: the following bash script 'works', just not when run as CGI...

echo -e "Content-type: text/html\n\n"
echo "Hello world..."
0
ITcrowCommented:
Have you tried looking at access log or error log ?
Could you post it here, what is logged in the file when you access the script through web.
0
boycyAuthor Commented:
The access log is this:
192.168.1.99 - - [12/Nov/1980:20:19:22 +0000] "GET /cgi-bin/bash.cgi HTTP/1.1" 200 50000 "" "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1"

I get same result using IE.
The following is the HTTP conversation recorded by ethereal:

GET /cgi-bin/bash.cgi HTTP/1.1
Host: 192.168.1.103
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.0 200 OK


As you can see, all seems fine, but the script just doesn't look like it's actually being run.
Grrrrrrr...

--Rob :D
0
ITcrowCommented:
Did you notice the amount of data transfered: 50000
Where as for your CGI it should have been about 25 to 30
192.168.1.99 - - [12/Nov/1980:20:19:22 +0000] "GET /cgi-bin/bash.cgi HTTP/1.1" 200 50000

It is not a good idea to name your script as bash.cgi
As httpd in certain cases will behave as follows:
/cgi-bin/bash.cgi    =    /cgi-bin/bash
Which can confuse it with bash, if the server implementation considers paths first.
0
boycyAuthor Commented:
The 50000 confused me too...I have no idea why it says that. There is no output following that message, certainly not 50KBs' worth of data!
I'll rename the script too :)

--Rob
0
ITcrowCommented:
I think it just executed your 'bash' shell.
0
boycyAuthor Commented:
I don't think it did - I renamed it and still get exactly the same. I also get a content-size of 50000 in the log *whatever* I return. I used a program that just returns without any output and the log reported that as sending 50000.
Something odd is going on...

--Rob
0
boycyAuthor Commented:
Ok problem has been solved - The company that manufacture the embedded system I'm working on (and who customised the red hat build) replied to me.
The problem was that the web server was being run in a chroot jail, which I didn't think was a problem as everything was under the www root.
Everything...except the C libraries! And the bash interpreter I assume.

All solved and working like magic now :)
Cheers for your help ITcrow.

--Rob
0
ITcrowCommented:
Cool! something to remember while stripping the OS.
0
LunchyCommented:
Closed, 125 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.