- URL: https://www.laruence.com/en/2010/02/23/1310.html
- Please include attribution when republishing.
Apache2 introduced the Hook mechanism, and correspondingly PHP provides the apache2handler SAPI.
Unlike the PHP lifecycle I described before, in this case PHP registers a handler hook, so it gets a chance to handle the request during the handler hook stage. By examining the request's handler, it determines whether it needs to handle it, and if so it calls its own handler.
In this case, then, there are several ways to configure it. We mainly consider the following two (the second can have several variants):
The first: AddType application/x-httpd-php .php
The second:
<filesMatch .php$>
SetHandler application/x-httpd-php
</filesMatch>
First, these two approaches take effect at different moments. For the first approach, it takes effect at the type_check hook stage — that is, in apache2src/modules/http/mod_mime.c, by registering the type_checker hook and adding find_ct(content_type). Inside find_ct, based on the mime mappings in the configuration file, or the mappings added via the addType directive, the request's handler field is filled in according to the file's extension:
For the second approach, it takes effect at the fixup hook stage — by registering the fixups hook and adding the core_override_type(apache2src/server/core.c) function — to make the directory-level configuration directives take effect.
The fixups hook comes later than the type_checker hook, and is also the last usable hook before the handler hook. So if you use both approaches 1 and 2 at the same time, the second approach overrides the handler set by the first.
Second, the two approaches rely on different data structures. The first relies on a global mime mapping table, extension_mappings, which comes from the mime configuration file and the AddType directive.
The second approach relies on the dir_config built from the configuration file:
....
core_dir_config *conf =
(core_dir_config *)ap_get_module_config(r->per_dir_config,
&core_module);
/* Check for overrides with ForceType / SetHandler
*/
if (conf->mime_type && strcmp(conf->mime_type, "none"))
ap_set_content_type(r, (char*) conf->mime_type);
if (conf->handler && strcmp(conf->handler, "none"))
r->handler = conf->handler;
.....
Be First to Comment