Apache2 发送两个带有映射的“nph-"的 HTTP 标头.电脑动画 [英] Apache2 sends two HTTP headers with a mapped "nph-" CGI

查看:38
本文介绍了Apache2 发送两个带有映射的“nph-"的 HTTP 标头.电脑动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结

我正在尝试映射一些要由 nph(非解析头)CGI 可执行文件执行的文件扩展名.

假设我想访问一个 URL http://server/file.ext 并将ext"文件扩展名映射到触发"我的 nph CGI (/var/www/cgi-bin/nph-test.sh).

Apache 配置

为此我使用了 mod_actions 和 mod_cgid,这是我的相关配置信息:

DocumentRoot/var/www/htdocsScriptAlias/cgi-bin/var/www/cgi-bin动作 cgi-wrapper/cgi-bin/nph-test.shAddHandler cgi-wrapper .ext</虚拟主机>

NPH CGI 脚本

这是我的 nph-test.sh 外壳:

#!/bin/shecho "HTTP/1.0 200 OK"回声服务器:Shell/1.0"echo "状态:200 \"OK\""回声连接:关闭"echo "内容类型:文本/html"回声"echo "<html><body><pre>"echo "QUERY_STRING = "${QUERY_STRING}echo "PATH_TRANSLATED = "${PATH_TRANSLATED}echo "</pre></body></html>"

问题

当我使用此 URL http://localhost/cgi-bin/nph-test.sh?Hello 访问 nph-test.sh 时,我得到:

HTTP/1.0 200 OK服务器:Shell/1.0状态:200正常"连接:关闭内容类型:文本/html<html><body><pre>QUERY_STRING = 你好PATH_TRANSLATED =</pre></body></html>

使用此 URL 中的文件映射时出现问题 http://localhost/file.ext 我得到一个 double http 标头(它正在处理 nph cgi脚本就像一个 nph cgi脚本):

HTTP/1.0 200 OK服务器:Shell/1.0状态:200正常"连接:关闭内容类型:文本/html<html><body><pre>QUERY_STRING =PATH_TRANSLATED =/var/www/htdocs/file.ext</pre></body></html>HTTP/1.1 200 OK^M日期:2014 年 3 月 18 日星期二 14:32:29 GMT^M服务器:Apache/2.4.6 (Ubuntu)^M内容长度:0^M保持活动:超时=5,最大值=100^M连接:Keep-Alive^M内容类型:text/x-sh^M^M

我的发现

搜索了几个小时后,我只找到了这个 2003 apache 错误报告.这也暗示了理论上尚未应用的补丁.

但是搜索当前的mod_cgid源代码 可以看到代码已经改成能够捕获nph-"cgi文件前缀(strrchr):

1372 if ((argv0 = strrchr(r->filename, '/')) != NULL) {第1373章第1374章第1375话第 1376 章第1377章1378第 1379 章

问题

apache2 有没有办法将文件扩展名映射到 nph cgi?(不返回两个http头)

是否有其他方法可以做到这一点或任何解决方法?

Apache 和 OS 服务器信息

我在 Ubuntu 13.10 中使用默认的 apache2 服务器:

# apachectl -V服务器版本:Apache/2.4.6 (Ubuntu)服务器建成时间:2013 年 12 月 5 日 18:32:22服务器模块魔数:20120211:23服务器加载:APR 1.4.8,APR-UTIL 1.5.2编译使用:APR 1.4.8,APR-UTIL 1.5.2架构:64位服务器 MPM:事件线程:是(固定线程数)分叉:是(可变进程数)服务器用...编译-D APR_HAS_SENDFILE-D APR_HAS_MMAP-D APR_HAVE_IPV6(启用 IPv4 映射地址)-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="日志/apache_runtime_status"-D DEFAULT_ERRORLOG="日志/错误日志"-D AP_TYPES_CONFIG_FILE="mime.types"-D SERVER_CONFIG_FILE="apache2.conf"

更新:解决方案

使用mod_rewrite模块解决问题的修改配置:

DocumentRoot/var/www/htdocsScriptAlias/cgi-bin/var/www/cgi-bin重写引擎开启重写条件 %{QUERY_STRING} ^$RewriteRule ^/?(.*\.ext)$/cgi-bin/nph-test.sh?$1 [PT]RewriteRule ^/?(.*\.ext)$/cgi-bin/nph-test.sh?$1&%{QUERY_STRING} [PT]</虚拟主机>

解决方案

我认为(直接)在 2.4.x 源代码上是不可能的.正如您正确指出的那样,该错误目前处于重新打开状态.>

提议的补丁包括两个主要修改:

  1. 修复 nph- 检测(这部分目前在源代码树中)
  2. 去掉所有剩余的输出过滤器(这部分在源树中缺失)

我无法测试,但我认为可以使用 mod_rewrite 解决此错误:查看 mod_rewrite 文档似乎可以很好地使用 nph

您可以在文档页面

Summary

I'm trying to map some file extensions to be executed by a nph (Non Parsed Header) CGI executable.

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

Apache configuration

To do that I'm using 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>

The problem appears when using the file mapping like in this URL http://localhost/file.ext I get a double http header (it's treating the nph cgi script like a non 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

My findings

After searching for some hours I only found this 2003 apache bug report. Which also suggests a patch that in theory has not being applied.

But searching the current mod_cgid source code I can see that the code has been changed to be able to capture the "nph-" cgi file 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));

Question

Is there a way with apache2 to map a file extension to a nph cgi? (without returning two http headers)

Are there other ways to do that or any workarounds?

Apache and OS server information

I'm using the default apache2 server in Ubuntu 13.10:

# apachectl -V
Server version: Apache/2.4.6 (Ubuntu)
Server built:   Dec  5 2013 18:32:22
Server's 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"

Update: Solution

The changed configuration to solve the problem using the mod_rewrite module:

<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>

解决方案

I don't think it's possible (directly) on the 2.4.x source code. As you correctly pointed out the bug is currently in reopened status.

The proposed patch consists of two main modification:

  1. Fix the nph- detection (and this part is currently in the source tree)
  2. Getting rid of all remaining output filters (this part is missing in the source tree)

I'm not able to test, but I think it's possible to workaround this bug using mod_rewrite: Looking in mod_rewrite documentation seems working just fine with nph

You can find a complete nph- + mod_rewrite example in documentation page

这篇关于Apache2 发送两个带有映射的“nph-"的 HTTP 标头.电脑动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆