"PHP Kernel Learning" sapi____php

Source: Internet
Author: User
Tags sapi

Brother Bird's blog mentioned SAPI, I also look at the source.

-------------

PHP architecture diagram (photo from http://stblog.baidu-tech.com/?p=763):

Middle-tier (SAPI) decoupling of Web server and PHP



Each SAPI implementation is a _SAPI_MODULE_STRUCT structure:

In the PHP source directory with the Grep-r _sapi_module_struct search to its definition in main/sapi.h:

struct _sapi_module_struct {char *name;

    Char *pretty_name;
    Int (*startup) (struct _sapi_module_struct *sapi_module);

    Int (*shutdown) (struct _sapi_module_struct *sapi_module);
    Int (*activate) (tsrmls_d);

    Int (*deactivate) (tsrmls_d);
    Int (*ub_write) (const char *STR, unsigned int str_length tsrmls_dc);
    void (*flush) (void *server_context);
    struct stat * (*get_stat) (tsrmls_d);

    char * (*getenv) (char *name, size_t name_len tsrmls_dc);

    void (*sapi_error) (int type, const char *error_msg, ...); Int (*header_handler) (Sapi_header_struct *sapi_header, Sapi_header_op_enum op, sapi_headers_struct *sapi_headers
    TSRMLS_DC);
    Int (*send_headers) (sapi_headers_struct *sapi_headers tsrmls_dc);

    void (*send_header) (Sapi_header_struct *sapi_header, void *server_context tsrmls_dc);
    Int (*read_post) (char *buffer, uint count_bytes tsrmls_dc);

    char * (*read_cookies) (tsrmls_d); void (*register_server_variables) (Zval *track_vars_array tsrmls_dc);
    void (*log_message) (char *message tsrmls_dc);
    Double (*get_request_time) (tsrmls_d);

    void (*terminate_process) (tsrmls_d);

    Char *php_ini_path_override;
    void (*block_interruptions) (void);

    void (*unblock_interruptions) (void);
    void (*default_post_reader) (tsrmls_d);
    void (*treat_data) (int arg, char *str, zval *destarray tsrmls_dc);

    Char *executable_location;
    int Php_ini_ignore; int php_ini_ignore_cwd;

    /* Don t look for php.ini in the current directory * * INT (*GET_FD) (int *fd tsrmls_dc);

    Int (*force_http_10) (tsrmls_d);
    Int (*get_target_uid) (uid_t * tsrmls_dc);
    Int (*get_target_gid) (gid_t * tsrmls_dc);

    unsigned int (*input_filter) (int arg, char *var, char **val, unsigned int val_len, unsigned int *new_val_len tsrmls_dc);
    void (*ini_defaults) (HashTable *configuration_hash);

    int phpinfo_as_text;
    Char *ini_entries;
    Const Zend_function_entry *additional_functions; unsigned int (*input_filter_init) (tsrmls_d);
 };

Below, take Cgi_sapi as an example to illustrate the implementation of SAPI:

Static Sapi_module_struct Cgi_sapi_module = {"cgi-fcgi",/* name */"cgi/fastcgi", /* Pretty name */php_cgi_startup,/* Startup when SAPI initialization, first call the function * * Php_module_shutdown_wra Pper,/* Shutdown closes the function wrapper, which is used to release all the SAPI data structure, memory, etc./sapi_cgi_activate,/* Activate this function will be invoked at the start of each request and it will

    The data structure before each request is initialized/* sapi_cgi_deactivate,/* Deactivate this function is invoked at the end of each request, to ensure that all data is available, and to release a data structure initialized in activate.                 Sapi_cgi_ub_write,/* unbuffered write does not cache writes (unbuffered write), which is used to output PHP data to the client * * Sapi_cgi_flush, /* Flush refresh output, in CLI mode by using the C language library function Fflush implement/NULL,/* Get UID/* sapi_cgi                           _getenv,/* getenv * * php_error,/* ERROR handler/NULL,                           /* Header handler/sapi_cgi_send_headers,/* Send headers Handler sending header information * * NULL, /* Send HeaDer Handler/Sapi_cgi_read_post,/* Read post data when the requested method is post, the program will operate $_post, $HTTP _raw_post_data variables * * Sapi_cgi_read_cookies, * Read cookies when SAPI is activated, the program calls this function and assigns the value obtained by this function to the SG (request_info). cookie_data*/SAP I_cgi_register_variables,/* Register server variables/sapi_cgi_log_message,/* Log message/* NUL L,/* GET request time */NULL,/* Child terminate */Standar
 D_sapi_module_properties};


Through the above CGI example, we can see that the entire SAPI is similar to an Object-oriented template method pattern application. Some functions contained in SAPI.C and SAPI.h files are abstract templates in template method patterns, and each server's definition and related implementation of Sapi_module is a specific template.


Reference:

1.http://stblog.baidu-tech.com/?p=763

2.http://www.laruence.com/2008/08/12/180.html

3.http://www.nowamagic.net/librarys/veda/detail/1292




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.