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