Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_headers, 0, 0, 1)
ZEND_ARG_INFO(0, url)
ZEND_ARG_INFO(0, format)
ZEND_ARG_INFO(0, specific)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ user_filters.c */
Expand Down
5 changes: 3 additions & 2 deletions ext/standard/tests/url/get_headers_error_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var_dump( get_headers() );
echo "\n-- Testing get_headers() function with more than expected no. of arguments --\n";
$url = 'string_val';
$format = 1;
$spec_arg = 'spec_val';
$extra_arg = 10;
var_dump( get_headers($url, $format, $extra_arg) );
var_dump( get_headers($url, $format, $spec_arg, $extra_arg) );

echo "Done";
?>
Expand All @@ -36,7 +37,7 @@ NULL

-- Testing get_headers() function with more than expected no. of arguments --

Warning: get_headers() expects at most 2 parameters, 3 given in %s on line 19
Warning: get_headers() expects at most 3 parameters, 4 given in %s on line 20
NULL
Done

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/url/get_headers_error_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ June Henriksen <juneih@redpill-linpro.com>
#PHPTestFest2009 Norway 2009-06-09 \o/
--FILE--
<?php
/* Prototype : proto array get_headers(string url[, int format])
/* Prototype : proto array get_headers(string url[, int format, [ string specific]])
* Description: Fetches all the headers sent by the server in response to a HTTP request
* Source code: ext/standard/url.c
* Alias to functions:
Expand Down
34 changes: 24 additions & 10 deletions ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,22 +694,25 @@ PHPAPI int php_raw_url_decode(char *str, int len)
}
/* }}} */

/* {{{ proto array get_headers(string url[, int format])
/* {{{ proto array get_headers(string url[, int format, [ string specific]])
fetches all the headers sent by the server in response to a HTTP request */
PHP_FUNCTION(get_headers)
{
char *url;
int url_len;
char *specific;
int specific_len;
php_stream_context *context;
php_stream *stream;
zval **prev_val, **hdr = NULL, **h;
HashPosition pos;
HashTable *hashT;
long format = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &url, &url_len, &format) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &url, &url_len, &format, &specific, &specific_len) == FAILURE) {
return;
}

context = FG(default_context) ? FG(default_context) : (FG(default_context) = php_stream_context_alloc(TSRMLS_C));

if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) {
Expand Down Expand Up @@ -743,7 +746,10 @@ PHP_FUNCTION(get_headers)
}
if (!format) {
no_name_header:
add_next_index_stringl(return_value, Z_STRVAL_PP(hdr), Z_STRLEN_PP(hdr), 1);
if (!specific ||
(strncasecmp(Z_STRVAL_PP(hdr), specific, (specific_len > Z_STRLEN_PP(hdr)) ? Z_STRLEN_PP(hdr) : specific_len) == SUCCESS)) {
add_next_index_stringl(return_value, Z_STRVAL_PP(hdr), Z_STRLEN_PP(hdr), 1);
}
} else {
char c;
char *s, *p;
Expand All @@ -756,13 +762,21 @@ PHP_FUNCTION(get_headers)
s++;
}

if (zend_hash_find(HASH_OF(return_value), Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), (void **) &prev_val) == FAILURE) {
add_assoc_stringl_ex(return_value, Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
} else { /* some headers may occur more then once, therefor we need to remake the string into an array */
convert_to_array(*prev_val);
add_next_index_stringl(*prev_val, s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
}

if (!specific ||
(strncasecmp(Z_STRVAL_PP(hdr), specific, (specific_len > Z_STRLEN_PP(hdr)) ? Z_STRLEN_PP(hdr) : specific_len) == SUCCESS)) {

if (zend_hash_find(HASH_OF(return_value), Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), (void **) &prev_val) == FAILURE) {
if (!specific) {
add_assoc_stringl_ex(return_value, Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
} else {
add_next_index_stringl(return_value, s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
}
} else { /* some headers may occur more then once, therefor we need to remake the string into an array */
convert_to_array(*prev_val);
add_next_index_stringl(*prev_val, s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
}
}

*p = c;
} else {
goto no_name_header;
Expand Down