View Issue Details

IDProjectCategoryView StatusLast Update
0000645XdebugUncategorizedpublic2020-03-12 17:01
Reporterwhatthejeff Assigned Toderick  
PrioritynormalSeverityfeatureReproducibilityalways
Status resolvedResolutionduplicate 
Product Version2.2dev 
Fixed in Version2.2.2 
Summary0000645: Suggested improvements for xdebug_header_handler
Description

I've been noticing, especially in php 5.3+, that using Xdebug to inspect headers can yield some unexpected results. The source of the problem is that xdebug_header_handler always adds headers even though the intent might actually be to replace or delete headers.

Steps To Reproduce

Sample Code:

<?php
header('Foo: Bar');
header('Foo: Baz');
header('Foot: Ball');
header_remove('Foot');
var_dump(xdebug_get_headers());
?>

Desired Output:

array(1) {
[0]=>
string(8) "Foo: Baz"
}

Actual Output:

array(4) {
[0]=>
string(8) "Foo: Bar"
[1]=>
string(8) "Foo: Baz"
[2]=>
string(10) "Foot: Ball"
[3]=>
string(4) "Foot"
}

TagsNo tags attached.
Operating System
PHP Version5.3.0

Relationships

duplicate of 0000903 closedderick xdebug_get_headers() returns replaced headers 

Activities

whatthejeff

2010-12-03 21:22

reporter   ~0001619

Suggested Patch (I'll provide tests in a bit):

Index: xdebug.c

--- xdebug.c (revision 3374)
+++ xdebug.c (working copy)
@@ -1440,15 +1440,68 @@
}
/ }}} /

+static int xdebug_find_header(char *key, unsigned int len, xdebug_llist_element **found)
+{

  • xdebug_llist_element *le;
  • for (le = XDEBUG_LLIST_HEAD(XG(headers)); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
  • if(!strncasecmp(key, XDEBUG_LLIST_VALP(le), len)) {
  • *found = le;
  • return 1;
  • }
  • }
  • return 0;
    +}
  • static int xdebug_header_handler(sapi_header_struct h XG_SAPI_HEADER_OP_DC, sapi_headers_struct s TSRMLS_DC)
    {

  • int return_value = 0;
  • char colon_offset, header_line;
  • xdebug_llist_element *el;
  • +#if PHP_VERSION_ID < 50300

  • int op = h->replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD;
    +#endif
  • if (XG(headers)) {

  • xdebug_llist_insert_next(XG(headers), XDEBUG_LLIST_TAIL(XG(headers)), xdstrdup(h->header));
  • switch (op) {
  • case SAPI_HEADER_DELETE_ALL:
  • xdebug_llist_empty(XG(headers), NULL);
  • break;
  • +#if PHP_VERSION_ID >= 50300

  • case SAPI_HEADER_DELETE:
  • if (xdebug_find_header(h->header, strlen(h->header), &el)) {
  • xdebug_llist_remove(XG(headers), el, NULL);
  • }
  • break;
    +#endif
  • case SAPI_HEADER_ADD:
  • case SAPI_HEADER_REPLACE:
  • header_line = xdstrdup(h->header);
  • colon_offset = strchr(header_line, ':');
  • if (colon_offset) {
  • *++colon_offset = 0;
  • if (op == SAPI_HEADER_REPLACE && xdebug_find_header(header_line, strlen(header_line), &el)) {
  • xdebug_llist_remove(XG(headers), el, NULL);
  • }
  • xdebug_llist_insert_next(XG(headers), XDEBUG_LLIST_TAIL(XG(headers)), xdstrdup(h->header));
  • return_value = SAPI_HEADER_ADD;
  • }
  • xdfree(header_line);
  • break;
  • }
    }
  • if (xdebug_orig_header_handler) {
    return xdebug_orig_header_handler(h XG_SAPI_HEADER_OP_CC, s TSRMLS_CC);
    }

  • return SAPI_HEADER_ADD;
  • return return_value;
    }

@@ -1683,7 +1736,6 @@
string = XDEBUG_LLIST_VALP(le);
add_next_index_string(return_value, string, 1);
}

  • xdebug_llist_empty(XG(headers), NULL);
    }

whatthejeff

2010-12-04 06:14

reporter   ~0001620

Test cases–including a test case for PHP 5.3+ with header_remove().

Index: tests/bug00645.phpt

--- tests/bug00645.phpt (revision 0)
+++ tests/bug00645.phpt (revision 0)
@@ -0,0 +1,44 @@
+--TEST--
+Test for bug 0000645: Suggested improvements for xdebug_header_handler for all versions of PHP
+--SKIPIF--
+<?php if (!extension_loaded("xdebug")) print "skip"; ?>
+--FILE--
+<?php
+// Test Add
+header('Foo: Bar');
+header('Foz: Bar');
+var_dump(xdebug_get_headers());
+
+// Test Replace
+header('Foz: Baz');
+var_dump(xdebug_get_headers());
+
+// Test setcookie Adds
+setcookie('foo', 'bar');
+setcookie('foz', 'baz');
+var_dump(xdebug_get_headers());
+
+--EXPECTF--
+array(2) {

  • [0]=>
  • string(8) "Foo: Bar"
  • [1]=>
  • string(8) "Foz: Bar"
    +}
    +array(2) {
  • [0]=>
  • string(8) "Foo: Bar"
  • [1]=>
  • string(8) "Foz: Baz"
    +}
    +array(4) {
  • [0]=>
  • string(8) "Foo: Bar"
  • [1]=>
  • string(8) "Foz: Baz"
  • [2]=>
  • string(19) "Set-Cookie: foo=bar"
  • [3]=>
  • string(19) "Set-Cookie: foz=baz"
    +}
  • Index: tests/bug00645-2.phpt

    --- tests/bug00645-2.phpt (revision 0)
    +++ tests/bug00645-2.phpt (revision 0)
    @@ -0,0 +1,39 @@
    +--TEST--
    +Test for bug 0000645: Suggested improvements for xdebug_header_handler for PHP 5.3+
    +--SKIPIF--
    +<?php if (!extension_loaded("xdebug")) print "skip"; ?>
    +<?php if(version_compare(phpversion(), "5.3.0", '<')) echo "skip PHP 5.3 needed\n"; ?>
    +--FILE--
    +<?php
    +// Test Add
    +header('Foo: Bar');
    +header('Fox: Bax');
    +header('Foz: Baz');
    +var_dump(xdebug_get_headers());

  • +// Test Delete
    +header_remove('Fox');
    +var_dump(xdebug_get_headers());

  • +// Test Delete All
    +header_remove();
    +var_dump(xdebug_get_headers());

  • +--EXPECTF--
    +array(3) {

  • [0]=>
  • string(8) "Foo: Bar"
  • [1]=>
  • string(8) "Fox: Bax"
  • [2]=>
  • string(8) "Foz: Baz"
    +}
    +array(2) {
  • [0]=>
  • string(8) "Foo: Bar"
  • [1]=>
  • string(8) "Foz: Baz"
    +}
    +array(0) {
    +}

derick

2014-02-27 19:08

administrator   ~0002683

Duplicate of 0000903 which is fixed in Xdebug 2.2.2.

Issue History

Date Modified Username Field Change
2010-12-03 21:14 whatthejeff New Issue
2010-12-03 21:22 whatthejeff Note Added: 0001619
2010-12-04 06:14 whatthejeff Note Added: 0001620
2014-02-27 19:08 derick Note Added: 0002683
2014-02-27 19:08 derick Relationship added duplicate of 0000903
2014-02-27 19:08 derick Status new => resolved
2014-02-27 19:08 derick Fixed in Version => 2.2.2
2014-02-27 19:08 derick Resolution open => duplicate
2014-02-27 19:08 derick Assigned To => derick
2020-03-12 17:01 derick Category Feature/Change request => Uncategorized