Mutant has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Currently having an intermittent problem with Apache, mod_perl and a content type handler. Might be fairly unlikely that anyone here can help, but thought I'd throw it out and see if anyone has seen anything similar.

Basically, I'm working on a high traffic site, which is running some mod_perl code. During the translation phase, it decides if it knows the content type of the file it's serving (which 99% of the time it does), and if so uses push_handlers() to add a content type handler. The content type handler simply sets the content type of the response correctly.

This is all well and good, but there are times (a very small percentage of traffic) when the content type for html files is set to text/plain, even though the app knows it should be text/html. As far as I can tell, there's no reason for the mod_perl content type handler to return text/plain.

To make matters slightly more interesting, Apache is actually configured with a DefaultType of text/html, which appears to be getting ignored (I've hacked the system so that the content type handler returns nothing, and I receive the text/plain header, not the text/html default I would expect).

So the problem seems to be (in the intermittent problem cases):
  1. Apache (or mod_perl?) is not reading the return value of the content type handler, and
  2. The DefaultType value is being ignored
Again, just to stress, the vast majority of requests are fine. There is no pattern in terms of specific pages on the site, specific servers, etc.

Admittedly, I can't be 100% sure the content type handler *is* actually returning something (and that it is returning text/html), but some of the other details of the system make me more sure than may be apparent in this post :)

So has anyone seen anything similar to this?

(BTW, Apache 1.3, mod_perl 1.3, perl 5.8.x)

Replies are listed 'Best First'.
Re: Apache, mod_perl and content type handlers
by Krambambuli (Curate) on Apr 26, 2007 at 16:10 UTC
    --(I've hacked the system so that the content type handler returns nothing, and I receive the text/plain header, not the text/html default I would expect).

    That's the track I'd try to follow - looks like a very promising way to come closer to the problem, actually already reproducing it...?

    No, I haven't seen this sort of behavior yet, but I'm interested and will be happy if you would update this node with your findings if you succeed to track it down.

    Did you maybe try to post the question also to the modperl@perl.apache.org mailing list ?
    Might be another good place to reach the people that might help.

    TiA
      That's the track I'd try to follow - looks like a very promising way to come closer to the problem, actually already reproducing it...?
      Well, that's *similar* to reproducing it, but I'm fairly sure the content type handler *is* returning something in these cases. So something else is clearing the content type, or Apache or mod_perl is not able to read it from the response object for some reason.

      Will certainly post anything I find, altho it may not be for sometime...
Re: Apache, mod_perl and content type handlers
by varian (Chaplain) on Apr 26, 2007 at 19:29 UTC
    If your content type handler runs as a PerlTypeHandler then you run the risk that the content-type is overruled by some later (Apache or your own) odd mechanism. The ModPerl documentation specifically warns against such:
    Of course later phases may override the mime type set in this phase
    Alternatively maybe you can run your handler as a PerlFixupHandler? That's likely to put you in better 'type' control.
      Good point, altho there's certainly nothing in my code that sets the content type. I wouldn't have thought Apache (or mod_perl) would do it anywhere else, and it certainly doesn't in most cases.
        An example is configuration directives that are set in .htaccess and that define typehandlers for a directory. Other handlers would be mod-autoindex etc.
Re: Apache, mod_perl and content type handlers
by derby (Abbot) on Apr 26, 2007 at 16:02 UTC

    Hmmm don't know but the code and the apache conf would be more helpful than just a description of the problem.

    -derby

    update: and is it just one web server or a farm of web servers? I've seen situations like this with web server farms where one of the machines is not configured correctly.

      As I mentioned above (altho perhaps not clearly), it's multiple servers, and it's occuring across all servers.

      The code in the content type handler is fairly simple, and given that it works in the vast majority of cases, I'm not so suspicious of it:
      sub type_handler { my $r = shift; my $type = $r->notes('CONTENT_TYPE'); if ($type) { $r->content_type($type); return OK; } else { return DECLINED: } }
      What sets the content type in the notes? That's done in the translation handler, read from a file for each piece of content it serves. I can be fairly sure that this is being done correctly for the cases that content type is being set to text/plain. (I haven't ruled it out, but if that *is* the problem, then it's not likely I can get much help from PM without going into a *lot* more detail, which I'm probably not prepared to do at this stage).