Repository navigation
SAPI: Convert sapi_getenv to zend_string #23386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -975,9 +975,9 @@ SAPI_API zend_stat_t *sapi_get_stat(void) | |
| } | ||
| } | ||
|
|
||
| SAPI_API char *sapi_getenv(const char *name, size_t name_len) | ||
| SAPI_API zend_string *sapi_getenv(const char *name, size_t name_len) | ||
| { | ||
| char *value, *tmp; | ||
| zend_string *value = NULL; | ||
|
|
||
| if (!sapi_module.getenv) { | ||
| return NULL; | ||
|
|
@@ -986,19 +986,18 @@ SAPI_API char *sapi_getenv(const char *name, size_t name_len) | |
| /* Ugly fix for HTTP_PROXY issue, see bug #72573 */ | ||
| return NULL; | ||
| } | ||
| tmp = sapi_module.getenv(name, name_len); | ||
| if (!tmp) { | ||
| value = sapi_module.getenv(name, name_len); | ||
| if (!value) { | ||
| return NULL; | ||
| } | ||
| value = estrdup(tmp); | ||
| #ifdef PHP_WIN32 | ||
| if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) { | ||
| /* XXX more modules to go, if needed. */ | ||
| free(tmp); | ||
| } | ||
| #endif | ||
| if (sapi_module.input_filter) { | ||
| sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL); | ||
| /* XXX: pretty ugly because input filters aren't zend_string yet. */ | ||
| char *tmp = estrdup(ZSTR_VAL(value)); | ||
| size_t tmp_len = 0; | ||
| sapi_module.input_filter(PARSE_STRING, name, &tmp, ZSTR_LEN(value), &tmp_len); | ||
| zend_string_release(value); | ||
| value = zend_string_init(tmp, tmp_len, 0); | ||
| efree(tmp); | ||
|
Comment on lines
+994
to
+1000
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh this is fine as this is going to be somewhat temporary I imagine. |
||
| } | ||
| return value; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -525,10 +525,10 @@ static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes) | |
| } | ||
|
|
||
| #ifdef PHP_WIN32 | ||
| /* The result needs to be freed! See sapi_getenv(). */ | ||
| static char *cgi_getenv_win32(const char *name, size_t name_len) | ||
| static zend_string *cgi_getenv_win32(const char *name, size_t name_len) | ||
| { | ||
| char *ret = NULL; | ||
| zend_string *ret = NULL; | ||
| char *tmp = NULL; | ||
| wchar_t *keyw, *valw; | ||
| size_t size; | ||
| int rc; | ||
|
|
@@ -548,26 +548,31 @@ static char *cgi_getenv_win32(const char *name, size_t name_len) | |
|
|
||
| rc = _wgetenv_s(&size, valw, size, keyw); | ||
| if (!rc) { | ||
| ret = php_win32_cp_w_to_any(valw); | ||
| tmp = php_win32_cp_w_to_any(valw); | ||
| } | ||
| if (tmp) { | ||
| ret = zend_string_init(tmp, strlen(tmp), 0); | ||
| } | ||
|
|
||
| free(tmp); | ||
| free(keyw); | ||
| efree(valw); | ||
|
|
||
| return ret; | ||
| } | ||
| #endif | ||
|
|
||
| static char *sapi_cgi_getenv(const char *name, size_t name_len) | ||
| static zend_string *sapi_cgi_getenv(const char *name, size_t name_len) | ||
| { | ||
| #ifndef PHP_WIN32 | ||
| return getenv(name); | ||
| char *ret = getenv(name); | ||
| return ret ? zend_string_init(ret, strlen(ret), 0) : NULL; | ||
| #else | ||
| return cgi_getenv_win32(name, name_len); | ||
| #endif | ||
| } | ||
|
|
||
| static char *sapi_fcgi_getenv(const char *name, size_t name_len) | ||
| static zend_string *sapi_fcgi_getenv(const char *name, size_t name_len) | ||
| { | ||
| /* when php is started by mod_fastcgi, no regular environment | ||
| * is provided to PHP. It is always sent to PHP at the start | ||
|
|
@@ -577,16 +582,15 @@ static char *sapi_fcgi_getenv(const char *name, size_t name_len) | |
| char *ret = fcgi_getenv(request, name, (int)name_len); | ||
|
|
||
| #ifndef PHP_WIN32 | ||
| if (ret) return ret; | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
| return getenv(name); | ||
| if (!ret) { | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
| ret = getenv(name); | ||
| } | ||
| return ret ? zend_string_init(ret, strlen(ret), 0) : NULL; | ||
| #else | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the logic. But seems like you've missed something here? if ret is not empty we should use ret, this is also true in the else branch.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot the logic in the Windows ifdef, that should be fixed. |
||
| if (ret) { | ||
| /* The functions outside here don't know, where does it come | ||
| from. They'll need to free the returned memory as it's | ||
| not necessary from the fcgi env. */ | ||
| return strdup(ret); | ||
| return zend_string_init(ret, strlen(ret), 0); | ||
| } | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,7 +100,7 @@ static int parent = 1; | |
| static int request_body_fd; | ||
| static int fpm_is_running = 0; | ||
|
|
||
| static char *sapi_cgibin_getenv(const char *name, size_t name_len); | ||
| static zend_string *sapi_cgibin_getenv(const char *name, size_t name_len); | ||
| static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg); | ||
|
|
||
| #define PHP_MODE_STANDARD 1 | ||
|
|
@@ -461,16 +461,19 @@ static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes) /* {{{ */ | |
| } | ||
| /* }}} */ | ||
|
|
||
| static char *sapi_cgibin_getenv(const char *name, size_t name_len) /* {{{ */ | ||
| static zend_string *sapi_cgibin_getenv(const char *name, size_t name_len) /* {{{ */ | ||
| { | ||
| char *var = NULL; | ||
| /* if fpm has started, use fcgi env */ | ||
| if (fpm_is_running) { | ||
| fcgi_request *request = (fcgi_request*) SG(server_context); | ||
| return fcgi_getenv(request, name, name_len); | ||
| var = fcgi_getenv(request, name, name_len); | ||
| } | ||
|
|
||
| /* if fpm has not started yet, use std env */ | ||
| return getenv(name); | ||
| if (!var) { | ||
| /* if fpm has not started yet, use std env */ | ||
| var = getenv(name); /* XXX: should we? */ | ||
| } | ||
| return var ? zend_string_init(var, strlen(var), 0) : NULL; | ||
|
Comment on lines
471
to
+476
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it should be an if-else rather than checking that var is NULL. |
||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -564,20 +567,27 @@ static void sapi_cgi_register_variables(zval *track_vars_array) /* {{{ */ | |
| if (CGIG(fix_pathinfo)) { | ||
| char *script_name = SG(request_info).request_uri; | ||
| unsigned int script_name_len = script_name ? strlen(script_name) : 0; | ||
| char *path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO") - 1); | ||
| unsigned int path_info_len = path_info ? strlen(path_info) : 0; | ||
|
|
||
| php_self_len = script_name_len + path_info_len; | ||
| /* Concat script_name and path_info into php_self */ | ||
| php_self = zend_cstr_concat( | ||
| script_name, script_name_len, | ||
| path_info, path_info_len); | ||
| zend_string *path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO") - 1); | ||
|
NattyNarwhal marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (path_info) { | ||
| php_self_len = script_name_len + ZSTR_LEN(path_info); | ||
| /* Concat script_name and path_info into php_self */ | ||
| php_self = zend_cstr_concat( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a future PR: it probably makes sense to change the |
||
| script_name, script_name_len, | ||
| ZSTR_VAL(path_info), ZSTR_LEN(path_info)); | ||
| } else { | ||
| php_self_len = script_name_len; | ||
| php_self = estrdup(script_name ? script_name : ""); | ||
| } | ||
|
|
||
| /* Build the special-case PHP_SELF variable for the CGI version */ | ||
| if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) { | ||
| php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array); | ||
| } | ||
| efree(php_self); | ||
| if (path_info) { | ||
| zend_string_release(path_info); | ||
| } | ||
| } else { | ||
| php_self = SG(request_info).request_uri ? SG(request_info).request_uri : ""; | ||
| php_self_len = strlen(php_self); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,13 +184,15 @@ static int sapi_lsapi_deactivate(void) | |
|
|
||
|
|
||
| /* {{{ sapi_lsapi_getenv */ | ||
| static char *sapi_lsapi_getenv(const char * name, size_t name_len ) | ||
| static zend_string *sapi_lsapi_getenv(const char * name, size_t name_len ) | ||
| { | ||
| char *var = NULL; | ||
| if ( lsapi_mode ) { | ||
| return LSAPI_GetEnv( name ); | ||
| var = LSAPI_GetEnv( name ); | ||
| } else { | ||
| return getenv( name ); | ||
| var = getenv( name ); /* XXX: Should a SAPI just reflect getenv? */ | ||
| } | ||
| return var ? zend_string_init( var, strlen( var ), 0 ) : NULL; | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -538,7 +540,8 @@ static int lsapi_activate_user_ini(void); | |
|
|
||
| static int sapi_lsapi_activate(void) | ||
| { | ||
| char *path, *server_name; | ||
| char *path; | ||
| zend_string *server_name; | ||
| size_t path_len, server_name_len; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the |
||
|
|
||
| /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */ | ||
|
|
@@ -550,11 +553,10 @@ static int sapi_lsapi_activate(void) | |
| server_name = sapi_lsapi_getenv("SERVER_NAME", 0); | ||
| /* SERVER_NAME should also be defined at this stage..but better check it anyway */ | ||
| if (server_name) { | ||
| server_name_len = strlen(server_name); | ||
| server_name = estrndup(server_name, server_name_len); | ||
| zend_str_tolower(server_name, server_name_len); | ||
| php_ini_activate_per_host_config(server_name, server_name_len); | ||
| efree(server_name); | ||
| zend_string *lowered = zend_string_tolower(server_name); | ||
| php_ini_activate_per_host_config(ZSTR_VAL(lowered), ZSTR_LEN(lowered)); | ||
| zend_string_release(lowered); | ||
| zend_string_release(server_name); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if
path_infocould be azend_string*too but that requires changing theSG(request_info).request_uriglobal to zend_string first.