加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

[PHP] PHP服务器接口SAPI中的结构体

发布时间:2020-05-25 03:13:36 所属栏目:PHP 来源:互联网
导读:SAPI:在各个服务器抽象层之间遵守着相同的约定,这里我们称之为SAPI接口。例如命令行程序的实现,Apache的mod_php模块实现以及fastcgi的实现等等1.结构体:使用结构体(Struct)来存放一组不同类型的数据struct 结构体名{结构体所包含的变量或数组};struct 结

SAPI:在各个服务器抽象层之间遵守着相同的约定,这里我们称之为SAPI接口。例如命令行程序的实现,Apache的mod_php模块实现以及fastcgi的实现等等

1.结构体:使用结构体(Struct)来存放一组不同类型的数据struct 结构体名{ 结构体所包含的变量或数组};struct 结构体名 结构体变量名

2.成员的获取和赋值结构体变量名.成员名;

3.结构体指针struct 结构体名 *结构体变量名=&结构体变量名使用结构体指针 ==> (*结构体变量名).成员名 ; 结构体变量名->成员名

PHP源码:

*name; *pretty_name;
<span style="color: #0000ff"&gt;int</span> (*startup)(<span style="color: #0000ff"&gt;struct</span> _sapi_module_struct *sapi_module);    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  启动函数</span>
<span style="color: #0000ff"&gt;int</span> (*shutdown)(<span style="color: #0000ff"&gt;struct</span> _sapi_module_struct *sapi_module);   <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  关闭方法</span>

<span style="color: #0000ff"&gt;int</span> (*activate)(TSRMLS_D);  <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 激活</span>
<span style="color: #0000ff"&gt;int</span> (*deactivate)(TSRMLS_D);    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  停用</span>

<span style="color: #0000ff"&gt;int</span> (*ub_write)(<span style="color: #0000ff"&gt;const</span> <span style="color: #0000ff"&gt;char</span> *str,unsigned <span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; str_length TSRMLS_DC);
 </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  不缓存的写操作(unbuffered write)</span>
<span style="color: #0000ff"&gt;void</span> (*flush)(<span style="color: #0000ff"&gt;void</span> *server_context);    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  flush</span>
<span style="color: #0000ff"&gt;struct</span> stat *(*get_stat)(TSRMLS_D);     <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  get uid</span>
<span style="color: #0000ff"&gt;char</span> *(*getenv)(<span style="color: #0000ff"&gt;char</span> *name,size_t name_len TSRMLS_DC); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  getenv</span>

<span style="color: #0000ff"&gt;void</span> (*sapi_error)(<span style="color: #0000ff"&gt;int</span> type,<span style="color: #0000ff"&gt;const</span> <span style="color: #0000ff"&gt;char</span> *error_msg,...);   <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; error handler </span><span style="color: #008000"&gt;*/</span>

<span style="color: #0000ff"&gt;int</span> (*header_handler)(sapi_header_struct *<span style="color: #000000"&gt;sapi_header,sapi_header_op_enum op,sapi_headers_struct </span>*sapi_headers TSRMLS_DC);   <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; header handler </span><span style="color: #008000"&gt;*/</span>

 <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; send headers handler </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;int</span> (*send_headers)(sapi_headers_struct *<span style="color: #000000"&gt;sapi_headers TSRMLS_DC);

</span><span style="color: #0000ff"&gt;void</span> (*send_header)(sapi_header_struct *<span style="color: #000000"&gt;sapi_header,</span><span style="color: #0000ff"&gt;void</span> *server_context TSRMLS_DC);   <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; send header handler </span><span style="color: #008000"&gt;*/</span>

<span style="color: #0000ff"&gt;int</span> (*read_post)(<span style="color: #0000ff"&gt;char</span> *buffer,<span style="color: #0000ff"&gt;uint</span> count_bytes TSRMLS_DC); <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; read POST data </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;char</span> *(*read_cookies)(TSRMLS_D);    <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; read Cookies </span><span style="color: #008000"&gt;*/</span>

<span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; register server variables </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;void</span> (*register_server_variables)(zval *<span style="color: #000000"&gt;track_vars_array TSRMLS_DC);

</span><span style="color: #0000ff"&gt;void</span> (*log_message)(<span style="color: #0000ff"&gt;char</span> *message);     <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; Log message </span><span style="color: #008000"&gt;*/</span><span style="color: #000000"&gt;
time_t (</span>*get_request_time)(TSRMLS_D);   <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; Request Time </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;void</span> (*terminate_process)(TSRMLS_D);    <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt; Child Terminate </span><span style="color: #008000"&gt;*/</span>

<span style="color: #0000ff"&gt;char</span> *php_ini_path_override;    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;  覆盖的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)。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读