http_fopen_wrapper.c 33 KB

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