我在 Perl 上有带有 fastcgi 的 lighttpd 服务器。 Lighttpd配置:
server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
"mod_rewrite",
"mod_accesslog",
)
server.document-root = "/var/www"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.max-keep-alive-requests = 10
server.max-keep-alive-idle = 5
#server.max-fds = 10240
server.max-connections = 8192
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm",
"index.lighttpd.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
dir-listing.encoding = "utf-8"
server.dir-listing = "disable"
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/x-javascript", "text/css", "text/html", "text/plain" )
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
$HTTP["host"] =~ "(^|\.)hostname\.net$" {
server.document-root = "/var/www/hostname.net"
url.rewrite-once = (
"^/index.php" => "/index.pl",
)
}
快速 CGI 已启用。这里配置fcgi.server:
fastcgi.server += ( ".pl" =>
((
"socket" => "/tmp/perl.socket" + var.PID,
"bin-path" => "/usr/bin/dispatch.fcgi",
"docroot" => "/var/www/hostname.net",
"check-local" => "disable",
))
)
调度.fcgi:
use strict;
use CGI::Fast;
use Embed::Persistent; {
my $p = Embed::Persistent->new();
while (new CGI::Fast) {
my $filename = $ENV{SCRIPT_FILENAME};
my $package = $p->valid_package_name($filename);
my $mtime;
if ($p->cached($filename, $package, \$mtime)) {
eval {$package->handler;};
}
else {
$p->eval_file($ENV{SCRIPT_FILENAME});
}
}
}
这是我的脚本(编辑后):
use strict;
use warnings;
use CGI;
my $q = new CGI;
open my $fh, '>', "/var/www/hostname.net/payload.body" or die "Can't open payload.body: $!";
{
local $/;
print $fh $q->param('arg1');
}
close $fh;
print $q->header;
print $q->param('arg1');
发送请求:
wget --post-data="arg1=dfsdfasf&arg2=sdfasfdsdf" http://hostname.net/test.pl --save-headers --quiet -O -
payload.body 为空(但更改时间更新)并且转储为:
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Content-Length: 0
Connection: keep-alive
Date: Fri, 05 Oct 2012 12:23:07 GMT
Server: lighttpd/1.4.28
仅此而已。
我尝试通过这种方式获取POST参数,但查询为空。我尝试了这种方法:
use Data::Dumper;
my $request;
use FCGI;
my $env;
my $q = FCGI::Request();
$env = $q->GetEnvironment();
my $buffer = "data\n";
if ( $ENV{'REQUEST_METHOD'} eq "POST" ){
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else {
print ("some error");
};
print("Content-type: text/plain\r\n\r\n", Dumper($buffer),"\n");
我需要从请求中获取 POST 参数(二进制发布数据)。
你能帮我吗?也许我以错误的方式获取 post 参数?
非常感谢!
最佳答案
open
解释
这就是您的第一个脚本不起作用的原因:
open FH, "<payload.body";
# ^---
perl 借用 open
来自 Bourne Shell 的语法。
<
means reading
>
means writing
>>
means appending
+<
means reading and writing
如果我们遵循一些最佳实践,您的open
那么声明将是:
open my $fh, '>', "payload.body" or die "Can't open payload.body: $!";
让 Perl 来帮助你
事实上,您试图 print
到只读的文件句柄。然而,Perl 可以警告您这个问题以及您可能遇到的许多其他问题。就use strict
和use warnings
位于脚本的顶部(实际上是所有脚本)。您可以使用 Carp
发出更好的错误消息模块。请注意,在服务器上,这些警告通常会记录在日志中,但您在那里发现的内容值得查找。
什么 Dumper
是
Data::Dumper
模块采用 Perl 数据结构并将它们转换为可执行的 Perl 代码。这对于调试来说非常有用,有时对于序列化来说还可以,但仅仅打印出字符串或标量就不好了。所有包含的数据都将被引用,以便成为有效的 Perl,这不是您想要的。
其他零碎内容
read
从头开始填充目标变量(索引 0)。指定偏移量作为第四个参数(例如 -1
)以附加您正在读取的数据。
binmode
没有第二个参数仅在 Windows 上有用。
在 Windows 上,\n
可以是逻辑换行符(例如“\r\n
”)。使用字节值 \015\012
在网络领域。
在第二个示例中,您覆盖 $buffer
与 $ENV{CONTENT_LENGTH}
来自 STDIN
的字符。您可能还想打印此内容长度以用于调试目的。
FCGI::Request()
返回一个请求对象。 documentation指出您必须 Accept
每个请求,可能在一个循环中。 Accept
方法返回一个值>= 0
如果成功的话。
关于perl - FCGI+Perl+lighttpd : POST request is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743751/