|
SAPI:在各个服务器抽象层之间遵守着相同的约定,这里我们称之为SAPI接口。例如命令行程序的实现,Apache的mod_php模块实现以及fastcgi的实现等等
1.结构体:使用结构体(Struct)来存放一组不同类型的数据struct 结构体名{ 结构体所包含的变量或数组};struct 结构体名 结构体变量名
2.成员的获取和赋值结构体变量名.成员名;
3.结构体指针struct 结构体名 *结构体变量名=&结构体变量名使用结构体指针 ==> (*结构体变量名).成员名 ; 结构体变量名->成员名
PHP源码:
*name;
*pretty_name;
<span style="color: #0000ff">int</span> (*startup)(<span style="color: #0000ff">struct</span> _sapi_module_struct *sapi_module); <span style="color: #008000">//</span><span style="color: #008000"> 启动函数</span>
<span style="color: #0000ff">int</span> (*shutdown)(<span style="color: #0000ff">struct</span> _sapi_module_struct *sapi_module); <span style="color: #008000">//</span><span style="color: #008000"> 关闭方法</span>
<span style="color: #0000ff">int</span> (*activate)(TSRMLS_D); <span style="color: #008000">//</span><span style="color: #008000"> 激活</span>
<span style="color: #0000ff">int</span> (*deactivate)(TSRMLS_D); <span style="color: #008000">//</span><span style="color: #008000"> 停用</span>
<span style="color: #0000ff">int</span> (*ub_write)(<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *str,unsigned <span style="color: #0000ff">int</span><span style="color: #000000"> str_length TSRMLS_DC);
</span><span style="color: #008000">//</span><span style="color: #008000"> 不缓存的写操作(unbuffered write)</span>
<span style="color: #0000ff">void</span> (*flush)(<span style="color: #0000ff">void</span> *server_context); <span style="color: #008000">//</span><span style="color: #008000"> flush</span>
<span style="color: #0000ff">struct</span> stat *(*get_stat)(TSRMLS_D); <span style="color: #008000">//</span><span style="color: #008000"> get uid</span>
<span style="color: #0000ff">char</span> *(*getenv)(<span style="color: #0000ff">char</span> *name,size_t name_len TSRMLS_DC); <span style="color: #008000">//</span><span style="color: #008000"> getenv</span>
<span style="color: #0000ff">void</span> (*sapi_error)(<span style="color: #0000ff">int</span> type,<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *error_msg,...); <span style="color: #008000">/*</span><span style="color: #008000"> error handler </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">int</span> (*header_handler)(sapi_header_struct *<span style="color: #000000">sapi_header,sapi_header_op_enum op,sapi_headers_struct </span>*sapi_headers TSRMLS_DC); <span style="color: #008000">/*</span><span style="color: #008000"> header handler </span><span style="color: #008000">*/</span>
<span style="color: #008000">/*</span><span style="color: #008000"> send headers handler </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">int</span> (*send_headers)(sapi_headers_struct *<span style="color: #000000">sapi_headers TSRMLS_DC);
</span><span style="color: #0000ff">void</span> (*send_header)(sapi_header_struct *<span style="color: #000000">sapi_header,</span><span style="color: #0000ff">void</span> *server_context TSRMLS_DC); <span style="color: #008000">/*</span><span style="color: #008000"> send header handler </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">int</span> (*read_post)(<span style="color: #0000ff">char</span> *buffer,<span style="color: #0000ff">uint</span> count_bytes TSRMLS_DC); <span style="color: #008000">/*</span><span style="color: #008000"> read POST data </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">char</span> *(*read_cookies)(TSRMLS_D); <span style="color: #008000">/*</span><span style="color: #008000"> read Cookies </span><span style="color: #008000">*/</span>
<span style="color: #008000">/*</span><span style="color: #008000"> register server variables </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">void</span> (*register_server_variables)(zval *<span style="color: #000000">track_vars_array TSRMLS_DC);
</span><span style="color: #0000ff">void</span> (*log_message)(<span style="color: #0000ff">char</span> *message); <span style="color: #008000">/*</span><span style="color: #008000"> Log message </span><span style="color: #008000">*/</span><span style="color: #000000">
time_t (</span>*get_request_time)(TSRMLS_D); <span style="color: #008000">/*</span><span style="color: #008000"> Request Time </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">void</span> (*terminate_process)(TSRMLS_D); <span style="color: #008000">/*</span><span style="color: #008000"> Child Terminate </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">char</span> *php_ini_path_override; <span style="color: #008000">//</span><span style="color: #008000"> 覆盖的ini路径</span>
<span style="color: #000000">
...
...
};
cgi_sapi_module.startup(&cgi_sapi_module) <span style="color: #008000">//<span style="color: #008000"> cgi模式 cgi/cgi_main.c文件
<span style="color: #000000">
apache2_sapi_module.startup(&<span style="color: #000000">apache2_sapi_module);
<span style="color: #008000">//<span style="color: #008000"> apache2服务器 apache2handler/sapi_apache2.c文件
<span style="color: #0000ff">static sapi_module_struct apache2_sapi_module =<span style="color: #000000"> {
<span style="color: #800000">"<span style="color: #800000">apache2handler<span style="color: #800000">"<span style="color: #000000">,<span style="color: #800000">"<span style="color: #800000">Apache 2.0 Handler<span style="color: #800000">"<span style="color: #000000">,php_apache2_startup,<span style="color: #008000">/<span style="color: #008000"> startup <span style="color: #008000">/<span style="color: #000000">
php_module_shutdown_wrapper,<span style="color: #008000">/<span style="color: #008000"> shutdown <span style="color: #008000">/<span style="color: #000000">
...
}
startup 当SAPI初始化时,首先会调用该函数。如果服务器处理多个请求时,该函数只会调用一次。 比如Apache的SAPI,它是以mod_php5的Apache模块的形式加载到Apache中的, 在这个SAPI中,startup函数只在父进程中创建一次,在其fork的子进程中不会调用。activate 此函数会在每个请求开始时调用,它会再次初始化每个请求前的数据结构。deactivate 此函数会在每个请求结束时调用,它用来确保所有的数据都,以及释放在activate中初始化的数据结构。shutdown 关闭函数,它用来释放所有的SAPI的数据结构、内存等。ub_write 不缓存的写操作(unbuffered write),它是用来将PHP的数据输出给客户端, 如在CLI模式下,其最终是调用fwrite实现向标准输出输出内容;在Apache模块中,它最终是调用Apache提供的方法rwrite。sapi_error 报告错误用,大多数的SAPI都是使用的PHP的默认实现php_error。flush 刷新输出,在CLI模式下通过使用C语言的库函数fflush实现,在php_mode5模式下,使用Apache的提供的函数函数rflush实现。read_cookie 在SAPI激活时,程序会调用此函数,并且将此函数获取的值赋值给SG(request_info).cookie_data。 在CLI模式下,此函数会返回NULL。read_post 此函数和read_cookie一样也是在SAPI激活时调用,它与请求的方法相关,当请求的方法是POST时,程序会操作$_POST、$HTTP_RAW_POST_DATA等变量。send_header 发送头部信息,此方法一般的SAPI都会定制,其所不同的是,有些的会调服务器自带的(如Apache),有些的需要你自己实现(如 FastCGI)。
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|