xyon wrote:
Hello everyone,

I am writing my first mod_perl handler. I've looked at some of the docs
online and have come up with the config/code below. However, when I go
to visit the URL in Apache, I get a download prompt for type:
httpd/unix-directory.

[snip]
try this :

sub handler {
    my $r        = shift;  # get the Apache Request object

    $r->content_type('text/html'); # set some Response header
    $r->print("<h1>It works !</h1>); # send some content

    return Apache2::Const::OK; # make Apache happy
}

In your original version, what happens is :

sub handler {
    my $self        = shift;  # you get the Request object here
    # you return no content at all
    return Apache2::Const::OK; # tell Apache it's OK
}

I guess that with no content returned (not even HTTP headers), Apache is left to wonder what to return, and returns something to the browser with a funny content type.

On the other hand, if in your http config you had :

        PerlResponseHandler Myserver::Handler->handler

then your handler sub should be :

sub handler {
    my $self = shift; # get Myserver::Handler class
    my $r        = shift;  # get the Apache Request object

    $r->content_type('text/html'); # set the Response headers
    $r->print("<h1>It works !</h1>); # send some content

    return Apache2::Const::OK; # make Apache happy
}

That is because with the Package->sub syntax in the http config, mod_perl sets up the call differently, and calls you sub() as a method.

One last tip :
If, instead of returning Apache2::const::OK in your handler, you send nothing back but return Apache2::const::DECLINED, then Apache will revert to its own default handler, and send back what it would normally send (probably a directory listing in this case).

Hope this helps.


André

Reply via email to