CGI module & cookies

I

IanW

Hi

I know this is not strictly a Perl question (ie: more to do with the usage
of the CGI module) but couldn't get anything from the ciwac group:

I have a Perl script that facilitates the download of files in a non-web
directory. when the follow the url to teh file they will first be presented
with a login page. On that login page is an option to store the login
details in a cookie. so asssuming they say yes to the cookie option the
following code is activated:

# store login details in cookie after manual login
if($fd{store} eq "yes"){
my $cookie = $query->cookie (
-name => 'FALOGIN',
-value => "$email-$password",
-path => '/',
-expires => '+1y'
);
print $query->header(-cookie => $cookie);
}

That works fine, except straight after that code is executed the script them
attempts to send them the file:

if(open(FH,"$fd{f}"){
(my $filename = $fd{f}) =~ s/^.*\///;
if($filename =~ /\.html*$/i){
print "Content-type:text/html\n\n";
elsif($filename =~ /\.jpg$/i){
print "Content-type:image/jpeg\n\n";
}
else{
print "Content-type: application/octet-stream\n";
print "Content-disposition: attachment;
filename=$filename\n\n";
binmode FH;
}
print while <FH>;
}

So, the script decides what type of file it is and chooses the appropriate
content type to send to the browser.

However, it seems that the "print $query->header(-cookie => $cookie);" line
includes a "print "Content-type:text/html\n\n";
" because when I try to access a test document, I get something like this
in my browser:

"Content-type: application/octet-stream Content-disposition: attachment;
filename=test.doc ÐÏࡱá>þÿ !#þÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿì¥Á%
¿bjbjÞøÞø " ¼'¼'ÿÿÿÿÿÿ]'''''''¦¦¦¦¦ ²
¦U¶ÊÊÊÊÊÊÊÊ$"

Is there a way round this? ie: saving the cookie without printing a
content-type?

Ian
 
B

Bart Van der Donck

IanW said:
I have a Perl script that facilitates the download of files in a non-web
directory. when the follow the url to teh file they will first be presented
with a login page. On that login page is an option to store the login
details in a cookie. so asssuming they say yes to the cookie option the
following code is activated:

# store login details in cookie after manual login
if($fd{store} eq "yes"){
my $cookie = $query->cookie (
-name => 'FALOGIN',
-value => "$email-$password",
-path => '/',
-expires => '+1y'
);
print $query->header(-cookie => $cookie);
}

That works fine, except straight after that code is executed the script them
attempts to send them the file:

if(open(FH,"$fd{f}"){
(my $filename = $fd{f}) =~ s/^.*\///;
if($filename =~ /\.html*$/i){
print "Content-type:text/html\n\n";
elsif($filename =~ /\.jpg$/i){
print "Content-type:image/jpeg\n\n";
}
else{
print "Content-type: application/octet-stream\n";
print "Content-disposition: attachment;
filename=$filename\n\n";
binmode FH;
}
print while <FH>;
}

So, the script decides what type of file it is and chooses the appropriate
content type to send to the browser.

However, it seems that the "print $query->header(-cookie => $cookie);" line
includes a "print "Content-type:text/html\n\n";
" because when I try to access a test document, I get something like this
in my browser:

"Content-type: application/octet-stream Content-disposition: attachment;
filename=test.doc [...]>
Is there a way round this? ie: saving the cookie without printing a
content-type?

Sure. While CGI.pm offers an easy and reliable way to write cookies, it
can be coded out by hand as well. Someting like this:

-----------------------------------------------
print <<"EndOfText";
Set-Cookie: FALOGIN=abc; path=/; expires=Tue, 05-Jun-2007 09:39:13 GMT
Date: Mon, 05 Jun 2006 09:39:13 GMT
Content-type: application/octet-stream
Content-disposition: attachment; filename=$filename

[CONTENT GOES HERE]

EndOfText
 
I

IanW

"Content-type: application/octet-stream Content-disposition: attachment;
filename=test.doc [...]>
Is there a way round this? ie: saving the cookie without printing a
content-type?

Sure. While CGI.pm offers an easy and reliable way to write cookies, it
can be coded out by hand as well. Someting like this:

-----------------------------------------------
print <<"EndOfText";
Set-Cookie: FALOGIN=abc; path=/; expires=Tue, 05-Jun-2007 09:39:13 GMT
Date: Mon, 05 Jun 2006 09:39:13 GMT
Content-type: application/octet-stream
Content-disposition: attachment; filename=$filename

[CONTENT GOES HERE]

EndOfText

Yes it does thanks and just been looking at the CGI::Cookie docs which looks
like it'll do the trick:
http://search.cpan.org/~lds/CGI.pm-3.20/CGI/Cookie.pm

Regards
Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top