summaryrefslogtreecommitdiff
path: root/sapi
diff options
context:
space:
mode:
authorArnaud Le Blanc <lbarnaud@php.net>2008-11-13 10:14:04 +0000
committerArnaud Le Blanc <lbarnaud@php.net>2008-11-13 10:14:04 +0000
commit8c4151ad72b6c5a82b341aaf59d2c31521ca4341 (patch)
treeccc7c01da54fd7f7a02d03d7017e78a359aa254e /sapi
parent06119f4748066e61163764bbb31d1db4b21b6d07 (diff)
downloadphp-git-8c4151ad72b6c5a82b341aaf59d2c31521ca4341.tar.gz
Added header_remove() (chsc at peytz dotdk, Arnaud)
[DOC] proto void header_remove([string header_name]) Removes an HTTP header previously set using header() The header_name parameter is optionnal, all headers are removed if it is not set [SAPIs] The header_handler callback in sapi_module_struct has been changed, it now take a new argument. When it is set to SAPI_HEADER_DELETE, sapi_header->header is the name of an header, header_handler has to delete it. When it is set to SAPI_HEADER_DELETE_ALL, header_handler has to delete all headers. When sapi_header_op_enum is SAPI_HEADER_ADD or _REPLACE, sapi_header->header is in the form "Name: value", header_handler has to add or replace the given header. In all cases, header_handler must not free sapi_header or sapi_header->header. SAPI_HEADER_ADD must be returned if the header has been added or replaced, or 0 in other cases.
Diffstat (limited to 'sapi')
-rw-r--r--sapi/apache/mod_php5.c63
-rw-r--r--sapi/apache2handler/sapi_apache2.c61
-rw-r--r--sapi/cgi/tests/011.phpt165
-rw-r--r--sapi/cli/php_cli.c5
4 files changed, 242 insertions, 52 deletions
diff --git a/sapi/apache/mod_php5.c b/sapi/apache/mod_php5.c
index a5afb77be4..3acf8a58f5 100644
--- a/sapi/apache/mod_php5.c
+++ b/sapi/apache/mod_php5.c
@@ -35,7 +35,7 @@ static void php_save_umask(void);
static void php_restore_umask(void);
static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC);
static char *sapi_apache_read_cookies(TSRMLS_D);
-static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC);
+static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC);
static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC);
static int send_php(request_rec *r, int display_source_mode, char *filename);
static int send_parsed_php(request_rec * r);
@@ -163,41 +163,54 @@ static char *sapi_apache_read_cookies(TSRMLS_D)
/* {{{ sapi_apache_header_handler
*/
-static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
+static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC)
{
char *header_name, *header_content, *p;
request_rec *r = (request_rec *) SG(server_context);
if(!r) {
- efree(sapi_header->header);
return 0;
}
- header_name = sapi_header->header;
+ switch(op) {
+ case SAPI_HEADER_DELETE_ALL:
+ clear_table(r->headers_out);
+ return 0;
- header_content = p = strchr(header_name, ':');
- if (!p) {
- efree(sapi_header->header);
- return 0;
- }
+ case SAPI_HEADER_DELETE:
+ table_unset(r->headers_out, sapi_header->header);
+ return 0;
- *p = 0;
- do {
- header_content++;
- } while (*header_content==' ');
-
- if (!strcasecmp(header_name, "Content-Type")) {
- r->content_type = pstrdup(r->pool, header_content);
- } else if (!strcasecmp(header_name, "Set-Cookie")) {
- table_add(r->headers_out, header_name, header_content);
- } else if (sapi_header->replace) {
- table_set(r->headers_out, header_name, header_content);
- } else {
- table_add(r->headers_out, header_name, header_content);
- }
+ case SAPI_HEADER_ADD:
+ case SAPI_HEADER_REPLACE:
+ header_name = sapi_header->header;
+
+ header_content = p = strchr(header_name, ':');
+ if (!p) {
+ return 0;
+ }
+
+ *p = 0;
+ do {
+ header_content++;
+ } while (*header_content==' ');
+
+ if (!strcasecmp(header_name, "Content-Type")) {
+ r->content_type = pstrdup(r->pool, header_content);
+ } else if (!strcasecmp(header_name, "Set-Cookie")) {
+ table_add(r->headers_out, header_name, header_content);
+ } else if (op == SAPI_HEADER_REPLACE) {
+ table_set(r->headers_out, header_name, header_content);
+ } else {
+ table_add(r->headers_out, header_name, header_content);
+ }
- *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
+ *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
- return SAPI_HEADER_ADD;
+ return SAPI_HEADER_ADD;
+
+ default:
+ return 0;
+ }
}
/* }}} */
diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c
index b8671c59d4..cf2e1427be 100644
--- a/sapi/apache2handler/sapi_apache2.c
+++ b/sapi/apache2handler/sapi_apache2.c
@@ -83,40 +83,55 @@ php_apache_sapi_ub_write(const char *str, uint str_length TSRMLS_DC)
}
static int
-php_apache_sapi_header_handler(sapi_header_struct *sapi_header,sapi_headers_struct *sapi_headers TSRMLS_DC)
+php_apache_sapi_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC)
{
php_struct *ctx;
char *val, *ptr;
ctx = SG(server_context);
- val = strchr(sapi_header->header, ':');
+ switch (op) {
+ case SAPI_HEADER_DELETE:
+ apr_table_unset(ctx->r->headers_out, sapi_header->header);
+ return 0;
- if (!val) {
- sapi_free_header(sapi_header);
- return 0;
- }
- ptr = val;
+ case SAPI_HEADER_DELETE_ALL:
+ apr_table_clear(ctx->r->headers_out);
+ return 0;
+
+ case SAPI_HEADER_ADD:
+ case SAPI_HEADER_REPLACE:
+ val = strchr(sapi_header->header, ':');
- *val = '\0';
+ if (!val) {
+ return 0;
+ }
+ ptr = val;
+
+ *val = '\0';
- do {
- val++;
- } while (*val == ' ');
+ do {
+ val++;
+ } while (*val == ' ');
+
+ if (!strcasecmp(sapi_header->header, "content-type")) {
+ if (ctx->content_type) {
+ efree(ctx->content_type);
+ }
+ ctx->content_type = estrdup(val);
+ } else if (op == SAPI_HEADER_REPLACE) {
+ apr_table_set(ctx->r->headers_out, sapi_header->header, val);
+ } else {
+ apr_table_add(ctx->r->headers_out, sapi_header->header, val);
+ }
- if (!strcasecmp(sapi_header->header, "content-type")) {
- if (ctx->content_type) {
- efree(ctx->content_type);
- }
- ctx->content_type = estrdup(val);
- } else if (sapi_header->replace) {
- apr_table_set(ctx->r->headers_out, sapi_header->header, val);
- } else {
- apr_table_add(ctx->r->headers_out, sapi_header->header, val);
+ *ptr = ':';
+
+ return SAPI_HEADER_ADD;
+
+ default:
+ return 0;
}
- *ptr = ':';
-
- return SAPI_HEADER_ADD;
}
static int
diff --git a/sapi/cgi/tests/011.phpt b/sapi/cgi/tests/011.phpt
new file mode 100644
index 0000000000..177df021a6
--- /dev/null
+++ b/sapi/cgi/tests/011.phpt
@@ -0,0 +1,165 @@
+--TEST--
+header_remove()
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+
+include "include.inc";
+
+$php = get_cgi_path();
+reset_env_vars();
+
+$f = tempnam(sys_get_temp_dir(), 'cgitest');
+
+function test($script) {
+ file_put_contents($GLOBALS['f'], $script);
+ $cmd = escapeshellcmd($GLOBALS['php']);
+ $cmd .= ' -n -dreport_zend_debug=0 -dhtml_errors=0 ' . escapeshellarg($GLOBALS['f']);
+ echo "----------\n";
+ echo rtrim($script) . "\n";
+ echo "----------\n";
+ passthru($cmd);
+}
+
+test('<?php ?>');
+test('<?php header_remove(); ?>');
+test('<?php header_remove("X-Foo"); ?>');
+test('<?php
+header("X-Foo: Bar");
+?>');
+test('<?php
+header("X-Foo: Bar");
+header("X-Bar: Baz");
+header_remove("X-Foo");
+?>');
+test('<?php
+header("X-Foo: Bar");
+header_remove("X-Foo: Bar");
+?>');
+test('<?php
+header("X-Foo: Bar");
+header_remove("X-Foo:");
+?>');
+test('<?php
+header("X-Foo: Bar");
+header_remove();
+?>');
+test('<?php
+header_remove("");
+?>');
+test('<?php
+header_remove(":");
+?>');
+test('<?php
+header("X-Foo: Bar");
+echo "flush\n";
+flush();
+header_remove("X-Foo");
+?>');
+
+@unlink($f);
+?>
+--EXPECTF--
+----------
+<?php ?>
+----------
+X-Powered-By: PHP/%s
+Content-type: text/html
+
+----------
+<?php header_remove(); ?>
+----------
+Content-type: text/html
+
+----------
+<?php header_remove("X-Foo"); ?>
+----------
+X-Powered-By: PHP/%s
+Content-type: text/html
+
+----------
+<?php
+header("X-Foo: Bar");
+?>
+----------
+X-Powered-By: PHP/%s
+X-Foo: Bar
+Content-type: text/html
+
+----------
+<?php
+header("X-Foo: Bar");
+header("X-Bar: Baz");
+header_remove("X-Foo");
+?>
+----------
+X-Powered-By: PHP/%s
+X-Bar: Baz
+Content-type: text/html
+
+----------
+<?php
+header("X-Foo: Bar");
+header_remove("X-Foo: Bar");
+?>
+----------
+X-Powered-By: PHP/%s
+X-Foo: Bar
+Content-type: text/html
+
+
+Warning: Header to delete may not contain colon. in %s on line 3
+----------
+<?php
+header("X-Foo: Bar");
+header_remove("X-Foo:");
+?>
+----------
+X-Powered-By: PHP/%s
+X-Foo: Bar
+Content-type: text/html
+
+
+Warning: Header to delete may not contain colon. in %s on line 3
+----------
+<?php
+header("X-Foo: Bar");
+header_remove();
+?>
+----------
+Content-type: text/html
+
+----------
+<?php
+header_remove("");
+?>
+----------
+X-Powered-By: PHP/%s
+Content-type: text/html
+
+----------
+<?php
+header_remove(":");
+?>
+----------
+X-Powered-By: PHP/%s
+Content-type: text/html
+
+
+Warning: Header to delete may not contain colon. in %s on line 2
+----------
+<?php
+header("X-Foo: Bar");
+echo "flush\n";
+flush();
+header_remove("X-Foo");
+?>
+----------
+X-Powered-By: PHP/%s
+X-Foo: Bar
+Content-type: text/html
+
+flush
+
+Warning: Cannot modify header information - headers already sent by (output started at %s:3) in %s on line 5
diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index 66e206df62..4b9d112ed7 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -347,11 +347,8 @@ static char* sapi_cli_read_cookies(TSRMLS_D) /* {{{ */
}
/* }}} */
-static int sapi_cli_header_handler(sapi_header_struct *h, sapi_headers_struct *s TSRMLS_DC) /* {{{ */
+static int sapi_cli_header_handler(sapi_header_struct *h, sapi_header_op_enum op, sapi_headers_struct *s TSRMLS_DC) /* {{{ */
{
- /* free allocated header line */
- efree(h->header);
- /* avoid pushing headers into SAPI headers list */
return 0;
}
/* }}} */