PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main - SAPI.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 395 533 74.1 %
Date: 2015-08-29 Functions: 33 43 76.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Original design:  Shane Caraveo <shane@caraveo.com>                  |
      16             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      17             :    |          Zeev Suraski <zeev@zend.com>                                |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include <ctype.h>
      24             : #include <sys/stat.h>
      25             : 
      26             : #include "php.h"
      27             : #include "SAPI.h"
      28             : #include "php_variables.h"
      29             : #include "php_ini.h"
      30             : #include "ext/standard/php_string.h"
      31             : #include "ext/standard/pageinfo.h"
      32             : #if (HAVE_PCRE || HAVE_BUNDLED_PCRE) && !defined(COMPILE_DL_PCRE)
      33             : #include "ext/pcre/php_pcre.h"
      34             : #endif
      35             : #ifdef ZTS
      36             : #include "TSRM.h"
      37             : #endif
      38             : #ifdef HAVE_SYS_TIME_H
      39             : #include <sys/time.h>
      40             : #elif defined(PHP_WIN32)
      41             : #include "win32/time.h"
      42             : #endif
      43             : 
      44             : #include "rfc1867.h"
      45             : 
      46             : #ifdef PHP_WIN32
      47             : #define STRCASECMP stricmp
      48             : #else
      49             : #define STRCASECMP strcasecmp
      50             : #endif
      51             : 
      52             : #include "php_content_types.h"
      53             : 
      54             : #ifdef ZTS
      55             : SAPI_API int sapi_globals_id;
      56             : #else
      57             : sapi_globals_struct sapi_globals;
      58             : #endif
      59             : 
      60       63949 : static void _type_dtor(zval *zv)
      61             : {
      62       63949 :         free(Z_PTR_P(zv));
      63       63949 : }
      64             : 
      65       21291 : static void sapi_globals_ctor(sapi_globals_struct *sapi_globals)
      66             : {
      67             : #ifdef ZTS
      68             :         ZEND_TSRMLS_CACHE_UPDATE();
      69             : #endif
      70       21291 :         memset(sapi_globals, 0, sizeof(*sapi_globals));
      71       21291 :         zend_hash_init_ex(&sapi_globals->known_post_content_types, 8, NULL, _type_dtor, 1, 0);
      72       21291 :         php_setup_sapi_content_types();
      73       21291 : }
      74             : 
      75       21329 : static void sapi_globals_dtor(sapi_globals_struct *sapi_globals)
      76             : {
      77       21329 :         zend_hash_destroy(&sapi_globals->known_post_content_types);
      78       21329 : }
      79             : 
      80             : /* True globals (no need for thread safety) */
      81             : SAPI_API sapi_module_struct sapi_module;
      82             : 
      83             : 
      84       21291 : SAPI_API void sapi_startup(sapi_module_struct *sf)
      85             : {
      86       21291 :         sf->ini_entries = NULL;
      87       21291 :         sapi_module = *sf;
      88             : 
      89             : #ifdef ZTS
      90             :         ts_allocate_id(&sapi_globals_id, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor);
      91             : # ifdef PHP_WIN32
      92             :         _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
      93             : # endif
      94             : #else
      95       21291 :         sapi_globals_ctor(&sapi_globals);
      96             : #endif
      97             : 
      98             : #ifdef PHP_WIN32
      99             :         tsrm_win32_startup();
     100             : #endif
     101             : 
     102             :         reentrancy_startup();
     103       21291 : }
     104             : 
     105       21329 : SAPI_API void sapi_shutdown(void)
     106             : {
     107             : #ifdef ZTS
     108             :         ts_free_id(sapi_globals_id);
     109             : #else
     110       21329 :         sapi_globals_dtor(&sapi_globals);
     111             : #endif
     112             : 
     113             :         reentrancy_shutdown();
     114             : 
     115             : #ifdef PHP_WIN32
     116             :         tsrm_win32_shutdown();
     117             : #endif
     118       21329 : }
     119             : 
     120             : 
     121       43787 : SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
     122             : {
     123       43787 :         efree(sapi_header->header);
     124       43787 : }
     125             : 
     126             : /* {{{ proto bool header_register_callback(mixed callback)
     127             :    call a header function */
     128           0 : PHP_FUNCTION(header_register_callback)
     129             : {
     130             :         zval *callback_func;
     131             : 
     132           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callback_func) == FAILURE) {
     133           0 :                 return;
     134             :         }
     135             : 
     136           0 :         if (!zend_is_callable(callback_func, 0, NULL)) {
     137           0 :                 RETURN_FALSE;
     138             :         }
     139             : 
     140           0 :         if (Z_TYPE(SG(callback_func)) != IS_UNDEF) {
     141           0 :                 zval_ptr_dtor(&SG(callback_func));
     142           0 :                 SG(fci_cache) = empty_fcall_info_cache;
     143             :         }
     144             : 
     145           0 :         ZVAL_COPY(&SG(callback_func), callback_func);
     146             : 
     147           0 :         RETURN_TRUE;
     148             : }
     149             : /* }}} */
     150             : 
     151           0 : static void sapi_run_header_callback(void)
     152             : {
     153             :         int   error;
     154             :         zend_fcall_info fci;
     155           0 :         char *callback_error = NULL;
     156             :         zval retval;
     157             : 
     158           0 :         if (zend_fcall_info_init(&SG(callback_func), 0, &fci, &SG(fci_cache), NULL, &callback_error) == SUCCESS) {
     159           0 :                 fci.retval = &retval;
     160             : 
     161           0 :                 error = zend_call_function(&fci, &SG(fci_cache));
     162           0 :                 if (error == FAILURE) {
     163           0 :                         goto callback_failed;
     164             :                 } else {
     165           0 :                         zval_ptr_dtor(&retval);
     166             :                 }
     167             :         } else {
     168             : callback_failed:
     169           0 :                 php_error_docref(NULL, E_WARNING, "Could not call the sapi_header_callback");
     170             :         }
     171             : 
     172           0 :         if (callback_error) {
     173           0 :                 efree(callback_error);
     174             :         }
     175           0 : }
     176             : 
     177          78 : SAPI_API void sapi_handle_post(void *arg)
     178             : {
     179          78 :         if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
     180          72 :                 SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg);
     181          72 :                 efree(SG(request_info).content_type_dup);
     182          72 :                 SG(request_info).content_type_dup = NULL;
     183             :         }
     184          78 : }
     185             : 
     186          73 : static void sapi_read_post_data(void)
     187             : {
     188             :         sapi_post_entry *post_entry;
     189          73 :         uint content_type_length = (uint)strlen(SG(request_info).content_type);
     190          73 :         char *content_type = estrndup(SG(request_info).content_type, content_type_length);
     191             :         char *p;
     192          73 :         char oldchar=0;
     193          73 :         void (*post_reader_func)(void) = NULL;
     194             : 
     195             : 
     196             :         /* dedicated implementation for increased performance:
     197             :          * - Make the content type lowercase
     198             :          * - Trim descriptive data, stay with the content-type only
     199             :          */
     200        2000 :         for (p=content_type; p<content_type+content_type_length; p++) {
     201        1927 :                 switch (*p) {
     202             :                         case ';':
     203             :                         case ',':
     204             :                         case ' ':
     205          36 :                                 content_type_length = p-content_type;
     206          36 :                                 oldchar = *p;
     207          36 :                                 *p = 0;
     208          36 :                                 break;
     209             :                         default:
     210        1891 :                                 *p = tolower(*p);
     211             :                                 break;
     212             :                 }
     213             :         }
     214             : 
     215             :         /* now try to find an appropriate POST content handler */
     216         146 :         if ((post_entry = zend_hash_str_find_ptr(&SG(known_post_content_types), content_type,
     217             :                         content_type_length)) != NULL) {
     218             :                 /* found one, register it for use */
     219          73 :                 SG(request_info).post_entry = post_entry;
     220          73 :                 post_reader_func = post_entry->post_reader;
     221             :         } else {
     222             :                 /* fallback */
     223           0 :                 SG(request_info).post_entry = NULL;
     224           0 :                 if (!sapi_module.default_post_reader) {
     225             :                         /* no default reader ? */
     226           0 :                         SG(request_info).content_type_dup = NULL;
     227           0 :                         sapi_module.sapi_error(E_WARNING, "Unsupported content type:  '%s'", content_type);
     228           0 :                         return;
     229             :                 }
     230             :         }
     231          73 :         if (oldchar) {
     232          36 :                 *(p-1) = oldchar;
     233             :         }
     234             : 
     235          73 :         SG(request_info).content_type_dup = content_type;
     236             : 
     237          73 :         if(post_reader_func) {
     238          36 :                 post_reader_func();
     239             :         }
     240             : 
     241          73 :         if(sapi_module.default_post_reader) {
     242          73 :                 sapi_module.default_post_reader();
     243             :         }
     244             : }
     245             : 
     246         404 : SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen)
     247             : {
     248             :         size_t read_bytes;
     249             : 
     250         404 :         if (!sapi_module.read_post) {
     251           0 :                 return 0;
     252             :         }
     253             : 
     254         404 :         read_bytes = sapi_module.read_post(buffer, buflen);
     255             : 
     256         404 :         if (read_bytes > 0) {
     257             :                 /* gogo */
     258          46 :                 SG(read_post_bytes) += read_bytes;
     259             :         }
     260         404 :         if (read_bytes < buflen) {
     261             :                 /* done */
     262         403 :                 SG(post_read) = 1;
     263             :         }
     264             : 
     265         404 :         return read_bytes;
     266             : }
     267             : 
     268          36 : SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
     269             : {
     270          36 :         if ((SG(post_max_size) > 0) && (SG(request_info).content_length > SG(post_max_size))) {
     271           1 :                 php_error_docref(NULL, E_WARNING, "POST Content-Length of %pd bytes exceeds the limit of %pd bytes",
     272             :                                         SG(request_info).content_length, SG(post_max_size));
     273           1 :                 return;
     274             :         }
     275             : 
     276             : 
     277          35 :         SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
     278             : 
     279          35 :         if (sapi_module.read_post) {
     280             :                 size_t read_bytes;
     281             : 
     282             :                 for (;;) {
     283             :                         char buffer[SAPI_POST_BLOCK_SIZE];
     284             : 
     285          35 :                         read_bytes = sapi_read_post_block(buffer, SAPI_POST_BLOCK_SIZE);
     286             : 
     287          35 :                         if (read_bytes > 0) {
     288          35 :                                 if (php_stream_write(SG(request_info).request_body, buffer, read_bytes) != read_bytes) {
     289             :                                         /* if parts of the stream can't be written, purge it completely */
     290           0 :                                         php_stream_truncate_set_size(SG(request_info).request_body, 0);
     291           0 :                                         php_error_docref(NULL, E_WARNING, "POST data can't be buffered; all data discarded");
     292           0 :                                         break;
     293             :                                 }
     294             :                         }
     295             : 
     296          35 :                         if ((SG(post_max_size) > 0) && (SG(read_post_bytes) > SG(post_max_size))) {
     297           0 :                                 php_error_docref(NULL, E_WARNING, "Actual POST length does not match Content-Length, and exceeds " ZEND_LONG_FMT " bytes", SG(post_max_size));
     298           0 :                                 break;
     299             :                         }
     300             : 
     301          35 :                         if (read_bytes < SAPI_POST_BLOCK_SIZE) {
     302             :                                 /* done */
     303          35 :                                 break;
     304             :                         }
     305           0 :                 }
     306          35 :                 php_stream_rewind(SG(request_info).request_body);
     307             :         }
     308             : }
     309             : 
     310             : 
     311       20936 : static inline char *get_default_content_type(uint prefix_len, uint *len)
     312             : {
     313             :         char *mimetype, *charset, *content_type;
     314             :         uint mimetype_len, charset_len;
     315             : 
     316       20936 :         if (SG(default_mimetype)) {
     317       20936 :                 mimetype = SG(default_mimetype);
     318       20936 :                 mimetype_len = (uint)strlen(SG(default_mimetype));
     319             :         } else {
     320           0 :                 mimetype = SAPI_DEFAULT_MIMETYPE;
     321           0 :                 mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1;
     322             :         }
     323       20936 :         if (SG(default_charset)) {
     324       20936 :                 charset = SG(default_charset);
     325       20936 :                 charset_len = (uint)strlen(SG(default_charset));
     326             :         } else {
     327           0 :                 charset = SAPI_DEFAULT_CHARSET;
     328           0 :                 charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1;
     329             :         }
     330             : 
     331       41862 :         if (*charset && strncasecmp(mimetype, "text/", 5) == 0) {
     332             :                 char *p;
     333             : 
     334       20926 :                 *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 + charset_len;
     335       20926 :                 content_type = (char*)emalloc(*len + 1);
     336       20926 :                 p = content_type + prefix_len;
     337       20926 :                 memcpy(p, mimetype, mimetype_len);
     338       20926 :                 p += mimetype_len;
     339       20926 :                 memcpy(p, "; charset=", sizeof("; charset=") - 1);
     340       20926 :                 p += sizeof("; charset=") - 1;
     341       20926 :                 memcpy(p, charset, charset_len + 1);
     342             :         } else {
     343          10 :                 *len = prefix_len + mimetype_len;
     344          10 :                 content_type = (char*)emalloc(*len + 1);
     345          10 :                 memcpy(content_type + prefix_len, mimetype, mimetype_len + 1);
     346             :         }
     347       20936 :         return content_type;
     348             : }
     349             : 
     350             : 
     351           0 : SAPI_API char *sapi_get_default_content_type(void)
     352             : {
     353             :         uint len;
     354             : 
     355           0 :         return get_default_content_type(0, &len);
     356             : }
     357             : 
     358             : 
     359           0 : SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header)
     360             : {
     361             :     uint len;
     362             : 
     363           0 :         default_header->header = get_default_content_type(sizeof("Content-type: ")-1, &len);
     364           0 :         default_header->header_len = len;
     365           0 :         memcpy(default_header->header, "Content-type: ", sizeof("Content-type: ") - 1);
     366           0 : }
     367             : 
     368             : /*
     369             :  * Add charset on content-type header if the MIME type starts with
     370             :  * "text/", the default_charset directive is not empty and
     371             :  * there is not already a charset option in there.
     372             :  *
     373             :  * If "mimetype" is non-NULL, it should point to a pointer allocated
     374             :  * with emalloc().  If a charset is added, the string will be
     375             :  * re-allocated and the new length is returned.  If mimetype is
     376             :  * unchanged, 0 is returned.
     377             :  *
     378             :  */
     379         194 : SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len)
     380             : {
     381             :         char *charset, *newtype;
     382             :         size_t newlen;
     383         194 :         charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
     384             : 
     385         194 :         if (*mimetype != NULL) {
     386         194 :                 if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) {
     387          12 :                         newlen = len + (sizeof(";charset=")-1) + strlen(charset);
     388          12 :                         newtype = emalloc(newlen + 1);
     389          12 :                         PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
     390          12 :                         strlcat(newtype, ";charset=", newlen + 1);
     391          12 :                         strlcat(newtype, charset, newlen + 1);
     392          12 :                         efree(*mimetype);
     393          12 :                         *mimetype = newtype;
     394          12 :                         return newlen;
     395             :                 }
     396             :         }
     397         182 :         return 0;
     398             : }
     399             : 
     400           0 : SAPI_API void sapi_activate_headers_only(void)
     401             : {
     402           0 :         if (SG(request_info).headers_read == 1)
     403           0 :                 return;
     404           0 :         SG(request_info).headers_read = 1;
     405           0 :         zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct),
     406             :                         (void (*)(void *)) sapi_free_header, 0);
     407           0 :         SG(sapi_headers).send_default_content_type = 1;
     408             : 
     409             :         /* SG(sapi_headers).http_response_code = 200; */
     410           0 :         SG(sapi_headers).http_status_line = NULL;
     411           0 :         SG(sapi_headers).mimetype = NULL;
     412           0 :         SG(read_post_bytes) = 0;
     413           0 :         SG(request_info).request_body = NULL;
     414           0 :         SG(request_info).current_user = NULL;
     415           0 :         SG(request_info).current_user_length = 0;
     416           0 :         SG(request_info).no_headers = 0;
     417           0 :         SG(request_info).post_entry = NULL;
     418           0 :         SG(global_request_time) = 0;
     419             : 
     420             :         /*
     421             :          * It's possible to override this general case in the activate() callback,
     422             :          * if necessary.
     423             :          */
     424           0 :         if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
     425           0 :                 SG(request_info).headers_only = 1;
     426             :         } else {
     427           0 :                 SG(request_info).headers_only = 0;
     428             :         }
     429           0 :         if (SG(server_context)) {
     430           0 :                 SG(request_info).cookie_data = sapi_module.read_cookies();
     431           0 :                 if (sapi_module.activate) {
     432           0 :                         sapi_module.activate();
     433             :                 }
     434             :         }
     435           0 :         if (sapi_module.input_filter_init ) {
     436           0 :                 sapi_module.input_filter_init();
     437             :         }
     438             : }
     439             : 
     440             : /*
     441             :  * Called from php_request_startup() for every request.
     442             :  */
     443             : 
     444       42539 : SAPI_API void sapi_activate(void)
     445             : {
     446       42539 :         zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
     447       42539 :         SG(sapi_headers).send_default_content_type = 1;
     448             : 
     449             :         /*
     450             :         SG(sapi_headers).http_response_code = 200;
     451             :         */
     452       42539 :         SG(sapi_headers).http_status_line = NULL;
     453       42539 :         SG(sapi_headers).mimetype = NULL;
     454       42539 :         SG(headers_sent) = 0;
     455       42539 :         SG(callback_run) = 0;
     456       42539 :         ZVAL_UNDEF(&SG(callback_func));
     457       42539 :         SG(read_post_bytes) = 0;
     458       42539 :         SG(request_info).request_body = NULL;
     459       42539 :         SG(request_info).current_user = NULL;
     460       42539 :         SG(request_info).current_user_length = 0;
     461       42539 :         SG(request_info).no_headers = 0;
     462       42539 :         SG(request_info).post_entry = NULL;
     463       42539 :         SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
     464       42539 :         SG(global_request_time) = 0;
     465       42539 :         SG(post_read) = 0;
     466             :         /* It's possible to override this general case in the activate() callback, if necessary. */
     467       42539 :         if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
     468           0 :                 SG(request_info).headers_only = 1;
     469             :         } else {
     470       42539 :                 SG(request_info).headers_only = 0;
     471             :         }
     472       42539 :         SG(rfc1867_uploaded_files) = NULL;
     473             : 
     474             :         /* Handle request method */
     475       42539 :         if (SG(server_context)) {
     476        1301 :                 if (PG(enable_post_data_reading)
     477             :                 &&      SG(request_info).content_type
     478         400 :                 &&  SG(request_info).request_method
     479         422 :                 && !strcmp(SG(request_info).request_method, "POST")) {
     480             :                         /* HTTP POST may contain form data to be processed into variables
     481             :                          * depending on given content type */
     482          73 :                         sapi_read_post_data();
     483             :                 } else {
     484         333 :                         SG(request_info).content_type_dup = NULL;
     485             :                 }
     486             : 
     487             :                 /* Cookies */
     488         406 :                 SG(request_info).cookie_data = sapi_module.read_cookies();
     489             : 
     490         406 :                 if (sapi_module.activate) {
     491         406 :                         sapi_module.activate();
     492             :                 }
     493             :         }
     494       42539 :         if (sapi_module.input_filter_init) {
     495       21248 :                 sapi_module.input_filter_init();
     496             :         }
     497       42539 : }
     498             : 
     499             : 
     500       63737 : static void sapi_send_headers_free(void)
     501             : {
     502       63737 :         if (SG(sapi_headers).http_status_line) {
     503         157 :                 efree(SG(sapi_headers).http_status_line);
     504         157 :                 SG(sapi_headers).http_status_line = NULL;
     505             :         }
     506       63737 : }
     507             : 
     508       42618 : SAPI_API void sapi_deactivate(void)
     509             : {
     510       42618 :         zend_llist_destroy(&SG(sapi_headers).headers);
     511       42618 :         if (SG(request_info).request_body) {
     512          42 :                 SG(request_info).request_body = NULL;
     513       42576 :         } else if (SG(server_context)) {
     514         362 :                 if (!SG(post_read)) {
     515             :                         /* make sure we've consumed all request input data */
     516             :                         char dummy[SAPI_POST_BLOCK_SIZE];
     517             :                         size_t read_bytes;
     518             : 
     519             :                         do {
     520         362 :                                 read_bytes = sapi_read_post_block(dummy, SAPI_POST_BLOCK_SIZE);
     521         362 :                         } while (SAPI_POST_BLOCK_SIZE == read_bytes);
     522             :                 }
     523             :         }
     524       42618 :         if (SG(request_info).auth_user) {
     525           0 :                 efree(SG(request_info).auth_user);
     526             :         }
     527       42618 :         if (SG(request_info).auth_password) {
     528           0 :                 efree(SG(request_info).auth_password);
     529             :         }
     530       42618 :         if (SG(request_info).auth_digest) {
     531           0 :                 efree(SG(request_info).auth_digest);
     532             :         }
     533       42618 :         if (SG(request_info).content_type_dup) {
     534           1 :                 efree(SG(request_info).content_type_dup);
     535             :         }
     536       42618 :         if (SG(request_info).current_user) {
     537           9 :                 efree(SG(request_info).current_user);
     538             :         }
     539       42618 :         if (sapi_module.deactivate) {
     540       42618 :                 sapi_module.deactivate();
     541             :         }
     542       42618 :         if (SG(rfc1867_uploaded_files)) {
     543          34 :                 destroy_uploaded_files_hash();
     544             :         }
     545       42618 :         if (SG(sapi_headers).mimetype) {
     546       21120 :                 efree(SG(sapi_headers).mimetype);
     547       21120 :                 SG(sapi_headers).mimetype = NULL;
     548             :         }
     549       42618 :         sapi_send_headers_free();
     550       42618 :         SG(sapi_started) = 0;
     551       42618 :         SG(headers_sent) = 0;
     552       42618 :         SG(callback_run) = 0;
     553       42618 :         zval_ptr_dtor(&SG(callback_func));
     554       42618 :         SG(request_info).headers_read = 0;
     555       42618 :         SG(global_request_time) = 0;
     556       42618 : }
     557             : 
     558             : 
     559       21291 : SAPI_API void sapi_initialize_empty_request(void)
     560             : {
     561       21291 :         SG(server_context) = NULL;
     562       21291 :         SG(request_info).request_method = NULL;
     563       21291 :         SG(request_info).auth_digest = SG(request_info).auth_user = SG(request_info).auth_password = NULL;
     564       21291 :         SG(request_info).content_type_dup = NULL;
     565       21291 : }
     566             : 
     567             : 
     568         158 : static int sapi_extract_response_code(const char *header_line)
     569             : {
     570         158 :         int code = 200;
     571             :         const char *ptr;
     572             : 
     573        1422 :         for (ptr = header_line; *ptr; ptr++) {
     574        1422 :                 if (*ptr == ' ' && *(ptr + 1) != ' ') {
     575         158 :                         code = atoi(ptr + 1);
     576         158 :                         break;
     577             :                 }
     578             :         }
     579             : 
     580         158 :         return code;
     581             : }
     582             : 
     583             : 
     584         165 : static void sapi_update_response_code(int ncode)
     585             : {
     586             :         /* if the status code did not change, we do not want
     587             :            to change the status line, and no need to change the code */
     588         165 :         if (SG(sapi_headers).http_response_code == ncode) {
     589           2 :                 return;
     590             :         }
     591             : 
     592         163 :         if (SG(sapi_headers).http_status_line) {
     593           1 :                 efree(SG(sapi_headers).http_status_line);
     594           1 :                 SG(sapi_headers).http_status_line = NULL;
     595             :         }
     596         163 :         SG(sapi_headers).http_response_code = ncode;
     597             : }
     598             : 
     599             : /*
     600             :  * since zend_llist_del_element only remove one matched item once,
     601             :  * we should remove them by ourself
     602             :  */
     603         532 : static void sapi_remove_header(zend_llist *l, char *name, size_t len) {
     604             :         sapi_header_struct *header;
     605             :         zend_llist_element *next;
     606         532 :         zend_llist_element *current=l->head;
     607             : 
     608        1267 :         while (current) {
     609         203 :                 header = (sapi_header_struct *)(current->data);
     610         203 :                 next = current->next;
     611         237 :                 if (header->header_len > len && header->header[len] == ':'
     612          34 :                                 && !strncasecmp(header->header, name, len)) {
     613           4 :                         if (current->prev) {
     614           4 :                                 current->prev->next = next;
     615             :                         } else {
     616           0 :                                 l->head = next;
     617             :                         }
     618           4 :                         if (next) {
     619           2 :                                 next->prev = current->prev;
     620             :                         } else {
     621           2 :                                 l->tail = current->prev;
     622             :                         }
     623           4 :                         sapi_free_header(header);
     624           4 :                         efree(current);
     625           4 :                         --l->count;
     626             :                 }
     627         203 :                 current = next;
     628             :         }
     629         532 : }
     630             : 
     631       23423 : SAPI_API int sapi_add_header_ex(char *header_line, size_t header_line_len, zend_bool duplicate, zend_bool replace)
     632             : {
     633       23423 :         sapi_header_line ctr = {0};
     634             :         int r;
     635             : 
     636       23423 :         ctr.line = header_line;
     637       23423 :         ctr.line_len = header_line_len;
     638             : 
     639       23423 :         r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD,
     640             :                         &ctr);
     641             : 
     642       23423 :         if (!duplicate)
     643         287 :                 efree(header_line);
     644             : 
     645       23423 :         return r;
     646             : }
     647             : 
     648       43793 : static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_header)
     649             : {
     650       87586 :         if (!sapi_module.header_handler ||
     651       43026 :                 (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op, &SG(sapi_headers)))) {
     652         767 :                 if (op == SAPI_HEADER_REPLACE) {
     653         529 :                         char *colon_offset = strchr(sapi_header->header, ':');
     654             : 
     655         529 :                         if (colon_offset) {
     656         529 :                                 char sav = *colon_offset;
     657             : 
     658         529 :                                 *colon_offset = 0;
     659         529 :                         sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, (int)strlen(sapi_header->header));
     660         529 :                                 *colon_offset = sav;
     661             :                         }
     662             :                 }
     663         767 :                 zend_llist_add_element(&SG(sapi_headers).headers, (void *) sapi_header);
     664             :         } else {
     665       43026 :                 sapi_free_header(sapi_header);
     666             :         }
     667       43793 : }
     668             : 
     669       23585 : SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
     670             : {
     671             :         sapi_header_struct sapi_header;
     672             :         char *colon_offset;
     673             :         char *header_line;
     674             :         size_t header_line_len;
     675             :         int http_response_code;
     676             : 
     677       23585 :         if (SG(headers_sent) && !SG(request_info).no_headers) {
     678         557 :                 const char *output_start_filename = php_output_get_start_filename();
     679         557 :                 int output_start_lineno = php_output_get_start_lineno();
     680             : 
     681         557 :                 if (output_start_filename) {
     682         556 :                         sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)",
     683             :                                 output_start_filename, output_start_lineno);
     684             :                 } else {
     685           1 :                         sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent");
     686             :                 }
     687         557 :                 return FAILURE;
     688             :         }
     689             : 
     690       23028 :         switch (op) {
     691             :                 case SAPI_HEADER_SET_STATUS:
     692           0 :                         sapi_update_response_code((int)(zend_intptr_t) arg);
     693           0 :                         return SUCCESS;
     694             : 
     695             :                 case SAPI_HEADER_ADD:
     696             :                 case SAPI_HEADER_REPLACE:
     697             :                 case SAPI_HEADER_DELETE: {
     698       23026 :                                 sapi_header_line *p = arg;
     699             : 
     700       23026 :                                 if (!p->line || !p->line_len) {
     701           1 :                                         return FAILURE;
     702             :                                 }
     703       23025 :                                 header_line = p->line;
     704       23025 :                                 header_line_len = p->line_len;
     705       23025 :                                 http_response_code = p->response_code;
     706       23025 :                                 break;
     707             :                         }
     708             : 
     709             :                 case SAPI_HEADER_DELETE_ALL:
     710           2 :                         if (sapi_module.header_handler) {
     711           0 :                                 sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
     712             :                         }
     713           2 :                         zend_llist_clean(&SG(sapi_headers).headers);
     714           2 :                         return SUCCESS;
     715             : 
     716             :                 default:
     717           0 :                         return FAILURE;
     718             :         }
     719             : 
     720       23025 :         header_line = estrndup(header_line, header_line_len);
     721             : 
     722             :         /* cut off trailing spaces, linefeeds and carriage-returns */
     723       23025 :         if (header_line_len && isspace(header_line[header_line_len-1])) {
     724             :                 do {
     725           7 :                         header_line_len--;
     726           7 :                 } while(header_line_len && isspace(header_line[header_line_len-1]));
     727           3 :                 header_line[header_line_len]='\0';
     728             :         }
     729             : 
     730       23025 :         if (op == SAPI_HEADER_DELETE) {
     731           6 :                 if (strchr(header_line, ':')) {
     732           3 :                         efree(header_line);
     733           3 :                         sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
     734           3 :                         return FAILURE;
     735             :                 }
     736           3 :                 if (sapi_module.header_handler) {
     737           0 :                         sapi_header.header = header_line;
     738           0 :                         sapi_header.header_len = header_line_len;
     739           0 :                         sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
     740             :                 }
     741           3 :                 sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len);
     742           3 :                 efree(header_line);
     743           3 :                 return SUCCESS;
     744             :         } else {
     745             :                 /* new line/NUL character safety check */
     746             :                 uint i;
     747      660248 :                 for (i = 0; i < header_line_len; i++) {
     748             :                         /* RFC 7230 ch. 3.2.4 deprecates folding support */
     749      637233 :                         if (header_line[i] == '\n' || header_line[i] == '\r') {
     750           3 :                                 efree(header_line);
     751           3 :                                 sapi_module.sapi_error(E_WARNING, "Header may not contain "
     752             :                                                 "more than a single header, new line detected");
     753           3 :                                 return FAILURE;
     754             :                         }
     755      637230 :                         if (header_line[i] == '\0') {
     756           1 :                                 efree(header_line);
     757           1 :                                 sapi_module.sapi_error(E_WARNING, "Header may not contain NUL bytes");
     758           1 :                                 return FAILURE;
     759             :                         }
     760             :                 }
     761             :         }
     762             : 
     763       23015 :         sapi_header.header = header_line;
     764       23015 :         sapi_header.header_len = header_line_len;
     765             : 
     766             :         /* Check the header for a few cases that we have special support for in SAPI */
     767       46030 :         if (header_line_len>=5
     768       23015 :                 && !strncasecmp(header_line, "HTTP/", 5)) {
     769             :                 /* filter out the response code */
     770         158 :                 sapi_update_response_code(sapi_extract_response_code(header_line));
     771             :                 /* sapi_update_response_code doesn't free the status line if the code didn't change */
     772         158 :                 if (SG(sapi_headers).http_status_line) {
     773           0 :                         efree(SG(sapi_headers).http_status_line);
     774             :                 }
     775         158 :                 SG(sapi_headers).http_status_line = header_line;
     776         158 :                 return SUCCESS;
     777             :         } else {
     778       22857 :                 colon_offset = strchr(header_line, ':');
     779       22857 :                 if (colon_offset) {
     780       22856 :                         *colon_offset = 0;
     781       22856 :                         if (!STRCASECMP(header_line, "Content-Type")) {
     782         194 :                                 char *ptr = colon_offset+1, *mimetype = NULL, *newheader;
     783         194 :                                 size_t len = header_line_len - (ptr - header_line), newlen;
     784         580 :                                 while (*ptr == ' ') {
     785         192 :                                         ptr++;
     786         192 :                                         len--;
     787             :                                 }
     788             : 
     789             :                                 /* Disable possible output compression for images */
     790         194 :                                 if (!strncmp(ptr, "image/", sizeof("image/")-1)) {
     791           4 :                                         zend_string *key = zend_string_init("zlib.output_compression", sizeof("zlib.output_compression")-1, 0);
     792           4 :                                         zend_alter_ini_entry_chars(key, "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
     793             :                                         zend_string_release(key);
     794             :                                 }
     795             : 
     796         194 :                                 mimetype = estrdup(ptr);
     797         194 :                                 newlen = sapi_apply_default_charset(&mimetype, len);
     798         194 :                                 if (!SG(sapi_headers).mimetype){
     799         183 :                                         SG(sapi_headers).mimetype = estrdup(mimetype);
     800             :                                 }
     801             : 
     802         194 :                                 if (newlen != 0) {
     803          12 :                                         newlen += sizeof("Content-type: ");
     804          12 :                                         newheader = emalloc(newlen);
     805          12 :                                         PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
     806          12 :                                         strlcat(newheader, mimetype, newlen);
     807          12 :                                         sapi_header.header = newheader;
     808          12 :                                         sapi_header.header_len = (uint)(newlen - 1);
     809          12 :                                         efree(header_line);
     810             :                                 }
     811         194 :                                 efree(mimetype);
     812         194 :                                 SG(sapi_headers).send_default_content_type = 0;
     813       22662 :                         } else if (!STRCASECMP(header_line, "Content-Length")) {
     814             :                                 /* Script is setting Content-length. The script cannot reasonably
     815             :                                  * know the size of the message body after compression, so it's best
     816             :                                  * do disable compression altogether. This contributes to making scripts
     817             :                                  * portable between setups that have and don't have zlib compression
     818             :                                  * enabled globally. See req #44164 */
     819         246 :                                 zend_string *key = zend_string_init("zlib.output_compression", sizeof("zlib.output_compression")-1, 0);
     820         246 :                                 zend_alter_ini_entry_chars(key,
     821             :                                         "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
     822             :                                 zend_string_release(key);
     823       22416 :                         } else if (!STRCASECMP(header_line, "Location")) {
     824          53 :                                 if ((SG(sapi_headers).http_response_code < 300 ||
     825          22 :                                         SG(sapi_headers).http_response_code > 399) &&
     826           5 :                                         SG(sapi_headers).http_response_code != 201) {
     827             :                                         /* Return a Found Redirect if one is not already specified */
     828           4 :                                         if (http_response_code) { /* user specified redirect code */
     829           2 :                                                 sapi_update_response_code(http_response_code);
     830           2 :                                         } else if (SG(request_info).proto_num > 1000 &&
     831           0 :                                            SG(request_info).request_method &&
     832           0 :                                            strcmp(SG(request_info).request_method, "HEAD") &&
     833           0 :                                            strcmp(SG(request_info).request_method, "GET")) {
     834           0 :                                                 sapi_update_response_code(303);
     835             :                                         } else {
     836           2 :                                                 sapi_update_response_code(302);
     837             :                                         }
     838             :                                 }
     839       22390 :                         } else if (!STRCASECMP(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
     840           0 :                                 sapi_update_response_code(401); /* authentication-required */
     841             :                         }
     842       22856 :                         if (sapi_header.header==header_line) {
     843       22844 :                                 *colon_offset = ':';
     844             :                         }
     845             :                 }
     846             :         }
     847       22857 :         if (http_response_code) {
     848           3 :                 sapi_update_response_code(http_response_code);
     849             :         }
     850       22857 :         sapi_header_add_op(op, &sapi_header);
     851       22857 :         return SUCCESS;
     852             : }
     853             : 
     854             : 
     855       21119 : SAPI_API int sapi_send_headers(void)
     856             : {
     857             :         int retval;
     858       21119 :         int ret = FAILURE;
     859             : 
     860       21119 :         if (SG(headers_sent) || SG(request_info).no_headers || SG(callback_run)) {
     861           0 :                 return SUCCESS;
     862             :         }
     863             : 
     864             :         /* Success-oriented.  We set headers_sent to 1 here to avoid an infinite loop
     865             :          * in case of an error situation.
     866             :          */
     867       21119 :         if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) {
     868             :                 sapi_header_struct default_header;
     869             :             uint len;
     870             : 
     871       20936 :                 SG(sapi_headers).mimetype = get_default_content_type(0, &len);
     872       20936 :                 default_header.header_len = sizeof("Content-type: ") - 1 + len;
     873       20936 :                 default_header.header = emalloc(default_header.header_len + 1);
     874       20936 :                 memcpy(default_header.header, "Content-type: ", sizeof("Content-type: ") - 1);
     875       20936 :                 memcpy(default_header.header + sizeof("Content-type: ") - 1, SG(sapi_headers).mimetype, len + 1);
     876       20936 :                 sapi_header_add_op(SAPI_HEADER_ADD, &default_header);
     877       20936 :                 SG(sapi_headers).send_default_content_type = 0;
     878             :         }
     879             : 
     880       21119 :         if (Z_TYPE(SG(callback_func)) != IS_UNDEF && !SG(callback_run)) {
     881           0 :                 SG(callback_run) = 1;
     882           0 :                 sapi_run_header_callback();
     883             :         }
     884             : 
     885       21119 :         SG(headers_sent) = 1;
     886             : 
     887       21119 :         if (sapi_module.send_headers) {
     888       21119 :                 retval = sapi_module.send_headers(&SG(sapi_headers));
     889             :         } else {
     890           0 :                 retval = SAPI_HEADER_DO_SEND;
     891             :         }
     892             : 
     893       21119 :         switch (retval) {
     894             :                 case SAPI_HEADER_SENT_SUCCESSFULLY:
     895       21119 :                         ret = SUCCESS;
     896       21119 :                         break;
     897             :                 case SAPI_HEADER_DO_SEND: {
     898             :                                 sapi_header_struct http_status_line;
     899             :                                 char buf[255];
     900             : 
     901           0 :                                 if (SG(sapi_headers).http_status_line) {
     902           0 :                                         http_status_line.header = SG(sapi_headers).http_status_line;
     903           0 :                                         http_status_line.header_len = (uint)strlen(SG(sapi_headers).http_status_line);
     904             :                                 } else {
     905           0 :                                         http_status_line.header = buf;
     906           0 :                                         http_status_line.header_len = slprintf(buf, sizeof(buf), "HTTP/1.0 %d X", SG(sapi_headers).http_response_code);
     907             :                                 }
     908           0 :                                 sapi_module.send_header(&http_status_line, SG(server_context));
     909             :                         }
     910           0 :                         zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context));
     911           0 :                         if(SG(sapi_headers).send_default_content_type) {
     912             :                                 sapi_header_struct default_header;
     913             : 
     914           0 :                                 sapi_get_default_content_type_header(&default_header);
     915           0 :                                 sapi_module.send_header(&default_header, SG(server_context));
     916           0 :                                 sapi_free_header(&default_header);
     917             :                         }
     918           0 :                         sapi_module.send_header(NULL, SG(server_context));
     919           0 :                         ret = SUCCESS;
     920           0 :                         break;
     921             :                 case SAPI_HEADER_SEND_FAILED:
     922           0 :                         SG(headers_sent) = 0;
     923           0 :                         ret = FAILURE;
     924             :                         break;
     925             :         }
     926             : 
     927       21119 :         sapi_send_headers_free();
     928             : 
     929       21119 :         return ret;
     930             : }
     931             : 
     932             : 
     933       42596 : SAPI_API int sapi_register_post_entries(sapi_post_entry *post_entries)
     934             : {
     935       42596 :         sapi_post_entry *p=post_entries;
     936             : 
     937      149065 :         while (p->content_type) {
     938       85178 :                 if (sapi_register_post_entry(p) == FAILURE) {
     939       21305 :                         return FAILURE;
     940             :                 }
     941       63873 :                 p++;
     942             :         }
     943       21291 :         return SUCCESS;
     944             : }
     945             : 
     946             : 
     947       85178 : SAPI_API int sapi_register_post_entry(sapi_post_entry *post_entry)
     948             : {
     949       85178 :         if (SG(sapi_started) && EG(current_execute_data)) {
     950           0 :                 return FAILURE;
     951             :         }
     952      170356 :         return zend_hash_str_add_mem(&SG(known_post_content_types),
     953      170356 :                         post_entry->content_type, post_entry->content_type_len,
     954             :                         (void *) post_entry, sizeof(sapi_post_entry)) ? SUCCESS : FAILURE;
     955             : }
     956             : 
     957       21291 : SAPI_API void sapi_unregister_post_entry(sapi_post_entry *post_entry)
     958             : {
     959       21291 :         if (SG(sapi_started) && EG(current_execute_data)) {
     960           0 :                 return;
     961             :         }
     962       21291 :         zend_hash_str_del(&SG(known_post_content_types), post_entry->content_type,
     963       21291 :                         post_entry->content_type_len);
     964             : }
     965             : 
     966             : 
     967       21291 : SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void))
     968             : {
     969       21291 :         if (SG(sapi_started) && EG(current_execute_data)) {
     970           0 :                 return FAILURE;
     971             :         }
     972       21291 :         sapi_module.default_post_reader = default_post_reader;
     973       21291 :         return SUCCESS;
     974             : }
     975             : 
     976             : 
     977       42582 : SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray))
     978             : {
     979       42582 :         if (SG(sapi_started) && EG(current_execute_data)) {
     980           0 :                 return FAILURE;
     981             :         }
     982       42582 :         sapi_module.treat_data = treat_data;
     983       42582 :         return SUCCESS;
     984             : }
     985             : 
     986       42582 : SAPI_API int sapi_register_input_filter(unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len), unsigned int (*input_filter_init)(void))
     987             : {
     988       42582 :         if (SG(sapi_started) && EG(current_execute_data)) {
     989           0 :                 return FAILURE;
     990             :         }
     991       42582 :         sapi_module.input_filter = input_filter;
     992       42582 :         sapi_module.input_filter_init = input_filter_init;
     993       42582 :         return SUCCESS;
     994             : }
     995             : 
     996      946701 : SAPI_API int sapi_flush(void)
     997             : {
     998      946701 :         if (sapi_module.flush) {
     999      946701 :                 sapi_module.flush(SG(server_context));
    1000      946701 :                 return SUCCESS;
    1001             :         } else {
    1002           0 :                 return FAILURE;
    1003             :         }
    1004             : }
    1005             : 
    1006          19 : SAPI_API zend_stat_t *sapi_get_stat(void)
    1007             : {
    1008          19 :         if (sapi_module.get_stat) {
    1009           0 :                 return sapi_module.get_stat();
    1010             :         } else {
    1011          19 :                 if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) {
    1012           0 :                         return NULL;
    1013             :                 }
    1014          19 :                 return &SG(global_stat);
    1015             :         }
    1016             : }
    1017             : 
    1018       26690 : SAPI_API char *sapi_getenv(char *name, size_t name_len)
    1019             : {
    1020       26690 :         if (sapi_module.getenv) {
    1021           8 :                 char *value, *tmp = sapi_module.getenv(name, name_len);
    1022           8 :                 if (tmp) {
    1023           8 :                         value = estrdup(tmp);
    1024             :                 } else {
    1025           0 :                         return NULL;
    1026             :                 }
    1027           8 :                 if (sapi_module.input_filter) {
    1028           8 :                         sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL);
    1029             :                 }
    1030           8 :                 return value;
    1031             :         }
    1032       26682 :         return NULL;
    1033             : }
    1034             : 
    1035           0 : SAPI_API int sapi_get_fd(int *fd)
    1036             : {
    1037           0 :         if (sapi_module.get_fd) {
    1038           0 :                 return sapi_module.get_fd(fd);
    1039             :         } else {
    1040           0 :                 return FAILURE;
    1041             :         }
    1042             : }
    1043             : 
    1044           0 : SAPI_API int sapi_force_http_10(void)
    1045             : {
    1046           0 :         if (sapi_module.force_http_10) {
    1047           0 :                 return sapi_module.force_http_10();
    1048             :         } else {
    1049           0 :                 return FAILURE;
    1050             :         }
    1051             : }
    1052             : 
    1053             : 
    1054           0 : SAPI_API int sapi_get_target_uid(uid_t *obj)
    1055             : {
    1056           0 :         if (sapi_module.get_target_uid) {
    1057           0 :                 return sapi_module.get_target_uid(obj);
    1058             :         } else {
    1059           0 :                 return FAILURE;
    1060             :         }
    1061             : }
    1062             : 
    1063           0 : SAPI_API int sapi_get_target_gid(gid_t *obj)
    1064             : {
    1065           0 :         if (sapi_module.get_target_gid) {
    1066           0 :                 return sapi_module.get_target_gid(obj);
    1067             :         } else {
    1068           0 :                 return FAILURE;
    1069             :         }
    1070             : }
    1071             : 
    1072       21334 : SAPI_API double sapi_get_request_time(void)
    1073             : {
    1074       21334 :         if(SG(global_request_time)) return SG(global_request_time);
    1075             : 
    1076       21334 :         if (sapi_module.get_request_time && SG(server_context)) {
    1077           0 :                 SG(global_request_time) = sapi_module.get_request_time();
    1078             :         } else {
    1079       21334 :                 struct timeval tp = {0};
    1080       21334 :                 if (!gettimeofday(&tp, NULL)) {
    1081       21334 :                         SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00);
    1082             :                 } else {
    1083           0 :                         SG(global_request_time) = (double)time(0);
    1084             :                 }
    1085             :         }
    1086       21334 :         return SG(global_request_time);
    1087             : }
    1088             : 
    1089           0 : SAPI_API void sapi_terminate_process(void) {
    1090           0 :         if (sapi_module.terminate_process) {
    1091           0 :                 sapi_module.terminate_process();
    1092             :         }
    1093           0 : }
    1094             : 
    1095             : /*
    1096             :  * Local variables:
    1097             :  * tab-width: 4
    1098             :  * c-basic-offset: 4
    1099             :  * End:
    1100             :  * vim600: sw=4 ts=4 fdm=marker
    1101             :  * vim<600: sw=4 ts=4
    1102             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:29 +0000 (23 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.