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

LTP GCOV extension - code coverage report
Current view: directory - standard - head.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 115
Code covered: 20.0 % Executed lines: 23
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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                 :    | Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                        |
      16                 :    +----------------------------------------------------------------------+
      17                 :  */
      18                 : /* $Id: head.c 286508 2009-07-29 13:44:16Z iliaa $ */
      19                 : 
      20                 : #include <stdio.h>
      21                 : #include "php.h"
      22                 : #include "ext/standard/php_standard.h"
      23                 : #include "ext/date/php_date.h"
      24                 : #include "SAPI.h"
      25                 : #include "php_main.h"
      26                 : #include "head.h"
      27                 : #ifdef TM_IN_SYS_TIME
      28                 : #include <sys/time.h>
      29                 : #else
      30                 : #include <time.h>
      31                 : #endif
      32                 : 
      33                 : #include "php_globals.h"
      34                 : #include "safe_mode.h"
      35                 : 
      36                 : 
      37                 : /* Implementation of the language Header() function */
      38                 : /* {{{ proto void header(string header [, bool replace, [int http_response_code]])
      39                 :    Sends a raw HTTP header */
      40                 : PHP_FUNCTION(header)
      41               9 : {
      42               9 :         zend_bool rep = 1;
      43               9 :         sapi_header_line ctr = {0};
      44                 :         
      45               9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &ctr.line,
      46                 :                                 &ctr.line_len, &rep, &ctr.response_code) == FAILURE)
      47               0 :                 return;
      48                 :         
      49               9 :         sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
      50                 : }
      51                 : /* }}} */
      52                 : 
      53                 : PHPAPI int php_header(TSRMLS_D)
      54            9153 : {
      55            9153 :         if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) {
      56               0 :                 return 0; /* don't allow output */
      57                 :         } else {
      58            9153 :                 return 1; /* allow output */
      59                 :         }
      60                 : }
      61                 : 
      62                 : 
      63                 : PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC)
      64               0 : {
      65               0 :         char *cookie, *encoded_value = NULL;
      66               0 :         int len=sizeof("Set-Cookie: ");
      67                 :         char *dt;
      68               0 :         sapi_header_line ctr = {0};
      69                 :         int result;
      70                 :         
      71               0 :         if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) {   /* man isspace for \013 and \014 */
      72               0 :                 zend_error( E_WARNING, "Cookie names can not contain any of the following '=,; \\t\\r\\n\\013\\014'" );
      73               0 :                 return FAILURE;
      74                 :         }
      75                 : 
      76               0 :         if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
      77               0 :                 zend_error( E_WARNING, "Cookie values can not contain any of the following ',; \\t\\r\\n\\013\\014'" );
      78               0 :                 return FAILURE;
      79                 :         }
      80                 : 
      81               0 :         len += name_len;
      82               0 :         if (value && url_encode) {
      83                 :                 int encoded_value_len;
      84                 : 
      85               0 :                 encoded_value = php_url_encode(value, value_len, &encoded_value_len);
      86               0 :                 len += encoded_value_len;
      87               0 :         } else if ( value ) {
      88               0 :                 encoded_value = estrdup(value);
      89               0 :                 len += value_len;
      90                 :         }
      91               0 :         if (path) {
      92               0 :                 len += path_len;
      93                 :         }
      94               0 :         if (domain) {
      95               0 :                 len += domain_len;
      96                 :         }
      97                 : 
      98               0 :         cookie = emalloc(len + 100);
      99                 : 
     100               0 :         if (value && value_len == 0) {
     101                 :                 /* 
     102                 :                  * MSIE doesn't delete a cookie when you set it to a null value
     103                 :                  * so in order to force cookies to be deleted, even on MSIE, we
     104                 :                  * pick an expiry date 1 year and 1 second in the past
     105                 :                  */
     106               0 :                 time_t t = time(NULL) - 31536001;
     107               0 :                 dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC);
     108               0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s", name, dt);
     109               0 :                 efree(dt);
     110                 :         } else {
     111               0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
     112               0 :                 if (expires > 0) {
     113                 :                         char *p;
     114               0 :                         strlcat(cookie, "; expires=", len + 100);
     115               0 :                         dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC);
     116                 :                         /* check to make sure that the year does not exceed 4 digits in length */
     117               0 :                         p = zend_memrchr(dt, '-', strlen(dt));
     118               0 :                         if (*(p + 5) != ' ') {
     119               0 :                                 efree(dt);
     120               0 :                                 efree(cookie);
     121               0 :                                 efree(encoded_value);
     122               0 :                                 zend_error(E_WARNING, "Expiry date cannot have a year greater then 9999");
     123               0 :                                 return FAILURE;
     124                 :                         }
     125               0 :                         strlcat(cookie, dt, len + 100);
     126               0 :                         efree(dt);
     127                 :                 }
     128                 :         }
     129                 : 
     130               0 :         if (encoded_value) {
     131               0 :                 efree(encoded_value);
     132                 :         }
     133                 : 
     134               0 :         if (path && path_len > 0) {
     135               0 :                 strlcat(cookie, "; path=", len + 100);
     136               0 :                 strlcat(cookie, path, len + 100);
     137                 :         }
     138               0 :         if (domain && domain_len > 0) {
     139               0 :                 strlcat(cookie, "; domain=", len + 100);
     140               0 :                 strlcat(cookie, domain, len + 100);
     141                 :         }
     142               0 :         if (secure) {
     143               0 :                 strlcat(cookie, "; secure", len + 100);
     144                 :         }
     145               0 :         if (httponly) {
     146               0 :                 strlcat(cookie, "; httponly", len + 100);
     147                 :         }
     148                 : 
     149               0 :         ctr.line = cookie;
     150               0 :         ctr.line_len = strlen(cookie);
     151                 : 
     152               0 :         result = sapi_header_op(SAPI_HEADER_ADD, &ctr TSRMLS_CC);
     153               0 :         efree(cookie);
     154               0 :         return result;
     155                 : }
     156                 : 
     157                 : 
     158                 : /* php_set_cookie(name, value, expires, path, domain, secure) */
     159                 : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     160                 :    Send a cookie */
     161                 : PHP_FUNCTION(setcookie)
     162               0 : {
     163               0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     164               0 :         long expires = 0;
     165               0 :         zend_bool secure = 0, httponly = 0;
     166               0 :         int name_len, value_len = 0, path_len = 0, domain_len = 0;
     167                 : 
     168               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     169                 :                                                           &name_len, &value, &value_len, &expires, &path,
     170                 :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     171               0 :                 return;
     172                 :         }
     173                 : 
     174               0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
     175               0 :                 RETVAL_TRUE;
     176                 :         } else {
     177               0 :                 RETVAL_FALSE;
     178                 :         }
     179                 : }
     180                 : /* }}} */
     181                 : 
     182                 : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     183                 :    Send a cookie with no url encoding of the value */
     184                 : PHP_FUNCTION(setrawcookie)
     185               0 : {
     186               0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     187               0 :         long expires = 0;
     188               0 :         zend_bool secure = 0, httponly = 0;
     189               0 :         int name_len, value_len = 0, path_len = 0, domain_len = 0;
     190                 : 
     191               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     192                 :                                                           &name_len, &value, &value_len, &expires, &path,
     193                 :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     194               0 :                 return;
     195                 :         }
     196                 : 
     197               0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
     198               0 :                 RETVAL_TRUE;
     199                 :         } else {
     200               0 :                 RETVAL_FALSE;
     201                 :         }
     202                 : }
     203                 : /* }}} */
     204                 : 
     205                 : 
     206                 : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
     207                 :    Returns true if headers have already been sent, false otherwise */
     208                 : PHP_FUNCTION(headers_sent)
     209               3 : {
     210                 :         zval *arg1, *arg2;
     211               3 :         char *file="";
     212               3 :         int line=0;
     213                 : 
     214               3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE)
     215               0 :                 return;
     216                 : 
     217               3 :         if (SG(headers_sent)) {
     218               2 :                 line = php_get_output_start_lineno(TSRMLS_C);
     219               2 :                 file = php_get_output_start_filename(TSRMLS_C);
     220                 :         }
     221                 : 
     222               3 :         switch(ZEND_NUM_ARGS()) {
     223                 :         case 2:
     224               0 :                 zval_dtor(arg2);
     225               0 :                 ZVAL_LONG(arg2, line);
     226                 :         case 1:
     227               0 :                 zval_dtor(arg1);
     228               0 :                 if (file) { 
     229               0 :                         ZVAL_STRING(arg1, file, 1);
     230                 :                 } else {
     231               0 :                         ZVAL_STRING(arg1, "", 1);
     232                 :                 }       
     233                 :                 break;
     234                 :         }
     235                 : 
     236               3 :         if (SG(headers_sent)) {
     237               2 :                 RETURN_TRUE;
     238                 :         } else {
     239               1 :                 RETURN_FALSE;
     240                 :         }
     241                 : }
     242                 : /* }}} */
     243                 : 
     244                 : /* {{{ php_head_apply_header_list_to_hash
     245                 :    Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
     246                 : static void php_head_apply_header_list_to_hash(void *data, void *arg TSRMLS_DC)
     247               0 : {
     248               0 :         sapi_header_struct *sapi_header = (sapi_header_struct *)data;
     249                 : 
     250               0 :         if (arg && sapi_header) {
     251               0 :                 add_next_index_string((zval *)arg, (char *)(sapi_header->header), 1);
     252                 :         }
     253               0 : }
     254                 : 
     255                 : /* {{{ proto array headers_list(void)
     256                 :    Return list of headers to be sent / already sent */
     257                 : PHP_FUNCTION(headers_list)
     258               3 : {
     259               3 :         if (ZEND_NUM_ARGS() > 0) {
     260               0 :                 WRONG_PARAM_COUNT;
     261                 :         }
     262                 : 
     263                 :         if (!&SG(sapi_headers).headers) {
     264                 :                 RETURN_FALSE;
     265                 :         }
     266               3 :         array_init(return_value);
     267               3 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value TSRMLS_CC);
     268                 : }
     269                 : /* }}} */
     270                 : 
     271                 : /*
     272                 :  * Local variables:
     273                 :  * tab-width: 4
     274                 :  * c-basic-offset: 4
     275                 :  * vim600: sw=4 ts=4 fdm=marker
     276                 :  * vim<600: sw=4 ts=4 * End:
     277                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:22 +0000 (5 days ago)

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