php_http.c 47 KB

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