搜尋
首頁php教程php手册PHP内核-Apache2的SAPI
PHP内核-Apache2的SAPIJun 13, 2016 am 10:52 AM
modulephpstr核心定義我們知道

我们知道定义SAPI之前,首先要定义sapi_module_struct这个结构,相看源码:/soft/php-5.2.9/sapi/apache2handler/sapi_apache2.c,可以看到定义该结构,我直接复制过来:

[cpp] 
static sapi_module_struct apache2_sapi_module = { 
        "apache2handler", 
        "Apache 2.0 Handler", 
 
        php_apache2_startup,                            /* startup */ 
        php_module_shutdown_wrapper,                    /* shutdown */ 
 
        NULL,                                           /* activate */ 
        NULL,                                           /* deactivate */ 
 
        php_apache_sapi_ub_write,                       /* unbuffered write */ 
        php_apache_sapi_flush,                          /* flush */ 
        php_apache_sapi_get_stat,                       /* get uid */ 
        php_apache_sapi_getenv,                         /* getenv */ 
 
        php_error,                                      /* error handler */ 
 
        php_apache_sapi_header_handler,                 /* header handler */ 
        php_apache_sapi_send_headers,                   /* send headers handler */ 
        NULL,                                           /* send header handler */ 
 
        php_apache_sapi_read_post,                      /* read POST data */ 
        php_apache_sapi_read_cookies,                   /* read Cookies */ 
 
        php_apache_sapi_register_variables, 
        php_apache_sapi_log_message,                    /* Log message */ 
        php_apache_sapi_get_request_time,               /* Request Time */ 
 
        STANDARD_SAPI_MODULE_PROPERTIES 
}; 
1,php_apache2_startup:当通过apache调用PHP时,这个函数会被调用。该函数定义如下,主要是对PHP进行初始化。
[cpp]
static int php_apache2_startup(sapi_module_struct *sapi_module) 

        if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) { 
                return FAILURE; 
        } 
        return SUCCESS; 

2,php_module_shutdown_wrapper :PHP的关闭函数。
3,PHP会在每个request的时候,处理一些初始化,资源分配的事务。这部分就是activate字段要定义的。

4,与activate的函数,就是deactiveate,它会提供一个handler, 用来处理收尾工作。

5,php_apache_sapi_ub_write:提供一个向Response数据写的接口。

[cpp]
static int 
php_apache_sapi_ub_write(const char *str, uint str_length TSRMLS_DC) 

        request_rec *r; 
        php_struct *ctx; 
 
        ctx = SG(server_context); 
        r = ctx->r; 
 
        if (ap_rwrite(str, str_length, r)                 php_handle_aborted_connection(); 
        } 
 
        return str_length; /* we always consume all the data passed to us. */ 


6,php_apache_sapi_flush:提供给zend刷新缓存的句柄。

[cpp]
static void 
php_apache_sapi_flush(void *server_context) 

        php_struct *ctx; 
        request_rec *r; 
        TSRMLS_FETCH(); 
 
        ctx = server_context; 
 
        /* If we haven't registered a server_context yet,
         * then don't bother flushing. */ 
        if (!server_context) { 
                return; 
        } 
 
        r = ctx->r; 
 
        sapi_send_headers(TSRMLS_C); 
 
        r->status = SG(sapi_headers).http_response_code; 
        SG(headers_sent) = 1; 
 
        if (ap_rflush(r) connection->aborted) { 
                php_handle_aborted_connection(); 
        } 

7,php_apache_sapi_get_stat:这部分用来让Zend可以验证一个要执行脚本文件的state,从而判断文件是否据有执行权限等等。
[cpp]
static struct stat* 
php_apache_sapi_get_stat(TSRMLS_D) 

        php_struct *ctx = SG(server_context); 
 
        ctx->finfo.st_uid = ctx->r->finfo.user; 
        ctx->finfo.st_gid = ctx->r->finfo.group; 
        ctx->finfo.st_dev = ctx->r->finfo.device; 
        ctx->finfo.st_ino = ctx->r->finfo.inode; 
#if defined(NETWARE) && defined(CLIB_STAT_PATCH) 
        ctx->finfo.st_atime.tv_sec = apr_time_sec(ctx->r->finfo.atime); 
        ctx->finfo.st_mtime.tv_sec = apr_time_sec(ctx->r->finfo.mtime); 
        ctx->finfo.st_ctime.tv_sec = apr_time_sec(ctx->r->finfo.ctime); 
#else 
        ctx->finfo.st_atime = apr_time_sec(ctx->r->finfo.atime); 
        ctx->finfo.st_mtime = apr_time_sec(ctx->r->finfo.mtime); 
        ctx->finfo.st_ctime = apr_time_sec(ctx->r->finfo.ctime); 
#endif 
 
        ctx->finfo.st_size = ctx->r->finfo.size; 
        ctx->finfo.st_nlink = ctx->r->finfo.nlink; 
 
        return &ctx->finfo; 

8,php_apache_sapi_getenv:为Zend提供了一个根据name来查找环境变量的接口,当我们在脚本中调用getenv的时候,就会间接的调用这个句柄。
[cpp] 
static char * 
php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC) 

        php_struct *ctx = SG(server_context); 
        const char *env_var; 
 
        env_var = apr_table_get(ctx->r->subprocess_env, name); 
 
        return (char *) env_var; 

9,php_error:错误处理函数,直接调用PHP错误处理函数。
10,php_apache_sapi_header_handler:在调用PHP的header()函数时,会调用这个函数。

[cpp]
static int 
php_apache_sapi_header_handler(sapi_header_struct *sapi_header,sapi_headers_struct *sapi_headers TSRMLS_DC) 

        php_struct *ctx; 
        char *val, *ptr; 
 
        ctx = SG(server_context); 
 
        val = strchr(sapi_header->header, ':'); 
 
        if (!val) { 
                sapi_free_header(sapi_header); 
                return 0; 
        } 
        ptr = val; 
 
        *val = '\0'; 
 
        do { 
                val++; 
        } while (*val == ' '); 
        if (!strcasecmp(sapi_header->header, "content-type")) { 
                if (ctx->content_type) { 
                        efree(ctx->content_type); 
                } 
                ctx->content_type = estrdup(val); 
        } else if (sapi_header->replace) { 
                apr_table_set(ctx->r->headers_out, sapi_header->header, val); 
        } else { 
                apr_table_add(ctx->r->headers_out, sapi_header->header, val); 
        } 
        *ptr = ':'; 
 
        return SAPI_HEADER_ADD; 

11,php_apache_sapi_send_headers:当要真正发送header的时候,这个函数会被调用。
[cpp] 
static int 
php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) 

        php_struct *ctx = SG(server_context); 
        const char *sline = SG(sapi_headers).http_status_line; 
 
        ctx->r->status = SG(sapi_headers).http_response_code; 
 
        /* httpd requires that r->status_line is set to the first digit of
         * the status-code: */ 
        if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ') { 
                ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9); 
                ctx->r->proto_num = 1000 + (sline[7]-'0'); 
                if ((sline[7]-'0') == 0) { 
                        apr_table_set(ctx->r->subprocess_env, "force-response-1.0", "true"); 
                } 
        } 
 
        /*      call ap_set_content_type only once, else each time we call it,
                configured output filters for that content type will be added */ 
        if (!ctx->content_type) { 
                ctx->content_type = sapi_get_default_content_type(TSRMLS_C); 
        } 
        ap_set_content_type(ctx->r, apr_pstrdup(ctx->r->pool, ctx->content_type)); 
        efree(ctx->content_type); 
        ctx->content_type = NULL; 
 
        return SAPI_HEADER_SENT_SUCCESSFULLY; 

12,在php_apache_sapi_send_headers指针下面有一个域,用来指明发送每一个单独的header时调用。
13,php_apache_sapi_read_post:表示如何读取POST数据。

[cpp]
static int  
php_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC) 

        apr_size_t len, tlen=0; 
        php_struct *ctx = SG(server_context); 
        request_rec *r; 
        apr_bucket_brigade *brigade; 
 
        r = ctx->r; 
        brigade = ctx->brigade; 
        len = count_bytes; 
 
        /*
         * This loop is needed because ap_get_brigade() can return us partial data
         * which would cause premature termination of request read. Therefor we
         * need to make sure that if data is available we fill the buffer completely.
         */ 
 
        while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) { 
                apr_brigade_flatten(brigade, buf, &len); 
                apr_brigade_cleanup(brigade); 
                tlen += len; 
                if (tlen == count_bytes || !len) { 
                        break; 
                } 
                buf += len; 
                len = count_bytes - tlen; 
        } 
 
        return tlen; 


14,php_apache_sapi_read_cookie:如何读取cookie。

[cpp]
static char * 
php_apache_sapi_read_cookies(TSRMLS_D) 

        php_struct *ctx = SG(server_context); 
        const char *http_cookie; 
 
        http_cookie = apr_table_get(ctx->r->headers_in, "cookie"); 
 
        /* The SAPI interface should use 'const char *' */ 
        return (char *) http_cookie; 

15,php_apache_sapi_register_variables:提供接口,用于给$_SERVER[]数组提供变量。
[cpp]
static void 
php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC) 

        php_struct *ctx = SG(server_context); 
        const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env); 
        char *key, *val; 
        int new_val_len; 
 
        APR_ARRAY_FOREACH_OPEN(arr, key, val) 
                if (!val) { 
                        val = ""; 
                } 
                if (sapi_module.input_filter(PARSE_SERVER, key, &val, strlen(val), &new_val_len TSRMLS_CC)) { 
                        php_register_variable_safe(key, val, new_val_len, track_vars_array TSRMLS_CC); 
                } 
        APR_ARRAY_FOREACH_CLOSE() 
 
        if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &ctx->r->uri, strlen(ctx->r->uri), &new_val_len TSRMLS_CC)) { 
                php_register_variable_safe("PHP_SELF", ctx->r->uri, new_val_len, track_vars_array TSRMLS_CC); 
        } 

16,php_apache_sapi_log_message:输出错误信息。
[cpp]
static void php_apache_sapi_log_message(char *msg) 

        php_struct *ctx; 
        TSRMLS_FETCH(); 
 
        ctx = SG(server_context); 
 
        if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */ 
                ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg); 
        } else { 
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "%s", msg); 
        } 


17,php_apache_sapi_get_request_time:获取请求时间。

[cpp] 
static time_t php_apache_sapi_get_request_time(TSRMLS_D) { 
        php_struct *ctx = SG(server_context); 
        return apr_time_sec(ctx->r->request_time); 

这就完成了apache的SAPI定义。之后,就是当用户用URL请求apache服务,这些函数指针就会在适当的时候,发挥作用了(被调用)。

 

 

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。