Bug 22898

Summary: nph scripts with two HTTP header
Product: Apache httpd-2 Reporter: Marek Chlup <mara>
Component: mod_cgiAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: REOPENED ---    
Severity: normal CC: cm, sacrom, sdhill
Priority: P3 Keywords: PatchAvailable
Version: 2.5-HEAD   
Target Milestone: ---   
Hardware: All   
OS: Windows XP   
Attachments: This is a drop in replacement that fixes bug 22898
mod_cgi diff file that fixes 22898 and related bugs

Description Marek Chlup 2003-09-02 21:39:06 UTC
Hi!

my conf:
--------
AddHandler application/x-httpd-eperl .phtml
Action application/x-httpd-eperl /cgi-bin/nph-test

my request:
-----------
telnet myhost 80
GET /test/t1.phtml HTTP 1.1
Host: myhost

apache send me:
---------------
HTTP/1.1 200 OK
Content-Type: text/plain

Hello!
HTTP/1.1 200 OK
Date: Tue, 02 Sep 2003 21:27:38 GMT
Server: Apache/2.0.47 (Debian GNU/Linux) mod_perl/1.99_09 Perl/v5.8.0
mod_ssl/2.0.47 OpenSSL/0.9.7b
Content-Length: 0
Connection: close
Content-Type: application/x-httpd-php

my script nph-test:
-------------------
#!/usr/bin/perl
print "HTTP/1.1 200 OK\n";
print "Content-Type: text/plain\n\n";
print "Hello!\n";

where is bug?
-------------
My script noprint seccond HTTP header. Why apache generate second header?

Bye
Marek
Comment 1 André Malo 2003-09-03 19:43:40 UTC
Hmm. e-perl is mod_perl, isn't it? I'd guess, mod_perl doesn't support
nph-scripts at all, you'd have to use mod_cgi instead.
Comment 2 Marek Chlup 2003-09-04 06:28:19 UTC
mod_perl no problem. I tested my example without loading mod_perl module too.
Problem is mod_cgi and mod_cgid. My example don't use e-perl. 
Comment 3 Simon Hill 2003-10-31 23:16:40 UTC
I am getting this problem too, with Meta-HTML. I have not been able to reproduce
by telnetting to port, b/c the second header seems to be sent after the
connection is closed! but all my pages are served with a second header at the
end. For example, http://www.metasystema.net/welcome.mhtml

telnet shows the Meta-HTML headers at the beginning where one would expect them:

get /welcome.mhtml
HTTP/1.0 200 OK
Server: MHttpd/4.1 (bfox; i686-linux; Meta-HTML/6.11)
Date: Fri, 31 Oct 2003 23:08:24 GMT
Expires: Thu, 30 Oct 2003 23:08:24 GMT
Last-modified: Fri, 31 Oct 2003 23:07:24 GMT
Content-length: 9585
Meta-HTML-Engine: MHttpd/4.1 (bfox; i686-linux; Meta-HTML/6.11)
Content-type: text/html

then at the end, the page displays the apache2 header (from view source):

HTTP/1.1 200 OK
Date: Fri, 31 Oct 2003 23:05:58 GMT
Server: Apache/2.0.48 (Gentoo/Linux)
Content-Length: 0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: metahtml/interpreted

My conf:
--------
AddType         metahtml/interpreted .mhtml
Action          metahtml/interpreted /cgi-bin/nph-engine
ScriptAlias     /cgi-bin/   /www/metasystema.net/cgi-bin/

I've verified this occurs in 2.0.48 as well as 2.0.47.
Comment 4 André Malo 2003-11-01 09:44:10 UTC
sounds really like a nasty bug ;-)
Comment 5 Joe Orton 2004-06-03 16:19:31 UTC
*** Bug 26724 has been marked as a duplicate of this bug. ***
Comment 6 Sean Conner 2006-02-15 21:33:04 UTC
I have a similar problem, using an "nph-" script via mod_cgi, being redirected
via mod_rewrite.  If I call the script directly, I do not get the additonal
header at the end, but if I go through mod_rewrite, I do.  This is with Apache
2.0.54 and 2.0.55.
Comment 7 Ben Griffin 2006-02-24 20:26:47 UTC
This bug persists on Apache 2.2.It is caused in generators/mod_cgi.c, line 760 
nph = !(strncmp(argv0, "nph-", 4));
    
Add the following just before it (recompile, start with debug logging on):
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Identifying cgi script:%s",
r->filename);

We can see that the strncmp will fail, because the cgi script will include a
full path: e.g.
 "Identifying cgi script:/home/www/site/test/bin/nph-foobar.cgi"

The conf lines for this are as follows:
 ScriptAlias  /x/         /home/www/site/test/bin
 Action       foo-file    /x/nph-foobar.cgi
 AddHandler   foo-file    .foo

So - changing line 760 to:
nph = ( strstr(argv0,"nph-") != NULL );
(which of course means that any "nph-" will return positive...)
now identifies nph scripts as such. However.. the additional headers STILL
appear... at which point.. I am stumped. 
Comment 8 Ben Griffin 2006-02-24 23:37:55 UTC
There is a work-around, which is to change the return line of any cgi script
from being e.g.
HTTP/1.1 200 OK
to
Status 200 OK
- and dropping nph- status. 

Of course this does NOT fix the legacy issue, and probably remains a typical
reason why so many people carry on using 1.3.x (slaps wrist).

I've done some more RA on this.
The dupe header always shows Content-Length: 0
It looks like a default / stub header is being pushed out.

Interestingly - this bug ONLY appears to occur when using the Action directive,
(It makes no difference if one uses AddType or AddHandler or SetHandler ) ..
guess where I will look next...

Output filters are being correctly stripped by mod-cgi - though it seems like it
would be easier just to do something like 
rv = ap_pass_brigade(r->output_filters, bb);
.. but hey, what do I know?


Comment 9 Ben Griffin 2006-02-25 00:35:53 UTC
Created attachment 17796 [details]
This is a drop in replacement that fixes bug 22898

This adds some APLOG_DEBUG messages, which are useful for nph- bug issues.
There are 2 major bugs that are now 'fixed' here. Of course, the fixes are
kludges, but then I'm not a core Apache programmer. Note that identification of
nph- now uses strstr, rather than strncmp! Also note that the nph- code part
strips out output_filters from it's immediate caller!
Comment 10 Ben Griffin 2006-02-25 00:38:18 UTC
This bug is essentially fixed - though it needs to be folded into the main cvs.
There were two basic bugs here - one was the faulty (ie not working)
identification of nph- cgi scripts. The other was the misconception that an
nph-cgi request will be the initial request; something patently not true when
being called via Action.

The fix that I have done is lame. It only deals with one previous process;
however, this seems to be the primary problem, so it works. Basically, we now
call ap_is_initial_req(r) and if it is !=1, then we also strip the
output_filters of the previous/container request.

I have posted the entire amended file. sorry. I don't understand the patch system..

Comment 11 Ben Griffin 2006-03-14 12:40:19 UTC
Created attachment 17891 [details]
mod_cgi diff file that fixes 22898 and related bugs

The Apache2.x fails to behave properly when nph- are used in Actions.
This patch includes some debugging messages, and fixes two related severe bugs
which cause nph- cgi scripts to fail when cgi,_handler is called via an Action
directive. The bugs are (1) apr_filepath_name_get(r->filename) returns a full
path when cgi_handler is accessed via Action, which means that the strncmp test
fails to identify the nph- prefix. (2) The clear up of the bucket brigade fails
unless the request is the ap_is_initial_req. The fix in the patch for (1) is to
use strstr instead of strncmp - though there may be a better fix for that.  The
fix in the patch for (2) is on false for ap_is_initial_req to clear the bucket
brigade at r->prev.
Comment 12 Ben Griffin 2007-06-11 02:30:44 UTC
After a year, the patch below has still not been folded into the 2.2 trunk.

Until it is,  nph- scripts addressed via AddHandler or Action fail on Apache 2.x!

I am aware that nph- is considered a dead area - but it's necessary for legacy
code and regardless, Apache 2.x claims support for it (and indeed does support
it when it is not referred to via Action or AddHandler)