引自:http://www.nowamagic.net/librarys/veda/detail/1285
SAPI:Server Application Programming Interface 服務(wù)器端應(yīng)用編程端口。研究過(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)圖:
SAPI指的是PHP具體應(yīng)用的編程接口, 就像PC一樣,無(wú)論安裝哪些操作系統(tǒng),只要滿足了PC的接口規(guī)范都可以在PC上正常運(yùn)行, PHP腳本要執(zhí)行有很多種方式,通過(guò)Web服務(wù)器,或者直接在命令行下,也可以嵌入在其他程序中。
通常,我們使用Apache或者Nginx這類Web服務(wù)器來(lái)測(cè)試PHP腳本,或者在命令行下通過(guò)PHP解釋器程序來(lái)執(zhí)行。 腳本執(zhí)行完后,Web服務(wù)器應(yīng)答,瀏覽器顯示應(yīng)答信息,或者在命令行標(biāo)準(zhǔn)輸出上顯示內(nèi)容。
我們很少關(guān)心PHP解釋器在哪里。雖然通過(guò)Web服務(wù)器和命令行程序執(zhí)行腳本看起來(lái)很不一樣, 實(shí)際上它們的工作流程是一樣的。命令行參數(shù)傳遞給PHP解釋器要執(zhí)行的腳本, 相當(dāng)于通過(guò)url請(qǐng)求一個(gè)PHP頁(yè)面。腳本執(zhí)行完成后返回響應(yīng)結(jié)果,只不過(guò)命令行的響應(yīng)結(jié)果是顯示在終端上。
腳本執(zhí)行的開(kāi)始都是以SAPI接口實(shí)現(xiàn)開(kāi)始的。只是不同的SAPI接口實(shí)現(xiàn)會(huì)完成他們特定的工作, 例如Apache的mod_php SAPI實(shí)現(xiàn)需要初始化從Apache獲取的一些信息,在輸出內(nèi)容是將內(nèi)容返回給Apache, 其他的SAPI實(shí)現(xiàn)也類似。
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:
| 02 | static?sapi_module_struct cgi_sapi_module = { |
| 04 | ????"cgi-fcgi",?????????????????????/* name */ |
| 05 | ????"CGI/FastCGI",??????????????????/* pretty name */ |
| 07 | ????"cgi",??????????????????????????/* name */ |
| 08 | ????"CGI",??????????????????????????/* pretty name */ |
| 11 | ????php_cgi_startup,????????????????/* startup */ |
| 12 | ????php_module_shutdown_wrapper,????/* shutdown */ |
| 14 | ????NULL,???????????????????????????/* activate */ |
| 15 | ????sapi_cgi_deactivate,????????????/* deactivate */ |
| 17 | ????sapi_cgibin_ub_write,???????????/* unbuffered write */ |
| 18 | ????sapi_cgibin_flush,??????????????/* flush */ |
| 19 | ????NULL,???????????????????????????/* get uid */ |
| 20 | ????sapi_cgibin_getenv,?????????????/* getenv */ |
| 22 | ????php_error,??????????????????????/* error handler */ |
| 24 | ????NULL,???????????????????????????/* header handler */ |
| 25 | ????sapi_cgi_send_headers,??????????/* send headers handler */ |
| 26 | ????NULL,???????????????????????????/* send header handler */ |
| 28 | ????sapi_cgi_read_post,?????????????/* read POST data */ |
| 29 | ????sapi_cgi_read_cookies,??????????/* read Cookies */ |
| 31 | ????sapi_cgi_register_variables,????/* register server variables */ |
| 32 | ????sapi_cgi_log_message,???????????/* Log message */ |
| 33 | ????NULL,???????????????????????????/* Get request time */ |
| 35 | ????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ù):
| 1 | static?int?php_cgi_startup(sapi_module_struct *sapi_module) |
| 3 | ????if?(php_module_startup(sapi_module, NULL, 0) == FAILURE) { |
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ū),用以保證用戶在Zend關(guān)閉前得到所有的輸出數(shù)據(jù):
| 01 | static?int?sapi_cgi_deactivate(TSRMLS_D) |
| 03 | ????/* flush only when SAPI was started. The reasons are: |
| 04 | ????????1. SAPI Deactivate is called from two places: module init and request shutdown |
| 05 | ????????2. When the first call occurs and the request is not set up, flush fails on |
| 08 | ????if?(SG(sapi_started)) { |
| 09 | ????????sapi_cgibin_flush(SG(server_context)); |
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:
| 01 | static?inline?size_t?sapi_cgibin_single_write(const?char?*str, uint str_length TSRMLS_DC) |
| 03 | #ifdef PHP_WRITE_STDOUT |
| 10 | ????if?(fcgi_is_fastcgi()) { |
| 11 | ????????fcgi_request *request = (fcgi_request*) SG(server_context); |
| 12 | ????????long?ret = fcgi_write(request, FCGI_STDOUT, str, str_length); |
| 13 | ????????if?(ret <= 0) { |
| 19 | #ifdef PHP_WRITE_STDOUT |
| 20 | ????ret = write(STDOUT_FILENO, str, str_length); |
| 21 | ????if?(ret <= 0)?return?0; |
| 24 | ????ret =?fwrite(str, 1, MIN(str_length, 16384), stdout); |
| 29 | static?int?sapi_cgibin_ub_write(const?char?*str, uint str_length TSRMLS_DC) |
| 31 | ????const?char?*ptr = str; |
| 32 | ????uint remaining = str_length; |
| 35 | ????while?(remaining > 0) { |
| 36 | ????????ret = sapi_cgibin_single_write(ptr, remaining TSRMLS_CC); |
| 38 | ????????????php_handle_aborted_connection(); |
| 39 | ????????????return?str_length - remaining; |
| 42 | ????????remaining -= ret; |
把真正的寫(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很類似,直接調(diào)用父級(jí)是Shell, 所以,只是簡(jiǎn)單的調(diào)用了系統(tǒng)提供的genenv:
| 01 | static?char?*sapi_cgibin_getenv(char?*name,?size_t?name_len TSRMLS_DC) |
| 04 | ????/* when php is started by mod_fastcgi, no regular environment |
| 05 | ???????is provided to PHP.? It is always sent to PHP at the start |
| 06 | ???????of a request.? So we have to do our own lookup to get env |
| 07 | ???????vars.? This could probably be faster somehow.? */ |
| 08 | ????if?(fcgi_is_fastcgi()) { |
| 09 | ????????fcgi_request *request = (fcgi_request*) SG(server_context); |
| 10 | ????????return?fcgi_getenv(request, name, name_len); |
| 13 | ????/*? if cgi, or fastcgi and not found in fcgi env |
| 14 | ????????check the regular environment */ |
| 15 | ????return?getenv(name); |
9. php_error, 錯(cuò)誤處理函數(shù), 到這里,說(shuō)幾句題外話,上次看到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ā)送之前:
| 01 | static?int?sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) |
| 03 | ????char?buf[SAPI_CGI_MAX_HEADER_LENGTH]; |
| 04 | ????sapi_header_struct *h; |
| 05 | ????zend_llist_position pos; |
| 07 | ????if?(SG(request_info).no_headers == 1) { |
| 08 | ????????return??SAPI_HEADER_SENT_SUCCESSFULLY; |
| 11 | ????if?(cgi_nph || SG(sapi_headers).http_response_code != 200) |
| 15 | ????????if?(rfc2616_headers && SG(sapi_headers).http_status_line) { |
| 16 | ????????????len = snprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, |
| 17 | ???????????????????????????"%s\r\n", SG(sapi_headers).http_status_line); |
| 19 | ????????????if?(len > SAPI_CGI_MAX_HEADER_LENGTH) { |
| 20 | ????????????????len = SAPI_CGI_MAX_HEADER_LENGTH; |
| 24 | ????????????len =?sprintf(buf,?"Status: %d\r\n", SG(sapi_headers).http_response_code); |
| 27 | ????????PHPWRITE_H(buf, len); |
| 30 | ????h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos); |
| 32 | ????????/* prevent CRLFCRLF */ |
| 33 | ????????if?(h->header_len) { |
| 34 | ????????????PHPWRITE_H(h->header, h->header_len); |
| 35 | ????????????PHPWRITE_H("\r\n", 2); |
| 37 | ????????h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos); |
| 39 | ????PHPWRITE_H("\r\n", 2); |
| 41 | ????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編程的話,我們就知道CGI是從stdin中讀取POST DATA的:
| 01 | static?int?sapi_cgi_read_post(char?*buffer, uint count_bytes TSRMLS_DC) |
| 03 | ????uint read_bytes=0, tmp_read_bytes; |
| 05 | ????char?*pos = buffer; |
| 08 | ????count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes)); |
| 09 | ????while?(read_bytes < count_bytes) { |
| 11 | ????????if?(fcgi_is_fastcgi()) { |
| 12 | ????????????fcgi_request *request = (fcgi_request*) SG(server_context); |
| 13 | ????????????tmp_read_bytes = fcgi_read(request, pos, count_bytes - read_bytes); |
| 14 | ????????????pos += tmp_read_bytes; |
| 16 | ????????????tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes); |
| 19 | ????????tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes); |
| 22 | ????????if?(tmp_read_bytes <= 0) { |
| 25 | ????????read_bytes += tmp_read_bytes; |
14. sapi_cgi_read_cookies, 這個(gè)和上面的函數(shù)一樣,只不過(guò)是去獲取cookie值:
| 1 | static?char?*sapi_cgi_read_cookies(TSRMLS_D) |
| 3 | ????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,這樣我們就可以在腳本中訪問(wèn)$_SERVER['PHP_SELF']來(lái)獲取本次的request_uri:
| 1 | static?void?sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC) |
| 3 | ????/* In CGI mode, we consider the environment to be a part of the server |
| 6 | ????php_import_environment_variables(track_vars_array TSRMLS_CC); |
| 7 | ????/* Build the special-case PHP_SELF variable for the CGI version */ |
| 8 | ????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:
| 01 | static?void?sapi_cgi_log_message(char?*message) |
| 04 | ????if?(fcgi_is_fastcgi() && fcgi_logging) { |
| 05 | ????????fcgi_request *request; |
| 06 | ????????TSRMLS_FETCH(); |
| 08 | ????????request = (fcgi_request*) SG(server_context); |
| 10 | ????????????int?len =?strlen(message); |
| 11 | ????????????char?*buf =?malloc(len+2); |
| 13 | ????????????memcpy(buf, message, len); |
| 14 | ????????????memcpy(buf + len,?"\n",?sizeof("\n")); |
| 15 | ????????????fcgi_write(request, FCGI_STDERR, buf, len+1); |
| 18 | ????????????fprintf(stderr,?"%s\n", message); |
| 20 | ????????/* ignore return code */ |
| 22 | #endif /* PHP_FASTCGI */ |
| 23 | ????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ī)制。
延伸閱讀
此文章所在專題列表如下:
PHP內(nèi)核探索:從SAPI接口開(kāi)始PHP內(nèi)核探索:一次請(qǐng)求的開(kāi)始與結(jié)束PHP內(nèi)核探索:一次請(qǐng)求生命周期PHP內(nèi)核探索:單進(jìn)程SAPI生命周期PHP內(nèi)核探索:多進(jìn)程/線程的SAPI生命周期PHP內(nèi)核探索:Zend引擎PHP內(nèi)核探索:再次探討SAPIPHP內(nèi)核探索:Apache模塊介紹PHP內(nèi)核探索:通過(guò)mod_php5支持PHPPHP內(nèi)核探索:Apache運(yùn)行與鉤子函數(shù)PHP內(nèi)核探索:嵌入式PHPPHP內(nèi)核探索:PHP的FastCGIPHP內(nèi)核探索:如何執(zhí)行PHP腳本PHP內(nèi)核探索:PHP腳本的執(zhí)行細(xì)節(jié)PHP內(nèi)核探索:操作碼OpCodePHP內(nèi)核探索:PHP里的opcodePHP內(nèi)核探索:解釋器的執(zhí)行過(guò)程PHP內(nèi)核探索:變量概述PHP內(nèi)核探索:變量存儲(chǔ)與類型PHP內(nèi)核探索:PHP中的哈希表PHP內(nèi)核探索:理解Zend里的哈希表PHP內(nèi)核探索:PHP哈希算法設(shè)計(jì)PHP內(nèi)核探索:翻譯一篇HashTables文章PHP內(nèi)核探索:哈希碰撞攻擊是什么?PHP內(nèi)核探索:常量的實(shí)現(xiàn)PHP內(nèi)核探索:變量的存儲(chǔ)PHP內(nèi)核探索:變量的類型PHP內(nèi)核探索:變量的值操作PHP內(nèi)核探索:變量的創(chuàng)建PHP內(nèi)核探索:預(yù)定義變量PHP內(nèi)核探索:變量的檢索PHP內(nèi)核探索:變量的類型轉(zhuǎn)換PHP內(nèi)核探索:弱類型變量的實(shí)現(xiàn)PHP內(nèi)核探索:靜態(tài)變量的實(shí)現(xiàn)PHP內(nèi)核探索:變量類型提示PHP內(nèi)核探索:變量的生命周期PHP內(nèi)核探索:變量賦值與銷毀PHP內(nèi)核探索:變量作用域PHP內(nèi)核探索:詭異的變量名PHP內(nèi)核探索:變量的value和type存儲(chǔ)PHP內(nèi)核探索:全局變量GlobalPHP內(nèi)核探索:變量類型的轉(zhuǎn)換PHP內(nèi)核探索:內(nèi)存管理開(kāi)篇PHP內(nèi)核探索:Zend內(nèi)存管理器PHP內(nèi)核探索:PHP的內(nèi)存管理PHP內(nèi)核探索:內(nèi)存的申請(qǐng)與銷毀PHP內(nèi)核探索:引用計(jì)數(shù)與寫(xiě)時(shí)復(fù)制PHP內(nèi)核探索:PHP5.3的垃圾回收機(jī)制PHP內(nèi)核探索:內(nèi)存管理中的cachePHP內(nèi)核探索:寫(xiě)時(shí)復(fù)制COW機(jī)制PHP內(nèi)核探索:數(shù)組與鏈表PHP內(nèi)核探索:使用哈希表APIPHP內(nèi)核探索:數(shù)組操作PHP內(nèi)核探索:數(shù)組源碼分析PHP內(nèi)核探索:函數(shù)的分類PHP內(nèi)核探索:函數(shù)的內(nèi)部結(jié)構(gòu)PHP內(nèi)核探索:函數(shù)結(jié)構(gòu)轉(zhuǎn)換PHP內(nèi)核探索:定義函數(shù)的過(guò)程PHP內(nèi)核探索:函數(shù)的參數(shù)PHP內(nèi)核探索:zend_parse_parameters函數(shù)PHP內(nèi)核探索:函數(shù)返回值PHP內(nèi)核探索:形參return valuePHP內(nèi)核探索:函數(shù)調(diào)用與執(zhí)行PHP內(nèi)核探索:引用與函數(shù)執(zhí)行PHP內(nèi)核探索:匿名函數(shù)及閉包PHP內(nèi)核探索:面向?qū)ο箝_(kāi)篇PHP內(nèi)核探索:類的結(jié)構(gòu)和實(shí)現(xiàn)PHP內(nèi)核探索:類的成員變量PHP內(nèi)核探索:類的成員方法PHP內(nèi)核探索:類的原型zend_class_entryPHP內(nèi)核探索:類的定義PHP內(nèi)核探索:訪問(wèn)控制PHP內(nèi)核探索:繼承,多態(tài)與抽象類PHP內(nèi)核探索:魔術(shù)函數(shù)與延遲綁定PHP內(nèi)核探索:保留類與特殊類PHP內(nèi)核探索:對(duì)象PHP內(nèi)核探索:創(chuàng)建對(duì)象實(shí)例PHP內(nèi)核探索:對(duì)象屬性讀寫(xiě)PHP內(nèi)核探索:命名空間PHP內(nèi)核探索:定義接口PHP內(nèi)核探索:繼承與實(shí)現(xiàn)接口PHP內(nèi)核探索:資源resource類型PHP內(nèi)核探索:Zend虛擬機(jī)PHP內(nèi)核探索:虛擬機(jī)的詞法解析PHP內(nèi)核探索:虛擬機(jī)的語(yǔ)法分析PHP內(nèi)核探索:中間代碼opcode的執(zhí)行PHP內(nèi)核探索:代碼的加密與解密PHP內(nèi)核探索:zend_execute的具體執(zhí)行過(guò)程PHP內(nèi)核探索:變量的引用與計(jì)數(shù)規(guī)則PHP內(nèi)核探索:新垃圾回收機(jī)制說(shuō)明
轉(zhuǎn)載于:https://www.cnblogs.com/lppblogs/archive/2013/02/21/2920111.html
總結(jié)
以上是生活随笔為你收集整理的php内核探索的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。