应该使用CGI.pm的标头方法来输出Content-Type标头吗?

发布于 2024-08-18 15:56:46 字数 478 浏览 3 评论 0原文

如果我在 Perl 中使用 CGI 模块,我可以这样做

$cgi = CGI->new();
$cgi->header(-type=>"text/html");

或者选择经典

print "Content-Type: text/html\r\n\r\n";

我们使用哪个重要吗?两者有什么区别吗?两者似乎都有效。

对我来说,如果我使用 CGI ,我会选择第一个,但如果没有的话我不会只为了一项操作而加载模块。但只是想知道是否有关于这个主题的任何事情?

心理

If I'm using the CGI module in Perl, I can do this

$cgi = CGI->new();
$cgi->header(-type=>"text/html");

Or go for the classic

print "Content-Type: text/html\r\n\r\n";

Does it matter which we use? Is there any difference between the two? Both seem to work.

For me I'd go for the first if I was using CGI anyway, but if not then I wouldn't bother loading the module for just one action. But just wondering if there is any thing on the subject?

Psy

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

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

发布评论

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

评论(4

风吹雪碎 2024-08-25 15:56:46

严格来说,你必须打印 \r 字符:

print "Content-Type: text/html\r\n\r\n"

这是表达你想说的内容的合法方式。

一般来说,我会坚持使用 CGI 提供的功能。
它允许更简洁和可读的代码,并且 CGI 比您更了解这些细节。

Strictly speaking, you have to print \r characters:

print "Content-Type: text/html\r\n\r\n"

is a legal way to express what you want to say.

Generally, I'd stick with what CGI offers.
It allows much more concise and readable code, and CGI knows a lot more about such details than you do.

反差帅 2024-08-25 15:56:46

严格来说,您必须打印 CRLF 对。这与“\r\n\r\n”不同,除非您首先使用binmode STDOUT

print "Content-Type: text/html\r\n\r\n";
C:\Temp> t | xxd
0000000: 436f 6e74 656e 742d 5479 7065 3a20 7465  Content-Type: te
0000010: 7874 2f68 746d 6c0d 0d0a 0d0d 0a         xt/html......

您应该使用CGI.pm。或者,如果像我一样,您不想要所有历史包袱,请使用 CGI::Simple

Strictly speaking, you have to print CRLF pairs. That is not the same thing as "\r\n\r\n" unless you use binmode STDOUT first.

print "Content-Type: text/html\r\n\r\n";
C:\Temp> t | xxd
0000000: 436f 6e74 656e 742d 5479 7065 3a20 7465  Content-Type: te
0000010: 7874 2f68 746d 6c0d 0d0a 0d0d 0a         xt/html......

You should use CGI.pm. Or, if, like me, you do not want all the historical baggage, use CGI::Simple.

凉宸 2024-08-25 15:56:46

在 Perl 中,转义码“\r”和“\n”不保证与“\015”和“\012”相同。

如果要打印网络协议的 CRLF,则应使用“\015\012”或“\cM\cJ”。

这是直接来自 perldoc perlop 的引用:

所有系统都使用虚拟的“\n”来表示行终止符,
称为“换行符”。不存在不变的、物理的东西
换行符。操作系统只是一种幻觉,
设备驱动程序、C 库和 Perl 都共同保存。不是
所有系统都将“\r”读取为 ASCII CR,将“\n”读取为 ASCII LF。例如,
在过去的古老 Mac(MacOS X 之前)上,这些曾经是
相反,在没有行终止符的系统上,打印“\n”可能
不发出实际数据。一般来说,当您指的是“换行符”时,请使用“\n”
适合您的系统,但当您需要精确的值时,请使用文字 ASCII
特点。例如,大多数网络协议期望并更喜欢
CR+LF(“\015\012”或“\cM\cJ”)用于行终止符,尽管
他们经常只接受“\012”,很少容忍“\015”。如果
你养成了使用“\n”进行联网的习惯,你可能会被烧伤
有一天。

还有更多类似的信息,来自 perldoc -f binmode:

操作系统、设备驱动程序、C 库和 Perl 运行时
系统都合谋让程序员处理单个字符(\n
) 作为行终止符,无论外部表示如何。在
许多操作系统,本机文本文件表示匹配
内部表示,但在某些平台上外部表示
\n 的表示由多个字符组成。

VMS 上 Unix、Mac OS(旧版和新版)和 Stream_LF 文件的所有变体
使用单个字符来结束外部表示中的每一行
文本(即使该单个字符是旧的回车符,
Mac OS 的前达尔文风格,并且在 Unix 和大多数 VMS 上是 LINE FEED
文件)。在其他系统中,例如 OS/2、DOS 以及各种风格的
MS-Windows,您的程序将 \n 视为简单的 \cJ ,但存储的内容
文本文件中是两个字符 \cM\cJ 。这意味着如果你
不要在这些系统上使用 binmode(),磁盘上的 \cM\cJ 序列将是
输入时转换为 \n,程序中的任何 \n 都将被转换
输出时返回\cM\cJ。这就是您想要的文本文件,但它
对于二进制文件来说可能是灾难性的。

In perl, the escape codes "\r" and "\n" are NOT guaranteed to be identical to "\015" and "\012".

If you want to print a CRLF for a network protocol, you should use "\015\012" or "\cM\cJ".

Here's the quote, direcly from perldoc perlop:

All systems use the virtual "\n" to represent a line terminator,
called a "newline". There is no such thing as an unvarying, physical
newline character. It is only an illusion that the operating system,
device drivers, C libraries, and Perl all conspire to preserve. Not
all systems read "\r" as ASCII CR and "\n" as ASCII LF. For example,
on the ancient Macs (pre-MacOS X) of yesteryear, these used to be
reversed, and on systems without line terminator, printing "\n" might
emit no actual data. In general, use "\n" when you mean a "newline"
for your system, but use the literal ASCII when you need an exact
character. For example, most networking protocols expect and prefer a
CR+LF ("\015\012" or "\cM\cJ" ) for line terminators, and although
they often accept just "\012" , they seldom tolerate just "\015" . If
you get in the habit of using "\n" for networking, you may be burned
some day.

And more, similar info, from perldoc -f binmode:

The operating system, device drivers, C libraries, and Perl run-time
system all conspire to let the programmer treat a single character (\n
) as the line terminator, irrespective of external representation. On
many operating systems, the native text file representation matches
the internal representation, but on some platforms the external
representation of \n is made up of more than one character.

All variants of Unix, Mac OS (old and new), and Stream_LF files on VMS
use a single character to end each line in the external representation
of text (even though that single character is CARRIAGE RETURN on old,
pre-Darwin flavors of Mac OS, and is LINE FEED on Unix and most VMS
files). In other systems like OS/2, DOS, and the various flavors of
MS-Windows, your program sees a \n as a simple \cJ , but what's stored
in text files are the two characters \cM\cJ . That means that if you
don't use binmode() on these systems, \cM\cJ sequences on disk will be
converted to \n on input, and any \n in your program will be converted
back to \cM\cJ on output. This is what you want for text files, but it
can be disastrous for binary files.

み格子的夏天 2024-08-25 15:56:46

不好的是,

print "Content-Type: text/html\r\n\r\n"

您无法添加其他标头,如果您想使用 CGI 添加 Content-TypeCookies,这里是工作示例解决方案

my $cgi = CGI->new;
my $cookie = $cgi->cookie(-name  => 'CookieName', -value => 'CookieValue');

print $cgi->header( -cookie => $cookie,
                    -type    => 'text/html',
                    -charset => 'charset=UTF-8');

the bad using

print "Content-Type: text/html\r\n\r\n"

you can't add another header, here working example solution if you want to add Content-Type and Cookies using CGI

my $cgi = CGI->new;
my $cookie = $cgi->cookie(-name  => 'CookieName', -value => 'CookieValue');

print $cgi->header( -cookie => $cookie,
                    -type    => 'text/html',
                    -charset => 'charset=UTF-8');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文