如何在 mod_perl 的 BEGIN 块中访问 Apache 服务器配置?

发布于 2024-07-12 00:42:38 字数 1823 浏览 9 评论 0原文

我一直在尝试从使用 PerlSetEnv 切换到使用 自定义配置指令< /a>. 我的配置模块包含文档中的 set_val 副本:

sub set_val
{
    local our ($key, $self, $parms, $arg) = @_;
    $self->{$key} = $arg;
    unless ($parms->path)
    {
        local our $srv_cfg = Apache2::Module::get_config($self, $parms->server);
        $srv_cfg->{$key} = $arg;
    }
}

...每个自定义指令子都会调用它。 然后我在我的 .conf 中:

PerlLoadModule MyModule::ServerConfig
MyCustomDirective 'hello'

这工作得很好,因为 httpd -t 允许文件的语法。 问题是我似乎无法从 BEGIN 块内获取配置文件的值,而我需要这样做。

我尝试过修补各种事情:

BEGIN
{
    use Apache2::CmdParms ();
#   use Apache2::Directive ();
    use Apache2::Module ();
#   use Apache2::ServerUtil ();
#   use Apache2::RequestUtil ();

    use Data::Dump;
    warn ddx(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::CmdParms->server));
#   warn ddx(Apache2::Directive->as_hash);
#   warn Apache2::ServerUtil->dir_config('MyCustomDirective);
#   warn Apache2::CmdParms->server->server_hostname();
}

……但无济于事。 我的大部分努力(例如尝试访问 CmdParms->server)导致 Parent: child process exited with status 3221225477 -- Restarting 并自动重新启动 Apache,如下所示它说。 如果我将 ServerUtil->server 传递给 get_config(),服务器保持活动状态,但警告仅打印出“1”。

我在某处读到,这是因为您无法在 BEGIN 块中获取与请求相关的任何内容,因为请求各不相同。 这有点有意义,除了使用 PerlOptions +GlobalRequest 我能够在 BEGIN 块中看到 $ENV ,那么为什么我看不到我的自己的指令,就像它们依赖于请求如何发生一样? 特别令人困惑的是,如果我尝试将 Apache2::RequestUtil->request->per\_dir\_config() 传递给 get_config(),它会显示 全局 $r 对象不可用。如果在 BEGIN 块中是这样,我如何才能在 $ENV 处获取它?

I've been trying to switch from using PerlSetEnv to using custom configuration directives. I have my configuration module with a copy of set_val from the docs:

sub set_val
{
    local our ($key, $self, $parms, $arg) = @_;
    $self->{$key} = $arg;
    unless ($parms->path)
    {
        local our $srv_cfg = Apache2::Module::get_config($self, $parms->server);
        $srv_cfg->{$key} = $arg;
    }
}

...which is called by every custom directive sub. Then I have in my .conf:

PerlLoadModule MyModule::ServerConfig
MyCustomDirective 'hello'

This works fine in that httpd -t okays the file's syntax. The problem is that I can't seem to get at the value from the config file from within a BEGIN block, which I need to do.

I've tried tinkering with all sorts of things:

BEGIN
{
    use Apache2::CmdParms ();
#   use Apache2::Directive ();
    use Apache2::Module ();
#   use Apache2::ServerUtil ();
#   use Apache2::RequestUtil ();

    use Data::Dump;
    warn ddx(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::CmdParms->server));
#   warn ddx(Apache2::Directive->as_hash);
#   warn Apache2::ServerUtil->dir_config('MyCustomDirective);
#   warn Apache2::CmdParms->server->server_hostname();
}

...but to no avail. Most of my efforts (trying to access CmdParms->server for instance) result in Parent: child process exited with status 3221225477 -- Restarting and an automatic restart of Apache as it says. If I pass ServerUtil->server to get_config(), the server stays alive but the warning only prints out '1'.

I read somewhere that this is because you can't get at anything request-related within a BEGIN block, because requests vary. It kind of makes sense, except that with PerlOptions +GlobalRequest I have been able to see $ENV within a BEGIN block, so why wouldn't I be able to see my own directives, just as dependent as they are on how the request happens? Especially confusing is that if I try to pass Apache2::RequestUtil->request->per\_dir\_config() to get_config(), it says Global $r object is not available. If that's true in a BEGIN block, how is it I can get at $ENV?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝试将您想要导入功能添加到其他模块,并在您通常放置BEGIN块的代码中使用此模块。 它应该工作相同。 也许有帮助。

Try add what you want to import function to other module and use this module in code where you usually put BEGIN block. It should work same. May be it helps.

甜心 2024-07-19 00:42:38

部分原因是未正确使用转储。 这效果更好:

use Data::Dump qw(pp);
warn pp(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::ServerUtil->server));

但是,它不会显示中出现的任何指令。 块。

不过,在我的具体情况下,我再想一想,我不需要这个功能; 那恰好是我把它们粘在的地方。

Partly, Dump isn't being used correctly. This works better:

use Data::Dump qw(pp);
warn pp(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::ServerUtil->server));

However, it doesn't show any directives that appear within <Directory> blocks.

In my particular case, though, I don't need that functionality, on second thought; that just happens to be where I had stuck them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文