1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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 zend_string *tmp = NULL;
121 char *ua_str = NULL;
122 zval *ua_zval = NULL, *tmpzval = NULL, ssl_proxy_peer_name;
123 char location[HTTP_HEADER_BLOCK_SIZE];
124 zval response_header;
125 int reqok = 0;
126 char *http_header_line = NULL;
127 char tmp_line[128];
128 size_t chunk_size = 0, file_size = 0;
129 int eol_detect = 0;
130 char *transport_string;
131 zend_string *errstr = NULL;
132 size_t transport_len;
133 int have_header = 0;
134 zend_bool request_fulluri = 0, ignore_errors = 0;
135 struct timeval timeout;
136 char *user_headers = NULL;
137 int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
138 int redirected = ((flags & HTTP_WRAPPER_REDIRECTED) != 0);
139 zend_bool follow_location = 1;
140 php_stream_filter *transfer_encoding = NULL;
141 int response_code;
142 zend_array *symbol_table;
143 smart_str req_buf = {0};
144 zend_bool custom_request_method;
145
146 ZVAL_UNDEF(&response_header);
147 tmp_line[0] = '\0';
148
149 if (redirect_max < 1) {
150 php_stream_wrapper_log_error(wrapper, options, "Redirection limit reached, aborting");
151 return NULL;
152 }
153
154 resource = php_url_parse(path);
155 if (resource == NULL) {
156 return NULL;
157 }
158
159 if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) {
160 if (!context ||
161 (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) == NULL ||
162 Z_TYPE_P(tmpzval) != IS_STRING ||
163 Z_STRLEN_P(tmpzval) <= 0) {
164 php_url_free(resource);
165 return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
166 }
167 /* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
168 request_fulluri = 1;
169 use_ssl = 0;
170 use_proxy = 1;
171
172 transport_len = Z_STRLEN_P(tmpzval);
173 transport_string = estrndup(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
174 } else {
175 /* Normal http request (possibly with proxy) */
176
177 if (strpbrk(mode, "awx+")) {
178 php_stream_wrapper_log_error(wrapper, options, "HTTP wrapper does not support writeable connections");
179 php_url_free(resource);
180 return NULL;
181 }
182
183 use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
184 /* choose default ports */
185 if (use_ssl && resource->port == 0)
186 resource->port = 443;
187 else if (resource->port == 0)
188 resource->port = 80;
189
190 if (context &&
191 (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) != NULL &&
192 Z_TYPE_P(tmpzval) == IS_STRING &&
193 Z_STRLEN_P(tmpzval) > 0) {
194 use_proxy = 1;
195 transport_len = Z_STRLEN_P(tmpzval);
196 transport_string = estrndup(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
197 } else {
198 transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", 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(transport_string, transport_len, 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(errstr);
231 errstr = NULL;
232 }
233
234 efree(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_STRING(&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, 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 || (Z_STRLEN_P(tmpzval) == 3 && memcmp("GET", Z_STRVAL_P(tmpzval), 3) == 0)
369 || (Z_STRLEN_P(tmpzval) == 4 && memcmp("HEAD",Z_STRVAL_P(tmpzval), 4) == 0)
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 && *resource->path) {
396 smart_str_appends(&req_buf, 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, 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.0\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_REFCOUNT(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 php_strtolower(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 ((s = strstr(t, "user-agent:")) &&
468 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
469 *(s-1) == '\t' || *(s-1) == ' ')) {
470 have_header |= HTTP_HEADER_USER_AGENT;
471 }
472 if ((s = strstr(t, "host:")) &&
473 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
474 *(s-1) == '\t' || *(s-1) == ' ')) {
475 have_header |= HTTP_HEADER_HOST;
476 }
477 if ((s = strstr(t, "from:")) &&
478 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
479 *(s-1) == '\t' || *(s-1) == ' ')) {
480 have_header |= HTTP_HEADER_FROM;
481 }
482 if ((s = strstr(t, "authorization:")) &&
483 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
484 *(s-1) == '\t' || *(s-1) == ' ')) {
485 have_header |= HTTP_HEADER_AUTH;
486 }
487 if ((s = strstr(t, "content-length:")) &&
488 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
489 *(s-1) == '\t' || *(s-1) == ' ')) {
490 have_header |= HTTP_HEADER_CONTENT_LENGTH;
491 }
492 if ((s = strstr(t, "content-type:")) &&
493 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
494 *(s-1) == '\t' || *(s-1) == ' ')) {
495 have_header |= HTTP_HEADER_TYPE;
496 }
497 if ((s = strstr(t, "connection:")) &&
498 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
499 *(s-1) == '\t' || *(s-1) == ' ')) {
500 have_header |= HTTP_HEADER_CONNECTION;
501 }
502 /* remove Proxy-Authorization header */
503 if (use_proxy && use_ssl && (s = strstr(t, "proxy-authorization:")) &&
504 (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
505 *(s-1) == '\t' || *(s-1) == ' ')) {
506 char *p = s + sizeof("proxy-authorization:") - 1;
507
508 while (s > t && (*(s-1) == ' ' || *(s-1) == '\t')) s--;
509 while (*p != 0 && *p != '\r' && *p != '\n') p++;
510 while (*p == '\r' || *p == '\n') p++;
511 if (*p == 0) {
512 if (s == t) {
513 efree(user_headers);
514 user_headers = NULL;
515 } else {
516 while (s > t && (*(s-1) == '\r' || *(s-1) == '\n')) s--;
517 user_headers[s - t] = 0;
518 }
519 } else {
520 memmove(user_headers + (s - t), user_headers + (p - t), strlen(p) + 1);
521 }
522 }
523
524 }
525 if (tmp) {
526 zend_string_release(tmp);
527 }
528 }
529
530 /* auth header if it was specified */
531 if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
532 /* make scratch large enough to hold the whole URL (over-estimate) */
533 size_t scratch_len = strlen(path) + 1;
534 char *scratch = emalloc(scratch_len);
535 zend_string *stmp;
536
537 /* decode the strings first */
538 php_url_decode(resource->user, strlen(resource->user));
539
540 strcpy(scratch, resource->user);
541 strcat(scratch, ":");
542
543 /* Note: password is optional! */
544 if (resource->pass) {
545 php_url_decode(resource->pass, strlen(resource->pass));
546 strcat(scratch, resource->pass);
547 }
548
549 stmp = php_base64_encode((unsigned char*)scratch, strlen(scratch));
550
551 smart_str_appends(&req_buf, "Authorization: Basic ");
552 smart_str_appends(&req_buf, ZSTR_VAL(stmp));
553 smart_str_appends(&req_buf, "\r\n");
554
555 php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
556
557 zend_string_free(stmp);
558 efree(scratch);
559 }
560
561 /* if the user has configured who they are, send a From: line */
562 if (!(have_header & HTTP_HEADER_FROM) && FG(from_address)) {
563 smart_str_appends(&req_buf, "From: ");
564 smart_str_appends(&req_buf, FG(from_address));
565 smart_str_appends(&req_buf, "\r\n");
566 }
567
568 /* Send Host: header so name-based virtual hosts work */
569 if ((have_header & HTTP_HEADER_HOST) == 0) {
570 smart_str_appends(&req_buf, "Host: ");
571 smart_str_appends(&req_buf, resource->host);
572 if ((use_ssl && resource->port != 443 && resource->port != 0) ||
573 (!use_ssl && resource->port != 80 && resource->port != 0)) {
574 smart_str_appendc(&req_buf, ':');
575 smart_str_append_unsigned(&req_buf, resource->port);
576 }
577 smart_str_appends(&req_buf, "\r\n");
578 }
579
580 /* Send a Connection: close header to avoid hanging when the server
581 * interprets the RFC literally and establishes a keep-alive connection,
582 * unless the user specifically requests something else by specifying a
583 * Connection header in the context options. Send that header even for
584 * HTTP/1.0 to avoid issues when the server respond with a HTTP/1.1
585 * keep-alive response, which is the preferred response type. */
586 if ((have_header & HTTP_HEADER_CONNECTION) == 0) {
587 smart_str_appends(&req_buf, "Connection: close\r\n");
588 }
589
590 if (context &&
591 (ua_zval = php_stream_context_get_option(context, "http", "user_agent")) != NULL &&
592 Z_TYPE_P(ua_zval) == IS_STRING) {
593 ua_str = Z_STRVAL_P(ua_zval);
594 } else if (FG(user_agent)) {
595 ua_str = FG(user_agent);
596 }
597
598 if (((have_header & HTTP_HEADER_USER_AGENT) == 0) && ua_str) {
599 #define _UA_HEADER "User-Agent: %s\r\n"
600 char *ua;
601 size_t ua_len;
602
603 ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
604
605 /* ensure the header is only sent if user_agent is not blank */
606 if (ua_len > sizeof(_UA_HEADER)) {
607 ua = emalloc(ua_len + 1);
608 if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
609 ua[ua_len] = 0;
610 smart_str_appendl(&req_buf, ua, ua_len);
611 } else {
612 php_error_docref(NULL, E_WARNING, "Cannot construct User-agent header");
613 }
614 efree(ua);
615 }
616 }
617
618 if (user_headers) {
619 /* A bit weird, but some servers require that Content-Length be sent prior to Content-Type for POST
620 * see bug #44603 for details. Since Content-Type maybe part of user's headers we need to do this check first.
621 */
622 if (
623 header_init &&
624 context &&
625 !(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
626 (tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
627 Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0
628 ) {
629 smart_str_appends(&req_buf, "Content-Length: ");
630 smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
631 smart_str_appends(&req_buf, "\r\n");
632 have_header |= HTTP_HEADER_CONTENT_LENGTH;
633 }
634
635 smart_str_appends(&req_buf, user_headers);
636 smart_str_appends(&req_buf, "\r\n");
637 efree(user_headers);
638 }
639
640 /* Request content, such as for POST requests */
641 if (header_init && context &&
642 (tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
643 Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0) {
644 if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
645 smart_str_appends(&req_buf, "Content-Length: ");
646 smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
647 smart_str_appends(&req_buf, "\r\n");
648 }
649 if (!(have_header & HTTP_HEADER_TYPE)) {
650 smart_str_appends(&req_buf, "Content-Type: application/x-www-form-urlencoded\r\n");
651 php_error_docref(NULL, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
652 }
653 smart_str_appends(&req_buf, "\r\n");
654 smart_str_appendl(&req_buf, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
655 } else {
656 smart_str_appends(&req_buf, "\r\n");
657 }
658
659 /* send it */
660 php_stream_write(stream, ZSTR_VAL(req_buf.s), ZSTR_LEN(req_buf.s));
661
662 location[0] = '\0';
663
664 symbol_table = zend_rebuild_symbol_table();
665
666 if (header_init) {
667 zval ztmp;
668 array_init(&ztmp);
669 zend_set_local_var_str("http_response_header", sizeof("http_response_header")-1, &ztmp, 0);
670 }
671
672 {
673 zval *response_header_ptr = zend_hash_str_find_ind(symbol_table, "http_response_header", sizeof("http_response_header")-1);
674 if (!response_header_ptr || Z_TYPE_P(response_header_ptr) != IS_ARRAY) {
675 ZVAL_UNDEF(&response_header);
676 goto out;
677 } else {
678 ZVAL_COPY(&response_header, response_header_ptr);
679 }
680 }
681
682 if (!php_stream_eof(stream)) {
683 size_t tmp_line_len;
684 /* get response header */
685
686 if (php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL) {
687 zval http_response;
688
689 if (tmp_line_len > 9) {
690 response_code = atoi(tmp_line + 9);
691 } else {
692 response_code = 0;
693 }
694 if (context && NULL != (tmpzval = php_stream_context_get_option(context, "http", "ignore_errors"))) {
695 ignore_errors = zend_is_true(tmpzval);
696 }
697 /* when we request only the header, don't fail even on error codes */
698 if ((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) {
699 reqok = 1;
700 }
701
702 /* status codes of 1xx are "informational", and will be followed by a real response
703 * e.g "100 Continue". RFC 7231 states that unexpected 1xx status MUST be parsed,
704 * and MAY be ignored. As such, we need to skip ahead to the "real" status*/
705 if (response_code >= 100 && response_code < 200) {
706 /* consume lines until we find a line starting 'HTTP/1' */
707 while (
708 !php_stream_eof(stream)
709 && php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL
710 && ( tmp_line_len < sizeof("HTTP/1") - 1 || strncasecmp(tmp_line, "HTTP/1", sizeof("HTTP/1") - 1) )
711 );
712
713 if (tmp_line_len > 9) {
714 response_code = atoi(tmp_line + 9);
715 } else {
716 response_code = 0;
717 }
718 }
719 /* all status codes in the 2xx range are defined by the specification as successful;
720 * all status codes in the 3xx range are for redirection, and so also should never
721 * fail */
722 if (response_code >= 200 && response_code < 400) {
723 reqok = 1;
724 } else {
725 switch(response_code) {
726 case 403:
727 php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT,
728 tmp_line, response_code);
729 break;
730 default:
731 /* safety net in the event tmp_line == NULL */
732 if (!tmp_line_len) {
733 tmp_line[0] = '\0';
734 }
735 php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE,
736 tmp_line, response_code);
737 }
738 }
739 if (tmp_line_len >= 1 && tmp_line[tmp_line_len - 1] == '\n') {
740 --tmp_line_len;
741 if (tmp_line_len >= 1 &&tmp_line[tmp_line_len - 1] == '\r') {
742 --tmp_line_len;
743 }
744 }
745 ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
746 zend_hash_next_index_insert(Z_ARRVAL(response_header), &http_response);
747 }
748 } else {
749 php_stream_wrapper_log_error(wrapper, options, "HTTP request failed, unexpected end of socket!");
750 goto out;
751 }
752
753 /* read past HTTP headers */
754
755 http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
756
757 while (!php_stream_eof(stream)) {
758 size_t http_header_line_length;
759
760 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') {
761 char *e = http_header_line + http_header_line_length - 1;
762 char *http_header_value;
763 if (*e != '\n') {
764 do { /* partial header */
765 if (php_stream_get_line(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE, &http_header_line_length) == NULL) {
766 php_stream_wrapper_log_error(wrapper, options, "Failed to read HTTP headers");
767 goto out;
768 }
769 e = http_header_line + http_header_line_length - 1;
770 } while (*e != '\n');
771 continue;
772 }
773 while (e >= http_header_line && (*e == '\n' || *e == '\r')) {
774 e--;
775 }
776
777 /* The primary definition of an HTTP header in RFC 7230 states:
778 * > Each header field consists of a case-insensitive field name followed
779 * > by a colon (":"), optional leading whitespace, the field value, and
780 * > optional trailing whitespace. */
781
782 /* Strip trailing whitespace */
783 while (e >= http_header_line && (*e == ' ' || *e == '\t')) {
784 e--;
785 }
786
787 /* Terminate header line */
788 e++;
789 *e = '\0';
790 http_header_line_length = e - http_header_line;
791
792 http_header_value = memchr(http_header_line, ':', http_header_line_length);
793 if (http_header_value) {
794 http_header_value++; /* Skip ':' */
795
796 /* Strip leading whitespace */
797 while (http_header_value < e
798 && (*http_header_value == ' ' || *http_header_value == '\t')) {
799 http_header_value++;
800 }
801 } else {
802 /* There is no colon. Set the value to the end of the header line, which is
803 * effectively an empty string. */
804 http_header_value = e;
805 }
806
807 if (!strncasecmp(http_header_line, "Location:", sizeof("Location:")-1)) {
808 if (context && (tmpzval = php_stream_context_get_option(context, "http", "follow_location")) != NULL) {
809 follow_location = zval_is_true(tmpzval);
810 } else if (!((response_code >= 300 && response_code < 304)
811 || 307 == response_code || 308 == response_code)) {
812 /* we shouldn't redirect automatically
813 if follow_location isn't set and response_code not in (300, 301, 302, 303 and 307)
814 see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1
815 RFC 7238 defines 308: http://tools.ietf.org/html/rfc7238 */
816 follow_location = 0;
817 }
818 strlcpy(location, http_header_value, sizeof(location));
819 } else if (!strncasecmp(http_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
820 php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_value, 0);
821 } else if (!strncasecmp(http_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
822 file_size = atoi(http_header_value);
823 php_stream_notify_file_size(context, file_size, http_header_line, 0);
824 } else if (
825 !strncasecmp(http_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
826 && !strncasecmp(http_header_value, "Chunked", sizeof("Chunked")-1)
827 ) {
828
829 /* create filter to decode response body */
830 if (!(options & STREAM_ONLY_GET_HEADERS)) {
831 zend_long decode = 1;
832
833 if (context && (tmpzval = php_stream_context_get_option(context, "http", "auto_decode")) != NULL) {
834 decode = zend_is_true(tmpzval);
835 }
836 if (decode) {
837 transfer_encoding = php_stream_filter_create("dechunk", NULL, php_stream_is_persistent(stream));
838 if (transfer_encoding) {
839 /* don't store transfer-encodeing header */
840 continue;
841 }
842 }
843 }
844 }
845
846 {
847 zval http_header;
848 ZVAL_STRINGL(&http_header, http_header_line, http_header_line_length);
849 zend_hash_next_index_insert(Z_ARRVAL(response_header), &http_header);
850 }
851 } else {
852 break;
853 }
854 }
855
856 if (!reqok || (location[0] != '\0' && follow_location)) {
857 if (!follow_location || (((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) && redirect_max <= 1)) {
858 goto out;
859 }
860
861 if (location[0] != '\0')
862 php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
863
864 php_stream_close(stream);
865 stream = NULL;
866
867 if (location[0] != '\0') {
868
869 char new_path[HTTP_HEADER_BLOCK_SIZE];
870 char loc_path[HTTP_HEADER_BLOCK_SIZE];
871
872 *new_path='\0';
873 if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) &&
874 strncasecmp(location, "https://", sizeof("https://")-1) &&
875 strncasecmp(location, "ftp://", sizeof("ftp://")-1) &&
876 strncasecmp(location, "ftps://", sizeof("ftps://")-1)))
877 {
878 if (*location != '/') {
879 if (*(location+1) != '\0' && resource->path) {
880 char *s = strrchr(resource->path, '/');
881 if (!s) {
882 s = resource->path;
883 if (!s[0]) {
884 efree(s);
885 s = resource->path = estrdup("/");
886 } else {
887 *s = '/';
888 }
889 }
890 s[1] = '\0';
891 if (resource->path && *(resource->path) == '/' && *(resource->path + 1) == '\0') {
892 snprintf(loc_path, sizeof(loc_path) - 1, "%s%s", resource->path, location);
893 } else {
894 snprintf(loc_path, sizeof(loc_path) - 1, "%s/%s", resource->path, location);
895 }
896 } else {
897 snprintf(loc_path, sizeof(loc_path) - 1, "/%s", location);
898 }
899 } else {
900 strlcpy(loc_path, location, sizeof(loc_path));
901 }
902 if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
903 snprintf(new_path, sizeof(new_path) - 1, "%s://%s:%d%s", resource->scheme, resource->host, resource->port, loc_path);
904 } else {
905 snprintf(new_path, sizeof(new_path) - 1, "%s://%s%s", resource->scheme, resource->host, loc_path);
906 }
907 } else {
908 strlcpy(new_path, location, sizeof(new_path));
909 }
910
911 php_url_free(resource);
912 /* check for invalid redirection URLs */
913 if ((resource = php_url_parse(new_path)) == NULL) {
914 php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path);
915 goto out;
916 }
917
918 #define CHECK_FOR_CNTRL_CHARS(val) { \
919 if (val) { \
920 unsigned char *s, *e; \
921 size_t l; \
922 l = php_url_decode(val, strlen(val)); \
923 s = (unsigned char*)val; e = s + l; \
924 while (s < e) { \
925 if (iscntrl(*s)) { \
926 php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); \
927 goto out; \
928 } \
929 s++; \
930 } \
931 } \
932 }
933 /* check for control characters in login, password & path */
934 if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
935 CHECK_FOR_CNTRL_CHARS(resource->user)
936 CHECK_FOR_CNTRL_CHARS(resource->pass)
937 CHECK_FOR_CNTRL_CHARS(resource->path)
938 }
939 stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, HTTP_WRAPPER_REDIRECTED STREAMS_CC);
940 } else {
941 php_stream_wrapper_log_error(wrapper, options, "HTTP request failed! %s", tmp_line);
942 }
943 }
944 out:
945
946 smart_str_free(&req_buf);
947
948 if (http_header_line) {
949 efree(http_header_line);
950 }
951
952 if (resource) {
953 php_url_free(resource);
954 }
955
956 if (stream) {
957 if (header_init) {
958 ZVAL_COPY(&stream->wrapperdata, &response_header);
959 }
960 php_stream_notify_progress_init(context, 0, file_size);
961
962 /* Restore original chunk size now that we're done with headers */
963 if (options & STREAM_WILL_CAST)
964 php_stream_set_chunk_size(stream, (int)chunk_size);
965
966 /* restore the users auto-detect-line-endings setting */
967 stream->flags |= eol_detect;
968
969 /* as far as streams are concerned, we are now at the start of
970 * the stream */
971 stream->position = 0;
972
973 /* restore mode */
974 strlcpy(stream->mode, mode, sizeof(stream->mode));
975
976 if (transfer_encoding) {
977 php_stream_filter_append(&stream->readfilters, transfer_encoding);
978 }
979 } else {
980 if (transfer_encoding) {
981 php_stream_filter_free(transfer_encoding);
982 }
983 }
984
985 zval_ptr_dtor(&response_header);
986
987 return stream;
988 }
989 /* }}} */
990
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)991 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) /* {{{ */
992 {
993 return php_stream_url_wrap_http_ex(wrapper, path, mode, options, opened_path, context, PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT STREAMS_CC);
994 }
995 /* }}} */
996
php_stream_http_stream_stat(php_stream_wrapper * wrapper,php_stream * stream,php_stream_statbuf * ssb)997 static int php_stream_http_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
998 {
999 /* one day, we could fill in the details based on Date: and Content-Length:
1000 * headers. For now, we return with a failure code to prevent the underlying
1001 * file's details from being used instead. */
1002 return -1;
1003 }
1004 /* }}} */
1005
1006 static php_stream_wrapper_ops http_stream_wops = {
1007 php_stream_url_wrap_http,
1008 NULL, /* stream_close */
1009 php_stream_http_stream_stat,
1010 NULL, /* stat_url */
1011 NULL, /* opendir */
1012 "http",
1013 NULL, /* unlink */
1014 NULL, /* rename */
1015 NULL, /* mkdir */
1016 NULL, /* rmdir */
1017 NULL
1018 };
1019
1020 PHPAPI php_stream_wrapper php_stream_http_wrapper = {
1021 &http_stream_wops,
1022 NULL,
1023 1 /* is_url */
1024 };
1025
1026 /*
1027 * Local variables:
1028 * tab-width: 4
1029 * c-basic-offset: 4
1030 * End:
1031 * vim600: sw=4 ts=4 fdm=marker
1032 * vim<600: sw=4 ts=4
1033 */
1034