【问题标题】:apache mod_perl : dynamic handler based on urlapache mod_perl:基于 url 的动态处理程序
【发布时间】:2013-02-27 13:26:36
【问题描述】:

我的要求如下。

如果请求的url是这样的

http://localhost/mod_perl/TopModule/ActualModule/method1

那我应该调用 TopModule::ActualModule->method1()

如何配置 Apache 来执行此操作?

【问题讨论】:

    标签: apache2 mod-perl2


    【解决方案1】:

    脚本名称后面的 URL 部分在 $ENV{PATH_INFO} 中传递给你的 perl 程序。因此,您可以创建一个称为 modulerunner 的 perl 脚本,您可以使用类似 'http://whatever.host/modulerunner/Top/Actual/method' 的 URL 调用它:

    my $arg=$ENV{PATH_INFO};        <-- contains Top/Actual/method
    my @arg=split("/", $arg);       <-- [ "Top", "Actual", "method" ]
    my $method=pop @arg;            <-- removes "method", "Top" and "Actual" remain in @arg
    my $modules=join("::", @arg);   <-- "Top::Actual"
    my $call="$modules->$method()"; <-- "Top::Actual->method()"
    eval $call;                     <-- actually execute the method
    

    然而,我一点也不推荐这个——它打开了太多的安全漏洞,让你的网站访问者可以调用任何模块中的任何 perl 函数。所以,除了你在自己的服务器上做这个没有连接到任何其他东西,我只会去一个非常无聊的 if-then-cascade。

    $p=$ENV{PATH_INFO};
    if     ($p eq "Top/Actual/method") { Top::Actual->method(); }
    elseif ($p eq "Other/Module/function" { Other::Module->function(); }
    else {
        print "<font color=red>Don't try to hack me this way, you can't.</font>\n";
    }
    

    哦,也不要将 标记用于任何有生产力的东西;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多