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: 0 534 0.0 %
Date: 2014-04-16 Functions: 0 42 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:48:02 +0000 (7 days ago)

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