PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/soap/php_http.c

http://github.com/php/php-src
C | 1548 lines | 1325 code | 144 blank | 79 comment | 584 complexity | 95e093becf8e9c02f92c07189a9d8bcf MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. | http://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: Brad Lafountain <rodif_bl@yahoo.com> |
  14. | Shane Caraveo <shane@caraveo.com> |
  15. | Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php_soap.h"
  19. #include "ext/standard/base64.h"
  20. #include "ext/standard/md5.h"
  21. #include "ext/standard/php_random.h"
  22. static char *get_http_header_value(char *headers, char *type);
  23. static zend_string *get_http_body(php_stream *socketd, int close, char *headers);
  24. static zend_string *get_http_headers(php_stream *socketd);
  25. #define smart_str_append_const(str, const) \
  26. smart_str_appendl(str,const,sizeof(const)-1)
  27. /* Proxy HTTP Authentication */
  28. int proxy_authentication(zval* this_ptr, smart_str* soap_headers)
  29. {
  30. zval *login, *password;
  31. if ((login = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_login", sizeof("_proxy_login")-1)) != NULL &&
  32. Z_TYPE_P(login) == IS_STRING) {
  33. zend_string *buf;
  34. smart_str auth = {0};
  35. smart_str_appendl(&auth, Z_STRVAL_P(login), Z_STRLEN_P(login));
  36. smart_str_appendc(&auth, ':');
  37. if ((password = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_password", sizeof("_proxy_password")-1)) != NULL &&
  38. Z_TYPE_P(password) == IS_STRING) {
  39. smart_str_appendl(&auth, Z_STRVAL_P(password), Z_STRLEN_P(password));
  40. }
  41. smart_str_0(&auth);
  42. buf = php_base64_encode((unsigned char*)ZSTR_VAL(auth.s), ZSTR_LEN(auth.s));
  43. smart_str_append_const(soap_headers, "Proxy-Authorization: Basic ");
  44. smart_str_appendl(soap_headers, (char*)ZSTR_VAL(buf), ZSTR_LEN(buf));
  45. smart_str_append_const(soap_headers, "\r\n");
  46. zend_string_release_ex(buf, 0);
  47. smart_str_free(&auth);
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. /* HTTP Authentication */
  53. int basic_authentication(zval* this_ptr, smart_str* soap_headers)
  54. {
  55. zval *login, *password;
  56. if ((login = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login")-1)) != NULL &&
  57. Z_TYPE_P(login) == IS_STRING &&
  58. !zend_hash_str_exists(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest")-1)) {
  59. zend_string* buf;
  60. smart_str auth = {0};
  61. smart_str_appendl(&auth, Z_STRVAL_P(login), Z_STRLEN_P(login));
  62. smart_str_appendc(&auth, ':');
  63. if ((password = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password")-1)) != NULL &&
  64. Z_TYPE_P(password) == IS_STRING) {
  65. smart_str_appendl(&auth, Z_STRVAL_P(password), Z_STRLEN_P(password));
  66. }
  67. smart_str_0(&auth);
  68. buf = php_base64_encode((unsigned char*)ZSTR_VAL(auth.s), ZSTR_LEN(auth.s));
  69. smart_str_append_const(soap_headers, "Authorization: Basic ");
  70. smart_str_appendl(soap_headers, (char*)ZSTR_VAL(buf), ZSTR_LEN(buf));
  71. smart_str_append_const(soap_headers, "\r\n");
  72. zend_string_release_ex(buf, 0);
  73. smart_str_free(&auth);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. /* Additional HTTP headers */
  79. void http_context_headers(php_stream_context* context,
  80. zend_bool has_authorization,
  81. zend_bool has_proxy_authorization,
  82. zend_bool has_cookies,
  83. smart_str* soap_headers)
  84. {
  85. zval *tmp;
  86. if (context &&
  87. (tmp = php_stream_context_get_option(context, "http", "header")) != NULL &&
  88. Z_TYPE_P(tmp) == IS_STRING && Z_STRLEN_P(tmp)) {
  89. char *s = Z_STRVAL_P(tmp);
  90. char *p;
  91. int name_len;
  92. while (*s) {
  93. /* skip leading newlines and spaces */
  94. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n') {
  95. s++;
  96. }
  97. /* extract header name */
  98. p = s;
  99. name_len = -1;
  100. while (*p) {
  101. if (*p == ':') {
  102. if (name_len < 0) name_len = p - s;
  103. break;
  104. } else if (*p == ' ' || *p == '\t') {
  105. if (name_len < 0) name_len = p - s;
  106. } else if (*p == '\r' || *p == '\n') {
  107. break;
  108. }
  109. p++;
  110. }
  111. if (*p == ':') {
  112. /* extract header value */
  113. while (*p && *p != '\r' && *p != '\n') {
  114. p++;
  115. }
  116. /* skip some predefined headers */
  117. if ((name_len != sizeof("host")-1 ||
  118. strncasecmp(s, "host", sizeof("host")-1) != 0) &&
  119. (name_len != sizeof("connection")-1 ||
  120. strncasecmp(s, "connection", sizeof("connection")-1) != 0) &&
  121. (name_len != sizeof("user-agent")-1 ||
  122. strncasecmp(s, "user-agent", sizeof("user-agent")-1) != 0) &&
  123. (name_len != sizeof("content-length")-1 ||
  124. strncasecmp(s, "content-length", sizeof("content-length")-1) != 0) &&
  125. (name_len != sizeof("content-type")-1 ||
  126. strncasecmp(s, "content-type", sizeof("content-type")-1) != 0) &&
  127. (!has_cookies ||
  128. name_len != sizeof("cookie")-1 ||
  129. strncasecmp(s, "cookie", sizeof("cookie")-1) != 0) &&
  130. (!has_authorization ||
  131. name_len != sizeof("authorization")-1 ||
  132. strncasecmp(s, "authorization", sizeof("authorization")-1) != 0) &&
  133. (!has_proxy_authorization ||
  134. name_len != sizeof("proxy-authorization")-1 ||
  135. strncasecmp(s, "proxy-authorization", sizeof("proxy-authorization")-1) != 0)) {
  136. /* add header */
  137. smart_str_appendl(soap_headers, s, p-s);
  138. smart_str_append_const(soap_headers, "\r\n");
  139. }
  140. }
  141. s = (*p) ? (p + 1) : p;
  142. }
  143. }
  144. }
  145. static php_stream* http_connect(zval* this_ptr, php_url *phpurl, int use_ssl, php_stream_context *context, int *use_proxy)
  146. {
  147. php_stream *stream;
  148. zval *proxy_host, *proxy_port, *tmp, ssl_proxy_peer_name;
  149. char *host;
  150. char *name;
  151. char *protocol;
  152. zend_long namelen;
  153. int port;
  154. int old_error_reporting;
  155. struct timeval tv;
  156. struct timeval *timeout = NULL;
  157. if ((proxy_host = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_host", sizeof("_proxy_host")-1)) != NULL &&
  158. Z_TYPE_P(proxy_host) == IS_STRING &&
  159. (proxy_port = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_port", sizeof("_proxy_port")-1)) != NULL &&
  160. Z_TYPE_P(proxy_port) == IS_LONG) {
  161. host = Z_STRVAL_P(proxy_host);
  162. port = Z_LVAL_P(proxy_port);
  163. *use_proxy = 1;
  164. } else {
  165. host = ZSTR_VAL(phpurl->host);
  166. port = phpurl->port;
  167. }
  168. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_connection_timeout", sizeof("_connection_timeout")-1)) != NULL &&
  169. Z_TYPE_P(tmp) == IS_LONG && Z_LVAL_P(tmp) > 0) {
  170. tv.tv_sec = Z_LVAL_P(tmp);
  171. tv.tv_usec = 0;
  172. timeout = &tv;
  173. }
  174. old_error_reporting = EG(error_reporting);
  175. EG(error_reporting) &= ~(E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE);
  176. /* Changed ternary operator to an if/else so that additional comparisons can be done on the ssl_method property */
  177. if (use_ssl && !*use_proxy) {
  178. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_ssl_method", sizeof("_ssl_method")-1)) != NULL &&
  179. Z_TYPE_P(tmp) == IS_LONG) {
  180. /* uses constants declared in soap.c to determine ssl uri protocol */
  181. switch (Z_LVAL_P(tmp)) {
  182. case SOAP_SSL_METHOD_TLS:
  183. protocol = "tls";
  184. break;
  185. case SOAP_SSL_METHOD_SSLv2:
  186. protocol = "sslv2";
  187. break;
  188. case SOAP_SSL_METHOD_SSLv3:
  189. protocol = "sslv3";
  190. break;
  191. case SOAP_SSL_METHOD_SSLv23:
  192. protocol = "ssl";
  193. break;
  194. default:
  195. protocol = "ssl";
  196. break;
  197. }
  198. } else {
  199. protocol = "ssl";
  200. }
  201. } else {
  202. protocol = "tcp";
  203. }
  204. namelen = spprintf(&name, 0, "%s://%s:%d", protocol, host, port);
  205. stream = php_stream_xport_create(name, namelen,
  206. REPORT_ERRORS,
  207. STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
  208. NULL /*persistent_id*/,
  209. timeout,
  210. context,
  211. NULL, NULL);
  212. efree(name);
  213. /* SSL & proxy */
  214. if (stream && *use_proxy && use_ssl) {
  215. smart_str soap_headers = {0};
  216. /* Set peer_name or name verification will try to use the proxy server name */
  217. if (!context || (tmp = php_stream_context_get_option(context, "ssl", "peer_name")) == NULL) {
  218. ZVAL_STR_COPY(&ssl_proxy_peer_name, phpurl->host);
  219. php_stream_context_set_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_name", &ssl_proxy_peer_name);
  220. zval_ptr_dtor(&ssl_proxy_peer_name);
  221. }
  222. smart_str_append_const(&soap_headers, "CONNECT ");
  223. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->host));
  224. smart_str_appendc(&soap_headers, ':');
  225. smart_str_append_unsigned(&soap_headers, phpurl->port);
  226. smart_str_append_const(&soap_headers, " HTTP/1.1\r\n");
  227. smart_str_append_const(&soap_headers, "Host: ");
  228. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->host));
  229. if (phpurl->port != 80) {
  230. smart_str_appendc(&soap_headers, ':');
  231. smart_str_append_unsigned(&soap_headers, phpurl->port);
  232. }
  233. smart_str_append_const(&soap_headers, "\r\n");
  234. proxy_authentication(this_ptr, &soap_headers);
  235. smart_str_append_const(&soap_headers, "\r\n");
  236. if (php_stream_write(stream, ZSTR_VAL(soap_headers.s), ZSTR_LEN(soap_headers.s)) != ZSTR_LEN(soap_headers.s)) {
  237. php_stream_close(stream);
  238. stream = NULL;
  239. }
  240. smart_str_free(&soap_headers);
  241. if (stream) {
  242. zend_string *http_headers = get_http_headers(stream);
  243. if (http_headers) {
  244. zend_string_free(http_headers);
  245. } else {
  246. php_stream_close(stream);
  247. stream = NULL;
  248. }
  249. }
  250. /* enable SSL transport layer */
  251. if (stream) {
  252. /* if a stream is created without encryption, check to see if SSL method parameter is specified and use
  253. proper encrypyion method based on constants defined in soap.c */
  254. int crypto_method = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
  255. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_ssl_method", sizeof("_ssl_method")-1)) != NULL &&
  256. Z_TYPE_P(tmp) == IS_LONG) {
  257. switch (Z_LVAL_P(tmp)) {
  258. case SOAP_SSL_METHOD_TLS:
  259. crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
  260. break;
  261. case SOAP_SSL_METHOD_SSLv2:
  262. crypto_method = STREAM_CRYPTO_METHOD_SSLv2_CLIENT;
  263. break;
  264. case SOAP_SSL_METHOD_SSLv3:
  265. crypto_method = STREAM_CRYPTO_METHOD_SSLv3_CLIENT;
  266. break;
  267. case SOAP_SSL_METHOD_SSLv23:
  268. crypto_method = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
  269. break;
  270. default:
  271. crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
  272. break;
  273. }
  274. }
  275. if (php_stream_xport_crypto_setup(stream, crypto_method, NULL) < 0 ||
  276. php_stream_xport_crypto_enable(stream, 1) < 0) {
  277. php_stream_close(stream);
  278. stream = NULL;
  279. }
  280. }
  281. }
  282. EG(error_reporting) = old_error_reporting;
  283. return stream;
  284. }
  285. static int in_domain(const char *host, const char *domain)
  286. {
  287. if (domain[0] == '.') {
  288. int l1 = strlen(host);
  289. int l2 = strlen(domain);
  290. if (l1 > l2) {
  291. return strcmp(host+l1-l2,domain) == 0;
  292. } else {
  293. return 0;
  294. }
  295. } else {
  296. return strcmp(host,domain) == 0;
  297. }
  298. }
  299. int make_http_soap_request(zval *this_ptr,
  300. zend_string *buf,
  301. char *location,
  302. char *soapaction,
  303. int soap_version,
  304. zval *return_value)
  305. {
  306. zend_string *request;
  307. smart_str soap_headers = {0};
  308. smart_str soap_headers_z = {0};
  309. size_t err;
  310. php_url *phpurl = NULL;
  311. php_stream *stream;
  312. zval *trace, *tmp;
  313. int use_proxy = 0;
  314. int use_ssl;
  315. zend_string *http_body;
  316. char *content_type, *http_version, *cookie_itt;
  317. int http_close;
  318. zend_string *http_headers;
  319. char *connection;
  320. int http_1_1;
  321. int http_status;
  322. int content_type_xml = 0;
  323. zend_long redirect_max = 20;
  324. char *content_encoding;
  325. char *http_msg = NULL;
  326. zend_bool old_allow_url_fopen;
  327. php_stream_context *context = NULL;
  328. zend_bool has_authorization = 0;
  329. zend_bool has_proxy_authorization = 0;
  330. zend_bool has_cookies = 0;
  331. if (this_ptr == NULL || Z_TYPE_P(this_ptr) != IS_OBJECT) {
  332. return FALSE;
  333. }
  334. request = buf;
  335. /* Compress request */
  336. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "compression", sizeof("compression")-1)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
  337. int level = Z_LVAL_P(tmp) & 0x0f;
  338. int kind = Z_LVAL_P(tmp) & SOAP_COMPRESSION_DEFLATE;
  339. if (level > 9) {level = 9;}
  340. if ((Z_LVAL_P(tmp) & SOAP_COMPRESSION_ACCEPT) != 0) {
  341. smart_str_append_const(&soap_headers_z,"Accept-Encoding: gzip, deflate\r\n");
  342. }
  343. if (level > 0) {
  344. zval func;
  345. zval retval;
  346. zval params[3];
  347. int n;
  348. ZVAL_STR_COPY(&params[0], buf);
  349. ZVAL_LONG(&params[1], level);
  350. if (kind == SOAP_COMPRESSION_DEFLATE) {
  351. n = 2;
  352. ZVAL_STRING(&func, "gzcompress");
  353. smart_str_append_const(&soap_headers_z,"Content-Encoding: deflate\r\n");
  354. } else {
  355. n = 3;
  356. ZVAL_STRING(&func, "gzencode");
  357. smart_str_append_const(&soap_headers_z,"Content-Encoding: gzip\r\n");
  358. ZVAL_LONG(&params[2], 0x1f);
  359. }
  360. if (call_user_function(CG(function_table), (zval*)NULL, &func, &retval, n, params) == SUCCESS &&
  361. Z_TYPE(retval) == IS_STRING) {
  362. zval_ptr_dtor(&params[0]);
  363. zval_ptr_dtor(&func);
  364. request = Z_STR(retval);
  365. } else {
  366. zval_ptr_dtor(&params[0]);
  367. zval_ptr_dtor(&func);
  368. if (request != buf) {
  369. zend_string_release_ex(request, 0);
  370. }
  371. smart_str_free(&soap_headers_z);
  372. return FALSE;
  373. }
  374. }
  375. }
  376. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1)) != NULL) {
  377. php_stream_from_zval_no_verify(stream,tmp);
  378. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
  379. use_proxy = Z_LVAL_P(tmp);
  380. }
  381. } else {
  382. stream = NULL;
  383. }
  384. if (location != NULL && location[0] != '\000') {
  385. phpurl = php_url_parse(location);
  386. }
  387. if (NULL != (tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr),
  388. "_stream_context", sizeof("_stream_context")-1))) {
  389. context = php_stream_context_from_zval(tmp, 0);
  390. }
  391. if (context &&
  392. (tmp = php_stream_context_get_option(context, "http", "max_redirects")) != NULL) {
  393. if (Z_TYPE_P(tmp) != IS_STRING || !is_numeric_string(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &redirect_max, NULL, 1)) {
  394. if (Z_TYPE_P(tmp) == IS_LONG)
  395. redirect_max = Z_LVAL_P(tmp);
  396. }
  397. }
  398. try_again:
  399. if (phpurl == NULL || phpurl->host == NULL) {
  400. if (phpurl != NULL) {php_url_free(phpurl);}
  401. if (request != buf) {
  402. zend_string_release_ex(request, 0);
  403. }
  404. add_soap_fault(this_ptr, "HTTP", "Unable to parse URL", NULL, NULL);
  405. smart_str_free(&soap_headers_z);
  406. return FALSE;
  407. }
  408. use_ssl = 0;
  409. if (phpurl->scheme != NULL && zend_string_equals_literal(phpurl->scheme, "https")) {
  410. use_ssl = 1;
  411. } else if (phpurl->scheme == NULL || !zend_string_equals_literal(phpurl->scheme, "http")) {
  412. php_url_free(phpurl);
  413. if (request != buf) {
  414. zend_string_release_ex(request, 0);
  415. }
  416. add_soap_fault(this_ptr, "HTTP", "Unknown protocol. Only http and https are allowed.", NULL, NULL);
  417. smart_str_free(&soap_headers_z);
  418. return FALSE;
  419. }
  420. old_allow_url_fopen = PG(allow_url_fopen);
  421. PG(allow_url_fopen) = 1;
  422. if (use_ssl && php_stream_locate_url_wrapper("https://", NULL, STREAM_LOCATE_WRAPPERS_ONLY) == NULL) {
  423. php_url_free(phpurl);
  424. if (request != buf) {
  425. zend_string_release_ex(request, 0);
  426. }
  427. add_soap_fault(this_ptr, "HTTP", "SSL support is not available in this build", NULL, NULL);
  428. PG(allow_url_fopen) = old_allow_url_fopen;
  429. smart_str_free(&soap_headers_z);
  430. return FALSE;
  431. }
  432. if (phpurl->port == 0) {
  433. phpurl->port = use_ssl ? 443 : 80;
  434. }
  435. /* Check if request to the same host */
  436. if (stream != NULL) {
  437. php_url *orig;
  438. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl")-1)) != NULL &&
  439. (orig = (php_url *) zend_fetch_resource_ex(tmp, "httpurl", le_url)) != NULL &&
  440. ((use_proxy && !use_ssl) ||
  441. (((use_ssl && orig->scheme != NULL && zend_string_equals_literal(orig->scheme, "https")) ||
  442. (!use_ssl && orig->scheme == NULL) ||
  443. (!use_ssl && !zend_string_equals_literal(orig->scheme, "https"))) &&
  444. strcmp(ZSTR_VAL(orig->host), ZSTR_VAL(phpurl->host)) == 0 &&
  445. orig->port == phpurl->port))) {
  446. } else {
  447. php_stream_close(stream);
  448. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl")-1);
  449. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  450. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  451. stream = NULL;
  452. use_proxy = 0;
  453. }
  454. }
  455. /* Check if keep-alive connection is still opened */
  456. if (stream != NULL && php_stream_eof(stream)) {
  457. php_stream_close(stream);
  458. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl")-1);
  459. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  460. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  461. stream = NULL;
  462. use_proxy = 0;
  463. }
  464. if (!stream) {
  465. stream = http_connect(this_ptr, phpurl, use_ssl, context, &use_proxy);
  466. if (stream) {
  467. php_stream_auto_cleanup(stream);
  468. add_property_resource(this_ptr, "httpsocket", stream->res);
  469. GC_ADDREF(stream->res);
  470. add_property_long(this_ptr, "_use_proxy", use_proxy);
  471. } else {
  472. php_url_free(phpurl);
  473. if (request != buf) {
  474. zend_string_release_ex(request, 0);
  475. }
  476. add_soap_fault(this_ptr, "HTTP", "Could not connect to host", NULL, NULL);
  477. PG(allow_url_fopen) = old_allow_url_fopen;
  478. smart_str_free(&soap_headers_z);
  479. return FALSE;
  480. }
  481. }
  482. PG(allow_url_fopen) = old_allow_url_fopen;
  483. if (stream) {
  484. zval *cookies, *login, *password;
  485. zend_resource *ret = zend_register_resource(phpurl, le_url);
  486. add_property_resource(this_ptr, "httpurl", ret);
  487. GC_ADDREF(ret);
  488. /*zend_list_addref(ret);*/
  489. if (context &&
  490. (tmp = php_stream_context_get_option(context, "http", "protocol_version")) != NULL &&
  491. Z_TYPE_P(tmp) == IS_DOUBLE &&
  492. Z_DVAL_P(tmp) == 1.0) {
  493. http_1_1 = 0;
  494. } else {
  495. http_1_1 = 1;
  496. }
  497. smart_str_append_const(&soap_headers, "POST ");
  498. if (use_proxy && !use_ssl) {
  499. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->scheme));
  500. smart_str_append_const(&soap_headers, "://");
  501. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->host));
  502. smart_str_appendc(&soap_headers, ':');
  503. smart_str_append_unsigned(&soap_headers, phpurl->port);
  504. }
  505. if (phpurl->path) {
  506. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->path));
  507. } else {
  508. smart_str_appendc(&soap_headers, '/');
  509. }
  510. if (phpurl->query) {
  511. smart_str_appendc(&soap_headers, '?');
  512. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->query));
  513. }
  514. if (phpurl->fragment) {
  515. smart_str_appendc(&soap_headers, '#');
  516. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->fragment));
  517. }
  518. if (http_1_1) {
  519. smart_str_append_const(&soap_headers, " HTTP/1.1\r\n");
  520. } else {
  521. smart_str_append_const(&soap_headers, " HTTP/1.0\r\n");
  522. }
  523. smart_str_append_const(&soap_headers, "Host: ");
  524. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->host));
  525. if (phpurl->port != (use_ssl?443:80)) {
  526. smart_str_appendc(&soap_headers, ':');
  527. smart_str_append_unsigned(&soap_headers, phpurl->port);
  528. }
  529. if (!http_1_1 ||
  530. ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_keep_alive", sizeof("_keep_alive")-1)) != NULL &&
  531. (Z_TYPE_P(tmp) == IS_FALSE || (Z_TYPE_P(tmp) == IS_LONG && Z_LVAL_P(tmp) == 0)))) {
  532. smart_str_append_const(&soap_headers, "\r\n"
  533. "Connection: close\r\n");
  534. } else {
  535. smart_str_append_const(&soap_headers, "\r\n"
  536. "Connection: Keep-Alive\r\n");
  537. }
  538. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_user_agent", sizeof("_user_agent")-1)) != NULL &&
  539. Z_TYPE_P(tmp) == IS_STRING) {
  540. if (Z_STRLEN_P(tmp) > 0) {
  541. smart_str_append_const(&soap_headers, "User-Agent: ");
  542. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  543. smart_str_append_const(&soap_headers, "\r\n");
  544. }
  545. } else if (context &&
  546. (tmp = php_stream_context_get_option(context, "http", "user_agent")) != NULL &&
  547. Z_TYPE_P(tmp) == IS_STRING) {
  548. if (Z_STRLEN_P(tmp) > 0) {
  549. smart_str_append_const(&soap_headers, "User-Agent: ");
  550. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  551. smart_str_append_const(&soap_headers, "\r\n");
  552. }
  553. } else if (FG(user_agent)) {
  554. smart_str_append_const(&soap_headers, "User-Agent: ");
  555. smart_str_appends(&soap_headers, FG(user_agent));
  556. smart_str_append_const(&soap_headers, "\r\n");
  557. } else {
  558. smart_str_append_const(&soap_headers, "User-Agent: PHP-SOAP/"PHP_VERSION"\r\n");
  559. }
  560. smart_str_append_smart_str(&soap_headers, &soap_headers_z);
  561. if (soap_version == SOAP_1_2) {
  562. if (context &&
  563. (tmp = php_stream_context_get_option(context, "http", "content_type")) != NULL &&
  564. Z_TYPE_P(tmp) == IS_STRING &&
  565. Z_STRLEN_P(tmp) > 0
  566. ) {
  567. smart_str_append_const(&soap_headers, "Content-Type: ");
  568. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  569. } else {
  570. smart_str_append_const(&soap_headers, "Content-Type: application/soap+xml; charset=utf-8");
  571. }
  572. if (soapaction) {
  573. smart_str_append_const(&soap_headers,"; action=\"");
  574. smart_str_appends(&soap_headers, soapaction);
  575. smart_str_append_const(&soap_headers,"\"");
  576. }
  577. smart_str_append_const(&soap_headers,"\r\n");
  578. } else {
  579. if (context &&
  580. (tmp = php_stream_context_get_option(context, "http", "content_type")) != NULL &&
  581. Z_TYPE_P(tmp) == IS_STRING &&
  582. Z_STRLEN_P(tmp) > 0
  583. ) {
  584. smart_str_append_const(&soap_headers, "Content-Type: ");
  585. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  586. smart_str_append_const(&soap_headers, "\r\n");
  587. } else {
  588. smart_str_append_const(&soap_headers, "Content-Type: text/xml; charset=utf-8\r\n");
  589. }
  590. if (soapaction) {
  591. smart_str_append_const(&soap_headers, "SOAPAction: \"");
  592. smart_str_appends(&soap_headers, soapaction);
  593. smart_str_append_const(&soap_headers, "\"\r\n");
  594. }
  595. }
  596. smart_str_append_const(&soap_headers,"Content-Length: ");
  597. smart_str_append_long(&soap_headers, request->len);
  598. smart_str_append_const(&soap_headers, "\r\n");
  599. /* HTTP Authentication */
  600. if ((login = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login")-1)) != NULL &&
  601. Z_TYPE_P(login) == IS_STRING) {
  602. zval *digest;
  603. has_authorization = 1;
  604. if ((digest = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest")-1)) != NULL) {
  605. if (Z_TYPE_P(digest) == IS_ARRAY) {
  606. char HA1[33], HA2[33], response[33], cnonce[33], nc[9];
  607. zend_long nonce;
  608. PHP_MD5_CTX md5ctx;
  609. unsigned char hash[16];
  610. php_random_bytes_throw(&nonce, sizeof(nonce));
  611. nonce &= 0x7fffffff;
  612. PHP_MD5Init(&md5ctx);
  613. snprintf(cnonce, sizeof(cnonce), ZEND_LONG_FMT, nonce);
  614. PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, strlen(cnonce));
  615. PHP_MD5Final(hash, &md5ctx);
  616. make_digest(cnonce, hash);
  617. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "nc", sizeof("nc")-1)) != NULL &&
  618. Z_TYPE_P(tmp) == IS_LONG) {
  619. Z_LVAL_P(tmp)++;
  620. snprintf(nc, sizeof(nc), "%08" ZEND_LONG_FMT_SPEC, Z_LVAL_P(tmp));
  621. } else {
  622. add_assoc_long(digest, "nc", 1);
  623. strcpy(nc, "00000001");
  624. }
  625. PHP_MD5Init(&md5ctx);
  626. PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_P(login), Z_STRLEN_P(login));
  627. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  628. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "realm", sizeof("realm")-1)) != NULL &&
  629. Z_TYPE_P(tmp) == IS_STRING) {
  630. PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  631. }
  632. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  633. if ((password = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password")-1)) != NULL &&
  634. Z_TYPE_P(password) == IS_STRING) {
  635. PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_P(password), Z_STRLEN_P(password));
  636. }
  637. PHP_MD5Final(hash, &md5ctx);
  638. make_digest(HA1, hash);
  639. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "algorithm", sizeof("algorithm")-1)) != NULL &&
  640. Z_TYPE_P(tmp) == IS_STRING &&
  641. Z_STRLEN_P(tmp) == sizeof("md5-sess")-1 &&
  642. stricmp(Z_STRVAL_P(tmp), "md5-sess") == 0) {
  643. PHP_MD5Init(&md5ctx);
  644. PHP_MD5Update(&md5ctx, (unsigned char*)HA1, 32);
  645. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  646. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "nonce", sizeof("nonce")-1)) != NULL &&
  647. Z_TYPE_P(tmp) == IS_STRING) {
  648. PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  649. }
  650. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  651. PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, 8);
  652. PHP_MD5Final(hash, &md5ctx);
  653. make_digest(HA1, hash);
  654. }
  655. PHP_MD5Init(&md5ctx);
  656. PHP_MD5Update(&md5ctx, (unsigned char*)"POST:", sizeof("POST:")-1);
  657. if (phpurl->path) {
  658. PHP_MD5Update(&md5ctx, (unsigned char*)ZSTR_VAL(phpurl->path), ZSTR_LEN(phpurl->path));
  659. } else {
  660. PHP_MD5Update(&md5ctx, (unsigned char*)"/", 1);
  661. }
  662. if (phpurl->query) {
  663. PHP_MD5Update(&md5ctx, (unsigned char*)"?", 1);
  664. PHP_MD5Update(&md5ctx, (unsigned char*)ZSTR_VAL(phpurl->query), ZSTR_LEN(phpurl->query));
  665. }
  666. PHP_MD5Final(hash, &md5ctx);
  667. make_digest(HA2, hash);
  668. PHP_MD5Init(&md5ctx);
  669. PHP_MD5Update(&md5ctx, (unsigned char*)HA1, 32);
  670. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  671. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "nonce", sizeof("nonce")-1)) != NULL &&
  672. Z_TYPE_P(tmp) == IS_STRING) {
  673. PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  674. }
  675. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  676. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "qop", sizeof("qop")-1)) != NULL &&
  677. Z_TYPE_P(tmp) == IS_STRING) {
  678. PHP_MD5Update(&md5ctx, (unsigned char*)nc, 8);
  679. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  680. PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, 8);
  681. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  682. /* TODO: Support for qop="auth-int" */
  683. PHP_MD5Update(&md5ctx, (unsigned char*)"auth", sizeof("auth")-1);
  684. PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
  685. }
  686. PHP_MD5Update(&md5ctx, (unsigned char*)HA2, 32);
  687. PHP_MD5Final(hash, &md5ctx);
  688. make_digest(response, hash);
  689. smart_str_append_const(&soap_headers, "Authorization: Digest username=\"");
  690. smart_str_appendl(&soap_headers, Z_STRVAL_P(login), Z_STRLEN_P(login));
  691. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "realm", sizeof("realm")-1)) != NULL &&
  692. Z_TYPE_P(tmp) == IS_STRING) {
  693. smart_str_append_const(&soap_headers, "\", realm=\"");
  694. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  695. }
  696. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "nonce", sizeof("nonce")-1)) != NULL &&
  697. Z_TYPE_P(tmp) == IS_STRING) {
  698. smart_str_append_const(&soap_headers, "\", nonce=\"");
  699. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  700. }
  701. smart_str_append_const(&soap_headers, "\", uri=\"");
  702. if (phpurl->path) {
  703. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->path));
  704. } else {
  705. smart_str_appendc(&soap_headers, '/');
  706. }
  707. if (phpurl->query) {
  708. smart_str_appendc(&soap_headers, '?');
  709. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->query));
  710. }
  711. if (phpurl->fragment) {
  712. smart_str_appendc(&soap_headers, '#');
  713. smart_str_appends(&soap_headers, ZSTR_VAL(phpurl->fragment));
  714. }
  715. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "qop", sizeof("qop")-1)) != NULL &&
  716. Z_TYPE_P(tmp) == IS_STRING) {
  717. /* TODO: Support for qop="auth-int" */
  718. smart_str_append_const(&soap_headers, "\", qop=\"auth");
  719. smart_str_append_const(&soap_headers, "\", nc=\"");
  720. smart_str_appendl(&soap_headers, nc, 8);
  721. smart_str_append_const(&soap_headers, "\", cnonce=\"");
  722. smart_str_appendl(&soap_headers, cnonce, 8);
  723. }
  724. smart_str_append_const(&soap_headers, "\", response=\"");
  725. smart_str_appendl(&soap_headers, response, 32);
  726. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "opaque", sizeof("opaque")-1)) != NULL &&
  727. Z_TYPE_P(tmp) == IS_STRING) {
  728. smart_str_append_const(&soap_headers, "\", opaque=\"");
  729. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  730. }
  731. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(digest), "algorithm", sizeof("algorithm")-1)) != NULL &&
  732. Z_TYPE_P(tmp) == IS_STRING) {
  733. smart_str_append_const(&soap_headers, "\", algorithm=\"");
  734. smart_str_appendl(&soap_headers, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  735. }
  736. smart_str_append_const(&soap_headers, "\"\r\n");
  737. }
  738. } else {
  739. zend_string *buf;
  740. smart_str auth = {0};
  741. smart_str_appendl(&auth, Z_STRVAL_P(login), Z_STRLEN_P(login));
  742. smart_str_appendc(&auth, ':');
  743. if ((password = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password")-1)) != NULL &&
  744. Z_TYPE_P(password) == IS_STRING) {
  745. smart_str_appendl(&auth, Z_STRVAL_P(password), Z_STRLEN_P(password));
  746. }
  747. smart_str_0(&auth);
  748. buf = php_base64_encode((unsigned char*)ZSTR_VAL(auth.s), ZSTR_LEN(auth.s));
  749. smart_str_append_const(&soap_headers, "Authorization: Basic ");
  750. smart_str_appendl(&soap_headers, (char*)ZSTR_VAL(buf), ZSTR_LEN(buf));
  751. smart_str_append_const(&soap_headers, "\r\n");
  752. zend_string_release_ex(buf, 0);
  753. smart_str_free(&auth);
  754. }
  755. }
  756. /* Proxy HTTP Authentication */
  757. if (use_proxy && !use_ssl) {
  758. has_proxy_authorization = proxy_authentication(this_ptr, &soap_headers);
  759. }
  760. /* Send cookies along with request */
  761. if ((cookies = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies")-1)) != NULL &&
  762. Z_TYPE_P(cookies) == IS_ARRAY) {
  763. zval *data;
  764. zend_string *key;
  765. uint32_t n = zend_hash_num_elements(Z_ARRVAL_P(cookies));
  766. has_cookies = 1;
  767. if (n > 0) {
  768. smart_str_append_const(&soap_headers, "Cookie: ");
  769. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(cookies), key, data) {
  770. if (key && Z_TYPE_P(data) == IS_ARRAY) {
  771. zval *value;
  772. if ((value = zend_hash_index_find(Z_ARRVAL_P(data), 0)) != NULL &&
  773. Z_TYPE_P(value) == IS_STRING) {
  774. zval *tmp;
  775. if (((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 1)) == NULL ||
  776. Z_TYPE_P(tmp) != IS_STRING ||
  777. strncmp(phpurl->path?ZSTR_VAL(phpurl->path):"/",Z_STRVAL_P(tmp),Z_STRLEN_P(tmp)) == 0) &&
  778. ((tmp = zend_hash_index_find(Z_ARRVAL_P(data), 2)) == NULL ||
  779. Z_TYPE_P(tmp) != IS_STRING ||
  780. in_domain(ZSTR_VAL(phpurl->host),Z_STRVAL_P(tmp))) &&
  781. (use_ssl || (tmp = zend_hash_index_find(Z_ARRVAL_P(data), 3)) == NULL)) {
  782. smart_str_append(&soap_headers, key);
  783. smart_str_appendc(&soap_headers, '=');
  784. smart_str_append(&soap_headers, Z_STR_P(value));
  785. smart_str_appendc(&soap_headers, ';');
  786. }
  787. }
  788. }
  789. } ZEND_HASH_FOREACH_END();
  790. smart_str_append_const(&soap_headers, "\r\n");
  791. }
  792. }
  793. http_context_headers(context, has_authorization, has_proxy_authorization, has_cookies, &soap_headers);
  794. smart_str_append_const(&soap_headers, "\r\n");
  795. smart_str_0(&soap_headers);
  796. if ((trace = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace")-1)) != NULL &&
  797. (Z_TYPE_P(trace) == IS_TRUE || (Z_TYPE_P(trace) == IS_LONG && Z_LVAL_P(trace) != 0))) {
  798. add_property_stringl(this_ptr, "__last_request_headers", ZSTR_VAL(soap_headers.s), ZSTR_LEN(soap_headers.s));
  799. }
  800. smart_str_appendl(&soap_headers, request->val, request->len);
  801. smart_str_0(&soap_headers);
  802. err = php_stream_write(stream, ZSTR_VAL(soap_headers.s), ZSTR_LEN(soap_headers.s));
  803. if (err != ZSTR_LEN(soap_headers.s)) {
  804. if (request != buf) {
  805. zend_string_release_ex(request, 0);
  806. }
  807. php_stream_close(stream);
  808. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl")-1);
  809. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  810. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  811. add_soap_fault(this_ptr, "HTTP", "Failed Sending HTTP SOAP request", NULL, NULL);
  812. smart_str_free(&soap_headers_z);
  813. return FALSE;
  814. }
  815. smart_str_free(&soap_headers);
  816. } else {
  817. add_soap_fault(this_ptr, "HTTP", "Failed to create stream??", NULL, NULL);
  818. smart_str_free(&soap_headers_z);
  819. return FALSE;
  820. }
  821. if (!return_value) {
  822. php_stream_close(stream);
  823. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  824. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  825. smart_str_free(&soap_headers_z);
  826. return TRUE;
  827. }
  828. do {
  829. http_headers = get_http_headers(stream);
  830. if (!http_headers) {
  831. if (request != buf) {
  832. zend_string_release_ex(request, 0);
  833. }
  834. php_stream_close(stream);
  835. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  836. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  837. add_soap_fault(this_ptr, "HTTP", "Error Fetching http headers", NULL, NULL);
  838. smart_str_free(&soap_headers_z);
  839. return FALSE;
  840. }
  841. if ((trace = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace")-1)) != NULL &&
  842. (Z_TYPE_P(trace) == IS_TRUE || (Z_TYPE_P(trace) == IS_LONG && Z_LVAL_P(trace) != 0))) {
  843. add_property_str(this_ptr, "__last_response_headers", zend_string_copy(http_headers));
  844. }
  845. /* Check to see what HTTP status was sent */
  846. http_1_1 = 0;
  847. http_status = 0;
  848. http_version = get_http_header_value(ZSTR_VAL(http_headers), "HTTP/");
  849. if (http_version) {
  850. char *tmp;
  851. if (!strncmp(http_version,"1.1", 3)) {
  852. http_1_1 = 1;
  853. }
  854. tmp = strstr(http_version," ");
  855. if (tmp != NULL) {
  856. tmp++;
  857. http_status = atoi(tmp);
  858. }
  859. tmp = strstr(tmp," ");
  860. if (tmp != NULL) {
  861. tmp++;
  862. if (http_msg) {
  863. efree(http_msg);
  864. }
  865. http_msg = estrdup(tmp);
  866. }
  867. efree(http_version);
  868. /* Try and get headers again */
  869. if (http_status == 100) {
  870. zend_string_release_ex(http_headers, 0);
  871. }
  872. }
  873. } while (http_status == 100);
  874. /* Grab and send back every cookie */
  875. /* Not going to worry about Path: because
  876. we shouldn't be changing urls so path doesn't
  877. matter too much
  878. */
  879. cookie_itt = strstr(ZSTR_VAL(http_headers), "Set-Cookie: ");
  880. while (cookie_itt) {
  881. char *cookie;
  882. char *eqpos, *sempos;
  883. zval *cookies;
  884. if ((cookies = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies")-1)) == NULL ||
  885. Z_TYPE_P(cookies) != IS_ARRAY) {
  886. zval tmp_cookies;
  887. array_init(&tmp_cookies);
  888. cookies = zend_hash_str_update(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies")-1, &tmp_cookies);
  889. }
  890. cookie = get_http_header_value(cookie_itt,"Set-Cookie: ");
  891. eqpos = strstr(cookie, "=");
  892. sempos = strstr(cookie, ";");
  893. if (eqpos != NULL && (sempos == NULL || sempos > eqpos)) {
  894. smart_str name = {0};
  895. int cookie_len;
  896. zval zcookie;
  897. if (sempos != NULL) {
  898. cookie_len = sempos-(eqpos+1);
  899. } else {
  900. cookie_len = strlen(cookie)-(eqpos-cookie)-1;
  901. }
  902. smart_str_appendl(&name, cookie, eqpos - cookie);
  903. smart_str_0(&name);
  904. array_init(&zcookie);
  905. add_index_stringl(&zcookie, 0, eqpos + 1, cookie_len);
  906. if (sempos != NULL) {
  907. char *options = cookie + cookie_len+1;
  908. while (*options) {
  909. while (*options == ' ') {options++;}
  910. sempos = strstr(options, ";");
  911. if (strstr(options,"path=") == options) {
  912. eqpos = options + sizeof("path=")-1;
  913. add_index_stringl(&zcookie, 1, eqpos, sempos?(size_t)(sempos-eqpos):strlen(eqpos));
  914. } else if (strstr(options,"domain=") == options) {
  915. eqpos = options + sizeof("domain=")-1;
  916. add_index_stringl(&zcookie, 2, eqpos, sempos?(size_t)(sempos-eqpos):strlen(eqpos));
  917. } else if (strstr(options,"secure") == options) {
  918. add_index_bool(&zcookie, 3, 1);
  919. }
  920. if (sempos != NULL) {
  921. options = sempos+1;
  922. } else {
  923. break;
  924. }
  925. }
  926. }
  927. if (!zend_hash_index_exists(Z_ARRVAL(zcookie), 1)) {
  928. char *t = phpurl->path?ZSTR_VAL(phpurl->path):"/";
  929. char *c = strrchr(t, '/');
  930. if (c) {
  931. add_index_stringl(&zcookie, 1, t, c-t);
  932. }
  933. }
  934. if (!zend_hash_index_exists(Z_ARRVAL(zcookie), 2)) {
  935. add_index_str(&zcookie, 2, phpurl->host);
  936. GC_ADDREF(phpurl->host);
  937. }
  938. zend_symtable_update(Z_ARRVAL_P(cookies), name.s, &zcookie);
  939. smart_str_free(&name);
  940. }
  941. cookie_itt = strstr(cookie_itt + sizeof("Set-Cookie: "), "Set-Cookie: ");
  942. efree(cookie);
  943. }
  944. /* See if the server requested a close */
  945. if (http_1_1) {
  946. http_close = FALSE;
  947. if (use_proxy && !use_ssl) {
  948. connection = get_http_header_value(ZSTR_VAL(http_headers), "Proxy-Connection: ");
  949. if (connection) {
  950. if (strncasecmp(connection, "close", sizeof("close")-1) == 0) {
  951. http_close = TRUE;
  952. }
  953. efree(connection);
  954. }
  955. }
  956. if (http_close == FALSE) {
  957. connection = get_http_header_value(ZSTR_VAL(http_headers), "Connection: ");
  958. if (connection) {
  959. if (strncasecmp(connection, "close", sizeof("close")-1) == 0) {
  960. http_close = TRUE;
  961. }
  962. efree(connection);
  963. }
  964. }
  965. } else {
  966. http_close = TRUE;
  967. if (use_proxy && !use_ssl) {
  968. connection = get_http_header_value(ZSTR_VAL(http_headers), "Proxy-Connection: ");
  969. if (connection) {
  970. if (strncasecmp(connection, "Keep-Alive", sizeof("Keep-Alive")-1) == 0) {
  971. http_close = FALSE;
  972. }
  973. efree(connection);
  974. }
  975. }
  976. if (http_close == TRUE) {
  977. connection = get_http_header_value(ZSTR_VAL(http_headers), "Connection: ");
  978. if (connection) {
  979. if (strncasecmp(connection, "Keep-Alive", sizeof("Keep-Alive")-1) == 0) {
  980. http_close = FALSE;
  981. }
  982. efree(connection);
  983. }
  984. }
  985. }
  986. http_body = get_http_body(stream, http_close, ZSTR_VAL(http_headers));
  987. if (!http_body) {
  988. if (request != buf) {
  989. zend_string_release_ex(request, 0);
  990. }
  991. php_stream_close(stream);
  992. zend_string_release_ex(http_headers, 0);
  993. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  994. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  995. add_soap_fault(this_ptr, "HTTP", "Error Fetching http body, No Content-Length, connection closed or chunked data", NULL, NULL);
  996. if (http_msg) {
  997. efree(http_msg);
  998. }
  999. smart_str_free(&soap_headers_z);
  1000. return FALSE;
  1001. }
  1002. if (request != buf) {
  1003. zend_string_release_ex(request, 0);
  1004. }
  1005. if (http_close) {
  1006. php_stream_close(stream);
  1007. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket")-1);
  1008. zend_hash_str_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy")-1);
  1009. stream = NULL;
  1010. }
  1011. /* Process HTTP status codes */
  1012. if (http_status >= 300 && http_status < 400) {
  1013. char *loc;
  1014. if ((loc = get_http_header_value(ZSTR_VAL(http_headers), "Location: ")) != NULL) {
  1015. php_url *new_url = php_url_parse(loc);
  1016. if (new_url != NULL) {
  1017. zend_string_release_ex(http_headers, 0);
  1018. zend_string_release_ex(http_body, 0);
  1019. efree(loc);
  1020. if (new_url->scheme == NULL && new_url->path != NULL) {
  1021. new_url->scheme = phpurl->scheme ? zend_string_copy(phpurl->scheme) : NULL;
  1022. new_url->host = phpurl->host ? zend_string_copy(phpurl->host) : NULL;
  1023. new_url->port = phpurl->port;
  1024. if (new_url->path && ZSTR_VAL(new_url->path)[0] != '/') {
  1025. if (phpurl->path) {
  1026. char *t = ZSTR_VAL(phpurl->path);
  1027. char *p = strrchr(t, '/');
  1028. if (p) {
  1029. zend_string *s = zend_string_alloc((p - t) + ZSTR_LEN(new_url->path) + 2, 0);
  1030. strncpy(ZSTR_VAL(s), t, (p - t) + 1);
  1031. ZSTR_VAL(s)[(p - t) + 1] = 0;
  1032. strcat(ZSTR_VAL(s), ZSTR_VAL(new_url->path));
  1033. zend_string_release_ex(new_url->path, 0);
  1034. new_url->path = s;
  1035. }
  1036. } else {
  1037. zend_string *s = zend_string_alloc(ZSTR_LEN(new_url->path) + 2, 0);
  1038. ZSTR_VAL(s)[0] = '/';
  1039. ZSTR_VAL(s)[1] = 0;
  1040. strcat(ZSTR_VAL(s), ZSTR_VAL(new_url->path));
  1041. zend_string_release_ex(new_url->path, 0);
  1042. new_url->path = s;
  1043. }
  1044. }
  1045. }
  1046. phpurl = new_url;
  1047. if (--redirect_max < 1) {
  1048. add_soap_fault(this_ptr, "HTTP", "Redirection limit reached, aborting", NULL, NULL);
  1049. smart_str_free(&soap_headers_z);
  1050. return FALSE;
  1051. }
  1052. goto try_again;
  1053. }
  1054. }
  1055. } else if (http_status == 401) {
  1056. /* Digest authentication */
  1057. zval *digest, *login, *password;
  1058. char *auth = get_http_header_value(ZSTR_VAL(http_headers), "WWW-Authenticate: ");
  1059. if (auth &&
  1060. strstr(auth, "Digest") == auth &&
  1061. ((digest = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest")-1)) == NULL ||
  1062. Z_TYPE_P(digest) != IS_ARRAY) &&
  1063. (login = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login")-1)) != NULL &&
  1064. Z_TYPE_P(login) == IS_STRING &&
  1065. (password = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password")-1)) != NULL &&
  1066. Z_TYPE_P(password) == IS_STRING) {
  1067. char *s;
  1068. zval digest;
  1069. ZVAL_UNDEF(&digest);
  1070. s = auth + sizeof("Digest")-1;
  1071. while (*s != '\0') {
  1072. char *name, *val;
  1073. while (*s == ' ') ++s;
  1074. name = s;
  1075. while (*s != '\0' && *s != '=') ++s;
  1076. if (*s == '=') {
  1077. *s = '\0';
  1078. ++s;
  1079. if (*s == '"') {
  1080. ++s;
  1081. val = s;
  1082. while (*s != '\0' && *s != '"') ++s;
  1083. } else {
  1084. val = s;
  1085. while (*s != '\0' && *s != ' ' && *s != ',') ++s;
  1086. }
  1087. if (*s != '\0') {
  1088. if (*s != ',') {
  1089. *s = '\0';
  1090. ++s;
  1091. while (*s != '\0' && *s != ',') ++s;
  1092. if (*s != '\0') ++s;
  1093. } else {
  1094. *s = '\0';
  1095. ++s;
  1096. }
  1097. }
  1098. if (Z_TYPE(digest) == IS_UNDEF) {
  1099. array_init(&digest);
  1100. }
  1101. add_assoc_string(&digest, name, val);
  1102. }
  1103. }
  1104. if (Z_TYPE(digest) != IS_UNDEF) {
  1105. php_url *new_url = emalloc(sizeof(php_url));
  1106. Z_DELREF(digest);
  1107. add_property_zval_ex(this_ptr, "_digest", sizeof("_digest")-1, &digest);
  1108. *new_url = *phpurl;
  1109. if (phpurl->scheme) phpurl->scheme = zend_string_copy(phpurl->scheme);
  1110. if (phpurl->user) phpurl->user = zend_string_copy(phpurl->user);
  1111. if (phpurl->pass) phpurl->pass = zend_string_copy(phpurl->pass);
  1112. if (phpurl->host) phpurl->host = zend_string_copy(phpurl->host);
  1113. if (phpurl->path) phpurl->path = zend_string_copy(phpurl->path);
  1114. if (phpurl->query) phpurl->query = zend_string_copy(phpurl->query);
  1115. if (phpurl->fragment) phpurl->fragment = zend_string_copy(phpurl->fragment);
  1116. phpurl = new_url;
  1117. efree(auth);
  1118. zend_string_release_ex(http_headers, 0);
  1119. zend_string_release_ex(http_body, 0);
  1120. goto try_again;
  1121. }
  1122. }
  1123. if (auth) efree(auth);
  1124. }
  1125. smart_str_free(&soap_headers_z);
  1126. /* Check and see if the server even sent a xml document */
  1127. content_type = get_http_header_value(ZSTR_VAL(http_headers), "Content-Type: ");
  1128. if (content_type) {
  1129. char *pos = NULL;
  1130. int cmplen;
  1131. pos = strstr(content_type,";");
  1132. if (pos != NULL) {
  1133. cmplen = pos - content_type;
  1134. } else {
  1135. cmplen = strlen(content_type);
  1136. }
  1137. if (strncmp(content_type, "text/xml", cmplen) == 0 ||
  1138. strncmp(content_type, "application/soap+xml", cmplen) == 0) {
  1139. content_type_xml = 1;
  1140. /*
  1141. if (strncmp(http_body, "<?xml", 5)) {
  1142. zval *err;
  1143. MAKE_STD_ZVAL(err);
  1144. ZVAL_STRINGL(err, http_body, http_body_size, 1);
  1145. add_soap_fault(this_ptr, "HTTP", "Didn't receive an xml document", NULL, err);
  1146. efree(content_type);
  1147. zend_string_release_ex(http_headers, 0);
  1148. efree(http_body);
  1149. return FALSE;
  1150. }
  1151. */
  1152. }
  1153. efree(content_type);
  1154. }
  1155. /* Decompress response */
  1156. content_encoding = get_http_header_value(ZSTR_VAL(http_headers), "Content-Encoding: ");
  1157. if (content_encoding) {
  1158. zval func;
  1159. zval retval;
  1160. zval params[1];
  1161. if ((strcmp(content_encoding,"gzip") == 0 ||
  1162. strcmp(content_encoding,"x-gzip") == 0) &&
  1163. zend_hash_str_exists(EG(function_table), "gzinflate", sizeof("gzinflate")-1)) {
  1164. ZVAL_STRING(&func, "gzinflate");
  1165. ZVAL_STRINGL(&params[0], http_body->val+10, http_body->len-10);
  1166. } else if (strcmp(content_encoding,"deflate") == 0 &&
  1167. zend_hash_str_exists(EG(function_table), "gzuncompress", sizeof("gzuncompress")-1)) {
  1168. ZVAL_STRING(&func, "gzuncompress");
  1169. ZVAL_STR_COPY(&params[0], http_body);
  1170. } else {
  1171. efree(content_encoding);
  1172. zend_string_release_ex(http_headers, 0);
  1173. zend_string_release_ex(http_body, 0);
  1174. if (http_msg) {
  1175. efree(http_msg);
  1176. }
  1177. add_soap_fault(this_ptr, "HTTP", "Unknown Content-Encoding", NULL, NULL);
  1178. return FALSE;
  1179. }
  1180. if (call_user_function(CG(function_table), (zval*)NULL, &func, &retval, 1, params) == SUCCESS &&
  1181. Z_TYPE(retval) == IS_STRING) {
  1182. zval_ptr_dtor(&params[0]);
  1183. zval_ptr_dtor(&func);
  1184. zend_string_release_ex(http_body, 0);
  1185. ZVAL_COPY_VALUE(return_value, &retval);
  1186. } else {
  1187. zval_ptr_dtor(&params[0]);
  1188. zval_ptr_dtor(&func);
  1189. efree(content_encoding);
  1190. zend_string_release_ex(http_headers, 0);
  1191. zend_string_release_ex(http_body, 0);
  1192. add_soap_fault(this_ptr, "HTTP", "Can't uncompress compressed response", NULL, NULL);
  1193. if (http_msg) {
  1194. efree(http_msg);
  1195. }
  1196. return FALSE;
  1197. }
  1198. efree(content_encoding);
  1199. } else {
  1200. ZVAL_STR(return_value, http_body);
  1201. }
  1202. zend_string_release_ex(http_headers, 0);
  1203. if (http_status >= 400) {
  1204. int error = 0;
  1205. if (Z_STRLEN_P(return_value) == 0) {
  1206. error = 1;
  1207. } else if (Z_STRLEN_P(return_value) > 0) {
  1208. if (!content_type_xml) {
  1209. char *s = Z_STRVAL_P(return_value);
  1210. while (*s != '\0' && *s < ' ') {
  1211. s++;
  1212. }
  1213. if (strncmp(s, "<?xml", 5)) {
  1214. error = 1;
  1215. }
  1216. }
  1217. }
  1218. if (error) {
  1219. zval_ptr_dtor(return_value);
  1220. ZVAL_UNDEF(return_value);
  1221. add_soap_fault(this_ptr, "HTTP", http_msg, NULL, NULL);
  1222. efree(http_msg);
  1223. return FALSE;
  1224. }
  1225. }
  1226. if (http_msg) {
  1227. efree(http_msg);
  1228. }
  1229. return TRUE;
  1230. }
  1231. static char *get_http_header_value(char *headers, char *type)
  1232. {
  1233. char *pos, *tmp = NULL;
  1234. int typelen, headerslen;
  1235. typelen = strlen(type);
  1236. headerslen = strlen(headers);
  1237. /* header `titles' can be lower case, or any case combination, according
  1238. * to the various RFC's. */
  1239. pos = headers;
  1240. do {
  1241. /* start of buffer or start of line */
  1242. if (strncasecmp(pos, type, typelen) == 0) {
  1243. char *eol;
  1244. /* match */
  1245. tmp = pos + typelen;
  1246. eol = strchr(tmp, '\n');
  1247. if (eol == NULL) {
  1248. eol = headers + headerslen;
  1249. } else if (eol > tmp && *(eol-1) == '\r') {
  1250. eol--;
  1251. }
  1252. return estrndup(tmp, eol - tmp);
  1253. }
  1254. /* find next line */
  1255. pos = strchr(pos, '\n');
  1256. if (pos) {
  1257. pos++;
  1258. }
  1259. } while (pos);
  1260. return NULL;
  1261. }
  1262. static zend_string* get_http_body(php_stream *stream, int close, char *headers)
  1263. {
  1264. zend_string *http_buf = NULL;
  1265. char *header;
  1266. int header_close = close, header_chunked = 0, header_length = 0, http_buf_size = 0;
  1267. if (!close) {
  1268. header = get_http_header_value(headers, "Connection: ");
  1269. if (header) {
  1270. if(!strncasecmp(header, "close", sizeof("close")-1)) header_close = 1;
  1271. efree(header);
  1272. }
  1273. }
  1274. header = get_http_header_value(headers, "Transfer-Encoding: ");
  1275. if (header) {
  1276. if(!strncasecmp(header, "chunked", sizeof("chunked")-1)) header_chunked = 1;
  1277. efree(header);
  1278. }
  1279. header = get_http_header_value(headers, "Content-Length: ");
  1280. if (header) {
  1281. header_length = atoi(header);
  1282. efree(header);
  1283. if (!header_length && !header_chunked) {
  1284. /* Empty response */
  1285. return ZSTR_EMPTY_ALLOC();
  1286. }
  1287. }
  1288. if (header_chunked) {
  1289. char ch, done, headerbuf[8192];
  1290. done = FALSE;
  1291. while (!done) {
  1292. int buf_size = 0;
  1293. php_stream_gets(stream, headerbuf, sizeof(headerbuf));
  1294. if (sscanf(headerbuf, "%x", &buf_size) > 0 ) {
  1295. if (buf_size > 0) {
  1296. size_t len_size = 0;
  1297. if (http_buf_size + buf_size + 1 < 0) {
  1298. if (http_buf) {
  1299. zend_string_release_ex(http_buf, 0);
  1300. }
  1301. return NULL;
  1302. }
  1303. if (http_buf) {
  1304. http_buf = zend_string_realloc(http_buf, http_buf_size + buf_size, 0);
  1305. } else {
  1306. http_buf = zend_string_alloc(buf_size, 0);
  1307. }
  1308. while (len_size < buf_size) {
  1309. ssize_t len_read = php_stream_read(stream, http_buf->val + http_buf_size, buf_size - len_size);
  1310. if (len_read <= 0) {
  1311. /* Error or EOF */
  1312. done = TRUE;
  1313. break;
  1314. }
  1315. len_size += len_read;
  1316. http_buf_size += len_read;
  1317. }
  1318. /* Eat up '\r' '\n' */
  1319. ch = php_stream_getc(stream);
  1320. if (ch == '\r') {
  1321. ch = php_stream_getc(stream);
  1322. }
  1323. if (ch != '\n') {
  1324. /* Something wrong in chunked encoding */
  1325. if (http_buf) {
  1326. zend_string_release_ex(http_buf, 0);
  1327. }
  1328. return NULL;
  1329. }
  1330. }
  1331. } else {
  1332. /* Something wrong in chunked encoding */
  1333. if (http_buf) {
  1334. zend_string_release_ex(http_buf, 0);
  1335. }
  1336. return NULL;
  1337. }
  1338. if (buf_size == 0) {
  1339. done = TRUE;
  1340. }
  1341. }
  1342. /* Ignore trailer headers */
  1343. while (1) {
  1344. if (!php_stream_gets(stream, headerbuf, sizeof(headerbuf))) {
  1345. break;
  1346. }
  1347. if ((headerbuf[0] == '\r' && headerbuf[1] == '\n') ||
  1348. (headerbuf[0] == '\n')) {
  1349. /* empty line marks end of headers */
  1350. break;
  1351. }
  1352. }
  1353. if (http_buf == NULL) {
  1354. return ZSTR_EMPTY_ALLOC();
  1355. }
  1356. } else if (header_length) {
  1357. if (header_length < 0 || header_length >= INT_MAX) {
  1358. return NULL;
  1359. }
  1360. http_buf = zend_string_alloc(header_length, 0);
  1361. while (http_buf_size < header_length) {
  1362. ssize_t len_read = php_stream_read(stream, http_buf->val + http_buf_size, header_length - http_buf_size);
  1363. if (len_read <= 0) {
  1364. break;
  1365. }
  1366. http_buf_size += len_read;
  1367. }
  1368. } else if (header_close) {
  1369. do {
  1370. ssize_t len_read;
  1371. if (http_buf) {
  1372. http_buf = z

Large files files are truncated, but you can click here to view the full file