談?wù)勀銓?duì)Zend SAPIs(Zend SAPI Internals)的理解
SAPI: Server abstraction API,研究過(guò)PHP架構(gòu)的同學(xué)應(yīng)該知道這個(gè)東東的重要性,它提供了一個(gè)接口,使得PHP可以和其他應(yīng)用進(jìn)行交互數(shù)據(jù)。 本文不會(huì)詳細(xì)介紹每個(gè)PHP的SAPI,只是針對(duì)最簡(jiǎn)單的CGI SAPI,來(lái)說(shuō)明SAPI的機(jī)制。
首先,我們來(lái)看看PHP的架構(gòu)圖:

圖1 PHP Architecture
SAPI提供了一個(gè)和外部通信的接口, 對(duì)于PHP5.2,默認(rèn)提供了很多種SAPI, 常見(jiàn)的給apache的mod_php5,CGI,給IIS的ISAPI,還有Shell的CLI,本文就從CGI SAPI入手 ,介紹SAPI的機(jī)制。 雖然CGI簡(jiǎn)單,但是不用擔(dān)心,它包含了絕大部分內(nèi)容,足以讓你深刻理解SAPI的工作原理。
要定義個(gè)SAPI,首先要定義個(gè)sapi_module_struct, 查看 PHP-SRC/sapi/cgi/cgi_main.c:
*/
static sapi_module_struct cgi_sapi_module = {
#if PHP_FASTCGI
"cgi-fcgi", /* name */
"CGI/FastCGI", /* pretty name */
#else
"cgi", /* name */
"CGI", /* pretty name */
#endif
php_cgi_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
sapi_cgi_deactivate, /* deactivate */
sapi_cgibin_ub_write, /* unbuffered write */
sapi_cgibin_flush, /* flush */
NULL, /* get uid */
sapi_cgibin_getenv, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
sapi_cgi_send_headers, /* send headers handler */
NULL, /* send header handler */
sapi_cgi_read_post, /* read POST data */
sapi_cgi_read_cookies, /* read Cookies */
sapi_cgi_register_variables, /* register server variables */
sapi_cgi_log_message, /* Log message */
NULL, /* Get request time */
STANDARD_SAPI_MODULE_PROPERTIES
};
這個(gè)結(jié)構(gòu),包含了一些常量,比如name, 這個(gè)會(huì)在我們調(diào)用php_info()的時(shí)候被使用。一些初始化,收尾函數(shù),以及一些函數(shù)指針,用來(lái)告訴Zend,如何獲取,和輸出數(shù)據(jù)。
1. php_cgi_startup, 當(dāng)一個(gè)應(yīng)用要調(diào)用PHP的時(shí)候,這個(gè)函數(shù)會(huì)被調(diào)用,對(duì)于CGI來(lái)說(shuō),它只是簡(jiǎn)單的調(diào)用了PHP的初始化函數(shù):
static int php_cgi_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, NULL, 0) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
2. php_module_shutdown_wrapper , 一個(gè)對(duì)PHP關(guān)閉函數(shù)的簡(jiǎn)單包裝。只是簡(jiǎn)單的調(diào)用php_module_shutdown;
3. PHP會(huì)在每個(gè)request的時(shí)候,處理一些初始化,資源分配的事務(wù)。這部分就是activate字段要定義的,從上面的結(jié)構(gòu)我們可以看出,對(duì)于CGI來(lái)說(shuō),它并沒(méi)有提供初始化處理句柄。對(duì)于mod_php來(lái)說(shuō),那就不同了,他要在apache的pool中注冊(cè)資源析構(gòu)函數(shù), 申請(qǐng)空間, 初始化環(huán)境變量,等等等等。
4. sapi_cgi_deactivate, 這個(gè)是對(duì)應(yīng)與activate的函數(shù),顧名思義,它會(huì)提供一個(gè)handler, 用來(lái)處理收尾工作,對(duì)于CGI來(lái)說(shuō),他只是簡(jiǎn)單的刷新緩沖區(qū),用以保證用戶(hù)在Zend關(guān)閉前得到所有的輸出數(shù)據(jù):
static int sapi_cgi_deactivate(TSRMLS_D)
{
/* flush only when SAPI was started. The reasons are:
1. SAPI Deactivate is called from two places: module init and request shutdown
2. When the first call occurs and the request is not set up, flush fails on
FastCGI.
*/
if (SG(sapi_started)) {
sapi_cgibin_flush(SG(server_context));
}
return SUCCESS;
}
5. sapi_cgibin_ub_write, 這個(gè)hanlder告訴了Zend,如何輸出數(shù)據(jù),對(duì)于mod_php來(lái)說(shuō),這個(gè)函數(shù)提供了一個(gè)向response數(shù)據(jù)寫(xiě)的接口,而對(duì)于CGI來(lái)說(shuō),只是簡(jiǎn)單的寫(xiě)到stdout:
static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC)
{
#ifdef PHP_WRITE_STDOUT
long ret;
#else
size_t ret;
#endif
#if PHP_FASTCGI
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
long ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
if (ret <= 0) {
return 0;
}
return ret;
}
#endif
#ifdef PHP_WRITE_STDOUT
ret = write(STDOUT_FILENO, str, str_length);
if (ret <= 0) return 0;
return ret;
#else
ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
return ret;
#endif
}
static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC)
{
const char *ptr = str;
uint remaining = str_length;
size_t ret;
while (remaining > 0) {
ret = sapi_cgibin_single_write(ptr, remaining TSRMLS_CC);
if (!ret) {
php_handle_aborted_connection();
return str_length - remaining;
}
ptr += ret;
remaining -= ret;
}
return str_length;
}
把真正的寫(xiě)的邏輯剝離出來(lái),就是為了簡(jiǎn)單實(shí)現(xiàn)兼容fastcgi的寫(xiě)方式。
6. sapi_cgibin_flush, 這個(gè)是提供給zend的刷新緩存的函數(shù)句柄,對(duì)于CGI來(lái)說(shuō),只是簡(jiǎn)單的調(diào)用系統(tǒng)提供的fflush;
7.NULL, 這部分用來(lái)讓Zend可以驗(yàn)證一個(gè)要執(zhí)行腳本文件的state,從而判斷文件是否據(jù)有執(zhí)行權(quán)限等等,CGI沒(méi)有提供。
8. sapi_cgibin_getenv, 為Zend提供了一個(gè)根據(jù)name來(lái)查找環(huán)境變量的接口,對(duì)于mod_php5來(lái)說(shuō),當(dāng)我們?cè)谀_本中調(diào)用getenv的時(shí)候,就會(huì)間接的調(diào)用這個(gè)句柄。而對(duì)于CGI來(lái)說(shuō),因?yàn)樗倪\(yùn)行機(jī)制和CLI很類(lèi)似,直接調(diào)用父級(jí)是Shell, 所以,只是簡(jiǎn)單的調(diào)用了系統(tǒng)提供的genenv:
static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
{
#if PHP_FASTCGI
/* when php is started by mod_fastcgi, no regular environment
is provided to PHP. It is always sent to PHP at the start
of a request. So we have to do our own lookup to get env
vars. This could probably be faster somehow. */
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
return fcgi_getenv(request, name, name_len);
}
#endif
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment */
return getenv(name);
}
9. php_error, 錯(cuò)誤處理函數(shù), 到這里,說(shuō)幾句題外話(huà),上次看到php maillist 提到的使得PHP的錯(cuò)誤處理機(jī)制完全OO化, 也就是,改寫(xiě)這個(gè)函數(shù)句柄,使得每當(dāng)有錯(cuò)誤發(fā)生的時(shí)候,都throw一個(gè)異常。而CGI只是簡(jiǎn)單的調(diào)用了PHP提供的錯(cuò)誤處理函數(shù)。
10. 這個(gè)函數(shù)會(huì)在我們調(diào)用PHP的header()函數(shù)的時(shí)候被調(diào)用,對(duì)于CGI來(lái)說(shuō),不提供。
11. sapi_cgi_send_headers, 這個(gè)函數(shù)會(huì)在要真正發(fā)送header的時(shí)候被調(diào)用,一般來(lái)說(shuō),就是當(dāng)有任何的輸出要發(fā)送之前:
static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
char buf[SAPI_CGI_MAX_HEADER_LENGTH];
sapi_header_struct *h;
zend_llist_position pos;
if (SG(request_info).no_headers == 1) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
if (cgi_nph || SG(sapi_headers).http_response_code != 200)
{
int len;
if (rfc2616_headers && SG(sapi_headers).http_status_line) {
len = snprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH,
"%s\r\n", SG(sapi_headers).http_status_line);
if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
len = SAPI_CGI_MAX_HEADER_LENGTH;
}
} else {
len = sprintf(buf, "Status: %d\r\n", SG(sapi_headers).http_response_code);
}
PHPWRITE_H(buf, len);
}
h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
while (h) {
/* prevent CRLFCRLF */
if (h->header_len) {
PHPWRITE_H(h->header, h->header_len);
PHPWRITE_H("\r\n", 2);
}
h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
}
PHPWRITE_H("\r\n", 2);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
12. NULL, 這個(gè)用來(lái)單獨(dú)發(fā)送每一個(gè)header, CGI沒(méi)有提供
13. sapi_cgi_read_post, 這個(gè)句柄指明了如何獲取POST的數(shù)據(jù),如果做過(guò)CGI編程的話(huà),我們就知道CGI是從stdin中讀取POST DATA的,
static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
{
uint read_bytes=0, tmp_read_bytes;
#if PHP_FASTCGI
char *pos = buffer;
#endif
count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes));
while (read_bytes < count_bytes) {
#if PHP_FASTCGI
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
tmp_read_bytes = fcgi_read(request, pos, count_bytes - read_bytes);
pos += tmp_read_bytes;
} else {
tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
}
#else
tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
#endif
if (tmp_read_bytes <= 0) {
break;
}
read_bytes += tmp_read_bytes;
}
return read_bytes;
}
14. sapi_cgi_read_cookies, 這個(gè)和上面的函數(shù)一樣,只不過(guò)是去獲取cookie值:
static char *sapi_cgi_read_cookies(TSRMLS_D)
{
return sapi_cgibin_getenv((char *) "HTTP_COOKIE", sizeof("HTTP_COOKIE")-1 TSRMLS_CC);
}
15. sapi_cgi_register_variables, 這個(gè)函數(shù)給了一個(gè)接口,用以給$_SERVER變量中添加變量,對(duì)于CGI來(lái)說(shuō),注冊(cè)了一個(gè)PHP_SELF,這樣我們就可以在腳本中訪(fǎng)問(wèn)$_SERVER['PHP_SELF']來(lái)獲取
本次的request_uri:
static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC)
{
/* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array TSRMLS_CC);
/* Build the special-case PHP_SELF variable for the CGI version */
php_register_variable("PHP_SELF", (SG(request_info).request_uri ? SG(request_info).request_uri : ""), track_vars_array TSRMLS_CC);
}
16. sapi_cgi_log_message ,用來(lái)輸出錯(cuò)誤信息,對(duì)于CGI來(lái)說(shuō),只是簡(jiǎn)單的輸出到stderr:
static void sapi_cgi_log_message(char *message)
{
#if PHP_FASTCGI
if (fcgi_is_fastcgi() && fcgi_logging) {
fcgi_request *request;
TSRMLS_FETCH();
request = (fcgi_request*) SG(server_context);
if (request) {
int len = strlen(message);
char *buf = malloc(len+2);
memcpy(buf, message, len);
memcpy(buf + len, "\n", sizeof("\n"));
fcgi_write(request, FCGI_STDERR, buf, len+1);
free(buf);
} else {
fprintf(stderr, "%s\n", message);
}
/* ignore return code */
} else
#endif /* PHP_FASTCGI */
fprintf(stderr, "%s\n", message);
}
經(jīng)過(guò)分析,我們已經(jīng)了解了一個(gè)SAPI是如何實(shí)現(xiàn)的了, 分析過(guò)CGI以后,我們也就可以想象mod_php, embed等SAPI的實(shí)現(xiàn)機(jī)制。 :)
怎么樣,本文介紹的是不是非常詳細(xì),希望大家喜歡。
- 在WAMP環(huán)境下搭建ZendDebugger php調(diào)試工具的方法
- php.ini文件配置好后,zend路徑也全部配置正確,但是phpinfo()還顯示沒(méi)有zend信息
- Win2003下配置iis+php+mysql+zend圖文 使其支持asp,.net,cgi,perl和php
- Zend Studio去除編輯器的語(yǔ)法警告設(shè)置方法
- windows下zendframework項(xiàng)目環(huán)境搭建(通過(guò)命令行配置)
- win2008 R2 下 IIS7.5+PHP5.2.17+Mysql5.5.16+Zend3.3.3
- 關(guān)于更改Zend Studio/Eclipse代碼風(fēng)格主題的介紹
- zend optimizer在wamp的基礎(chǔ)上安裝圖文教程
- 阿里云完美教程 Window2003 iis+mysql+php+zend環(huán)境配置
- 用Zend Studio+PHPnow+Zend Debugger搭建PHP服務(wù)器調(diào)試環(huán)境步驟
- IIS下Zend 出現(xiàn) Unable to view file mapping 問(wèn)題的解決方法匯總
- 當(dāng)前比較流行的兩款PHP加密、解密工具Zend Guard和iconCube介紹
- Windows下的PHP 5.3.x安裝 Zend Guard Loader教程
- PHP5.3安裝Zend Guard Loader圖文教程
相關(guān)文章
Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)數(shù)據(jù)到視圖的方法
這篇文章主要介紹了Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)數(shù)據(jù)到視圖的方法,涉及thinkPHP5數(shù)據(jù)庫(kù)配置、讀取、模型操作及視圖調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
php用戶(hù)注冊(cè)頁(yè)面利用js進(jìn)行表單驗(yàn)證具體實(shí)例
這篇文章介紹了php用戶(hù)注冊(cè)頁(yè)面利用js進(jìn)行表單驗(yàn)證具體實(shí)例,有需要的朋友可以參考一下2013-10-10
Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移與支持中文的填充
最近在學(xué)習(xí)Laravel數(shù)據(jù)庫(kù)方面的內(nèi)容,發(fā)現(xiàn)了一些資料不錯(cuò)整理出來(lái)分享給大家,下面這篇文章主要給大家介紹了關(guān)于Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移與支持中文填充的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11
Win10 下安裝配置IIS + MySQL + nginx + php7.1.7
本文給大家分享的是在WIN10系統(tǒng)中,安裝配置IIS + MySQL5.7.19 + nginx1.12.1 + php7.1.7的詳細(xì)教程,非常實(shí)用,有需要的小伙伴可以參考下2017-08-08
Zend Framework教程之Zend_Db_Table表關(guān)聯(lián)實(shí)例詳解
這篇文章主要介紹了Zend Framework教程之Zend_Db_Table表關(guān)聯(lián)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Zend_Db_Table表關(guān)聯(lián)的定義,實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼
microtime()函數(shù)返回當(dāng)前 Unix 時(shí)間戳的微秒數(shù)。用于檢測(cè)程序執(zhí)行時(shí)間的函數(shù),也是PHP內(nèi)置的時(shí)間函數(shù)之一,在PHP中可以用于對(duì)程序執(zhí)行時(shí)間的判斷,以及相同功能函數(shù)的執(zhí)行效率高低快慢的判斷。2022-12-12
php中文驗(yàn)證碼實(shí)現(xiàn)示例分享
這篇文章主要介紹了使用php實(shí)現(xiàn)中文驗(yàn)證碼,代碼簡(jiǎn)單,大家可以直接使用2014-01-01
php實(shí)現(xiàn)對(duì)短信驗(yàn)證碼發(fā)送次數(shù)的限制實(shí)例講解
這篇文章主要介紹了php實(shí)現(xiàn)對(duì)短信驗(yàn)證碼發(fā)送次數(shù)的限制實(shí)例講解,案例中列舉了具體代碼實(shí)現(xiàn),有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03
PHP實(shí)現(xiàn)chrome表單請(qǐng)求數(shù)據(jù)轉(zhuǎn)換為接口使用的json數(shù)據(jù)
這篇文章主要介紹了PHP實(shí)現(xiàn)chrome表單請(qǐng)求數(shù)據(jù)轉(zhuǎn)換為接口使用的json數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

