Show Contents Previous Page Next Page
Chapter 4 - Content Handlers / Redirection
Apache::RandPicture optimizations that we showed in the previous section involve a lot of typing, and the resulting code is a bit obscure. A far more elegant solution is to let Apache do all the work for you with its internal redirection mechanism. In this scheme, Apache handles the entire redirection internally. It pretends that the web browser made the request for the new URI and sends the contents of the file, without letting the browser in on the secret. It is functionally equivalent to the solution that we showed at the end of the preceding section.
To invoke the Apache internal redirection system, modify the last two lines of Apache::RandPicture's handler() subroutine to read like this:
The request object's internal_redirect() method takes a single argument consisting of an absolute local URI (one starting with a
/). The method does all the work of translating the URI, invoking its content handler, and returning the file contents, if any. Unfortunately internal_redirect() returns no result code, so there's no way of knowing whether the redirect was successful (you can't do this from a conventional redirect either). However, the call will return in any case, allowing you to do whatever cleanup is needed. You should exit the handler with a result code of
In informal benchmarks, replacing the basic Apache::RandPicture with a version that uses internal redirection increased the throughput by a factor of two, exactly what we'd expect from halving the number of trips through the network. In contrast, replacing all the MIME type lookups with a simpler direct grep for image file extensions had negligible effect on the speed of the module. Apache's subrequest mechanism is very efficient.
If you have very many images in the random pictures directory (more than a few hundred), iterating through the directory listing each time you need to fetch an image will result in a noticeable performance hit. In this case, you'll want to cache the directory listing in a package variable the first time you generate it and only rebuild the listing when the directory's modification time changes (or just wait for a server restart, if the directory doesn't change often). You could adapt the Apache::ESSI caching system for this purpose.
Internal redirection is a win for most cases when you want to redirect the
browser to a different URI on your own site. Be careful not to use it for external
URIs, however. For these, you must either use standard redirection or invoke
Apache's proxy API (Chapter 7).
When you use internal redirection to pass control from one module to another,
the second module in the chain can retrieve the original query string, the document
URI, and other information about the original request by calling the request
object's prev() method or, in Apache::Registry scripts only,
by examining certain environment variables. There is also a way, using Apache::err_header_out()
for the original module to set various HTTP header fields, such as cookies,
that will be transferred to the second across the internal redirect. Because
internal redirects are most commonly used in error handlers, these techniques
are discussed in the section "Handling Errors" later
in this chapter. Show Contents Previous Page Next Page
Copyright © 1999 by O'Reilly & Associates, Inc.