如何强制 FastCGI 将表单数据编码为 UTF-8,就像 CGI.pm 有选项一样?
尝试在 FastCGI 下运行旧的 CGI 脚本。不带额外参数的打印会给出正确的输出: print $q->div( $q->param("text") )
但是当使用 CGI 方法的额外参数哈希打印时 print $q->div( {-id=>"id"}, $q->param("text") )
,它会破坏 UTF-8 格式的数据 ('õäöüžš' ->; '
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????一切都在普通 CGI 下完美运行(使用“-utf8”-flag)。
FastCGI 转换的示例脚本,称为 test.fcgi?text=õäöüžš
应该给出四个相等的块:
#!/usr/bin/perl -w --
use strict;
use CGI::Fast qw(:all);
use locale;
use utf8;
BEGIN {
binmode(STDIN); # Form data
binmode(STDOUT, ':encoding(UTF-8)'); # HTML
binmode(STDERR, ':encoding(UTF-8)'); # Error messages
}
my ($q) = ();
my $test = "õäöüžš";
while ($q = new CGI::Fast) {
print $q->header(-type=>"text/html", -charset=>"utf-8"),
$q->start_html(-encoding=>"utf-8");
print "1: ",
$q->div( $q->param('text') ),
"<br />",
"2: ",
$q->div( {-id=>"id"}, $q->param('text') ),
"<br />",
"3: ",
$q->div( $test ),
"<br />",
"4: ",
$q->div( {-id=>"id"}, $test ),
$q->end_html();
}
第一个块很好,第二个块损坏,第三个和第四个也很好:
普通的 CGI 示例,因为它给出了所有4正确的方法:
#!/usr/bin/perl -w --
use strict;
use CGI qw(:all -utf8);
use locale;
use utf8;
BEGIN {
binmode(STDIN); # Form data
binmode(STDOUT, ':encoding(UTF-8)'); # HTML
binmode(STDERR, ':encoding(UTF-8)'); # Error messages
}
my ($q) = ();
my $test = "õäöüžš";
$q = new CGI;
print $q->header(-type=>"text/html", -charset=>"utf-8"),
$q->start_html(-encoding=>"utf-8");
print "1: ",
$q->div( $q->param('text') ),
"<br />",
"2: ",
$q->div( {-id=>"id"}, $q->param('text') ),
"<br />",
"3: ",
$q->div( $test ),
"<br />",
"4: ",
$q->div( {-id=>"id"}, $test ),
$q->end_html();
在我看来,FastCGI表单数据没有utf8标志,我不明白,如何正确强制它?在 CGI.pm 下我声明为:
use CGI qw(:all -utf8);
但是 FastCGI 怎么样?
Trying to run old CGI-scripts under FastCGI. Printing without extra parameters gives proper output: print $q->div( $q->param("text") )
But when printing out with extra parameters hash for CGI-methods print $q->div( {-id=>"id"}, $q->param("text") )
, it ruins UTF-8 formed data ('õäöüžš' -> 'õäöüžš')
It happens only with CGI parameters, in script defined variables work fine (examples 3 and 4). Everything works perfecty under ordinary CGI (with "-utf8"-flag ).
FastCGI-turned example script, called as test.fcgi?text=õäöüžš
should give four equal blocks:
#!/usr/bin/perl -w --
use strict;
use CGI::Fast qw(:all);
use locale;
use utf8;
BEGIN {
binmode(STDIN); # Form data
binmode(STDOUT, ':encoding(UTF-8)'); # HTML
binmode(STDERR, ':encoding(UTF-8)'); # Error messages
}
my ($q) = ();
my $test = "õäöüžš";
while ($q = new CGI::Fast) {
print $q->header(-type=>"text/html", -charset=>"utf-8"),
$q->start_html(-encoding=>"utf-8");
print "1: ",
$q->div( $q->param('text') ),
"<br />",
"2: ",
$q->div( {-id=>"id"}, $q->param('text') ),
"<br />",
"3: ",
$q->div( $test ),
"<br />",
"4: ",
$q->div( {-id=>"id"}, $test ),
$q->end_html();
}
First block is fine, second broken, 3rd and 4th also fine:
Ordinary CGI-example as that gives all 4 right way:
#!/usr/bin/perl -w --
use strict;
use CGI qw(:all -utf8);
use locale;
use utf8;
BEGIN {
binmode(STDIN); # Form data
binmode(STDOUT, ':encoding(UTF-8)'); # HTML
binmode(STDERR, ':encoding(UTF-8)'); # Error messages
}
my ($q) = ();
my $test = "õäöüžš";
$q = new CGI;
print $q->header(-type=>"text/html", -charset=>"utf-8"),
$q->start_html(-encoding=>"utf-8");
print "1: ",
$q->div( $q->param('text') ),
"<br />",
"2: ",
$q->div( {-id=>"id"}, $q->param('text') ),
"<br />",
"3: ",
$q->div( $test ),
"<br />",
"4: ",
$q->div( {-id=>"id"}, $test ),
$q->end_html();
It seems to me, that with FastCGI form-data has no utf8-flag on and i don't understand, how to properly force it? Under CGI.pm i declare as:
use CGI qw(:all -utf8);
But how with FastCGI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Git 2.27(2020 年第 2 季度)中,Gitweb 应该可以解决该问题。
请参阅 提交 2ecfcde(2020 年 3 月 29 日),作者:朱利安·穆蒂尼奥 (
ju1m
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 7a8bb6d,2020 年 4 月 22 日)With Git 2.27 (Q2 2020), Gitweb should fix the issue.
See commit 2ecfcde (29 Mar 2020) by Julien Moutinho (
ju1m
).(Merged by Junio C Hamano --
gitster
-- in commit 7a8bb6d, 22 Apr 2020)我会这样做:
I would do:
1) CGI::Fast 是 CGI.pm,因此您可以指定相同的导入参数。
2) FCGI 流是使用旧的流 API 实现的,TIEHANDLE。使用 binmode() 应用 PerlIO 层没有任何效果。正确的解决方案是在输出数据之前对其进行编码,但如果这不是一个选项,我可以提供此热补丁:
1) CGI::Fast is a subclass of CGI.pm, so you can specify the same import arguments.
2) FCGI streams are implemented using the older stream API, TIEHANDLE. Applying PerlIO layers using binmode() has no effect. The proper solution would be to encode your data before outputting it, but if thats not an option I can offer this hotpatch: