|
|
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);
}
|
|
|
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) {
+}
-
|