PHP Kernel-apache2 sapi_php Tutorial

Source: Internet
Author: User
Before we know the definition of SAPI, we must first define SAPI_MODULE_STRUCT this structure, tearful source:/soft/php-5.2.9/sapi/apache2handler/sapi_ APACHE2.C, you can see that the structure is defined and I copy it directly:

[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: This function is called when PHP is invoked via Apache. The function is defined as follows, primarily to initialize 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;
}

The close function of the 2,php_module_shutdown_wrapper:php.
3,php will handle a number of initialization, resource-allocation transactions at each request. This is part of the Activate field to be defined.

4, with Activate's function, is deactiveate, which will provide a handler to handle the finishing touches.

5,php_apache_sapi_ub_write: Provides an interface for writing to response data.

[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) < 0) {
Php_handle_aborted_connection ();
}

return str_length; /* We always consume all the data passed to us. */
}


6,php_apache_sapi_flush: Provides a handle to the Zend flush cache.

[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) < 0 | | r->connection->aborted) {
Php_handle_aborted_connection ();
}
}
7,php_apache_sapi_get_stat: This section is used to allow Zend to validate a state to execute a script file, to determine whether the file has execute permissions, and so on.
[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: Provides an interface for Zend to find environment variables by name, which is invoked indirectly when we call getenv in a script.
[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: Error handling function, call PHP error handler directly.
10,php_apache_sapi_header_handler: This function is called when the header () function of PHP is called.

[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 = ' + ';

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: This function is called when the header is actually sent.
[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 are set to the first digit of
* The Status-code: * *
if (sline && strlen (sline) > && strncmp (sline, "HTTP/1.", 7) = = 0 && sline[8] = = ") {
Ctx->r->status_line = Apr_pstrdup (Ctx->r->pool, sline + 9);
Ctx->r->proto_num = + (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-once, else each time we call it,
Configured output filters for that content type would 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, there is a field below the php_apache_sapi_send_headers pointer to indicate that each individual header is sent when it is called.
13,php_apache_sapi_read_post: Indicates how to read the post data.

[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 this 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: How to read cookies.

[CPP]
Static char *
Php_apache_sapi_read_cookies (tsrmls_d)
{
Php_struct *ctx = SG (Server_context); br> 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: Provides an interface for supplying variables to the $_server[] array.
[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-& Gt;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: Output error 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: Gets the 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);
}

This completes the SAPI definition of Apache. After that, when the user requests the Apache service with a URL, these function pointers will be useful (called) at the appropriate time.

http://www.bkjia.com/PHPjc/477971.html www.bkjia.com true http://www.bkjia.com/PHPjc/477971.html techarticle before we know the definition of SAPI, we must first define SAPI_MODULE_STRUCT this structure, tearful source:/soft/php-5.2.9/sapi/apache2handler/sapi_ APACHE2.C, you can see the definition of the structure, I am straight ...

  • 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.