2

我想知道是否以及如何为 PHP 中的 Apache 2 编写自定义“协议处理程序”(在自定义端口上侦听)?

在 C 和 mod_perl 中,您可以编写所谓的“协议处理程序”,它拦截早期的 Apache 阶段(在客户端套接字连接被接受()之后,但在任何内容被写入之前)并且可以例如处理FTPSMTP协议。在PHP中也可以吗?

例如,我有以下简单的 mod_perl 处理程序,我想将其移植到 PHP(以比较内存使用情况——因为我的 mod_perl 处理程序需要每个孩子 20m)。我的处理程序侦听端口 843 并将字符串 POLICY 写入客户端套接字:

package SocketPolicy;

# Run: semanage port -a -t http_port_t -p tcp 843
# And add following lines to the httpd.conf
# Listen 843
# <VirtualHost _default_:843>
#       PerlModule                   SocketPolicy
#       PerlProcessConnectionHandler SocketPolicy
# </VirtualHost>

use strict;
use warnings FATAL => 'all';
use APR::Const(-compile => 'SO_NONBLOCK');
use APR::Socket();
use Apache2::ServerRec();
use Apache2::Connection();
use Apache2::Const(-compile => qw(OK DECLINED));

use constant POLICY =>
qq{<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" to-ports="8080"/>
</cross-domain-policy>
\0};

sub handler {
        my $conn   = shift;
        my $socket = $conn->client_socket();
        my $offset = 0;

        # set the socket to the blocking mode
        $socket->opt_set(APR::Const::SO_NONBLOCK => 0);

        do {
                my $nbytes = $socket->send(substr(POLICY, $offset),
                             length(POLICY) - $offset);
                # client connection closed or interrupted
                return Apache2::Const::DECLINED unless $nbytes;
                $offset += $nbytes;
        } while ($offset < length(POLICY));

        my $slog = $conn->base_server()->log();
        $slog->warn('served socket policy to: ', $conn->remote_ip());
        return Apache2::Const::OK;
}

1;

谢谢,亚历克斯

4

4 回答 4

1

不,mod_php 没有实现 mod_perl 所做的 Apache 处理程序阶段。

http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3/sapi/apache2handler/php_functions.c?revision=296107&view=markup#l516

于 2010-10-22T15:05:38.540 回答
0

从技术上讲,您可以使用套接字扩展,因为您还允许脚本运行足够长的时间。默认情况下,PHP 脚本将在 60 秒左右后终止。

所以主要问题是让你的 PHP 脚本像守护进程一样运行。

于 2010-10-22T13:47:43.850 回答
0

完全有可能......甚至还有一个用 PHP 编写的完整的 Web 服务器

于 2010-10-22T14:09:39.423 回答
0

Yes and no.... PHP has an Apache interface which uses the Apache handler API, as well as other interfaces, such as CLI, CGI etc. The PHP Apache API integration chooses not to expose the lower-level handling details like mod_perl does, so if you need access to these callbacks you'll have to write your own PHP Apache handler API implementation.

于 2010-10-22T15:15:37.643 回答