1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
14    |          Jim Winstead <jimw@php.net>                                 |
15    |          Hartmut Holzgraefe <hholzgra@php.net>                       |
16    |          Wez Furlong <wez@thebrainroom.com>                          |
17    |          Sara Golemon <pollita@php.net>                              |
18    +----------------------------------------------------------------------+
19  */
20 
21 #include "php.h"
22 #include "php_globals.h"
23 #include "php_streams.h"
24 #include "php_network.h"
25 #include "php_ini.h"
26 #include "ext/standard/basic_functions.h"
27 #include "zend_smart_str.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 
36 #ifdef PHP_WIN32
37 #define O_RDONLY _O_RDONLY
38 #include "win32/param.h"
39 #else
40 #include <sys/param.h>
41 #endif
42 
43 #include "php_standard.h"
44 
45 #include <sys/types.h>
46 #if HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 
50 #ifdef PHP_WIN32
51 #include <winsock2.h>
52 #else
53 #include <netinet/in.h>
54 #include <netdb.h>
55 #if HAVE_ARPA_INET_H
56 #include <arpa/inet.h>
57 #endif
58 #endif
59 
60 #if defined(PHP_WIN32) || defined(__riscos__)
61 #undef AF_UNIX
62 #endif
63 
64 #if defined(AF_UNIX)
65 #include <sys/un.h>
66 #endif
67 
68 #include "php_fopen_wrappers.h"
69 
70 #define HTTP_HEADER_BLOCK_SIZE		1024
71 #define PHP_URL_REDIRECT_MAX		20
72 #define HTTP_HEADER_USER_AGENT		1
73 #define HTTP_HEADER_HOST			2
74 #define HTTP_HEADER_AUTH			4
75 #define HTTP_HEADER_FROM			8
76 #define HTTP_HEADER_CONTENT_LENGTH	16
77 #define HTTP_HEADER_TYPE			32
78 #define HTTP_HEADER_CONNECTION		64
79 
80 #define HTTP_WRAPPER_HEADER_INIT    1
81 #define HTTP_WRAPPER_REDIRECTED     2
82 
strip_header(char * header_bag,char * lc_header_bag,const char * lc_header_name)83 static inline void strip_header(char *header_bag, char *lc_header_bag,
84 		const char *lc_header_name)
85 {
86 	char *lc_header_start = strstr(lc_header_bag, lc_header_name);
87 	if (lc_header_start
88 	&& (lc_header_start == lc_header_bag || *(lc_header_start-1) == '\n')
89 	) {
90 		char *header_start = header_bag + (lc_header_start - lc_header_bag);
91 		char *lc_eol = strchr(lc_header_start, '\n');
92 
93 		if (lc_eol) {
94 			char *eol = header_start + (lc_eol - lc_header_start);
95 			size_t eollen = strlen(lc_eol);
96 
97 			memmove(lc_header_start, lc_eol+1, eollen);
98 			memmove(header_start, eol+1, eollen);
99 		} else {
100 			*lc_header_start = '\0';
101 			*header_start = '\0';
102 		}
103 	}
104 }
105 
check_has_header(const char * headers,const char * header)106 static bool check_has_header(const char *headers, const char *header) {
107 	const char *s = headers;
108 	while ((s = strstr(s, header))) {
109 		if (s == headers || *(s-1) == '\n') {
110 			return 1;
111 		}
112 		s++;
113 	}
114 	return 0;
115 }
116 
php_stream_url_wrap_http_ex(php_stream_wrapper * wrapper,const char * path,const char * mode,int options,zend_string ** opened_path,php_stream_context * context,int redirect_max,int flags,zval * response_header STREAMS_DC)117 static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
118 		const char *path, const char *mode, int options, zend_string **opened_path,
119 		php_stream_context *context, int redirect_max, int flags,
120 		zval *response_header STREAMS_DC) /* {{{ */
121 {
122 	php_stream *stream = NULL;
123 	php_url *resource = NULL;
124 	int use_ssl;
125 	int use_proxy = 0;
126 	zend_string *tmp = NULL;
127 	char *ua_str = NULL;
128 	zval *ua_zval = NULL, *tmpzval = NULL, ssl_proxy_peer_name;
129 	char location[HTTP_HEADER_BLOCK_SIZE];
130 	int reqok = 0;
131 	char *http_header_line = NULL;
132 	char tmp_line[128];
133 	size_t chunk_size = 0, file_size = 0;
134 	int eol_detect = 0;
135 	zend_string *transport_string;
136 	zend_string *errstr = NULL;
137 	int have_header = 0;
138 	bool request_fulluri = 0, ignore_errors = 0;
139 	struct timeval timeout;
140 	char *user_headers = NULL;
141 	int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
142 	int redirected = ((flags & HTTP_WRAPPER_REDIRECTED) != 0);
143 	bool follow_location = 1;
144 	php_stream_filter *transfer_encoding = NULL;
145 	int response_code;
146 	smart_str req_buf = {0};
147 	bool custom_request_method;
148 
149 	tmp_line[0] = '\0';
150 
151 	if (redirect_max < 1) {
152 		php_stream_wrapper_log_error(wrapper, options, "Redirection limit reached, aborting");
153 		return NULL;
154 	}
155 
156 	resource = php_url_parse(path);
157 	if (resource == NULL) {
158 		return NULL;
159 	}
160 
161 	if (!zend_string_equals_literal_ci(resource->scheme, "http") &&
162 		!zend_string_equals_literal_ci(resource->scheme, "https")) {
163 		if (!context ||
164 			(tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) == NULL ||
165 			Z_TYPE_P(tmpzval) != IS_STRING ||
166 			Z_STRLEN_P(tmpzval) == 0) {
167 			php_url_free(resource);
168 			return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
169 		}
170 		/* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
171 		request_fulluri = 1;
172 		use_ssl = 0;
173 		use_proxy = 1;
174 		transport_string = zend_string_copy(Z_STR_P(tmpzval));
175 	} else {
176 		/* Normal http request (possibly with proxy) */
177 
178 		if (strpbrk(mode, "awx+")) {
179 			php_stream_wrapper_log_error(wrapper, options, "HTTP wrapper does not support writeable connections");
180 			php_url_free(resource);
181 			return NULL;
182 		}
183 
184 		use_ssl = resource->scheme && (ZSTR_LEN(resource->scheme) > 4) && ZSTR_VAL(resource->scheme)[4] == 's';
185 		/* choose default ports */
186 		if (use_ssl && resource->port == 0)
187 			resource->port = 443;
188 		else if (resource->port == 0)
189 			resource->port = 80;
190 
191 		if (context &&
192 			(tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) != NULL &&
193 			Z_TYPE_P(tmpzval) == IS_STRING &&
194 			Z_STRLEN_P(tmpzval) > 0) {
195 			use_proxy = 1;
196 			transport_string = zend_string_copy(Z_STR_P(tmpzval));
197 		} else {
198 			transport_string = zend_strpprintf(0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", ZSTR_VAL(resource->host), resource->port);
199 		}
200 	}
201 
202 	if (context && (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "timeout")) != NULL) {
203 		double d = zval_get_double(tmpzval);
204 #ifndef PHP_WIN32
205 		timeout.tv_sec = (time_t) d;
206 		timeout.tv_usec = (size_t) ((d - timeout.tv_sec) * 1000000);
207 #else
208 		timeout.tv_sec = (long) d;
209 		timeout.tv_usec = (long) ((d - timeout.tv_sec) * 1000000);
210 #endif
211 	} else {
212 #ifndef PHP_WIN32
213 		timeout.tv_sec = FG(default_socket_timeout);
214 #else
215 		timeout.tv_sec = (long)FG(default_socket_timeout);
216 #endif
217 		timeout.tv_usec = 0;
218 	}
219 
220 	stream = php_stream_xport_create(ZSTR_VAL(transport_string), ZSTR_LEN(transport_string), options,
221 			STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
222 			NULL, &timeout, context, &errstr, NULL);
223 
224 	if (stream) {
225 		php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &timeout);
226 	}
227 
228 	if (errstr) {
229 		php_stream_wrapper_log_error(wrapper, options, "%s", ZSTR_VAL(errstr));
230 		zend_string_release_ex(errstr, 0);
231 		errstr = NULL;
232 	}
233 
234 	zend_string_release(transport_string);
235 
236 	if (stream && use_proxy && use_ssl) {
237 		smart_str header = {0};
238 
239 		/* Set peer_name or name verification will try to use the proxy server name */
240 		if (!context || (tmpzval = php_stream_context_get_option(context, "ssl", "peer_name")) == NULL) {
241 			ZVAL_STR_COPY(&ssl_proxy_peer_name, resource->host);
242 			php_stream_context_set_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_name", &ssl_proxy_peer_name);
243 			zval_ptr_dtor(&ssl_proxy_peer_name);
244 		}
245 
246 		smart_str_appendl(&header, "CONNECT ", sizeof("CONNECT ")-1);
247 		smart_str_appends(&header, ZSTR_VAL(resource->host));
248 		smart_str_appendc(&header, ':');
249 		smart_str_append_unsigned(&header, resource->port);
250 		smart_str_appendl(&header, " HTTP/1.0\r\n", sizeof(" HTTP/1.0\r\n")-1);
251 
252 	    /* check if we have Proxy-Authorization header */
253 		if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
254 			char *s, *p;
255 
256 			if (Z_TYPE_P(tmpzval) == IS_ARRAY) {
257 				zval *tmpheader = NULL;
258 
259 				ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmpzval), tmpheader) {
260 					if (Z_TYPE_P(tmpheader) == IS_STRING) {
261 						s = Z_STRVAL_P(tmpheader);
262 						do {
263 							while (*s == ' ' || *s == '\t') s++;
264 							p = s;
265 							while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
266 							if (*p == ':') {
267 								p++;
268 								if (p - s == sizeof("Proxy-Authorization:") - 1 &&
269 								    zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
270 								        "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
271 									while (*p != 0 && *p != '\r' && *p !='\n') p++;
272 									smart_str_appendl(&header, s, p - s);
273 									smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
274 									goto finish;
275 								} else {
276 									while (*p != 0 && *p != '\r' && *p !='\n') p++;
277 								}
278 							}
279 							s = p;
280 							while (*s == '\r' || *s == '\n') s++;
281 						} while (*s != 0);
282 					}
283 				} ZEND_HASH_FOREACH_END();
284 			} else if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval)) {
285 				s = Z_STRVAL_P(tmpzval);
286 				do {
287 					while (*s == ' ' || *s == '\t') s++;
288 					p = s;
289 					while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
290 					if (*p == ':') {
291 						p++;
292 						if (p - s == sizeof("Proxy-Authorization:") - 1 &&
293 						    zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
294 						        "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
295 							while (*p != 0 && *p != '\r' && *p !='\n') p++;
296 							smart_str_appendl(&header, s, p - s);
297 							smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
298 							goto finish;
299 						} else {
300 							while (*p != 0 && *p != '\r' && *p !='\n') p++;
301 						}
302 					}
303 					s = p;
304 					while (*s == '\r' || *s == '\n') s++;
305 				} while (*s != 0);
306 			}
307 		}
308 finish:
309 		smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
310 
311 		if (php_stream_write(stream, ZSTR_VAL(header.s), ZSTR_LEN(header.s)) != ZSTR_LEN(header.s)) {
312 			php_stream_wrapper_log_error(wrapper, options, "Cannot connect to HTTPS server through proxy");
313 			php_stream_close(stream);
314 			stream = NULL;
315 		}
316 		smart_str_free(&header);
317 
318 		if (stream) {
319 			char header_line[HTTP_HEADER_BLOCK_SIZE];
320 
321 			/* get response header */
322 			while (php_stream_gets(stream, header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) {
323 				if (header_line[0] == '\n' ||
324 				    header_line[0] == '\r' ||
325 				    header_line[0] == '\0') {
326 				  break;
327 				}
328 			}
329 		}
330 
331 		/* enable SSL transport layer */
332 		if (stream) {
333 			if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 ||
334 			    php_stream_xport_crypto_enable(stream, 1) < 0) {
335 				php_stream_wrapper_log_error(wrapper, options, "Cannot connect to HTTPS server through proxy");
336 				php_stream_close(stream);
337 				stream = NULL;
338 			}
339 		}
340 	}
341 
342 	if (stream == NULL)
343 		goto out;
344 
345 	/* avoid buffering issues while reading header */
346 	if (options & STREAM_WILL_CAST)
347 		chunk_size = php_stream_set_chunk_size(stream, 1);
348 
349 	/* avoid problems with auto-detecting when reading the headers -> the headers
350 	 * are always in canonical \r\n format */
351 	eol_detect = stream->flags & (PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
352 	stream->flags &= ~(PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
353 
354 	php_stream_context_set(stream, context);
355 
356 	php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
357 
358 	if (header_init && context && (tmpzval = php_stream_context_get_option(context, "http", "max_redirects")) != NULL) {
359 		redirect_max = (int)zval_get_long(tmpzval);
360 	}
361 
362 	custom_request_method = 0;
363 	if (context && (tmpzval = php_stream_context_get_option(context, "http", "method")) != NULL) {
364 		if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0) {
365 			/* As per the RFC, automatically redirected requests MUST NOT use other methods than
366 			 * GET and HEAD unless it can be confirmed by the user */
367 			if (!redirected
368 				|| zend_string_equals_literal(Z_STR_P(tmpzval), "GET")
369 				|| zend_string_equals_literal(Z_STR_P(tmpzval), "HEAD")
370 			) {
371 				custom_request_method = 1;
372 				smart_str_append(&req_buf, Z_STR_P(tmpzval));
373 				smart_str_appendc(&req_buf, ' ');
374 			}
375 		}
376 	}
377 
378 	if (!custom_request_method) {
379 		smart_str_appends(&req_buf, "GET ");
380 	}
381 
382 	/* Should we send the entire path in the request line, default to no. */
383 	if (!request_fulluri && context &&
384 		(tmpzval = php_stream_context_get_option(context, "http", "request_fulluri")) != NULL) {
385 		request_fulluri = zend_is_true(tmpzval);
386 	}
387 
388 	if (request_fulluri) {
389 		/* Ask for everything */
390 		smart_str_appends(&req_buf, path);
391 	} else {
392 		/* Send the traditional /path/to/file?query_string */
393 
394 		/* file */
395 		if (resource->path && ZSTR_LEN(resource->path)) {
396 			smart_str_appends(&req_buf, ZSTR_VAL(resource->path));
397 		} else {
398 			smart_str_appendc(&req_buf, '/');
399 		}
400 
401 		/* query string */
402 		if (resource->query) {
403 			smart_str_appendc(&req_buf, '?');
404 			smart_str_appends(&req_buf, ZSTR_VAL(resource->query));
405 		}
406 	}
407 
408 	/* protocol version we are speaking */
409 	if (context && (tmpzval = php_stream_context_get_option(context, "http", "protocol_version")) != NULL) {
410 		char *protocol_version;
411 		spprintf(&protocol_version, 0, "%.1F", zval_get_double(tmpzval));
412 
413 		smart_str_appends(&req_buf, " HTTP/");
414 		smart_str_appends(&req_buf, protocol_version);
415 		smart_str_appends(&req_buf, "\r\n");
416 		efree(protocol_version);
417 	} else {
418 		smart_str_appends(&req_buf, " HTTP/1.1\r\n");
419 	}
420 
421 	if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
422 		tmp = NULL;
423 
424 		if (Z_TYPE_P(tmpzval) == IS_ARRAY) {
425 			zval *tmpheader = NULL;
426 			smart_str tmpstr = {0};
427 
428 			ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmpzval), tmpheader) {
429 				if (Z_TYPE_P(tmpheader) == IS_STRING) {
430 					smart_str_append(&tmpstr, Z_STR_P(tmpheader));
431 					smart_str_appendl(&tmpstr, "\r\n", sizeof("\r\n") - 1);
432 				}
433 			} ZEND_HASH_FOREACH_END();
434 			smart_str_0(&tmpstr);
435 			/* Remove newlines and spaces from start and end. there's at least one extra \r\n at the end that needs to go. */
436 			if (tmpstr.s) {
437 				tmp = php_trim(tmpstr.s, NULL, 0, 3);
438 				smart_str_free(&tmpstr);
439 			}
440 		} else if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval)) {
441 			/* Remove newlines and spaces from start and end php_trim will estrndup() */
442 			tmp = php_trim(Z_STR_P(tmpzval), NULL, 0, 3);
443 		}
444 		if (tmp && ZSTR_LEN(tmp)) {
445 			char *s;
446 			char *t;
447 
448 			user_headers = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
449 
450 			if (ZSTR_IS_INTERNED(tmp)) {
451 				tmp = zend_string_init(ZSTR_VAL(tmp), ZSTR_LEN(tmp), 0);
452 			} else if (GC_REFCOUNT(tmp) > 1) {
453 				GC_DELREF(tmp);
454 				tmp = zend_string_init(ZSTR_VAL(tmp), ZSTR_LEN(tmp), 0);
455 			}
456 
457 			/* Make lowercase for easy comparison against 'standard' headers */
458 			zend_str_tolower(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
459 			t = ZSTR_VAL(tmp);
460 
461 			if (!header_init) {
462 				/* strip POST headers on redirect */
463 				strip_header(user_headers, t, "content-length:");
464 				strip_header(user_headers, t, "content-type:");
465 			}
466 
467 			if (check_has_header(t, "user-agent:")) {
468 				have_header |= HTTP_HEADER_USER_AGENT;
469 			}
470 			if (check_has_header(t, "host:")) {
471 				have_header |= HTTP_HEADER_HOST;
472 			}
473 			if (check_has_header(t, "from:")) {
474 				have_header |= HTTP_HEADER_FROM;
475 			}
476 			if (check_has_header(t, "authorization:")) {
477 				have_header |= HTTP_HEADER_AUTH;
478 			}
479 			if (check_has_header(t, "content-length:")) {
480 				have_header |= HTTP_HEADER_CONTENT_LENGTH;
481 			}
482 			if (check_has_header(t, "content-type:")) {
483 				have_header |= HTTP_HEADER_TYPE;
484 			}
485 			if (check_has_header(t, "connection:")) {
486 				have_header |= HTTP_HEADER_CONNECTION;
487 			}
488 
489 			/* remove Proxy-Authorization header */
490 			if (use_proxy && use_ssl && (s = strstr(t, "proxy-authorization:")) &&
491 			    (s == t || *(s-1) == '\n')) {
492 				char *p = s + sizeof("proxy-authorization:") - 1;
493 
494 				while (s > t && (*(s-1) == ' ' || *(s-1) == '\t')) s--;
495 				while (*p != 0 && *p != '\r' && *p != '\n') p++;
496 				while (*p == '\r' || *p == '\n') p++;
497 				if (*p == 0) {
498 					if (s == t) {
499 						efree(user_headers);
500 						user_headers = NULL;
501 					} else {
502 						while (s > t && (*(s-1) == '\r' || *(s-1) == '\n')) s--;
503 						user_headers[s - t] = 0;
504 					}
505 				} else {
506 					memmove(user_headers + (s - t), user_headers + (p - t), strlen(p) + 1);
507 				}
508 			}
509 
510 		}
511 		if (tmp) {
512 			zend_string_release_ex(tmp, 0);
513 		}
514 	}
515 
516 	/* auth header if it was specified */
517 	if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
518 		/* make scratch large enough to hold the whole URL (over-estimate) */
519 		size_t scratch_len = strlen(path) + 1;
520 		char *scratch = emalloc(scratch_len);
521 		zend_string *stmp;
522 
523 		/* decode the strings first */
524 		php_url_decode(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user));
525 
526 		strcpy(scratch, ZSTR_VAL(resource->user));
527 		strcat(scratch, ":");
528 
529 		/* Note: password is optional! */
530 		if (resource->pass) {
531 			php_url_decode(ZSTR_VAL(resource->pass), ZSTR_LEN(resource->pass));
532 			strcat(scratch, ZSTR_VAL(resource->pass));
533 		}
534 
535 		stmp = php_base64_encode((unsigned char*)scratch, strlen(scratch));
536 
537 		smart_str_appends(&req_buf, "Authorization: Basic ");
538 		smart_str_appends(&req_buf, ZSTR_VAL(stmp));
539 		smart_str_appends(&req_buf, "\r\n");
540 
541 		php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
542 
543 		zend_string_free(stmp);
544 		efree(scratch);
545 	}
546 
547 	/* if the user has configured who they are, send a From: line */
548 	if (!(have_header & HTTP_HEADER_FROM) && FG(from_address)) {
549 		smart_str_appends(&req_buf, "From: ");
550 		smart_str_appends(&req_buf, FG(from_address));
551 		smart_str_appends(&req_buf, "\r\n");
552 	}
553 
554 	/* Send Host: header so name-based virtual hosts work */
555 	if ((have_header & HTTP_HEADER_HOST) == 0) {
556 		smart_str_appends(&req_buf, "Host: ");
557 		smart_str_appends(&req_buf, ZSTR_VAL(resource->host));
558 		if ((use_ssl && resource->port != 443 && resource->port != 0) ||
559 			(!use_ssl && resource->port != 80 && resource->port != 0)) {
560 			smart_str_appendc(&req_buf, ':');
561 			smart_str_append_unsigned(&req_buf, resource->port);
562 		}
563 		smart_str_appends(&req_buf, "\r\n");
564 	}
565 
566 	/* Send a Connection: close header to avoid hanging when the server
567 	 * interprets the RFC literally and establishes a keep-alive connection,
568 	 * unless the user specifically requests something else by specifying a
569 	 * Connection header in the context options. Send that header even for
570 	 * HTTP/1.0 to avoid issues when the server respond with a HTTP/1.1
571 	 * keep-alive response, which is the preferred response type. */
572 	if ((have_header & HTTP_HEADER_CONNECTION) == 0) {
573 		smart_str_appends(&req_buf, "Connection: close\r\n");
574 	}
575 
576 	if (context &&
577 	    (ua_zval = php_stream_context_get_option(context, "http", "user_agent")) != NULL &&
578 		Z_TYPE_P(ua_zval) == IS_STRING) {
579 		ua_str = Z_STRVAL_P(ua_zval);
580 	} else if (FG(user_agent)) {
581 		ua_str = FG(user_agent);
582 	}
583 
584 	if (((have_header & HTTP_HEADER_USER_AGENT) == 0) && ua_str) {
585 #define _UA_HEADER "User-Agent: %s\r\n"
586 		char *ua;
587 		size_t ua_len;
588 
589 		ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
590 
591 		/* ensure the header is only sent if user_agent is not blank */
592 		if (ua_len > sizeof(_UA_HEADER)) {
593 			ua = emalloc(ua_len + 1);
594 			if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
595 				ua[ua_len] = 0;
596 				smart_str_appendl(&req_buf, ua, ua_len);
597 			} else {
598 				php_error_docref(NULL, E_WARNING, "Cannot construct User-agent header");
599 			}
600 			efree(ua);
601 		}
602 	}
603 
604 	if (user_headers) {
605 		/* A bit weird, but some servers require that Content-Length be sent prior to Content-Type for POST
606 		 * see bug #44603 for details. Since Content-Type maybe part of user's headers we need to do this check first.
607 		 */
608 		if (
609 				header_init &&
610 				context &&
611 				!(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
612 				(tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
613 				Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0
614 		) {
615 			smart_str_appends(&req_buf, "Content-Length: ");
616 			smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
617 			smart_str_appends(&req_buf, "\r\n");
618 			have_header |= HTTP_HEADER_CONTENT_LENGTH;
619 		}
620 
621 		smart_str_appends(&req_buf, user_headers);
622 		smart_str_appends(&req_buf, "\r\n");
623 		efree(user_headers);
624 	}
625 
626 	/* Request content, such as for POST requests */
627 	if (header_init && context &&
628 		(tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
629 		Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0) {
630 		if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
631 			smart_str_appends(&req_buf, "Content-Length: ");
632 			smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
633 			smart_str_appends(&req_buf, "\r\n");
634 		}
635 		if (!(have_header & HTTP_HEADER_TYPE)) {
636 			smart_str_appends(&req_buf, "Content-Type: application/x-www-form-urlencoded\r\n");
637 			php_error_docref(NULL, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
638 		}
639 		smart_str_appends(&req_buf, "\r\n");
640 		smart_str_appendl(&req_buf, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
641 	} else {
642 		smart_str_appends(&req_buf, "\r\n");
643 	}
644 
645 	/* send it */
646 	php_stream_write(stream, ZSTR_VAL(req_buf.s), ZSTR_LEN(req_buf.s));
647 
648 	location[0] = '\0';
649 
650 	if (Z_ISUNDEF_P(response_header)) {
651 		array_init(response_header);
652 	}
653 
654 	{
655 		/* get response header */
656 		size_t tmp_line_len;
657 		if (!php_stream_eof(stream) &&
658 			php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL) {
659 			zval http_response;
660 
661 			if (tmp_line_len > 9) {
662 				response_code = atoi(tmp_line + 9);
663 			} else {
664 				response_code = 0;
665 			}
666 			if (context && NULL != (tmpzval = php_stream_context_get_option(context, "http", "ignore_errors"))) {
667 				ignore_errors = zend_is_true(tmpzval);
668 			}
669 			/* when we request only the header, don't fail even on error codes */
670 			if ((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) {
671 				reqok = 1;
672 			}
673 
674 			/* status codes of 1xx are "informational", and will be followed by a real response
675 			 * e.g "100 Continue". RFC 7231 states that unexpected 1xx status MUST be parsed,
676 			 * and MAY be ignored. As such, we need to skip ahead to the "real" status*/
677 			if (response_code >= 100 && response_code < 200 && response_code != 101) {
678 				/* consume lines until we find a line starting 'HTTP/1' */
679 				while (
680 					!php_stream_eof(stream)
681 					&& php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL
682 					&& ( tmp_line_len < sizeof("HTTP/1") - 1 || strncasecmp(tmp_line, "HTTP/1", sizeof("HTTP/1") - 1) )
683 				);
684 
685 				if (tmp_line_len > 9) {
686 					response_code = atoi(tmp_line + 9);
687 				} else {
688 					response_code = 0;
689 				}
690 			}
691 			/* all status codes in the 2xx range are defined by the specification as successful;
692 			 * all status codes in the 3xx range are for redirection, and so also should never
693 			 * fail */
694 			if (response_code >= 200 && response_code < 400) {
695 				reqok = 1;
696 			} else {
697 				switch(response_code) {
698 					case 403:
699 						php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT,
700 								tmp_line, response_code);
701 						break;
702 					default:
703 						/* safety net in the event tmp_line == NULL */
704 						if (!tmp_line_len) {
705 							tmp_line[0] = '\0';
706 						}
707 						php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE,
708 								tmp_line, response_code);
709 				}
710 			}
711 			if (tmp_line_len >= 1 && tmp_line[tmp_line_len - 1] == '\n') {
712 				--tmp_line_len;
713 				if (tmp_line_len >= 1 &&tmp_line[tmp_line_len - 1] == '\r') {
714 					--tmp_line_len;
715 				}
716 			}
717 			ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
718 			zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response);
719 		} else {
720 			php_stream_close(stream);
721 			stream = NULL;
722 			php_stream_wrapper_log_error(wrapper, options, "HTTP request failed!");
723 			goto out;
724 		}
725 	}
726 
727 	/* read past HTTP headers */
728 
729 	while (!php_stream_eof(stream)) {
730 		size_t http_header_line_length;
731 
732 		if (http_header_line != NULL) {
733 			efree(http_header_line);
734 		}
735 		if ((http_header_line = php_stream_get_line(stream, NULL, 0, &http_header_line_length)) && *http_header_line != '\n' && *http_header_line != '\r') {
736 			char *e = http_header_line + http_header_line_length - 1;
737 			char *http_header_value;
738 
739 			while (e >= http_header_line && (*e == '\n' || *e == '\r')) {
740 				e--;
741 			}
742 
743 			/* The primary definition of an HTTP header in RFC 7230 states:
744 			 * > Each header field consists of a case-insensitive field name followed
745 			 * > by a colon (":"), optional leading whitespace, the field value, and
746 			 * > optional trailing whitespace. */
747 
748 			/* Strip trailing whitespace */
749 			while (e >= http_header_line && (*e == ' ' || *e == '\t')) {
750 				e--;
751 			}
752 
753 			/* Terminate header line */
754 			e++;
755 			*e = '\0';
756 			http_header_line_length = e - http_header_line;
757 
758 			http_header_value = memchr(http_header_line, ':', http_header_line_length);
759 			if (http_header_value) {
760 				http_header_value++; /* Skip ':' */
761 
762 				/* Strip leading whitespace */
763 				while (http_header_value < e
764 						&& (*http_header_value == ' ' || *http_header_value == '\t')) {
765 					http_header_value++;
766 				}
767 			} else {
768 				/* There is no colon. Set the value to the end of the header line, which is
769 				 * effectively an empty string. */
770 				http_header_value = e;
771 			}
772 
773 			if (!strncasecmp(http_header_line, "Location:", sizeof("Location:")-1)) {
774 				if (context && (tmpzval = php_stream_context_get_option(context, "http", "follow_location")) != NULL) {
775 					follow_location = zval_is_true(tmpzval);
776 				} else if (!((response_code >= 300 && response_code < 304)
777 						|| 307 == response_code || 308 == response_code)) {
778 					/* we shouldn't redirect automatically
779 					if follow_location isn't set and response_code not in (300, 301, 302, 303 and 307)
780 					see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1
781 					RFC 7238 defines 308: http://tools.ietf.org/html/rfc7238 */
782 					follow_location = 0;
783 				}
784 				strlcpy(location, http_header_value, sizeof(location));
785 			} else if (!strncasecmp(http_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
786 				php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_value, 0);
787 			} else if (!strncasecmp(http_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
788 				file_size = atoi(http_header_value);
789 				php_stream_notify_file_size(context, file_size, http_header_line, 0);
790 			} else if (
791 				!strncasecmp(http_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
792 				&& !strncasecmp(http_header_value, "Chunked", sizeof("Chunked")-1)
793 			) {
794 
795 				/* create filter to decode response body */
796 				if (!(options & STREAM_ONLY_GET_HEADERS)) {
797 					zend_long decode = 1;
798 
799 					if (context && (tmpzval = php_stream_context_get_option(context, "http", "auto_decode")) != NULL) {
800 						decode = zend_is_true(tmpzval);
801 					}
802 					if (decode) {
803 						transfer_encoding = php_stream_filter_create("dechunk", NULL, php_stream_is_persistent(stream));
804 						if (transfer_encoding) {
805 							/* don't store transfer-encodeing header */
806 							continue;
807 						}
808 					}
809 				}
810 			}
811 
812 			{
813 				zval http_header;
814 				ZVAL_STRINGL(&http_header, http_header_line, http_header_line_length);
815 				zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header);
816 			}
817 		} else {
818 			break;
819 		}
820 	}
821 
822 	if (!reqok || (location[0] != '\0' && follow_location)) {
823 		if (!follow_location || (((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) && redirect_max <= 1)) {
824 			goto out;
825 		}
826 
827 		if (location[0] != '\0')
828 			php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
829 
830 		php_stream_close(stream);
831 		stream = NULL;
832 
833 		if (transfer_encoding) {
834 			php_stream_filter_free(transfer_encoding);
835 			transfer_encoding = NULL;
836 		}
837 
838 		if (location[0] != '\0') {
839 
840 			char new_path[HTTP_HEADER_BLOCK_SIZE];
841 			char loc_path[HTTP_HEADER_BLOCK_SIZE];
842 
843 			*new_path='\0';
844 			if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) &&
845 							strncasecmp(location, "https://", sizeof("https://")-1) &&
846 							strncasecmp(location, "ftp://", sizeof("ftp://")-1) &&
847 							strncasecmp(location, "ftps://", sizeof("ftps://")-1)))
848 			{
849 				if (*location != '/') {
850 					if (*(location+1) != '\0' && resource->path) {
851 						char *s = strrchr(ZSTR_VAL(resource->path), '/');
852 						if (!s) {
853 							s = ZSTR_VAL(resource->path);
854 							if (!ZSTR_LEN(resource->path)) {
855 								zend_string_release_ex(resource->path, 0);
856 								resource->path = zend_string_init("/", 1, 0);
857 								s = ZSTR_VAL(resource->path);
858 							} else {
859 								*s = '/';
860 							}
861 						}
862 						s[1] = '\0';
863 						if (resource->path &&
864 							ZSTR_VAL(resource->path)[0] == '/' &&
865 							ZSTR_VAL(resource->path)[1] == '\0') {
866 							snprintf(loc_path, sizeof(loc_path) - 1, "%s%s", ZSTR_VAL(resource->path), location);
867 						} else {
868 							snprintf(loc_path, sizeof(loc_path) - 1, "%s/%s", ZSTR_VAL(resource->path), location);
869 						}
870 					} else {
871 						snprintf(loc_path, sizeof(loc_path) - 1, "/%s", location);
872 					}
873 				} else {
874 					strlcpy(loc_path, location, sizeof(loc_path));
875 				}
876 				if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
877 					snprintf(new_path, sizeof(new_path) - 1, "%s://%s:%d%s", ZSTR_VAL(resource->scheme), ZSTR_VAL(resource->host), resource->port, loc_path);
878 				} else {
879 					snprintf(new_path, sizeof(new_path) - 1, "%s://%s%s", ZSTR_VAL(resource->scheme), ZSTR_VAL(resource->host), loc_path);
880 				}
881 			} else {
882 				strlcpy(new_path, location, sizeof(new_path));
883 			}
884 
885 			php_url_free(resource);
886 			/* check for invalid redirection URLs */
887 			if ((resource = php_url_parse(new_path)) == NULL) {
888 				php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path);
889 				goto out;
890 			}
891 
892 #define CHECK_FOR_CNTRL_CHARS(val) { \
893 	if (val) { \
894 		unsigned char *s, *e; \
895 		ZSTR_LEN(val) = php_url_decode(ZSTR_VAL(val), ZSTR_LEN(val)); \
896 		s = (unsigned char*)ZSTR_VAL(val); e = s + ZSTR_LEN(val); \
897 		while (s < e) { \
898 			if (iscntrl(*s)) { \
899 				php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); \
900 				goto out; \
901 			} \
902 			s++; \
903 		} \
904 	} \
905 }
906 			/* check for control characters in login, password & path */
907 			if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
908 				CHECK_FOR_CNTRL_CHARS(resource->user);
909 				CHECK_FOR_CNTRL_CHARS(resource->pass);
910 				CHECK_FOR_CNTRL_CHARS(resource->path);
911 			}
912 			stream = php_stream_url_wrap_http_ex(
913 				wrapper, new_path, mode, options, opened_path, context,
914 				--redirect_max, HTTP_WRAPPER_REDIRECTED, response_header STREAMS_CC);
915 		} else {
916 			php_stream_wrapper_log_error(wrapper, options, "HTTP request failed! %s", tmp_line);
917 		}
918 	}
919 out:
920 
921 	smart_str_free(&req_buf);
922 
923 	if (http_header_line) {
924 		efree(http_header_line);
925 	}
926 
927 	if (resource) {
928 		php_url_free(resource);
929 	}
930 
931 	if (stream) {
932 		if (header_init) {
933 			ZVAL_COPY(&stream->wrapperdata, response_header);
934 		}
935 		php_stream_notify_progress_init(context, 0, file_size);
936 
937 		/* Restore original chunk size now that we're done with headers */
938 		if (options & STREAM_WILL_CAST)
939 			php_stream_set_chunk_size(stream, (int)chunk_size);
940 
941 		/* restore the users auto-detect-line-endings setting */
942 		stream->flags |= eol_detect;
943 
944 		/* as far as streams are concerned, we are now at the start of
945 		 * the stream */
946 		stream->position = 0;
947 
948 		/* restore mode */
949 		strlcpy(stream->mode, mode, sizeof(stream->mode));
950 
951 		if (transfer_encoding) {
952 			php_stream_filter_append(&stream->readfilters, transfer_encoding);
953 		}
954 	}
955 
956 	return stream;
957 }
958 /* }}} */
959 
php_stream_url_wrap_http(php_stream_wrapper * wrapper,const char * path,const char * mode,int options,zend_string ** opened_path,php_stream_context * context STREAMS_DC)960 php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
961 {
962 	php_stream *stream;
963 	zval headers;
964 	ZVAL_UNDEF(&headers);
965 
966 	stream = php_stream_url_wrap_http_ex(
967 		wrapper, path, mode, options, opened_path, context,
968 		PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT, &headers STREAMS_CC);
969 
970 	if (!Z_ISUNDEF(headers)) {
971 		if (FAILURE == zend_set_local_var_str(
972 				"http_response_header", sizeof("http_response_header")-1, &headers, 0)) {
973 			zval_ptr_dtor(&headers);
974 		}
975 	}
976 
977 	return stream;
978 }
979 /* }}} */
980 
php_stream_http_stream_stat(php_stream_wrapper * wrapper,php_stream * stream,php_stream_statbuf * ssb)981 static int php_stream_http_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
982 {
983 	/* one day, we could fill in the details based on Date: and Content-Length:
984 	 * headers.  For now, we return with a failure code to prevent the underlying
985 	 * file's details from being used instead. */
986 	return -1;
987 }
988 /* }}} */
989 
990 static const php_stream_wrapper_ops http_stream_wops = {
991 	php_stream_url_wrap_http,
992 	NULL, /* stream_close */
993 	php_stream_http_stream_stat,
994 	NULL, /* stat_url */
995 	NULL, /* opendir */
996 	"http",
997 	NULL, /* unlink */
998 	NULL, /* rename */
999 	NULL, /* mkdir */
1000 	NULL, /* rmdir */
1001 	NULL
1002 };
1003 
1004 PHPAPI const php_stream_wrapper php_stream_http_wrapper = {
1005 	&http_stream_wops,
1006 	NULL,
1007 	1 /* is_url */
1008 };
1009