手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

PHP内核-Apache2的SAPI

[日期:2012-09-24] 来源:Linux社区  作者:zmxiangde [字体:大 中 小]
9,php_error:错误处理函数,直接调用PHP错误处理函数。

10,php_apache_sapi_header_handler:在调用PHP的header()函数时,会调用这个函数。

  1. static int  
  2. php_apache_sapi_header_handler(sapi_header_struct *sapi_header,sapi_headers_struct *sapi_headers TSRMLS_DC)  
  3. {  
  4.         php_struct *ctx;  
  5.         char *val, *ptr;  
  6.   
  7.         ctx = SG(server_context);  
  8.   
  9.         val = strchr(sapi_header->header, ':');  
  10.   
  11.         if (!val) {  
  12.                 sapi_free_header(sapi_header);  
  13.                 return 0;  
  14.         }  
  15.         ptr = val;  
  16.   
  17.         *val = '\0';  
  18.   
  19.         do {  
  20.                 val++;  
  21.         } while (*val == ' ');  
  22.         if (!strcasecmp(sapi_header->header, "content-type")) {  
  23.                 if (ctx->content_type) {  
  24.                         efree(ctx->content_type);  
  25.                 }  
  26.                 ctx->content_type = estrdup(val);  
  27.         } else if (sapi_header->replace) {  
  28.                 apr_table_set(ctx->r->headers_out, sapi_header->header, val);  
  29.         } else {  
  30.                 apr_table_add(ctx->r->headers_out, sapi_header->header, val);  
  31.         }  
  32.         *ptr = ':';  
  33.   
  34.         return SAPI_HEADER_ADD;  
  35. }  
11,php_apache_sapi_send_headers:当要真正发送header的时候,这个函数会被调用。
  1. static int  
  2. php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)  
  3. {  
  4.         php_struct *ctx = SG(server_context);  
  5.         const char *sline = SG(sapi_headers).http_status_line;  
  6.   
  7.         ctx->r->status = SG(sapi_headers).http_response_code;  
  8.   
  9.         /* httpd requires that r->status_line is set to the first digit of 
  10.          * the status-code: */  
  11.         if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ') {  
  12.                 ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9);  
  13.                 ctx->r->proto_num = 1000 + (sline[7]-'0');  
  14.                 if ((sline[7]-'0') == 0) {  
  15.                         apr_table_set(ctx->r->subprocess_env, "force-response-1.0", "true");  
  16.                 }  
  17.         }  
  18.   
  19.         /*      call ap_set_content_type only once, else each time we call it, 
  20.                 configured output filters for that content type will be added */  
  21.         if (!ctx->content_type) {  
  22.                 ctx->content_type = sapi_get_default_content_type(TSRMLS_C);  
  23.         }  
  24.         ap_set_content_type(ctx->r, apr_pstrdup(ctx->r->pool, ctx->content_type));  
  25.         efree(ctx->content_type);  
  26.         ctx->content_type = NULL;  
  27.   
  28.         return SAPI_HEADER_SENT_SUCCESSFULLY;  
  29. }  
12,在php_apache_sapi_send_headers指针下面有一个域,用来��明发送每一个单独的header时调用。

13,php_apache_sapi_read_post:表示如何读取POST数据。

  1. static int   
  2. php_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC)  
  3. {  
  4.         apr_size_t len, tlen=0;  
  5.         php_struct *ctx = SG(server_context);  
  6.         request_rec *r;  
  7.         apr_bucket_brigade *brigade;  
  8.   
  9.         r = ctx->r;  
  10.         brigade = ctx->brigade;  
  11.         len = count_bytes;  
  12.   
  13.         /* 
  14.          * This loop is needed because ap_get_brigade() can return us partial data 
  15.          * which would cause premature termination of request read. Therefor we 
  16.          * need to make sure that if data is available we fill the buffer completely. 
  17.          */  
  18.   
  19.         while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {  
  20.                 apr_brigade_flatten(brigade, buf, &len);  
  21.                 apr_brigade_cleanup(brigade);  
  22.                 tlen += len;  
  23.                 if (tlen == count_bytes || !len) {  
  24.                         break;  
  25.                 }  
  26.                 buf += len;  
  27.                 len = count_bytes - tlen;  
  28.         }  
  29.   
  30.         return tlen;  
  31. }  

14,php_apache_sapi_read_cookie:如何读取cookie。

  1. static char *  
  2. php_apache_sapi_read_cookies(TSRMLS_D)  
  3. {  
  4.         php_struct *ctx = SG(server_context);  
  5.         const char *http_cookie;  
  6.   
  7.         http_cookie = apr_table_get(ctx->r->headers_in, "cookie");  
  8.   
  9.         /* The SAPI interface should use 'const char *' */  
  10.         return (char *) http_cookie;  
  11. }  
15,php_apache_sapi_register_variables:提供接口,用于给$_SERVER[]数组提供变量。
  1. static void  
  2. php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC)  
  3. {  
  4.         php_struct *ctx = SG(server_context);  
  5.         const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env);  
  6.         char *key, *val;  
  7.         int new_val_len;  
  8.   
  9.         APR_ARRAY_FOREACH_OPEN(arr, key, val)  
  10.                 if (!val) {  
  11.                         val = "";  
  12.                 }  
  13.                 if (sapi_module.input_filter(PARSE_SERVER, key, &val, strlen(val), &new_val_len TSRMLS_CC)) {  
  14.                         php_register_variable_safe(key, val, new_val_len, track_vars_array TSRMLS_CC);  
  15.                 }  
  16.         APR_ARRAY_FOREACH_CLOSE()  
  17.   
  18.         if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &ctx->r->uri, strlen(ctx->r->uri), &new_val_len TSRMLS_CC)) {  
  19.                 php_register_variable_safe("PHP_SELF", ctx->r->uri, new_val_len, track_vars_array TSRMLS_CC);  
  20.         }  
  21. }  
16,php_apache_sapi_log_message:输出错误信息。
  1. static void php_apache_sapi_log_message(char *msg)  
  2. {  
  3.         php_struct *ctx;  
  4.         TSRMLS_FETCH();  
  5.   
  6.         ctx = SG(server_context);  
  7.   
  8.         if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */  
  9.                 ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg);  
  10.         } else {  
  11.                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "%s", msg);  
  12.         }  
  13. }  

17,php_apache_sapi_get_request_time:获取请求时间。

  1. static time_t php_apache_sapi_get_request_time(TSRMLS_D) {  
  2.         php_struct *ctx = SG(server_context);  
  3.         return apr_time_sec(ctx->r->request_time);  
  4. }  
这就完成了apache的SAPI定义。之后,就是当用户用URL请求apache服务,这些函数指针就会在适当的时候,发挥作用了(被调用)。linux
相关资讯       PHP 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款