PHP 输出控制
以apache2 的mod_php5模块为例,通过php_ap2_register_hook()函数来注册钩子,pre_config,post_config,child_init是启动挂钩,它们在服务器启动时调用,handler 是请求钩子,挂钩到apache 的一次请求。很自然的,要在apache启动和请求的时候,分别完成不同的工作。
void php_ap2_register_hook(apr_pool_t *p) { ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE); }
在php_apache_server_startup中
static int php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) { void *data = NULL; const char *userdata_key = "apache2hook_post_config"; /* Apache will load, unload and then reload a DSO module. This * prevents us from starting PHP until the second load. */ apr_pool_userdata_get(&data, userdata_key, s->process->pool); if (data == NULL) { /* We must use set() here and *not* setn(), otherwise the * static string pointed to by userdata_key will be mapped * to a different location when the DSO is reloaded and the * pointers won't match, causing get() to return NULL when * we expected it to return non-NULL. */ apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); return OK; } /* Set up our overridden path. */ if (apache2_php_ini_path_override) { apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override; } #ifdef ZTS tsrm_startup(1, 1, 0, NULL); #endif sapi_startup(&apache2_sapi_module);//完成sapi的启动,初始化全局变量等 apache2_sapi_module.startup(&apache2_sapi_module);//通过调用_sapi_module_struct的startup函数,注册apache sapi module 一系列函数 apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null); php_apache_add_version(pconf); return OK; }
随后又会调用php_module_startup来启动作为apache一个模块的PHP,初始化一些PHP的状态,函数等等。这篇文章要用到的代码为:
int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules) { zend_utility_functions zuf; 。。。//省略部分代码 php_output_startup();//初始化PHP的输出函数 zuf.error_function = php_error_cb; zuf.printf_function = php_printf; zuf.write_function = php_body_write_wrapper;//PHP的默认输出函数 zuf.fopen_function = php_fopen_wrapper_for_zend; zuf.message_handler = php_message_handler_for_zend; zuf.block_interruptions = sapi_module.block_interruptions; zuf.unblock_interruptions = sapi_module.unblock_interruptions; zuf.get_configuration_directive = php_get_configuration_directive_for_zend; zuf.ticks_function = php_run_ticks; zuf.on_timeout = php_on_timeout; zuf.stream_open_function = php_stream_open_for_zend; zuf.vspprintf_function = vspprintf; zuf.getenv_function = sapi_getenv; zuf.resolve_path_function = php_resolve_path_for_zend; zend_startup(&zuf, NULL TSRMLS_CC); 。。。//省略部分代码 }
PHPAPI void php_output_startup(void)->static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC)
此时PHP的默认一系列输出函数为:
static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC) { OG(php_body_write) = php_default_output_func; OG(php_header_write) = php_default_output_func; OG(implicit_flush) = 0; OG(output_start_filename) = NULL; OG(output_start_lineno) = 0; }
/* {{{ php_default_output_func */ PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC) { fwrite(str, 1, str_len, stderr);//默认输出到标准错误 /* See http://support.microsoft.com/kb/190351 */ #ifdef PHP_WIN32 fflush(stderr); #endif return str_len; }
上述情况是发生在apache启动的时候,初始化PHP的过程,在每一次请求过程中,显然输出函数不对。跟踪函数php_handler->php_apache_request_ctor->php_request_startup-> php_output_activate,在这个函数中:
PHPAPI void php_output_activate(TSRMLS_D) { OG(php_body_write) = php_ub_body_write;//输出函数 OG(php_header_write) = sapi_module.ub_write; OG(ob_nesting_level) = 0; OG(ob_lock) = 0; OG(disable_output) = 0; OG(output_start_filename) = NULL; OG(output_start_lineno) = 0; } PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC) { int result = 0; if (SG(request_info).headers_only) { if(SG(headers_sent)) { return 0; } php_header(TSRMLS_C); zend_bailout(); } if (php_header(TSRMLS_C)) { if (zend_is_compiling(TSRMLS_C)) { OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C); OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C); } else if (zend_is_executing(TSRMLS_C)) { OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C); OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C); } OG(php_body_write) = php_ub_body_write_no_header;//输出函数重新赋值 result = php_ub_body_write_no_header(str, str_length TSRMLS_CC); } return result; } PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC) { int result; if (OG(disable_output)) { return 0; } result = OG(php_header_write)(str, str_length TSRMLS_CC);//实际上调用的是OG(php_header_write)也就是sapi_module.ub_write为apache的函数 if (OG(implicit_flush)) { sapi_flush(TSRMLS_C); } return result; }
这样一来结果就了然了,在PHP启动的时候,输出函数指向的fd是stderr,在每一次请求的时候,再将函数指针指向apache的输出函数。
在zend_startup的时候会有:
ZEND_API zend_write_func_t zend_write; zend_write = (zend_write_func_t) utility_functions->write_function;
在实际使用的时候,以echo为例,最终会调用:
ZEND_API int zend_print_zval(zval *expr, int indent) /* {{{ */ { return zend_print_zval_ex(zend_write, expr, indent);//zend_write为实际调用的指针函数 }
想了一下PHP在请求的时候,可以动态的指定输出函数,这样更加方便SAPI层的接口编写,这个输出函数的控制权力在server端才合理,但是PHP在自身上面包装了很多层,不知道是不是命名和年代久远的关系。