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