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