Skip to content

Commit 93f795b

Browse files
committed
Add proxy support to ftp using http wrapper
1 parent 49e31e4 commit 93f795b

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ PHP NEWS
1515
. stream_socket_enable_crypto() (Wez)
1616
- PHP will now respect extension dependencies when initializing. (Wez)
1717
- Added Cursor support for MySQL 5.0.x in mysqli (Georg)
18+
- Added proxy support to ftp wrapper via http. (Sara)
1819
- Added MDTM support to ftp_url_stat. (Sara)
1920
- Added zlib stream filter suport. (Sara)
2021
- Added bz2 stream filter support. (Sara)

ext/standard/ftp_fopen_wrapper.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
384384
int allow_overwrite = 0;
385385
int read_write = 0;
386386

387+
if (context &&
388+
php_stream_context_get_option(context, "ftp", "proxy", &tmpzval) == SUCCESS) {
389+
/* Use http wrapper to proxy ftp request */
390+
return php_stream_url_wrap_http(wrapper, path, mode, options, opened_path, context STREAMS_CC TSRMLS_CC);
391+
}
392+
387393
tmp_line[0] = '\0';
388394

389395
if (strpbrk(mode, "r+")) {
@@ -1130,7 +1136,7 @@ static php_stream_wrapper_ops ftp_stream_wops = {
11301136
php_stream_ftp_stream_stat,
11311137
php_stream_ftp_url_stat, /* stat_url */
11321138
php_stream_ftp_opendir, /* opendir */
1133-
"FTP",
1139+
"ftp",
11341140
php_stream_ftp_unlink, /* unlink */
11351141
php_stream_ftp_rename, /* rename */
11361142
php_stream_ftp_mkdir, /* mkdir */

ext/standard/http_fopen_wrapper.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,37 +114,50 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
114114
return NULL;
115115
}
116116

117-
if (strpbrk(mode, "awx+")) {
118-
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections.");
119-
return NULL;
120-
}
121-
122117
resource = php_url_parse(path);
123118
if (resource == NULL) {
124119
return NULL;
125120
}
126121

127122
if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) {
128-
php_url_free(resource);
129-
return php_stream_open_wrapper_ex(path, mode, ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
130-
}
131-
132-
use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
133-
/* choose default ports */
134-
if (use_ssl && resource->port == 0)
135-
resource->port = 443;
136-
else if (resource->port == 0)
137-
resource->port = 80;
138-
139-
if (context && !use_ssl &&
140-
php_stream_context_get_option(context, "http", "proxy", &tmpzval) == SUCCESS &&
141-
Z_TYPE_PP(tmpzval) == IS_STRING &&
142-
Z_STRLEN_PP(tmpzval) > 0) {
143-
/* Don't use proxy server for SSL resources */
123+
if (!context ||
124+
php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == FAILURE ||
125+
Z_TYPE_PP(tmpzval) != IS_STRING ||
126+
Z_STRLEN_PP(tmpzval) <= 0) {
127+
php_url_free(resource);
128+
return php_stream_open_wrapper_ex(path, mode, ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
129+
}
130+
/* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
131+
request_fulluri = 1;
132+
use_ssl = 0;
133+
144134
transport_len = Z_STRLEN_PP(tmpzval);
145135
transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
146136
} else {
147-
transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port);
137+
/* Normal http request (possibly with proxy) */
138+
139+
if (strpbrk(mode, "awx+")) {
140+
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections.");
141+
return NULL;
142+
}
143+
144+
use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
145+
/* choose default ports */
146+
if (use_ssl && resource->port == 0)
147+
resource->port = 443;
148+
else if (resource->port == 0)
149+
resource->port = 80;
150+
151+
if (context && !use_ssl &&
152+
php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == SUCCESS &&
153+
Z_TYPE_PP(tmpzval) == IS_STRING &&
154+
Z_STRLEN_PP(tmpzval) > 0) {
155+
/* Don't use proxy server for SSL resources */
156+
transport_len = Z_STRLEN_PP(tmpzval);
157+
transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
158+
} else {
159+
transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port);
160+
}
148161
}
149162

150163
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,
192205
}
193206

194207
/* Should we send the entire path in the request line, default to no. */
195-
if (context &&
208+
if (!request_fulluri &&
209+
context &&
196210
php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) {
197211
(*tmpzval)->refcount++;
198212
SEPARATE_ZVAL(tmpzval);
@@ -292,7 +306,8 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
292306

293307
/* Send Host: header so name-based virtual hosts work */
294308
if ((have_header & HTTP_HEADER_HOST) == 0) {
295-
if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
309+
if ((use_ssl && resource->port != 443 && resource->port != 0) ||
310+
(!use_ssl && resource->port != 80 && resource->port != 0)) {
296311
if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
297312
php_stream_write(stream, scratch, strlen(scratch));
298313
} else {
@@ -525,7 +540,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
525540
} else {
526541
strlcpy(new_path, location, sizeof(new_path));
527542
}
528-
stream = php_stream_url_wrap_http_ex(NULL, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC);
543+
stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC);
529544
if (stream && stream->wrapperdata) {
530545
entryp = &entry;
531546
MAKE_STD_ZVAL(entry);
@@ -594,7 +609,7 @@ static php_stream_wrapper_ops http_stream_wops = {
594609
php_stream_http_stream_stat,
595610
NULL, /* stat_url */
596611
NULL, /* opendir */
597-
"HTTP",
612+
"http",
598613
NULL, /* unlink */
599614
NULL, /* rename */
600615
NULL, /* mkdir */

0 commit comments

Comments
 (0)