From 93f795bb9e74a2e7cae0ead7deacdf311a7aa6c7 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Sat, 24 Jul 2004 04:01:48 +0000 Subject: [PATCH] Add proxy support to ftp using http wrapper --- NEWS | 1 + ext/standard/ftp_fopen_wrapper.c | 8 +++- ext/standard/http_fopen_wrapper.c | 67 +++++++++++++++++++------------ 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/NEWS b/NEWS index e38dd004c0..6da907236c 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,7 @@ PHP NEWS . stream_socket_enable_crypto() (Wez) - PHP will now respect extension dependencies when initializing. (Wez) - Added Cursor support for MySQL 5.0.x in mysqli (Georg) +- Added proxy support to ftp wrapper via http. (Sara) - Added MDTM support to ftp_url_stat. (Sara) - Added zlib stream filter suport. (Sara) - Added bz2 stream filter support. (Sara) diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c index 3ef9c117fd..4d6b28b53d 100644 --- a/ext/standard/ftp_fopen_wrapper.c +++ b/ext/standard/ftp_fopen_wrapper.c @@ -384,6 +384,12 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch int allow_overwrite = 0; int read_write = 0; + if (context && + php_stream_context_get_option(context, "ftp", "proxy", &tmpzval) == SUCCESS) { + /* Use http wrapper to proxy ftp request */ + return php_stream_url_wrap_http(wrapper, path, mode, options, opened_path, context STREAMS_CC TSRMLS_CC); + } + tmp_line[0] = '\0'; if (strpbrk(mode, "r+")) { @@ -1130,7 +1136,7 @@ static php_stream_wrapper_ops ftp_stream_wops = { php_stream_ftp_stream_stat, php_stream_ftp_url_stat, /* stat_url */ php_stream_ftp_opendir, /* opendir */ - "FTP", + "ftp", php_stream_ftp_unlink, /* unlink */ php_stream_ftp_rename, /* rename */ php_stream_ftp_mkdir, /* mkdir */ diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index d45864fedd..682ef2759d 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -114,37 +114,50 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path, return NULL; } - if (strpbrk(mode, "awx+")) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections."); - return NULL; - } - resource = php_url_parse(path); if (resource == NULL) { return NULL; } if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) { - php_url_free(resource); - return php_stream_open_wrapper_ex(path, mode, ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); - } - - use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's'; - /* choose default ports */ - if (use_ssl && resource->port == 0) - resource->port = 443; - else if (resource->port == 0) - resource->port = 80; - - if (context && !use_ssl && - php_stream_context_get_option(context, "http", "proxy", &tmpzval) == SUCCESS && - Z_TYPE_PP(tmpzval) == IS_STRING && - Z_STRLEN_PP(tmpzval) > 0) { - /* Don't use proxy server for SSL resources */ + if (!context || + php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == FAILURE || + Z_TYPE_PP(tmpzval) != IS_STRING || + Z_STRLEN_PP(tmpzval) <= 0) { + php_url_free(resource); + return php_stream_open_wrapper_ex(path, mode, ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); + } + /* Called from a non-http wrapper with http proxying requested (i.e. ftp) */ + request_fulluri = 1; + use_ssl = 0; + transport_len = Z_STRLEN_PP(tmpzval); transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)); } else { - transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port); + /* Normal http request (possibly with proxy) */ + + if (strpbrk(mode, "awx+")) { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections."); + return NULL; + } + + use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's'; + /* choose default ports */ + if (use_ssl && resource->port == 0) + resource->port = 443; + else if (resource->port == 0) + resource->port = 80; + + if (context && !use_ssl && + php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == SUCCESS && + Z_TYPE_PP(tmpzval) == IS_STRING && + Z_STRLEN_PP(tmpzval) > 0) { + /* Don't use proxy server for SSL resources */ + transport_len = Z_STRLEN_PP(tmpzval); + transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)); + } else { + transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port); + } } stream = php_stream_xport_create(transport_string, transport_len, options, @@ -192,7 +205,8 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path, } /* Should we send the entire path in the request line, default to no. */ - if (context && + if (!request_fulluri && + context && php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) { (*tmpzval)->refcount++; SEPARATE_ZVAL(tmpzval); @@ -292,7 +306,8 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path, /* Send Host: header so name-based virtual hosts work */ if ((have_header & HTTP_HEADER_HOST) == 0) { - if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) { + if ((use_ssl && resource->port != 443 && resource->port != 0) || + (!use_ssl && resource->port != 80 && resource->port != 0)) { if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0) php_stream_write(stream, scratch, strlen(scratch)); } else { @@ -525,7 +540,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path, } else { strlcpy(new_path, location, sizeof(new_path)); } - stream = php_stream_url_wrap_http_ex(NULL, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC); + stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC); if (stream && stream->wrapperdata) { entryp = &entry; MAKE_STD_ZVAL(entry); @@ -594,7 +609,7 @@ static php_stream_wrapper_ops http_stream_wops = { php_stream_http_stream_stat, NULL, /* stat_url */ NULL, /* opendir */ - "HTTP", + "http", NULL, /* unlink */ NULL, /* rename */ NULL, /* mkdir */ -- 2.40.0