A more generalized approach to fopen-wrappers / did i get it right ?
[email protected] (Hartmut Holzgraefe) Wed, 31 May 2000 23:34:44 +0200
| Newsgroups | php.version4 |
|---|---|
| Organization | SIX Offene Systeme GmbH |
| Message-ID | <[email protected]> |
the attached diff is an attempt to get a more generalized fopen-wrappers feature that should allow dynamic registration of additional url/uri types by splitting up the wrapper function into handler functions for one protocol type each and putting pointers to these functions into a hash with the protocol name as key it should allow the addition of protocols like https: through extension modules in a very near future, for now it just gets a little more functional encapsulation into fopen-wrappers.c and as a bonus makes it possible to read from file: urls as this is my first greater work that leaves the relative secure ground of extension functions i'm not really sure if i did the hash handling and friends right, especially as i im currently not able to test any build besides unix/cgi comments anyone ? -- Hartmut Holzgraefe [email protected] http://www.six.de
fopen-wrappers.diff
(text/plain, 6.7 KB)
Index: main.c
===================================================================
RCS file: /repository/php4/main.c,v
retrieving revision 1.258
diff -u -b -w -B -r1.258 main.c
--- main.c 2000/05/31 14:02:36 1.258
+++ main.c 2000/05/31 21:21:48
@@ -748,6 +748,10 @@
php_printf("PHP: Unable to parse configuration file.\n");
return FAILURE;
}
+ if (php_init_fopen_wrappers() == FAILURE) {
+ php_printf("PHP: Unable to initialize fopen url wrappers.\n");
+ return FAILURE;
+ }
return SUCCESS;
}
Index: fopen-wrappers.c
===================================================================
RCS file: /repository/php4/fopen-wrappers.c,v
retrieving revision 1.60
diff -u -b -w -B -r1.60 fopen-wrappers.c
--- fopen-wrappers.c 2000/05/27 16:38:49 1.60
+++ fopen-wrappers.c 2000/05/31 21:21:48
@@ -77,10 +77,50 @@
#include <sys/un.h>
#endif
-static FILE *php_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path);
+typedef FILE * (*php_fopen_url_wrapper_t) (const char *, char *, int, int *, int *, char **) ;
+
+static FILE *php_fopen_url_wrapper(const char *, char *, int, int *, int *, char **);
+static FILE *php_fopen_url_wrap_http(const char *, char *, int, int *, int *, char **);
+static FILE *php_fopen_url_wrap_ftp(const char *, char *, int, int *, int *, char **);
+static FILE *php_fopen_url_wrap_php(const char *, char *, int, int *, int *, char **);
+
int php_get_ftp_result(int socketd);
+
+
+HashTable fopen_url_wrappers_hash;
+
+PHPAPI int php_register_url_wrapper(char *protocol, FILE * (*wrapper)(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)) {
+ return zend_hash_add(&fopen_url_wrappers_hash, protocol, strlen(protocol)+1, &wrapper, sizeof(wrapper), NULL);
+}
+
+int php_init_fopen_wrappers(void)
+{
+ int status = SUCCESS;
+#if PHP_URL_FOPEN
+ if (zend_hash_init(&fopen_url_wrappers_hash, 0, NULL, NULL, 1)==FAILURE) {
+ return FAILURE;
+ }
+
+ if(FAILURE==php_register_url_wrapper("http",php_fopen_url_wrap_http)) {
+ status = FAILURE;
+ } else
+ if(FAILURE==php_register_url_wrapper("ftp",php_fopen_url_wrap_ftp)) {
+ status = FAILURE;
+ } else
+ if(FAILURE==php_register_url_wrapper("php",php_fopen_url_wrap_php)) {
+ status = FAILURE;
+ }
+
+ if(FAILURE==status) {
+ zend_hash_destroy(&fopen_url_wrappers_hash);
+ }
+#endif
+ return status;
+}
+
+
/*
When open_basedir is not NULL, check if the given filename is located in
open_basedir. Returns -1 if error or not in the open_basedir, else 0
@@ -446,26 +486,21 @@
* Otherwise, fopen is called as usual and the file pointer is returned.
*/
-static FILE *php_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
-{
- url *resource;
- int result;
- char *scratch;
- unsigned char *tmp;
+static FILE *php_fopen_url_wrap_http(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
+{
+ FILE *fp=NULL;
+ url *resource=NULL;
+ struct sockaddr_in server;
char tmp_line[512];
char location[512];
char hdr_line[8192];
- char *tpath, *ttpath;
int body = 0;
+ char *scratch;
+ unsigned char *tmp;
+ int len;
int reqok = 0;
- int i, len;
-
- FILE *fp = NULL;
- struct sockaddr_in server;
- unsigned short portno;
- if (!strncasecmp(path, "http://", 7)) {
resource = url_parse((char *) path);
if (resource == NULL) {
php_error(E_WARNING, "Invalid URL specified, %s", path);
@@ -617,17 +652,20 @@
free_url(resource);
*issock = 1;
return (fp);
- } else if (!strncasecmp(path, "php://", 6)) {
- const char *res = path + 6;
-
- if (!strcasecmp(res, "stdin")) {
- return fdopen(STDIN_FILENO, mode);
- } else if (!strcasecmp(res, "stdout")) {
- return fdopen(STDOUT_FILENO, mode);
- } else if (!strcasecmp(res, "stderr")) {
- return fdopen(STDERR_FILENO, mode);
}
- } else if (!strncasecmp(path, "ftp://", 6)) {
+
+ static FILE *php_fopen_url_wrap_ftp(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
+{
+ FILE *fp=NULL;
+ url *resource=NULL;
+ struct sockaddr_in server;
+ char tmp_line[512];
+ unsigned short portno;
+ char *scratch;
+ int result;
+ int i;
+ char *tpath, *ttpath;
+
resource = url_parse((char *) path);
if (resource == NULL) {
php_error(E_WARNING, "Invalid URL specified, %s", path);
@@ -902,9 +940,69 @@
free_url(resource);
*issock = 1;
return (fp);
- } else {
+}
+
+static FILE *php_fopen_url_wrap_php(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
+{
+ const char *res = path + 6;
+
+ *issock = 0;
+
+ if (!strcasecmp(res, "stdin")) {
+ return fdopen(STDIN_FILENO, mode);
+ } else if (!strcasecmp(res, "stdout")) {
+ return fdopen(STDOUT_FILENO, mode);
+ } else if (!strcasecmp(res, "stderr")) {
+ return fdopen(STDERR_FILENO, mode);
+ }
+
+ return NULL;
+}
+
+static FILE *php_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
+{
+ FILE *fp = NULL;
+ const char *p;
+ const char *protocol=NULL;
+ int n=0;
+
+ for(p=path;isalnum(*p);p++)
+ n++;
+ if((*p==':')&&(n>1)) {
+ protocol=path;
+ }
+
+ if(protocol) {
+ php_fopen_url_wrapper_t *wrapper=NULL;
+ char *protocopy = emalloc(n+1);
+
+ if(protocopy) {
+ strncpy(protocopy,protocol,n);
+ protocopy[n]='\0';
+ if(FAILURE==zend_hash_find(&fopen_url_wrappers_hash, protocopy, n+1,(void **)&wrapper)) {
+ *wrapper=NULL;
+ }
+ efree(protocopy);
+ }
+ if(!wrapper) return NULL;
+ return (*wrapper)(path, mode, options, issock, socketd, opened_path);
+ }
+
+ if( !protocol || !strncasecmp(protocol, "file",n)){
PLS_FETCH();
+ *issock = 0;
+
+ if(protocol) {
+ if(path[n+1]=='/') {
+ if(path[n+2]=='/') {
+ php_error(E_WARNING, "remote host file access not supported, %s", path);
+ return NULL;
+ }
+ }
+ path+= n+1;
+ }
+
if (options & USE_PATH) {
fp = php_fopen_with_path((char *) path, mode, PG(include_path), opened_path);
} else {
@@ -921,14 +1019,11 @@
}
}
- *issock = 0;
return (fp);
}
- /* NOTREACHED */
- SOCK_FCLOSE(*socketd);
- *socketd = 0;
+ php_error(E_WARNING, "Invalid URL specified, %s", path);
return NULL;
}
Index: fopen-wrappers.h
===================================================================
RCS file: /repository/php4/fopen-wrappers.h,v
retrieving revision 1.18
diff -u -b -w -B -r1.18 fopen-wrappers.h
--- fopen-wrappers.h 2000/05/18 15:34:21 1.18
+++ fopen-wrappers.h 2000/05/31 21:21:48
@@ -78,6 +78,9 @@
PHPAPI char *expand_filepath(char *filepath);
+int php_init_fopen_wrappers(void);
+PHPAPI int php_register_url_wrapper(char *protocol, FILE * (*wrapper)(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path));
+
#endif
/*
* Local variables: