http_fopen_wrapper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. }
  629. ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
  630. zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response);
  631. } else {
  632. php_stream_close(stream);
  633. stream = NULL;
  634. php_stream_wrapper_log_error(wrapper, options, "HTTP request failed!");
  635. goto out;
  636. }
  637. }
  638. /* read past HTTP headers */
  639. while (!php_stream_eof(stream)) {
  640. size_t http_header_line_length;
  641. if (http_header_line != NULL) {
  642. efree(http_header_line);
  643. }
  644. if ((http_header_line = php_stream_get_line(stream, NULL, 0, &http_header_line_length)) && *http_header_line != '\n' && *http_header_line != '\r') {
  645. char *e = http_header_line + http_header_line_length - 1;
  646. char *http_header_value;
  647. while (e >= http_header_line && (*e == '\n' || *e == '\r')) {
  648. e--;
  649. }
  650. /* The primary definition of an HTTP header in RFC 7230 states:
  651. * > Each header field consists of a case-insensitive field name followed
  652. * > by a colon (":"), optional leading whitespace, the field value, and
  653. * > optional trailing whitespace. */
  654. /* Strip trailing whitespace */
  655. while (e >= http_header_line && (*e == ' ' || *e == '\t')) {
  656. e--;
  657. }
  658. /* Terminate header line */
  659. e++;
  660. *e = '\0';
  661. http_header_line_length = e - http_header_line;
  662. http_header_value = memchr(http_header_line, ':', http_header_line_length);
  663. if (http_header_value) {
  664. http_header_value++; /* Skip ':' */
  665. /* Strip leading whitespace */
  666. while (http_header_value < e
  667. && (*http_header_value == ' ' || *http_header_value == '\t')) {
  668. http_header_value++;
  669. }
  670. } else {
  671. /* There is no colon. Set the value to the end of the header line, which is
  672. * effectively an empty string. */
  673. http_header_value = e;
  674. }
  675. if (!strncasecmp(http_header_line, "Location:", sizeof("Location:")-1)) {
  676. if (context && (tmpzval = php_stream_context_get_option(context, "http", "follow_location")) != NULL) {
  677. follow_location = zval_is_true(tmpzval);
  678. } else if (!((response_code >= 300 && response_code < 304)
  679. || 307 == response_code || 308 == response_code)) {
  680. /* we shouldn't redirect automatically
  681. if follow_location isn't set and response_code not in (300, 301, 302, 303 and 307)
  682. see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1
  683. RFC 7238 defines 308: http://tools.ietf.org/html/rfc7238 */
  684. follow_location = 0;
  685. }
  686. strlcpy(location, http_header_value, sizeof(location));
  687. } else if (!strncasecmp(http_header_line, "Content-Type:", sizeof("Content-Type:")-1)) {
  688. php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_value, 0);
  689. } else if (!strncasecmp(http_header_line, "Content-Length:", sizeof("Content-Length:")-1)) {
  690. file_size = atoi(http_header_value);
  691. php_stream_notify_file_size(context, file_size, http_header_line, 0);
  692. } else if (
  693. !strncasecmp(http_header_line, "Transfer-Encoding:", sizeof("Transfer-Encoding:")-1)
  694. && !strncasecmp(http_header_value, "Chunked", sizeof("Chunked")-1)
  695. ) {
  696. /* create filter to decode response body */
  697. if (!(options & STREAM_ONLY_GET_HEADERS)) {
  698. zend_long decode = 1;
  699. if (context && (tmpzval = php_stream_context_get_option(context, "http", "auto_decode")) != NULL) {
  700. decode = zend_is_true(tmpzval);
  701. }
  702. if (decode) {
  703. transfer_encoding = php_stream_filter_create("dechunk", NULL, php_stream_is_persistent(stream));
  704. if (transfer_encoding) {
  705. /* don't store transfer-encodeing header */
  706. continue;
  707. }
  708. }
  709. }
  710. }
  711. {
  712. zval http_header;
  713. ZVAL_STRINGL(&http_header, http_header_line, http_header_line_length);
  714. zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header);
  715. }
  716. } else {
  717. break;
  718. }
  719. }
  720. if (!reqok || (location[0] != '\0' && follow_location)) {
  721. if (!follow_location || (((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) && redirect_max <= 1)) {
  722. goto out;
  723. }
  724. if (location[0] != '\0')
  725. php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
  726. php_stream_close(stream);
  727. stream = NULL;
  728. if (transfer_encoding) {
  729. php_stream_filter_free(transfer_encoding);
  730. transfer_encoding = NULL;
  731. }
  732. if (location[0] != '\0') {
  733. char new_path[HTTP_HEADER_BLOCK_SIZE];
  734. char loc_path[HTTP_HEADER_BLOCK_SIZE];
  735. *new_path='\0';
  736. if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) &&
  737. strncasecmp(location, "https://", sizeof("https://")-1) &&
  738. strncasecmp(location, "ftp://", sizeof("ftp://")-1) &&
  739. strncasecmp(location, "ftps://", sizeof("ftps://")-1)))
  740. {
  741. if (*location != '/') {
  742. if (*(location+1) != '\0' && resource->path) {
  743. char *s = strrchr(ZSTR_VAL(resource->path), '/');
  744. if (!s) {
  745. s = ZSTR_VAL(resource->path);
  746. if (!ZSTR_LEN(resource->path)) {
  747. zend_string_release_ex(resource->path, 0);
  748. resource->path = zend_string_init("/", 1, 0);
  749. s = ZSTR_VAL(resource->path);
  750. } else {
  751. *s = '/';
  752. }
  753. }
  754. s[1] = '\0';
  755. if (resource->path &&
  756. ZSTR_VAL(resource->path)[0] == '/' &&
  757. ZSTR_VAL(resource->path)[1] == '\0') {
  758. snprintf(loc_path, sizeof(loc_path) - 1, "%s%s", ZSTR_VAL(resource->path), location);
  759. } else {
  760. snprintf(loc_path, sizeof(loc_path) - 1, "%s/%s", ZSTR_VAL(resource->path), location);
  761. }
  762. } else {
  763. snprintf(loc_path, sizeof(loc_path) - 1, "/%s", location);
  764. }
  765. } else {
  766. strlcpy(loc_path, location, sizeof(loc_path));
  767. }
  768. if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
  769. snprintf(new_path, sizeof(new_path) - 1, "%s://%s:%d%s", ZSTR_VAL(resource->scheme), ZSTR_VAL(resource->host), resource->port, loc_path);
  770. } else {
  771. snprintf(new_path, sizeof(new_path) - 1, "%s://%s%s", ZSTR_VAL(resource->scheme), ZSTR_VAL(resource->host), loc_path);
  772. }
  773. } else {
  774. strlcpy(new_path, location, sizeof(new_path));
  775. }
  776. php_url_free(resource);
  777. /* check for invalid redirection URLs */
  778. if ((resource = php_url_parse(new_path)) == NULL) {
  779. php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path);
  780. goto out;
  781. }
  782. #define CHECK_FOR_CNTRL_CHARS(val) { \
  783. if (val) { \
  784. unsigned char *s, *e; \
  785. ZSTR_LEN(val) = php_url_decode(ZSTR_VAL(val), ZSTR_LEN(val)); \
  786. s = (unsigned char*)ZSTR_VAL(val); e = s + ZSTR_LEN(val); \
  787. while (s < e) { \
  788. if (iscntrl(*s)) { \
  789. php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); \
  790. goto out; \
  791. } \
  792. s++; \
  793. } \
  794. } \
  795. }
  796. /* check for control characters in login, password & path */
  797. if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
  798. CHECK_FOR_CNTRL_CHARS(resource->user);
  799. CHECK_FOR_CNTRL_CHARS(resource->pass);
  800. CHECK_FOR_CNTRL_CHARS(resource->path);
  801. }
  802. stream = php_stream_url_wrap_http_ex(
  803. wrapper, new_path, mode, options, opened_path, context,
  804. --redirect_max, HTTP_WRAPPER_REDIRECTED, response_header STREAMS_CC);
  805. } else {
  806. php_stream_wrapper_log_error(wrapper, options, "HTTP request failed! %s", tmp_line);
  807. }
  808. }
  809. out:
  810. smart_str_free(&req_buf);
  811. if (http_header_line) {
  812. efree(http_header_line);
  813. }
  814. if (resource) {
  815. php_url_free(resource);
  816. }
  817. if (stream) {
  818. if (header_init) {
  819. ZVAL_COPY(&stream->wrapperdata, response_header);
  820. }
  821. php_stream_notify_progress_init(context, 0, file_size);
  822. /* Restore original chunk size now that we're done with headers */
  823. if (options & STREAM_WILL_CAST)
  824. php_stream_set_chunk_size(stream, (int)chunk_size);
  825. /* restore the users auto-detect-line-endings setting */
  826. stream->flags |= eol_detect;
  827. /* as far as streams are concerned, we are now at the start of
  828. * the stream */
  829. stream->position = 0;
  830. /* restore mode */
  831. strlcpy(stream->mode, mode, sizeof(stream->mode));
  832. if (transfer_encoding) {
  833. php_stream_filter_append(&stream->readfilters, transfer_encoding);
  834. }
  835. }
  836. return stream;
  837. }
  838. /* }}} */
  839. 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) /* {{{ */
  840. {
  841. php_stream *stream;
  842. zval headers;
  843. ZVAL_UNDEF(&headers);
  844. stream = php_stream_url_wrap_http_ex(
  845. wrapper, path, mode, options, opened_path, context,
  846. PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT, &headers STREAMS_CC);
  847. if (!Z_ISUNDEF(headers)) {
  848. if (FAILURE == zend_set_local_var_str(
  849. "http_response_header", sizeof("http_response_header")-1, &headers, 0)) {
  850. zval_ptr_dtor(&headers);
  851. }
  852. }
  853. return stream;
  854. }
  855. /* }}} */
  856. static int php_stream_http_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
  857. {
  858. /* one day, we could fill in the details based on Date: and Content-Length:
  859. * headers. For now, we return with a failure code to prevent the underlying
  860. * file's details from being used instead. */
  861. return -1;
  862. }
  863. /* }}} */
  864. static const php_stream_wrapper_ops http_stream_wops = {
  865. php_stream_url_wrap_http,
  866. NULL, /* stream_close */
  867. php_stream_http_stream_stat,
  868. NULL, /* stat_url */
  869. NULL, /* opendir */
  870. "http",
  871. NULL, /* unlink */
  872. NULL, /* rename */
  873. NULL, /* mkdir */
  874. NULL, /* rmdir */
  875. NULL
  876. };
  877. PHPAPI const php_stream_wrapper php_stream_http_wrapper = {
  878. &http_stream_wops,
  879. NULL,
  880. 1 /* is_url */
  881. };