Skip to content
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

geallheaders php-fpm #62596 #523

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 84 additions & 1 deletion sapi/fpm/fpm/fpm_main.c
Expand Up @@ -115,6 +115,8 @@ struct sigaction act, old_term, old_quit, old_int;

static void (*php_php_import_environment_variables)(zval *array_ptr TSRMLS_DC);

typedef void (*fcgi_apply_func)(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg TSRMLS_DC);

#ifndef PHP_WIN32
/* these globals used for forking children on unix systems */

Expand Down Expand Up @@ -1527,8 +1529,89 @@ PHP_FUNCTION(fastcgi_finish_request) /* {{{ */
}
/* }}} */

static inline void add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg TSRMLS_DC) /* {{{ */
{
zval *return_value = (zval*)arg;
char *str = NULL;
char *p;
ALLOCA_FLAG(use_heap)

if (var_len > 5 &&
var[0] == 'H' &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's this copied from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be better to move this function into somewhere in /sapi where both can be linked to it and include it?

var[1] == 'T' &&
var[2] == 'T' &&
var[3] == 'P' &&
var[4] == '_') {

var_len -= 5;
p = var + 5;
var = str = do_alloca(var_len + 1, use_heap);
*str++ = *p++;
while (*p) {
if (*p == '_') {
*str++ = '-';
p++;
if (*p) {
*str++ = *p++;
}
} else if (*p >= 'A' && *p <= 'Z') {
*str++ = (*p++ - 'A' + 'a');
} else {
*str++ = *p++;
}
}
*str = 0;
} else if (var_len == sizeof("CONTENT_TYPE")-1 &&
memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
var = "Content-Type";
} else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
var = "Content-Length";
} else {
return;
}
add_assoc_stringl_ex(return_value, var, var_len+1, val, val_len, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho "var_len + 1" is wrong here as in FPM request->env already contains the terminating char.
See http://lxr.php.net/xref/PHP_5_6/sapi/cgi/fastcgi.c#1480 vs. http://lxr.php.net/xref/PHP_5_6/sapi/fpm/fpm/fastcgi.c#1072

Subsequently the previous checks should be var_len == sizeof(....).

I've tested this on Apache/2.4.10 + mod_fastcgi and Apache/2.4.10 + mod_proxy_fcgi. In both environments var_dump shows a trailing random character for every header.

if (str) {
free_alloca(var, use_heap);
}
}
/* }}} */

static inline void fcgi_hash_apply(HashTable *h, fcgi_apply_func func, void *arg TSRMLS_DC) /* {{{ */
{
Bucket *p = h->pListHead;

while (p) {
if (EXPECTED(p->arKey != NULL)) {
func((char*)p->arKey, p->nKeyLength, *(char**)p->pData, strlen(*(char**)p->pData), arg TSRMLS_CC);
}
p = p->pListNext;
}
} /* }}} */

PHP_FUNCTION(getallheaders) /* {{{ */
{
fcgi_request *request;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

array_init(return_value);

if ((request = (fcgi_request*) SG(server_context))) {
fcgi_hash_apply(
request->env, add_request_header, return_value TSRMLS_CC);
}
} /* }}} */

ZEND_BEGIN_ARG_INFO(cgi_fcgi_sapi_no_arginfo, 0)
ZEND_END_ARG_INFO()

static const zend_function_entry cgi_fcgi_sapi_functions[] = {
PHP_FE(fastcgi_finish_request, NULL)
PHP_FE(fastcgi_finish_request, cgi_fcgi_sapi_no_arginfo)
PHP_FE(getallheaders, cgi_fcgi_sapi_no_arginfo)
PHP_FALIAS(apache_request_headers, getallheaders, cgi_fcgi_sapi_no_arginfo)
{NULL, NULL, NULL}
};

Expand Down