Should use the CGI.pm header method to display the Content-Type header?

If I use the CGI module in Perl, I can do it

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

Or for the classic

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

Does what we use matter? Is there a difference between the two? Both seem to work.

For me, I would go first if I used CGI in any case, but if not, then I would not bother loading the module in just one action. But just wondering if there is anything on this?

Psi

+3
source share
4 answers

Strictly speaking, you need to type the characters \ r:

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

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

, , CGI. , CGI , .

+2

, 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.

+2

perl escape- "\ r" "\n" "\ 015" "\ 012".

CRLF , "\ 015\012" "\ cM\cJ".

, perldoc perlop:

"\n" , " ". , , . , , , C Perl, . "\ r" ASCII CR "\n" ASCII LF. , (pre-MacOS X) , , , "\n" . , "\n" , " ", , ASCII, . , CR + LF ( "\ 015\012" "\ cM\cJ" ) , "\ 012", "\ 015". "\n" , -.

, , perldoc -f binmode:

, , C Perl , (\n ) , . , , \n .

Unix, Mac OS ( ) Stream_LF VMS ( - CARRIAGE RETURN , - Mac OS LINE FEED Unix VMS ). , OS/2, DOS MS-Windows, \n \cJ, , \cM\cJ. , binmode() , \cM\cJ \n , \n \cM\cJ . , , .

0
source

poor use

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

you cannot add another header, here is a working example of a 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');
0
source

Source: https://habr.com/ru/post/1729214/


All Articles