David Arnold wrote:
> All,
> 
> I am working on Recipe 3.4. I have the following saved as cgi-perl/try.pl:
> 
> #! /usr/bin/perl -w
> # file: try.pl
> 
> use strict;
> use Apache::Const qw(:common);
> 
> my $r=shift;
> 
> # Grab all of the headers at once
> my %headers_in=$r->headers_in;
> 
> # or get a specific header and do something with it
> my $gzip=$r->headers_in->get('Accept-Encoding')=~m/gzip/;
> 
> $r->send_http_header('text/plain');

I guess you are using mod_perl 2.0 with Apache::compat someplace?
$r->send_http_header is not a part of the 2.0 API, so I would think you are.
 not that it matters much at this point...

> 
> print "The host in your URL is: ", $headers_in{'Host'}, "\n";
> print "Your browser is: ", $headers_in{'User-Agent'}, "\n";
> print "Your browser accepts gzip encoded data\n" if $gzip;
> 
> return OK;
> 
> When I access it (using Lynx) via localhost/cgi-perl/try.pl, the response I
> get is:
> 
> The host in your URL is:
> Your browser is:
> Your browser accepts gzip encoded data
> 
> Can someone explain why the Host and User-Agent are missing?

I don't know what lynx sends for these fields - User-Agent isn't required,
and Host is not required for HTTP/1.0 (though it is sent the vast majority
of the time).

the real test is to use telnet:

$ telnet localhost 80
GET /foo.html HTTP/1.1
Host: www.foo.com

where www.foo.com matches the ServerName directive if you are using virtual
hosts.

> 
> Secondly, if I save it as cgi-perl/try.pl like this:
> 
> #! /usr/bin/perl -w
> # file: try.pl

you are missing the package declaration here - see below.

> 
> use strict;
> use Apache::Const qw(:common);
> 
> sub handler {
> 
>     my $r=shift;
> 
>     # Grab all of the headers at once
>     my %headers_in=$r->headers_in;
> 
>     # or get a specific header and do something with it
>     my $gzip=$r->headers_in->get('Accept-Encoding')=~m/gzip/;
> 
>     $r->send_http_header('text/plain');
> 
>     print "The host in your URL is: ", $headers_in{'Host'}, "\n";
>     print "Your browser is: ", $headers_in{'User-Agent'}, "\n";
>     print "Your browser accepts gzip encoded data\n" if $gzip;
> 
>     return OK;
> 
> }
> 
> Then when I access it in lynx, I get a blank screen. Also, I am used to
> doing something in httpd.conf when using these handlers. What do I have to
> do in this second case to get a result?

in general, you need to set up a <Location> like this

<Location /foo>
  SetHandler perl-script
  PerlResponseHandler My::Handler
</Location>

where the file My/Handler.pm contains the above handler (with the missing
package declaration).  for better or worse, the cookbook does not give a
configuration for every example - setting up a basic <Location> directive is
really an Apache thing and amply covered by other books and documentation.
within the cookbook itself you will want to check out recipe 7.1 for some
handler basics to get you started.

HTH

--Geoff

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html

Reply via email to