cvs: php4 / README.STREAMS /ext/standard ftp_fopen_wrapper.c http_fopen_wrapper.c /main php_streams.h streams.c
| From: | Wez Furlong | Date: | Sun, 11 Aug 2002 10:53:11 +0000 |
| Subject: | cvs: php4 / README.STREAMS /ext/standard ftp_fopen_wrapper.c http_fopen_wrapper.c /main php_streams.h streams.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-13258@lists.php.net to get a copy of this message | ||
wez Sun Aug 11 06:53:11 2002 EDT
Modified files:
/php4 README.STREAMS
/php4/ext/standard ftp_fopen_wrapper.c http_fopen_wrapper.c
/php4/main php_streams.h streams.c
Log:
Introduce an error stack for wrappers, to help prevent multiple errors
and warnings (some of which are bogus) when there are problems opening
streams.
Implement sanity check on the mode used to open ftp and http connections.
This fixes Bug 12004.
Index: php4/README.STREAMS
diff -u php4/README.STREAMS:1.6 php4/README.STREAMS:1.7
--- php4/README.STREAMS:1.6 Sun Mar 17 20:40:56 2002
+++ php4/README.STREAMS Sun Aug 11 06:53:10 2002
@@ -1,6 +1,10 @@
An Overview of the PHP Streams abstraction
==========================================
-$Id: README.STREAMS,v 1.6 2002/03/18 01:40:56 wez Exp $
+$Id: README.STREAMS,v 1.7 2002/08/11 10:53:10 wez Exp $
+
+WARNING: some prototypes in this file are out of date.
+The information contained here is being integrated into
+the php manual - stay tuned...
Please send comments to: Wez Furlong <wez@thebrainroom.com>
Index: php4/ext/standard/ftp_fopen_wrapper.c
diff -u php4/ext/standard/ftp_fopen_wrapper.c:1.24 php4/ext/standard/ftp_fopen_wrapper.c:1.25
--- php4/ext/standard/ftp_fopen_wrapper.c:1.24 Mon Jul 22 14:46:26 2002
+++ php4/ext/standard/ftp_fopen_wrapper.c Sun Aug 11 06:53:10 2002
@@ -17,7 +17,7 @@
| Hartmut Holzgraefe <hholzgra@php.net> |
+----------------------------------------------------------------------+
*/
-/* $Id: ftp_fopen_wrapper.c,v 1.24 2002/07/22 18:46:26 jason Exp $ */
+/* $Id: ftp_fopen_wrapper.c,v 1.25 2002/08/11 10:53:10 wez Exp $ */
#include "php.h"
#include "php_globals.h"
@@ -115,6 +115,11 @@
int i;
char *tpath, *ttpath;
size_t file_size = 0;
+
+ if (strchr(mode, 'a') || strchr(mode, '+')) {
+ php_stream_wrapper_log_error(wrapper, options, "FTP does not support simultaneous read/write
connections.");
+ return NULL;
+ }
resource = php_url_parse((char *) path);
if (resource == NULL || resource->path == NULL)
@@ -314,7 +319,7 @@
php_stream_close(stream);
}
if (tmp_line[0] != '\0')
- zend_error(E_WARNING, "FTP server reports %s", tmp_line);
+ php_stream_wrapper_log_error(wrapper, options, "FTP server reports %s", tmp_line);
return NULL;
}
/* }}} */
Index: php4/ext/standard/http_fopen_wrapper.c
diff -u php4/ext/standard/http_fopen_wrapper.c:1.37 php4/ext/standard/http_fopen_wrapper.c:1.38
--- php4/ext/standard/http_fopen_wrapper.c:1.37 Mon Jul 22 14:46:26 2002
+++ php4/ext/standard/http_fopen_wrapper.c Sun Aug 11 06:53:10 2002
@@ -18,7 +18,7 @@
| Wez Furlong <wez@thebrainroom.com> |
+----------------------------------------------------------------------+
*/
-/* $Id: http_fopen_wrapper.c,v 1.37 2002/07/22 18:46:26 jason Exp $ */
+/* $Id: http_fopen_wrapper.c,v 1.38 2002/08/11 10:53:10 wez Exp $ */
#include "php.h"
#include "php_globals.h"
@@ -86,11 +86,16 @@
char tmp_line[128];
size_t chunk_size = 0, file_size = 0;
+ if (strchr(mode, 'a') || strchr(mode, '+') || strchr(mode, 'w')) {
+ php_stream_wrapper_log_error(wrapper, options, "HTTP wrapper does not writeable
connections.");
+ return NULL;
+ }
+
resource = php_url_parse(path);
if (resource == NULL)
return NULL;
- use_ssl = resource->scheme && resource->scheme[4] == 's';
+ use_ssl = resource->scheme && (strlen(resource->scheme) > 4) &&
resource->scheme[4] == 's';
/* choose default ports */
if (use_ssl && resource->port == 0)
@@ -113,9 +118,7 @@
#if HAVE_OPENSSL_EXT
if (use_ssl) {
if (php_stream_sock_ssl_activate(stream, 1) == FAILURE) {
- if (options & REPORT_ERRORS) {
- zend_error(E_WARNING, "Unable to activate SSL mode");
- }
+ php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode");
php_stream_close(stream);
stream = NULL;
goto out;
@@ -323,8 +326,7 @@
FREE_ZVAL(stream->wrapperdata);
}
} else {
- if (options & REPORT_ERRORS)
- zend_error(E_WARNING, "HTTP request failed! %s", tmp_line);
+ php_stream_wrapper_log_error(wrapper, options, "HTTP request failed! %s", tmp_line);
}
}
out:
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.34 php4/main/php_streams.h:1.35
--- php4/main/php_streams.h:1.34 Tue Jun 11 05:36:48 2002
+++ php4/main/php_streams.h Sun Aug 11 06:53:10 2002
@@ -169,7 +169,14 @@
php_stream_wrapper_ops *wops; /* operations the wrapper can perform */
void *abstract; /* context for the wrapper */
int is_url; /* so that PG(allow_url_fopen) can be respected */
+
+ /* support for wrappers to return (multiple) error messages to the stream opener */
+ int err_count;
+ char **err_stack;
};
+
+/* pushes an error message onto the stack for a wrapper instance */
+PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options TSRMLS_DC, const
char *fmt, ...);
struct _php_stream {
php_stream_ops *ops;
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.59 php4/main/streams.c:1.60
--- php4/main/streams.c:1.59 Sat Aug 10 16:20:55 2002
+++ php4/main/streams.c Sun Aug 11 06:53:10 2002
@@ -20,7 +20,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: streams.c,v 1.59 2002/08/10 20:20:55 wez Exp $ */
+/* $Id: streams.c,v 1.60 2002/08/11 10:53:10 wez Exp $ */
#define _GNU_SOURCE
#include "php.h"
@@ -1286,6 +1286,26 @@
return NULL;
}
+PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options TSRMLS_DC, const
char *fmt, ...)
+{
+ va_list args;
+ char *buffer = NULL;
+
+ va_start(args, fmt);
+ vspprintf(&buffer, 0, fmt, args);
+ va_end(args);
+
+ if (options & REPORT_ERRORS || wrapper == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, buffer);
+ efree(buffer);
+ } else {
+ /* append to stack */
+ wrapper->err_stack = erealloc(wrapper->err_stack, (wrapper->err_count + 1) * sizeof(char
*));
+ if (wrapper->err_stack)
+ wrapper->err_stack[wrapper->err_count++] = buffer;
+ }
+}
+
/* {{{ php_stream_open_wrapper_ex */
PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options,
char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
@@ -1305,8 +1325,13 @@
wrapper = locate_url_wrapper(path, &path_to_open, options TSRMLS_CC);
if (wrapper) {
+
+ /* prepare error stack */
+ wrapper->err_count = 0;
+ wrapper->err_stack = NULL;
+
stream = wrapper->wops->stream_opener(wrapper,
- path_to_open, mode, options,
+ path_to_open, mode, options ^ REPORT_ERRORS,
opened_path, context STREAMS_REL_CC TSRMLS_CC);
if (stream)
stream->wrapper = wrapper;
@@ -1326,8 +1351,8 @@
if (options & REPORT_ERRORS) {
char *tmp = estrdup(path);
php_strip_url_passwd(tmp);
- zend_error(E_WARNING, "%s(\"%s\") - could not make seekable - %s",
- get_active_function_name(TSRMLS_C), tmp, strerror(errno));
+ php_error_docref1(NULL TSRMLS_CC, tmp, E_WARNING, "could not make seekable - %s",
+ tmp, strerror(errno));
efree(tmp);
options ^= REPORT_ERRORS;
@@ -1338,14 +1363,54 @@
char *tmp = estrdup(path);
char *msg;
- if (wrapper)
- msg = strerror(errno);
- else
+ if (wrapper) {
+ if (wrapper->err_count) {
+ int i;
+ size_t l;
+ int brlen;
+ char *br;
+
+ if (PG(html_errors)) {
+ brlen = 7;
+ br = "<br />\n";
+ } else {
+ brlen = 1;
+ br = "\n";
+ }
+
+ for (i = 0, l = 0; i < wrapper->err_count; i++) {
+ l += strlen(wrapper->err_stack[i]);
+ if (i < wrapper->err_count - 1)
+ l += brlen;
+ }
+ msg = emalloc(l + 1);
+ msg[0] = '\0';
+ for (i = 0; i < wrapper->err_count; i++) {
+ strcat(msg, wrapper->err_stack[i]);
+ if (i < wrapper->err_count - 1)
+ strcat(msg, br);
+ }
+
+ } else {
+ msg = strerror(errno);
+ }
+ } else {
msg = "no suitable wrapper could be found";
+ }
php_strip_url_passwd(tmp);
- zend_error(E_WARNING, "%s(\"%s\") - %s", get_active_function_name(TSRMLS_C),
tmp, msg);
+ php_error_docref1(NULL TSRMLS_CC, tmp, E_WARNING, "failed to create stream: %s", msg);
efree(tmp);
+ }
+ if (wrapper) {
+ /* tidy up the error stack */
+ int i;
+
+ for (i = 0; i < wrapper->err_count; i++)
+ efree(wrapper->err_stack[i]);
+ if (wrapper->err_stack)
+ efree(wrapper->err_stack);
+ wrapper->err_stack = NULL;
}
return stream;
}