Skip to content

Commit 8b57a5b

Browse files
committed
Follow up on bug #75574 for FCGI side
1 parent 2873316 commit 8b57a5b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

main/SAPI.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,12 @@ SAPI_API char *sapi_getenv(char *name, size_t name_len)
10271027
char *value, *tmp = sapi_module.getenv(name, name_len);
10281028
if (tmp) {
10291029
value = estrdup(tmp);
1030+
#ifdef PHP_WIN32
1031+
if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) {
1032+
/* XXX more modules to go, if needed. */
1033+
free(tmp);
1034+
}
1035+
#endif
10301036
} else {
10311037
return NULL;
10321038
}

sapi/cgi/cgi_main.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,47 @@ static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
530530
return read_bytes;
531531
}
532532

533+
#ifdef PHP_WIN32
534+
/* The result needs to be freed! See sapi_getenv(). */
535+
static char *cgi_getenv_win32(const char *name, size_t name_len)
536+
{
537+
char *ret = NULL;
538+
wchar_t *keyw, *valw;
539+
size_t size;
540+
int rc;
541+
542+
keyw = php_win32_cp_conv_any_to_w(name, name_len, PHP_WIN32_CP_IGNORE_LEN_P);
543+
if (!keyw) {
544+
return NULL;
545+
}
546+
547+
rc = _wgetenv_s(&size, NULL, 0, keyw);
548+
if (rc || 0 == size) {
549+
free(keyw);
550+
return NULL;
551+
}
552+
553+
valw = emalloc((size + 1) * sizeof(wchar_t));
554+
555+
rc = _wgetenv_s(&size, valw, size, keyw);
556+
if (!rc) {
557+
ret = php_win32_cp_w_to_any(valw);
558+
}
559+
560+
free(keyw);
561+
efree(valw);
562+
563+
return ret;
564+
}
565+
#endif
566+
533567
static char *sapi_cgi_getenv(char *name, size_t name_len)
534568
{
569+
#ifndef PHP_WIN32
535570
return getenv(name);
571+
#else
572+
return cgi_getenv_win32(name, name_len);
573+
#endif
536574
}
537575

538576
static char *sapi_fcgi_getenv(char *name, size_t name_len)
@@ -547,7 +585,11 @@ static char *sapi_fcgi_getenv(char *name, size_t name_len)
547585
if (ret) return ret;
548586
/* if cgi, or fastcgi and not found in fcgi env
549587
check the regular environment */
588+
#ifndef PHP_WIN32
550589
return getenv(name);
590+
#else
591+
return cgi_getenv_win32(name, name_len);
592+
#endif
551593
}
552594

553595
static char *_sapi_cgi_putenv(char *name, size_t name_len, char *value)

sapi/cgi/tests/bug75574_utf8_win.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Bug #75574 putenv does not work properly if parameter contains non-ASCII unicode character, UTF-8
3+
--SKIPIF--
4+
<?php
5+
6+
if (substr(PHP_OS, 0, 3) != 'WIN') {
7+
die("skip Valid only on Windows");
8+
}
9+
include "skipif.inc";
10+
?>
11+
--FILE--
12+
<?php
13+
/*
14+
#vim: set fileencoding=utf-8
15+
#vim: set encoding=utf-8
16+
*/
17+
18+
include "include.inc";
19+
20+
$php = get_cgi_path();
21+
reset_env_vars();
22+
23+
$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . md5(uniqid());
24+
file_put_contents($fn, "<?php\nvar_dump(putenv('FOO=啊'));\n//var_dump(`echo %FOO%`);\nvar_dump(getenv('FOO'));");
25+
26+
echo shell_exec("$php -n -f $fn");
27+
28+
unlink($fn);
29+
30+
?>
31+
===DONE===
32+
--EXPECTF--
33+
bool(true)
34+
string(3) "啊"
35+
===DONE===

0 commit comments

Comments
 (0)