http_fopen_wrapper.c 32 KB

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