Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CGI program doesn't return anything

Posted on 2004-10-28
15
Medium Priority
?
308 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:boycy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 5

Expert Comment

by:ITcrow
ID: 12439463
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
 
LVL 13

Expert Comment

by:gripe
ID: 12440096
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
 
LVL 13

Expert Comment

by:gripe
ID: 12440110
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Author Comment

by:boycy
ID: 12443253
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
 
LVL 6

Author Comment

by:boycy
ID: 12443281
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12443292
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
 
LVL 6

Author Comment

by:boycy
ID: 12443364
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12443474
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
 
LVL 6

Author Comment

by:boycy
ID: 12443571
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12443892
I think it just executed your 'bash' shell.
0
 
LVL 6

Author Comment

by:boycy
ID: 12444063
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
 
LVL 6

Author Comment

by:boycy
ID: 12444165
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12444195
Cool! something to remember while stripping the OS.
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 12478883
Closed, 125 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question