10,php_apache_sapi_header_handler:在调用PHP的header()函数时,会调用这个函数。
- 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 = '\0';
- 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;
- }
- 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 is set to the first digit of
- * the status-code: */
- if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ') {
- ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9);
- ctx->r->proto_num = 1000 + (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 only once, else each time we call it,
- configured output filters for that content type will 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;
- }
13,php_apache_sapi_read_post:表示如何读取POST数据。
- 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 that 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:如何读取cookie。
- static char *
- php_apache_sapi_read_cookies(TSRMLS_D)
- {
- php_struct *ctx = SG(server_context);
- 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;
- }
- 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->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);
- }
- }
- 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:获取请求时间。
- 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);
- }
