CGI : Mojolicious::Lite renders http headers when running with $app->start('cgi')

229 views
Skip to first unread message

Revlin John

unread,
Apr 21, 2013, 4:58:06 PM4/21/13
to mojol...@googlegroups.com
Hi,

Probably not many are running Mojolicious as a CGI script, but I'm testing it out on a LAMP server with Firefox as my client. Here's the script:

#!/usr/bin/perl
use warnings;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Mojolicious::Lite;

my $app = app;
my $version = Mojolicious->VERSION;

get '/' => sub {
 
    $self->render( inline => <<HTML );
<!DOCTYPE html>
<html><head>
  <title>Mojolicious Title App</title>
</head>
<body>
  Mojolicious: $version <br />
</body></html>
HTML

};

$app->secret('p@$$w0rd');
$app->start('cgi');

And here's what I see in my browser:

Content-Type: text/html;charset=UTF-8
X-Powered-By: Mojolicious (Perl)
Status: 200 OK
Date: Sun, 21 Apr 2013 20:50:41 GMT
Content-Length: 137

<!DOCTYPE html>
<html><head>
  <title>Mojolicious Title App</title>
</head>
<body>
  Mojolicious: 2.23 <br />
  Reading: 
</body></html>

So, somehow the http headers are not being treated as headers by the client, but rather plain text which is rendered on screen. How do I fix this?

Thanks,
Rev


Sebastian Riedel

unread,
Apr 21, 2013, 5:04:55 PM4/21/13
to mojol...@googlegroups.com
> Mojolicious: 2.23 <br />

That version of Mojolicious is from 2011.

--
Sebastian Riedel
http://twitter.com/kraih
http://mojolicio.us

Revlin John

unread,
Apr 21, 2013, 5:34:08 PM4/21/13
to mojol...@googlegroups.com, kra...@googlemail.com
Thanks, Sebastian.

I also had a 'say' statement in there which I didn't include in my example get routine. I think that was breaking the response header process when running as CGI.
How can I detect whether the runtime environment is CGI or Morbo?

The following works fine once I comment out the say statements, even with my old-arse release of Mojolicious:

get '/' => sub {
  my $self = shift;

#1) How to get info about the host environment(something like CGI's @ENV)

# Some host environment info is modeled by Mojo::Message::Request
# which I guess is intended to emulate the HTTP request process
  my $req = $self->req;

# $req can now be used to access other objects like header, url,
# and env, this last being a hash which is equivalent to @ENV if
# running Mojolicious within a CGI environment [ with app->start('cgi') ]
  my $URL = $req->url;    # now we should be able to display parts of the URL...
  #say $URL;
  #say $URL->host;
  my $base_url = $URL->base;
  my $mojoENV = ( join "\n<br />", sort keys %{$req->env} );


#2) How to use inline text to "render" HTML

 
    $self->render( inline => <<HTML );
<!DOCTYPE html>
<html><head>
  <title>Mojolicious Title App</title>
</head>
<body>
  Mojolicious: $version <br />
  Reading: $base_url <br />
  ENV Vars: $mojoENV
</body></html>
HTML

};

Revlin John

unread,
May 6, 2013, 2:50:21 PM5/6/13
to mojol...@googlegroups.com
I just added a full post about this issue to http://uni-sol.ca/my-mojo/?p=41

Peace,
Rev

Tekki

unread,
May 6, 2013, 3:16:38 PM5/6/13
to mojol...@googlegroups.com, kra...@googlemail.com
Am Sonntag, 21. April 2013 23:34:08 UTC+2 schrieb Revlin John:
I also had a 'say' statement in there ...

"say" prints to STDOUT, which in CGI mode is the browser. Where do you want to print these informations to?

Revlin John

unread,
May 6, 2013, 4:38:07 PM5/6/13
to mojol...@googlegroups.com
Hi Tekki,

Originally, to the console or STDOUT, when the script was running in morbo. This thread is just my process of realizing that  if I want to take a Mojolicious program that was previously running in morbo and have it run in CGI instead, I have to comment out all the stuff that was previously printing to STDOUT or STDERR.

Thanks,
Rev




--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/grivePrVtMg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
dream, play and have fun.

Sebastian Riedel

unread,
May 6, 2013, 5:04:18 PM5/6/13
to mojol...@googlegroups.com
> I have to comment out all the stuff that was previously printing to STDOUT or STDERR.

This is why there's a logger built-in, i've just updated the tutorial with some more information on how to use it.

http://mojolicio.us/perldoc/Mojolicious/Lite#Mode

Revlin John

unread,
May 6, 2013, 5:40:23 PM5/6/13
to mojol...@googlegroups.com
Awesome! Now I'll have to go back through my scripts and start using this. Great update to the guides.

Rev


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/grivePrVtMg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Charlie Brady

unread,
May 8, 2013, 1:36:37 PM5/8/13
to mojol...@googlegroups.com

On Mon, 6 May 2013, Revlin John wrote:

> Hi Tekki,
>
> Originally, to the console or STDOUT, when the script was running in morbo.
> This thread is just my process of realizing that if I want to take a
> Mojolicious program that was previously running in morbo and have it run in
> CGI instead, I have to comment out all the stuff that was previously
> printing to STDOUT or STDERR.

No, not STDERR. Anything printed to STDERR will be saved in the web
server's error log - or at least, it will with apache.

Revlin John

unread,
May 8, 2013, 1:57:31 PM5/8/13
to mojol...@googlegroups.com
Thanks for the tip, Charlie, but have you tried running a Mojolicious CGI script that prints to STDERR?
Just want to make sure that's tested before putting it out there.

Peace,
Rev


Reply all
Reply to author
Forward
0 new messages