返回首页
当前位置: 主页 > 编程语言 > PHP教程 >

PHP源码 — Fast-CGI执行过程

时间:2015-05-26 23:33来源:电脑教程学习网 www.etwiki.cn 编辑:admin

php 的fast-cgi 入口在sapi/fpm-main.c里的main函数,

第一部分还是常规的命令模式,通过php_getopt获取用户启动php-fpm

第二部分cgi_sapi_module.startup(&cgi_sapi_module) ,SAPI启动,载入PHP扩展,执行每个扩展的PHP_MINIT_FUNCTION。

核心部分fpm_init初始化,包括读取php-fpm.conf 配置文件。初始化执行过程:

/*Php module 和 sapi shutdown等系列清理操作*/
fpm_php_init_main

/*打开/dev/null,把所有的输出信息、Warning等往这个黑洞里扔*/
fpm_stdio_init_main

/*读取php-fpm.conf 配置文件*/
fpm_conf_init_main

/*读取unix内核数据,检查系统rlimit等*/
fpm_unix_init_main
fpm_scoreboard_init_main
fpm_pctl_init_main
fpm_env_init_main
fpm_signals_init_main
fpm_children_init_main
fpm_sockets_init_main
fpm_worker_pool_init_main
fpm_event_init_main


初始化线程池
fcgi_fd = fpm_run(&max_requests);

 
Fcig 初始化完毕,现在开始进入请求初始化(request init)
fcgi_init_request(&request, fcgi_fd);

 

request 数据结构
  typedef struct _fcgi_request {
   int listen_socket;
  #ifdef _WIN32
   int tcp;
  #endif
   int fd;
   int id;
   int keep;
   int closed;
  
   int in_len;
   int in_pad;
  
   fcgi_header *out_hdr;
   unsigned char *out_pos;
   unsigned char out_buf[1024*8];
   unsigned char reserved[sizeof(fcgi_end_request_rec)];
  
   HashTable *env;
  } fcgi_request;
  typedef struct _fcgi_header {
   unsigned char version; //fast-cgi 版本
   unsigned char type;//消息类型
   unsigned char requestIdB1;//消息请求唯一标记
   unsigned char requestIdB0;
   unsigned char contentLengthB1;
   unsigned char contentLengthB0;
   unsigned char paddingLength;
   unsigned char reserved;
  } fcgi_header;
具体阅读 http://www.fastcgi.com/drupal/node/6?q=node/22#S4

接下来进入请求等待循环,通过accept创建一个新的链接、每个http请求都会执行创建
while (fcgi_accept_request(&request) >= 0) {

fcgi_accept_request :
int fcgi_accept_request(fcgi_request *req)
{
...
req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
...

把请求消息压入到SG(server_context)
SG(server_context) = (void *) &request;

SG(server_context) 数据结构
  typedef struct _sapi_globals_struct {
   void *server_context;
/*
http 请求数据
*/
   sapi_request_info request_info;
   sapi_headers_struct sapi_headers;
   int64_t read_post_bytes;
   unsigned char post_read;
   unsigned char headers_sent;
   struct stat global_stat;
   char *default_mimetype;
   char *default_charset;
   HashTable *rfc1867_uploaded_files;
   long post_max_size;
   int options;
   zend_bool sapi_started;
   double global_request_time;
   HashTable known_post_content_types;
   zval *callback_func;
   zend_fcall_info_cache fci_cache;
   zend_bool callback_run;
  } sapi_globals_struct;
  
  
  typedef struct {
   const char *request_method;
   char *query_string;
   char *cookie_data;
   long content_length;
  
   char *path_translated;
   char *request_uri;
  
   /* Do not use request_body directly, but the php://input stream wrapper instead */
   struct _php_stream *request_body;
  
   const char *content_type;
  
   zend_bool headers_only;
   zend_bool no_headers;
   zend_bool headers_read;
  
   sapi_post_entry *post_entry;
  
   char *content_type_dup;
  
   /* for HTTP authentication */
   char *auth_user;
   char *auth_password;
   char *auth_digest;
  
   /* this is necessary for the CGI SAPI module */
   char *argv0;
  
   char *current_user;
   int current_user_length;
  
   /* this is necessary for CLI module */
   int argc;

------分隔线----------------------------
标签(Tag):php php实例教程 php5 php源代码 php基础教程 php技巧 php6 php教程
------分隔线----------------------------
推荐内容
猜你感兴趣