Bug 36710

Summary: CGI output not captured
Product: Apache httpd-2 Reporter: Stefan Krude <sk_np>
Component: mod_cgiAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: REOPENED ---    
Severity: blocker CC: szg0000
Priority: P2    
Version: 2.0.54   
Target Milestone: ---   
Hardware: PC   
OS: Windows 2000   

Description Stefan Krude 2005-09-19 15:58:41 UTC
When a batch file is executed via CGI, the output of programs this batch file
calls is not captured and sent to the client.

Example:
contents of test.cmd
--------------------
@echo off
echo Content-type: text/plain
echo.
echo pinging 192.168.0.160
ping.exe 192.168.0.160
echo done
--------------------

The host is pinged, thus the ping.exe is executed (I can see the ICMP packets
with Ethereal, but the output of the ping program does not appear in the browser
window (only the text 'pinging 192.168.2.160' and 'done').
Comment 1 Nick Kew 2005-09-19 16:37:53 UTC
Please ask in a user support forum, if you have difficulty with the numerous  
examples of CGI scripts that capture the output of external programs. 
Comment 2 Stefan Krude 2005-09-19 16:48:15 UTC
Sorry, I *asked* in users@httpd.apache.org but got no answer. And I have
extensively googled - without result.
What 'numerous examples of CGI scripts that capture the output of external
programs' do you refer to?
I don't think my example CGI script is wrong (it runs with IIS which I want to
replace by Apache) and I don't see any mod_cgi directive that could be related
to my problem.
Comment 3 Nick Kew 2005-09-19 17:50:23 UTC
I didn't see that.  Maybe if you said windows I just assumed it lay outside my  
expertise and I had nothing to say.  
  
You just capture the output from your child command, and print it back to  
stdout.  So in *X your script could be something like:  
 
echo Content-type: text/plain 
echo 
echo pinging 192.168.0.160 
/path/to/ping.exe 192.168.0.160 | while read line 
do 
  echo $line 
done 
echo done 
Comment 4 Stefan Krude 2005-09-19 18:24:32 UTC
Yes, this is what in *X will work, but in *X the problem doesn't exist, script
output and output from progs called by the script is captured properly.
Unfortunately Windows' cmd.exe doesn't know 'while' and 'read' and this would be
an odd workaround anyway.
I think if capturing from programs invoced by a script doesn't work with
Windows, it must be a bug.
Comment 5 cjm 2005-09-28 08:53:47 UTC
(In reply to comment #4)
> Yes, this is what in *X will work, but in *X the problem doesn't exist, script
> output and output from progs called by the script is captured properly.
> Unfortunately Windows' cmd.exe doesn't know 'while' and 'read' and this would be
> an odd workaround anyway.
> I think if capturing from programs invoced by a script doesn't work with
> Windows, it must be a bug.
> 

The bug is actually not an apache bug.  Under windows a command script will not
echo the output of any program it calls if @echo off is called.

I suggest changing the script to:

@echo off
echo Content-type: text/plain
echo.
echo pinging 192.168.0.160
@echo on
ping.exe 192.168.0.160
@echo off
echo done

and leave it there.  If that doesn't work, reopen this bug.
Comment 6 Stefan Krude 2005-09-28 09:20:43 UTC
Of course that doesn't help. @echo off switches off the echoing of the command
line, not the output of any program the script is calling.
And as I said before: My scripts (that script with ping.exe is only a test
script) are running well with Microsoft IIS, but I want to migrate to Apache and
would expect that the scripts will work as well as with IIS (if not better ;-).
Comment 7 André Malo 2005-09-28 09:35:15 UTC
I would suspect that PATH isn't set as expected. May that be?
what does echo %PATH% put out? Is there anything in the error log that ping.exe
is not found? (Can't test myself right now, sorry).
Comment 8 Stefan Krude 2005-09-28 09:40:33 UTC
No, the ping is executed, pls. look at my first posting:
> The host is pinged, thus the ping.exe is executed (I can see the ICMP packets
> with Ethereal, but the output of the ping program does not appear in the browser
> window (only the text 'pinging 192.168.2.160' and 'done').

The problem is *NOT* that the .exe isn't called but that it's output isn't
caputered and sent to the web client. The output of cmd.exe (the scripting host)
*IS* captured but the output of all of it's child processes is not.
Comment 9 André Malo 2005-09-28 12:08:33 UTC
oh well. Anyway; perhaps I've missed that also - there's still the question if
there's anything in the error log.
Comment 10 cjm 2005-09-28 17:54:35 UTC
(In reply to comment #8)
> No, the ping is executed, pls. look at my first posting:
> > The host is pinged, thus the ping.exe is executed (I can see the ICMP packets
> > with Ethereal, but the output of the ping program does not appear in the browser
> > window (only the text 'pinging 192.168.2.160' and 'done').
> 
> The problem is *NOT* that the .exe isn't called but that it's output isn't
> caputered and sent to the web client. The output of cmd.exe (the scripting host)
> *IS* captured but the output of all of it's child processes is not.

Sorry, I hastily threew that together earlier without thinking and I can see
your problem.  It appears that ping opens in a NEW command session rather than
the Apache cmd session and therefore does not behave as expected.

In other words, take this up with M$.
Comment 11 William A. Rowe Jr. 2005-09-28 18:01:05 UTC
This is a bug reporting forum, not a user support forum.

Your problem is that on win32, ping never terminates unless you tell it to.
Fix your script to perform it's action and -terminate- and perhaps you will
have a working CGI.

Bill
Comment 12 Stefan Krude 2005-09-29 22:58:52 UTC
Sorry, but that's rubbish. Of course ping.exe will terminate, but ping.exe is
not the matter, it was just an example. Take for another example any hello world
program. It's output will not be seen by the client if this program is called by
cmd.exe running as an Apache CGI.
Can anybody understand the problem?
Isn't it a bug if the output of an scripted CGI never reaches the user?
Isn't it a bug if the same script runs well with Microsoft IIS?
Isn't it a bug if (nearly) the same script runs well with Apache/Unix?
Comment 13 William A. Rowe Jr. 2006-06-22 08:37:12 UTC
Please change your script to invoke

ping.exe -n 4 192.168.0.160

and I think your problem will be solved (just tested locally).
Comment 14 Charles Shapiro 2007-01-09 11:12:11 UTC
Yep, this still is an Issue on Windows XP with Apache 2.2.3.
I am Not a Windows Guy, so this took me about a week to figure out.
Some additional observations:  1) Internal cmd.exe commands (e.g. "echo", "dir")
work fine  2) Re-directing the output of an external program (such as "ping.exe"
or "netstat.exe") to a file yields a 0-length file. 3) If you set the "Allow
service to interact with desktop" checkbox in "Administrative
Tools->Services->Apache->properties->Log On", you can see the stdout from your
commands in a desktop window which closes when the command completes.
Also NB that running the script from a command line yields the expected results.
The only work-around I could find was to drop back to apache 1.3.34, which works
correctly. See also http://archive.apache.org/gnats/4898