Apache2 sends two HTTP headers with displayed "nph-" CGI

Summary

I am trying to map some file extensions that will be executed by the nph (Non Parsed Header) executable.

Let's say I want to access the URL http://server/file.extand map the ext file extension to “run” the execution of my nph CGI (/var/www/cgi-bin/nph-test.sh).

Apache configuration

For this I use mod_actions and mod_cgid, this is my relevant configuration information:

<VirtualHost *:80>
        DocumentRoot /var/www/htdocs
        ScriptAlias /cgi-bin /var/www/cgi-bin

        Action cgi-wrapper /cgi-bin/nph-test.sh

        AddHandler cgi-wrapper .ext
</VirtualHost>

NPH CGI script

This is my nph-test.sh shell:

#!/bin/sh

echo "HTTP/1.0 200 OK"
echo "Server: Shell/1.0"
echo "Status: 200 \"OK\""
echo "Connection: close"
echo "Content-type: text/html"
echo ""
echo "<html><body><pre>"
echo "QUERY_STRING = "${QUERY_STRING}
echo "PATH_TRANSLATED = "${PATH_TRANSLATED}
echo "</pre></body></html>"

Problem

When I access nph-test.sh with this URL http://localhost/cgi-bin/nph-test.sh?Hello, I get:

HTTP/1.0 200 OK
Server: Shell/1.0
Status: 200 "OK"
Connection: close
Content-type: text/html

<html><body><pre>
QUERY_STRING = Hello
PATH_TRANSLATED =
</pre></body></html>

, URL http://localhost/file.ext double http ( nph cgi script nph cgi script):

HTTP/1.0 200 OK
Server: Shell/1.0
Status: 200 "OK"
Connection: close
Content-type: text/html

<html><body><pre>
QUERY_STRING =
PATH_TRANSLATED = /var/www/htdocs/file.ext
</pre></body></html>
HTTP/1.1 200 OK^M
Date: Tue, 18 Mar 2014 14:32:29 GMT^M
Server: Apache/2.4.6 (Ubuntu)^M
Content-Length: 0^M
Keep-Alive: timeout=5, max=100^M
Connection: Keep-Alive^M
Content-Type: text/x-sh^M
^M

2003 apache. , .

mod_cgid , , nph- cgi prefix (strrchr):

1372 if ((argv0 = strrchr(r->filename, '/')) != NULL) {
1373    argv0++;
1374 }
1375 else {
1376    argv0 = r->filename;
1377 }
1378    
1379 nph = !(strncmp(argv0, "nph-", 4));

apache2 nph cgi? ( http)

?

Apache

apache2 Ubuntu 13.10:

# apachectl -V
Server version: Apache/2.4.6 (Ubuntu)
Server built:   Dec  5 2013 18:32:22
Server Module Magic Number: 20120211:23
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

:

mod_rewrite:

<VirtualHost *:80>
        DocumentRoot /var/www/htdocs
        ScriptAlias /cgi-bin /var/www/cgi-bin

        RewriteEngine on

        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^/?(.*\.ext)$ /cgi-bin/nph-test.sh?$1 [PT]

        RewriteRule ^/?(.*\.ext)$ /cgi-bin/nph-test.sh?$1&%{QUERY_STRING} [PT]
</VirtualHost>
+4
1

, () 2.4.x. , .

:

  • nph- ( )
  • ( )

, , mod_rewrite: mod_rewrite, , nph

nph- + mod_rewrite

+1

Source: https://habr.com/ru/post/1532315/


All Articles