Your understanding of ZendSAPIs (ZendSAPIInternals)

Source: Internet
Author: User
Tags sapi
This article describes your understanding of ZendSAPIs (ZendSAPIInternals). For more information, see

This article describes your understanding of Zend SAPIs (Zend SAPI Internals). For more information, see

SAPI: Server interaction action API. If you have studied the PHP architecture, you should know the importance of this stuff. It provides an interface for PHP to interact with other applications. This article will not detail the SAPI of each PHP, but describes the SAPI Mechanism for the simplest cgi sapi.

First, let's take a look at the PHP architecture:

PHP Architecture

SAPI provides an interface for external communication. For PHP5.2, many types of sapis are provided by default. common interfaces include mod_php5 and CGI for apache, ISAPI for IIS, and CLI for Shell, this article starts with cgi sapi and introduces the SAPI mechanism. Although CGI is simple, you don't have to worry about it. It contains a vast majority of content, which is enough for you to deeply understand how SAPI works.

To define a SAPI, first define a sapi_module_struct to view the 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 */# endifphp_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 */handle,/* 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 };

This structure contains some constants, such as name, which will be used when we call php_info. Some initialization, final functions, and some function pointers are used to tell Zend how to obtain and output data.

1. php_cgi_startup: This function is called when an application calls PHP. For CGI, it simply calls the PHP initialization function:

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, a simple package for PHP function closures. It's just a simple call to php_module_shutdown;

3. PHP processes initialization and resource allocation transactions for each request. This part is the activate field to be defined. From the above structure, we can see that for CGI, it does not provide initialization processing handle. For mod_php, it is different. He wants to register resource destructor, apply for space, initialize environment variables, and so on in the apache pool.

4. sapi_cgi_deactivate, which corresponds to the activate function. As the name suggests, it provides a handler to handle the final work. For CGI, it just refreshes the buffer, to ensure that you get all output data before Zend is disabled:

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 onFastCGI. */if (SG (sapi_started) {sapi_cgibin_flush (SG (server_context);} return SUCCESS ;}

5. sapi_cgibin_ub_write. This hanlder tells Zend how to output data. For mod_php, this function provides an interface for writing data to response. For CGI, it is simply written to stdout:

Static inline size_t convert (const char * str, uint str_length TSRMLS_DC) {# ifdef PHP_WRITE_STDOUT long ret; # else size_t ret; # endif # if PHP_FASTCGI if (limit ()) fc{ gi_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_FIL ENO, 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 ;}

The true write logic is stripped out to implement a write method compatible with fastcgi.

6. sapi_cgibin_flush: This is the function handle provided to zend to refresh the cache. For CGI, it is just a simple call to the fflush provided by the system;

7. NULL. This part allows Zend to verify the state of the script file to be executed and determine whether the file has the execution permission. CGI does not provide it.

8. sapi_cgibin_getenv provides Zend with an interface for finding environment variables based on name. For mod_php5, when we call getenv in the script, this handle is indirectly called. For CGI, because its operating mechanism is similar to CLI, directly calling the parent class is Shell, it simply calls the genenv provided by the system:

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 cocould 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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.