Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CGI::Application header problem

by morgon (Deacon)
on Dec 27, 2010 at 18:41 UTC ( #879315=perlquestion: print w/ replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using CGI::Application together with CGI::Application::Plugin::Authentication.

As "Store" I use "Session", which basically means that CGI::Application will set a cookie on the client after an successful login.

All of this works fine so far.

But now I want to add an HTTP-header to tell the client that the response is UTF-8 encoded.

I have tried to set it in a cgi_prerun-method like this:

sub cgiapp_prerun { my($this)=@_; $this->header_add( -type => "text/html; charset=UTF-8"); }
This for itself also works but unfortunately (I have traced it), for some reason I don't understand it breaks the cookie-setting (no cookie is sent to the client) and so the whole authentication-system.

I have also tried to set the header with header_props but the result is the same.

Can someone help me here?

Many thanks!

Comment on CGI::Application header problem
Select or Download Code
Re: CGI::Application header problem
by Corion (Pope) on Dec 27, 2010 at 18:49 UTC

    Look at what headers actually get sent, via Mozilla Live HTTP Headers for example, and via telnet your.host 80, and by running your script from the command line.

    Maybe you get/got a cached page, and something else broke your application?

    If you have automated tests, these will spot if something like this happens as they do not use caching.

      I have traced the response/request with firebug and it is reproducible that when commenting out the one line that adds the header the authentication works and the cookie is set, whereas when I set the header no cookie is set and hence the client cannot log in.

        So, does the cookie header get sent in the reply or not?

        Do not trust the browser to show you this.

Re: CGI::Application header problem
by jaldhar (Vicar) on Dec 27, 2010 at 23:06 UTC

    You need to separate the headers with commas like this:

    $this->header_add( -type => 'text/html', -charset => 'UTF-8');

    --
    જલધર

      Shouldn't make any difference here (as charset is part of the Content-Type header):

      $ perl -MCGI -E "say CGI->new()->header(-type => 'text/html', -charset + =>'UTF-8')" Content-Type: text/html; charset=UTF-8

      vs. what the OP had:

      $ perl -MCGI -E "say CGI->new()->header(-type => 'text/html; charset=U +TF-8')" Content-Type: text/html; charset=UTF-8

      (CGI::App's header_add is eventually passed to CGI.pm's header method)

Re: CGI::Application header problem
by trwww (Priest) on Dec 28, 2010 at 01:35 UTC

    This works fine for me:

    $ cat cgiapp.pl #!/usr/local/bin/perl use warnings; use strict; TestApp->new->run; package TestApp; use base qw|CGI::Application|; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::Session; sub default : StartRunmode { my $self = shift; my $output; # get current access count my $access_count = $self->session->param('access_count') || 0; # increment access count $access_count++; # store new access count in session $self->session->param(access_count => $access_count); # get CGI object out of application my $q = $self->query; #build output $output = $q->start_html( -head => $q->meta({ -http_equiv => 'Content-Type', -content => 'text/html; charset=UTF-8' }), -title => 'access count application' ); $output .= $q->div("accessed $access_count time(s) this session"); $output .= $q->div(' '); $output .= $q->div( $q->a( {-href => $q->self_url} => 'access again' + ) ); $output .= $q->end_html; # set charset $self->header_add( -type => "text/html; charset=UTF-8"); # give output to app to send to user return( $output ); } $ perl cgiapp.pl Set-Cookie: CGISESSID=357a2d9c8f6ec5830c08beb44d12afb2; path=/ Date: Tue, 28 Dec 2010 01:31:47 GMT Content-Type: text/html; charset=UTF-8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>access count application</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <div>accessed 1 time(s) this session</div><div> </div><div><a href="ht +tp://localhost">access again</a></div> </body> </html>

    I'm sure if you run the code on the server, perhaps in the debugger, the cause of the problem will quickly jump out at you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://879315]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-12-27 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls