php_http.c 51 KB

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