PageRenderTime 40ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 1ms

/ext/soap/php_sdl.c

https://github.com/php/php-src
C | 3673 lines | 3138 code | 461 blank | 74 comment | 877 complexity | 99f8f392426facf20ff26b707cd33ac4 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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/libxml/php_libxml.h"
  20. #include "libxml/uri.h"
  21. #include "ext/standard/md5.h"
  22. #include "zend_virtual_cwd.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #ifndef O_BINARY
  27. # define O_BINARY 0
  28. #endif
  29. static void delete_fault(zval *zv);
  30. static void delete_fault_persistent(zval *zv);
  31. static void delete_binding(zval *zv);
  32. static void delete_binding_persistent(zval *zv);
  33. static void delete_function(zval *zv);
  34. static void delete_function_persistent(zval *zv);
  35. static void delete_parameter(zval *zv);
  36. static void delete_parameter_persistent(zval *zv);
  37. static void delete_header(zval *header);
  38. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr);
  39. static void delete_header_persistent(zval *zv);
  40. static void delete_document(zval *zv);
  41. encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  42. {
  43. encodePtr enc = NULL;
  44. xmlNsPtr nsptr;
  45. char *ns, *cptype;
  46. parse_namespace(type, &cptype, &ns);
  47. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  48. if (nsptr != NULL) {
  49. enc = get_encoder(sdl, (char*)nsptr->href, cptype);
  50. if (enc == NULL) {
  51. enc = get_encoder_ex(sdl, cptype, strlen(cptype));
  52. }
  53. } else {
  54. enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
  55. }
  56. efree(cptype);
  57. if (ns) {efree(ns);}
  58. return enc;
  59. }
  60. static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  61. {
  62. sdlTypePtr ret = NULL;
  63. if (sdl->elements) {
  64. xmlNsPtr nsptr;
  65. char *ns, *cptype;
  66. sdlTypePtr sdl_type;
  67. parse_namespace(type, &cptype, &ns);
  68. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  69. if (nsptr != NULL) {
  70. int ns_len = xmlStrlen(nsptr->href);
  71. int type_len = strlen(cptype);
  72. int len = ns_len + type_len + 1;
  73. char *nscat = emalloc(len + 1);
  74. memcpy(nscat, nsptr->href, ns_len);
  75. nscat[ns_len] = ':';
  76. memcpy(nscat+ns_len+1, cptype, type_len);
  77. nscat[len] = '\0';
  78. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, nscat, len)) != NULL) {
  79. ret = sdl_type;
  80. } else if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, type_len)) != NULL) {
  81. ret = sdl_type;
  82. }
  83. efree(nscat);
  84. } else {
  85. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, xmlStrlen(type))) != NULL) {
  86. ret = sdl_type;
  87. }
  88. }
  89. efree(cptype);
  90. if (ns) {efree(ns);}
  91. }
  92. return ret;
  93. }
  94. encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
  95. {
  96. encodePtr enc = NULL;
  97. char *nscat;
  98. int ns_len = ns ? strlen(ns) : 0;
  99. int type_len = strlen(type);
  100. int len = ns_len + type_len + 1;
  101. nscat = emalloc(len + 1);
  102. if (ns) {
  103. memcpy(nscat, ns, ns_len);
  104. }
  105. nscat[ns_len] = ':';
  106. memcpy(nscat+ns_len+1, type, type_len);
  107. nscat[len] = '\0';
  108. enc = get_encoder_ex(sdl, nscat, len);
  109. if (enc == NULL &&
  110. ((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  111. memcmp(ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  112. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  113. memcmp(ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  114. char *enc_nscat;
  115. int enc_ns_len;
  116. int enc_len;
  117. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  118. enc_len = enc_ns_len + type_len + 1;
  119. enc_nscat = emalloc(enc_len + 1);
  120. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  121. enc_nscat[enc_ns_len] = ':';
  122. memcpy(enc_nscat+enc_ns_len+1, type, type_len);
  123. enc_nscat[enc_len] = '\0';
  124. enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  125. efree(enc_nscat);
  126. if (enc && sdl) {
  127. encodePtr new_enc = pemalloc(sizeof(encode), sdl->is_persistent);
  128. memcpy(new_enc, enc, sizeof(encode));
  129. if (sdl->is_persistent) {
  130. new_enc->details.ns = zend_strndup(ns, ns_len);
  131. new_enc->details.type_str = strdup(new_enc->details.type_str);
  132. } else {
  133. new_enc->details.ns = estrndup(ns, ns_len);
  134. new_enc->details.type_str = estrdup(new_enc->details.type_str);
  135. }
  136. if (sdl->encoders == NULL) {
  137. sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
  138. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, sdl->is_persistent);
  139. }
  140. zend_hash_str_update_ptr(sdl->encoders, nscat, len, new_enc);
  141. enc = new_enc;
  142. }
  143. }
  144. efree(nscat);
  145. return enc;
  146. }
  147. encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, int len)
  148. {
  149. encodePtr enc;
  150. if ((enc = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEnc), (char*)nscat, len)) != NULL) {
  151. return enc;
  152. } else if (sdl && sdl->encoders && (enc = zend_hash_str_find_ptr(sdl->encoders, (char*)nscat, len)) != NULL) {
  153. return enc;
  154. }
  155. return NULL;
  156. }
  157. sdlBindingPtr get_binding_from_type(sdlPtr sdl, sdlBindingType type)
  158. {
  159. sdlBindingPtr binding;
  160. if (sdl == NULL) {
  161. return NULL;
  162. }
  163. ZEND_HASH_MAP_FOREACH_PTR(sdl->bindings, binding) {
  164. if (binding->bindingType == type) {
  165. return binding;
  166. }
  167. } ZEND_HASH_FOREACH_END();
  168. return NULL;
  169. }
  170. sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns)
  171. {
  172. sdlBindingPtr binding;
  173. smart_str key = {0};
  174. smart_str_appends(&key, ns);
  175. smart_str_appendc(&key, ':');
  176. smart_str_appends(&key, name);
  177. smart_str_0(&key);
  178. binding = zend_hash_find_ptr(sdl->bindings, key.s);
  179. smart_str_free(&key);
  180. return binding;
  181. }
  182. static int is_wsdl_element(xmlNodePtr node)
  183. {
  184. if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
  185. xmlAttrPtr attr;
  186. if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
  187. attr->children && attr->children->content &&
  188. (strcmp((char*)attr->children->content, "1") == 0 ||
  189. strcmp((char*)attr->children->content, "true") == 0)) {
  190. soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
  191. }
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
  197. {
  198. char *s;
  199. size_t l1, l2;
  200. zval context;
  201. zval *header = NULL;
  202. /* check if we load xsd from the same server */
  203. s = strstr(ctx->sdl->source, "://");
  204. if (!s) return;
  205. s = strchr(s+3, '/');
  206. l1 = s ? (size_t)(s - ctx->sdl->source) : strlen(ctx->sdl->source);
  207. s = strstr((char*)uri, "://");
  208. if (!s) return;
  209. s = strchr(s+3, '/');
  210. l2 = s ? (size_t)(s - (char*)uri) : strlen((char*)uri);
  211. if (l1 != l2) {
  212. /* check for http://...:80/ */
  213. if (l1 > 11 &&
  214. ctx->sdl->source[4] == ':' &&
  215. ctx->sdl->source[l1-3] == ':' &&
  216. ctx->sdl->source[l1-2] == '8' &&
  217. ctx->sdl->source[l1-1] == '0') {
  218. l1 -= 3;
  219. }
  220. if (l2 > 11 &&
  221. uri[4] == ':' &&
  222. uri[l2-3] == ':' &&
  223. uri[l2-2] == '8' &&
  224. uri[l2-1] == '0') {
  225. l2 -= 3;
  226. }
  227. /* check for https://...:443/ */
  228. if (l1 > 13 &&
  229. ctx->sdl->source[4] == 's' &&
  230. ctx->sdl->source[l1-4] == ':' &&
  231. ctx->sdl->source[l1-3] == '4' &&
  232. ctx->sdl->source[l1-2] == '4' &&
  233. ctx->sdl->source[l1-1] == '3') {
  234. l1 -= 4;
  235. }
  236. if (l2 > 13 &&
  237. uri[4] == 's' &&
  238. uri[l2-4] == ':' &&
  239. uri[l2-3] == '4' &&
  240. uri[l2-2] == '4' &&
  241. uri[l2-1] == '3') {
  242. l2 -= 4;
  243. }
  244. }
  245. if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
  246. /* another server. clear authentication credentals */
  247. php_libxml_switch_context(NULL, &context);
  248. php_libxml_switch_context(&context, NULL);
  249. if (Z_TYPE(context) != IS_UNDEF) {
  250. zval *context_ptr = &context;
  251. ctx->context = php_stream_context_from_zval(context_ptr, 1);
  252. if (ctx->context &&
  253. (header = php_stream_context_get_option(ctx->context, "http", "header")) != NULL) {
  254. s = strstr(Z_STRVAL_P(header), "Authorization: Basic");
  255. if (s && (s == Z_STRVAL_P(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
  256. char *rest = strstr(s, "\r\n");
  257. if (rest) {
  258. zval new_header;
  259. rest += 2;
  260. ZVAL_NEW_STR(&new_header, zend_string_alloc(Z_STRLEN_P(header) - (rest - s), 0));
  261. memcpy(Z_STRVAL(new_header), Z_STRVAL_P(header), s - Z_STRVAL_P(header));
  262. memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_P(header)), rest, Z_STRLEN_P(header) - (rest - Z_STRVAL_P(header)) + 1);
  263. ZVAL_COPY(&ctx->old_header, header);
  264. php_stream_context_set_option(ctx->context, "http", "header", &new_header);
  265. zval_ptr_dtor(&new_header);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. void sdl_restore_uri_credentials(sdlCtx *ctx)
  273. {
  274. if (Z_TYPE(ctx->old_header) != IS_UNDEF) {
  275. php_stream_context_set_option(ctx->context, "http", "header", &ctx->old_header);
  276. zval_ptr_dtor(&ctx->old_header);
  277. ZVAL_UNDEF(&ctx->old_header);
  278. }
  279. ctx->context = NULL;
  280. }
  281. #define SAFE_STR(a) ((a)?((const char *)a):"")
  282. static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include)
  283. {
  284. sdlPtr tmpsdl = ctx->sdl;
  285. xmlDocPtr wsdl;
  286. xmlNodePtr root, definitions, trav;
  287. xmlAttrPtr targetNamespace;
  288. if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
  289. return;
  290. }
  291. sdl_set_uri_credentials(ctx, struri);
  292. wsdl = soap_xmlParseFile(struri);
  293. sdl_restore_uri_credentials(ctx);
  294. if (!wsdl) {
  295. xmlErrorPtr xmlErrorPtr = xmlGetLastError();
  296. if (xmlErrorPtr) {
  297. soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
  298. } else {
  299. soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
  300. }
  301. }
  302. zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
  303. root = wsdl->children;
  304. definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
  305. if (!definitions) {
  306. if (include) {
  307. xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
  308. if (schema) {
  309. load_schema(ctx, schema);
  310. return;
  311. }
  312. }
  313. soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
  314. }
  315. if (!include) {
  316. targetNamespace = get_attribute(definitions->properties, "targetNamespace");
  317. if (targetNamespace) {
  318. tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
  319. }
  320. }
  321. trav = definitions->children;
  322. while (trav != NULL) {
  323. if (!is_wsdl_element(trav)) {
  324. trav = trav->next;
  325. continue;
  326. }
  327. if (node_is_equal(trav,"types")) {
  328. /* TODO: Only one "types" is allowed */
  329. xmlNodePtr trav2 = trav->children;
  330. while (trav2 != NULL) {
  331. if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  332. load_schema(ctx, trav2);
  333. } else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  334. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  335. }
  336. trav2 = trav2->next;
  337. }
  338. } else if (node_is_equal(trav,"import")) {
  339. /* TODO: namespace ??? */
  340. xmlAttrPtr tmp = get_attribute(trav->properties, "location");
  341. if (tmp) {
  342. xmlChar *uri;
  343. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  344. if (base == NULL) {
  345. uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
  346. } else {
  347. uri = xmlBuildURI(tmp->children->content, base);
  348. xmlFree(base);
  349. }
  350. load_wsdl_ex(this_ptr, (char*)uri, ctx, 1);
  351. xmlFree(uri);
  352. }
  353. } else if (node_is_equal(trav,"message")) {
  354. xmlAttrPtr name = get_attribute(trav->properties, "name");
  355. if (name && name->children && name->children->content) {
  356. if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  357. soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
  358. }
  359. } else {
  360. soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
  361. }
  362. } else if (node_is_equal(trav,"portType")) {
  363. xmlAttrPtr name = get_attribute(trav->properties, "name");
  364. if (name && name->children && name->children->content) {
  365. if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  366. soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
  367. }
  368. } else {
  369. soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
  370. }
  371. } else if (node_is_equal(trav,"binding")) {
  372. xmlAttrPtr name = get_attribute(trav->properties, "name");
  373. if (name && name->children && name->children->content) {
  374. if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  375. soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
  376. }
  377. } else {
  378. soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
  379. }
  380. } else if (node_is_equal(trav,"service")) {
  381. xmlAttrPtr name = get_attribute(trav->properties, "name");
  382. if (name && name->children && name->children->content) {
  383. if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  384. soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
  385. }
  386. } else {
  387. soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  388. }
  389. } else if (!node_is_equal(trav,"documentation")) {
  390. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  391. }
  392. trav = trav->next;
  393. }
  394. }
  395. static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
  396. {
  397. xmlAttrPtr tmp;
  398. xmlNodePtr message, part;
  399. char *ctype;
  400. sdlSoapBindingFunctionHeaderPtr h;
  401. tmp = get_attribute(header->properties, "message");
  402. if (!tmp) {
  403. soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
  404. }
  405. ctype = strrchr((char*)tmp->children->content,':');
  406. if (ctype == NULL) {
  407. ctype = (char*)tmp->children->content;
  408. } else {
  409. ++ctype;
  410. }
  411. if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  412. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
  413. }
  414. tmp = get_attribute(header->properties, "part");
  415. if (!tmp) {
  416. soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
  417. }
  418. part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
  419. if (!part) {
  420. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
  421. }
  422. h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  423. memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
  424. h->name = estrdup((char*)tmp->children->content);
  425. tmp = get_attribute(header->properties, "use");
  426. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  427. h->use = SOAP_ENCODED;
  428. } else {
  429. h->use = SOAP_LITERAL;
  430. }
  431. tmp = get_attribute(header->properties, "namespace");
  432. if (tmp) {
  433. h->ns = estrdup((char*)tmp->children->content);
  434. }
  435. if (h->use == SOAP_ENCODED) {
  436. tmp = get_attribute(header->properties, "encodingStyle");
  437. if (tmp) {
  438. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  439. h->encodingStyle = SOAP_ENCODING_1_1;
  440. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  441. h->encodingStyle = SOAP_ENCODING_1_2;
  442. } else {
  443. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  444. }
  445. } else {
  446. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  447. }
  448. }
  449. tmp = get_attribute(part->properties, "type");
  450. if (tmp != NULL) {
  451. h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
  452. } else {
  453. tmp = get_attribute(part->properties, "element");
  454. if (tmp != NULL) {
  455. h->element = get_element(ctx->sdl, part, tmp->children->content);
  456. if (h->element) {
  457. h->encode = h->element->encode;
  458. if (!h->ns && h->element->namens) {
  459. h->ns = estrdup(h->element->namens);
  460. }
  461. if (h->element->name) {
  462. efree(h->name);
  463. h->name = estrdup(h->element->name);
  464. }
  465. }
  466. }
  467. }
  468. if (!fault) {
  469. xmlNodePtr trav = header->children;
  470. while (trav != NULL) {
  471. if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
  472. sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
  473. smart_str key = {0};
  474. if (h->headerfaults == NULL) {
  475. h->headerfaults = emalloc(sizeof(HashTable));
  476. zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
  477. }
  478. if (hf->ns) {
  479. smart_str_appends(&key,hf->ns);
  480. smart_str_appendc(&key,':');
  481. }
  482. smart_str_appends(&key,hf->name);
  483. smart_str_0(&key);
  484. if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
  485. delete_header_int(hf);
  486. }
  487. smart_str_free(&key);
  488. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  489. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  490. }
  491. trav = trav->next;
  492. }
  493. }
  494. return h;
  495. }
  496. static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
  497. {
  498. xmlNodePtr body, trav;
  499. xmlAttrPtr tmp;
  500. trav = node->children;
  501. while (trav != NULL) {
  502. if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
  503. body = trav;
  504. tmp = get_attribute(body->properties, "use");
  505. if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
  506. binding->use = SOAP_LITERAL;
  507. } else {
  508. binding->use = SOAP_ENCODED;
  509. }
  510. tmp = get_attribute(body->properties, "namespace");
  511. if (tmp) {
  512. binding->ns = estrdup((char*)tmp->children->content);
  513. }
  514. tmp = get_attribute(body->properties, "parts");
  515. if (tmp) {
  516. HashTable ht;
  517. char *parts = (char*)tmp->children->content;
  518. /* Delete all parts those are not in the "parts" attribute */
  519. zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
  520. while (*parts) {
  521. sdlParamPtr param;
  522. int found = 0;
  523. char *end;
  524. while (*parts == ' ') ++parts;
  525. if (*parts == '\0') break;
  526. end = strchr(parts, ' ');
  527. if (end) *end = '\0';
  528. ZEND_HASH_FOREACH_PTR(params, param) {
  529. if (param->paramName &&
  530. strcmp(parts, param->paramName) == 0) {
  531. sdlParamPtr x_param;
  532. x_param = emalloc(sizeof(sdlParam));
  533. *x_param = *param;
  534. param->paramName = NULL;
  535. zend_hash_next_index_insert_ptr(&ht, x_param);
  536. found = 1;
  537. break;
  538. }
  539. } ZEND_HASH_FOREACH_END();
  540. if (!found) {
  541. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
  542. }
  543. parts += strlen(parts);
  544. if (end) *end = ' ';
  545. }
  546. zend_hash_destroy(params);
  547. *params = ht;
  548. }
  549. if (binding->use == SOAP_ENCODED) {
  550. tmp = get_attribute(body->properties, "encodingStyle");
  551. if (tmp) {
  552. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  553. binding->encodingStyle = SOAP_ENCODING_1_1;
  554. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  555. binding->encodingStyle = SOAP_ENCODING_1_2;
  556. } else {
  557. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  558. }
  559. } else {
  560. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  561. }
  562. }
  563. } else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
  564. sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
  565. smart_str key = {0};
  566. if (binding->headers == NULL) {
  567. binding->headers = emalloc(sizeof(HashTable));
  568. zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
  569. }
  570. if (h->ns) {
  571. smart_str_appends(&key,h->ns);
  572. smart_str_appendc(&key,':');
  573. }
  574. smart_str_appends(&key,h->name);
  575. smart_str_0(&key);
  576. if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
  577. delete_header_int(h);
  578. }
  579. smart_str_free(&key);
  580. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  581. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  582. }
  583. trav = trav->next;
  584. }
  585. }
  586. static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  587. {
  588. xmlNodePtr trav, part, message = NULL, tmp;
  589. HashTable* parameters = NULL;
  590. char *ctype;
  591. ctype = strrchr((char*)message_name,':');
  592. if (ctype == NULL) {
  593. ctype = (char*)message_name;
  594. } else {
  595. ++ctype;
  596. }
  597. if ((tmp = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  598. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
  599. }
  600. message = tmp;
  601. parameters = emalloc(sizeof(HashTable));
  602. zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
  603. trav = message->children;
  604. while (trav != NULL) {
  605. xmlAttrPtr element, type, name;
  606. sdlParamPtr param;
  607. if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  608. soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", SAFE_STR(trav->name));
  609. }
  610. if (node_is_equal(trav,"documentation")) {
  611. trav = trav->next;
  612. continue;
  613. }
  614. if (!node_is_equal(trav,"part")) {
  615. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  616. }
  617. part = trav;
  618. param = emalloc(sizeof(sdlParam));
  619. memset(param,0,sizeof(sdlParam));
  620. param->order = 0;
  621. name = get_attribute(part->properties, "name");
  622. if (name == NULL) {
  623. soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", SAFE_STR(message->name));
  624. }
  625. param->paramName = estrdup((char*)name->children->content);
  626. type = get_attribute(part->properties, "type");
  627. if (type != NULL) {
  628. param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
  629. } else {
  630. element = get_attribute(part->properties, "element");
  631. if (element != NULL) {
  632. param->element = get_element(ctx->sdl, part, element->children->content);
  633. if (param->element) {
  634. param->encode = param->element->encode;
  635. }
  636. }
  637. }
  638. zend_hash_next_index_insert_ptr(parameters, param);
  639. trav = trav->next;
  640. }
  641. return parameters;
  642. }
  643. static sdlPtr load_wsdl(zval *this_ptr, char *struri)
  644. {
  645. sdlCtx ctx;
  646. int i,n;
  647. memset(&ctx,0,sizeof(ctx));
  648. ctx.sdl = emalloc(sizeof(sdl));
  649. memset(ctx.sdl, 0, sizeof(sdl));
  650. ctx.sdl->source = estrdup(struri);
  651. zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
  652. zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
  653. zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
  654. zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
  655. zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
  656. zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
  657. zend_try {
  658. load_wsdl_ex(this_ptr, struri, &ctx, 0);
  659. schema_pass2(&ctx);
  660. n = zend_hash_num_elements(&ctx.services);
  661. if (n > 0) {
  662. zend_hash_internal_pointer_reset(&ctx.services);
  663. for (i = 0; i < n; i++) {
  664. xmlNodePtr service, tmp;
  665. xmlNodePtr trav, port;
  666. int has_soap_port = 0;
  667. service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
  668. trav = service->children;
  669. while (trav != NULL) {
  670. xmlAttrPtr type, name, bindingAttr, location;
  671. xmlNodePtr portType, operation;
  672. xmlNodePtr address, binding, trav2;
  673. char *ctype;
  674. sdlBindingPtr tmpbinding;
  675. char *wsdl_soap_namespace = NULL;
  676. if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
  677. trav = trav->next;
  678. continue;
  679. }
  680. if (!node_is_equal(trav,"port")) {
  681. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  682. }
  683. port = trav;
  684. tmpbinding = emalloc(sizeof(sdlBinding));
  685. memset(tmpbinding, 0, sizeof(sdlBinding));
  686. bindingAttr = get_attribute(port->properties, "binding");
  687. if (bindingAttr == NULL) {
  688. soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
  689. }
  690. /* find address and figure out binding type */
  691. address = NULL;
  692. trav2 = port->children;
  693. while (trav2 != NULL) {
  694. if (node_is_equal(trav2,"address") && trav2->ns) {
  695. if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
  696. address = trav2;
  697. wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
  698. tmpbinding->bindingType = BINDING_SOAP;
  699. } else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
  700. address = trav2;
  701. wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
  702. tmpbinding->bindingType = BINDING_SOAP;
  703. } else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
  704. address = trav2;
  705. wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
  706. tmpbinding->bindingType = BINDING_SOAP;
  707. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
  708. address = trav2;
  709. tmpbinding->bindingType = BINDING_HTTP;
  710. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
  711. address = trav2;
  712. tmpbinding->bindingType = BINDING_HTTP;
  713. }
  714. }
  715. if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  716. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  717. }
  718. trav2 = trav2->next;
  719. }
  720. if (!address || tmpbinding->bindingType == BINDING_HTTP) {
  721. if (has_soap_port || trav->next || i < n-1) {
  722. efree(tmpbinding);
  723. trav = trav->next;
  724. continue;
  725. } else if (!address) {
  726. soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
  727. }
  728. }
  729. has_soap_port = 1;
  730. location = get_attribute(address->properties, "location");
  731. if (!location) {
  732. soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
  733. }
  734. tmpbinding->location = estrdup((char*)location->children->content);
  735. ctype = strrchr((char*)bindingAttr->children->content,':');
  736. if (ctype == NULL) {
  737. ctype = (char*)bindingAttr->children->content;
  738. } else {
  739. ++ctype;
  740. }
  741. if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
  742. soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
  743. }
  744. binding = tmp;
  745. if (tmpbinding->bindingType == BINDING_SOAP) {
  746. sdlSoapBindingPtr soapBinding;
  747. xmlNodePtr soapBindingNode;
  748. xmlAttrPtr tmp;
  749. soapBinding = emalloc(sizeof(sdlSoapBinding));
  750. memset(soapBinding, 0, sizeof(sdlSoapBinding));
  751. soapBinding->style = SOAP_DOCUMENT;
  752. soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
  753. if (soapBindingNode) {
  754. tmp = get_attribute(soapBindingNode->properties, "style");
  755. if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  756. soapBinding->style = SOAP_RPC;
  757. }
  758. tmp = get_attribute(soapBindingNode->properties, "transport");
  759. if (tmp) {
  760. if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
  761. soapBinding->transport = SOAP_TRANSPORT_HTTP;
  762. } else {
  763. /* try the next binding */
  764. efree(soapBinding);
  765. efree(tmpbinding->location);
  766. efree(tmpbinding);
  767. trav = trav->next;
  768. continue;
  769. }
  770. }
  771. }
  772. tmpbinding->bindingAttributes = (void *)soapBinding;
  773. }
  774. name = get_attribute(binding->properties, "name");
  775. if (name == NULL) {
  776. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
  777. }
  778. tmpbinding->name = estrdup((char*)name->children->content);
  779. type = get_attribute(binding->properties, "type");
  780. if (type == NULL) {
  781. soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
  782. }
  783. ctype = strrchr((char*)type->children->content,':');
  784. if (ctype == NULL) {
  785. ctype = (char*)type->children->content;
  786. } else {
  787. ++ctype;
  788. }
  789. if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
  790. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
  791. }
  792. portType = tmp;
  793. trav2 = binding->children;
  794. while (trav2 != NULL) {
  795. sdlFunctionPtr function;
  796. xmlNodePtr input, output, fault, portTypeOperation, trav3;
  797. xmlAttrPtr op_name, paramOrder;
  798. if ((tmpbinding->bindingType == BINDING_SOAP &&
  799. node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
  800. !is_wsdl_element(trav2) ||
  801. node_is_equal(trav2,"documentation")) {
  802. trav2 = trav2->next;
  803. continue;
  804. }
  805. if (!node_is_equal(trav2,"operation")) {
  806. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  807. }
  808. operation = trav2;
  809. op_name = get_attribute(operation->properties, "name");
  810. if (op_name == NULL) {
  811. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
  812. }
  813. trav3 = operation->children;
  814. while (trav3 != NULL) {
  815. if (tmpbinding->bindingType == BINDING_SOAP &&
  816. node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
  817. } else if (is_wsdl_element(trav3) &&
  818. !node_is_equal(trav3,"input") &&
  819. !node_is_equal(trav3,"output") &&
  820. !node_is_equal(trav3,"fault") &&
  821. !node_is_equal(trav3,"documentation")) {
  822. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav3->name));
  823. }
  824. trav3 = trav3->next;
  825. }
  826. portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
  827. if (portTypeOperation == NULL) {
  828. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
  829. }
  830. function = emalloc(sizeof(sdlFunction));
  831. memset(function, 0, sizeof(sdlFunction));
  832. function->functionName = estrdup((char*)op_name->children->content);
  833. if (tmpbinding->bindingType == BINDING_SOAP) {
  834. sdlSoapBindingFunctionPtr soapFunctionBinding;
  835. sdlSoapBindingPtr soapBinding;
  836. xmlNodePtr soapOperation;
  837. xmlAttrPtr tmp;
  838. soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
  839. memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
  840. soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
  841. soapFunctionBinding->style = soapBinding->style;
  842. soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
  843. if (soapOperation) {
  844. tmp = get_attribute(soapOperation->properties, "soapAction");
  845. if (tmp) {
  846. soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
  847. }
  848. tmp = get_attribute(soapOperation->properties, "style");
  849. if (tmp) {
  850. if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  851. soapFunctionBinding->style = SOAP_RPC;
  852. } else {
  853. soapFunctionBinding->style = SOAP_DOCUMENT;
  854. }
  855. } else {
  856. soapFunctionBinding->style = soapBinding->style;
  857. }
  858. }
  859. function->bindingAttributes = (void *)soapFunctionBinding;
  860. }
  861. input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
  862. if (input != NULL) {
  863. xmlAttrPtr message;
  864. message = get_attribute(input->properties, "message");
  865. if (message == NULL) {
  866. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
  867. }
  868. function->requestParameters = wsdl_message(&ctx, message->children->content);
  869. /* FIXME
  870. xmlAttrPtr name = get_attribute(input->properties, "name");
  871. if (name != NULL) {
  872. function->requestName = estrdup(name->children->content);
  873. } else {
  874. */
  875. {
  876. function->requestName = estrdup(function->functionName);
  877. }
  878. if (tmpbinding->bindingType == BINDING_SOAP) {
  879. input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
  880. if (input != NULL) {
  881. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  882. wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
  883. }
  884. }
  885. }
  886. output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
  887. if (output != NULL) {
  888. xmlAttrPtr message;
  889. message = get_attribute(output->properties, "message");
  890. if (message == NULL) {
  891. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  892. }
  893. function->responseParameters = wsdl_message(&ctx, message->children->content);
  894. /* FIXME
  895. xmlAttrPtr name = get_attribute(output->properties, "name");
  896. if (name != NULL) {
  897. function->responseName = estrdup(name->children->content);
  898. } else if (input == NULL) {
  899. function->responseName = estrdup(function->functionName);
  900. } else {
  901. */
  902. {
  903. int len = strlen(function->functionName);
  904. function->responseName = emalloc(len + sizeof("Response"));
  905. memcpy(function->responseName, function->functionName, len);
  906. memcpy(function->responseName+len, "Response", sizeof("Response"));
  907. }
  908. if (tmpbinding->bindingType == BINDING_SOAP) {
  909. output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
  910. if (output != NULL) {
  911. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  912. wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
  913. }
  914. }
  915. }
  916. paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
  917. if (paramOrder) {
  918. /* FIXME: */
  919. }
  920. fault = portTypeOperation->children;
  921. while (fault != NULL) {
  922. if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
  923. xmlAttrPtr message, name;
  924. sdlFaultPtr f;
  925. name = get_attribute(fault->properties, "name");
  926. if (name == NULL) {
  927. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
  928. }
  929. message = get_attribute(fault->properties, "message");
  930. if (message == NULL) {
  931. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  932. }
  933. f = emalloc(sizeof(sdlFault));
  934. memset(f, 0, sizeof(sdlFault));
  935. f->name = estrdup((char*)name->children->content);
  936. f->details = wsdl_message(&ctx, message->children->content);
  937. if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
  938. soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
  939. }
  940. if (tmpbinding->bindingType == BINDING_SOAP) {
  941. xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
  942. if (soap_fault != NULL) {
  943. xmlNodePtr trav = soap_fault->children;
  944. while (trav != NULL) {
  945. if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
  946. xmlAttrPtr tmp;
  947. sdlSoapBindingFunctionFaultPtr binding;
  948. binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  949. memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
  950. tmp = get_attribute(trav->properties, "use");
  951. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  952. binding->use = SOAP_ENCODED;
  953. } else {
  954. binding->use = SOAP_LITERAL;
  955. }
  956. tmp = get_attribute(trav->properties, "namespace");
  957. if (tmp) {
  958. binding->ns = estrdup((char*)tmp->children->content);
  959. }
  960. if (binding->use == SOAP_ENCODED) {
  961. tmp = get_attribute(trav->properties, "encodingStyle");
  962. if (tmp) {
  963. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  964. binding->encodingStyle = SOAP_ENCODING_1_1;
  965. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  966. binding->encodingStyle = SOAP_ENCODING_1_2;
  967. } else {
  968. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  969. }
  970. } else {
  971. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  972. }
  973. }
  974. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  975. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  976. }
  977. trav = trav->next;
  978. }
  979. }
  980. }
  981. if (function->faults == NULL) {
  982. function->faults = emalloc(sizeof(HashTable));
  983. zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
  984. }
  985. if (zend_hash_str_add_ptr(function->faults, f->name, strlen(f->name), f) == NULL) {
  986. soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
  987. }
  988. }
  989. fault = fault->next;
  990. }
  991. function->binding = tmpbinding;
  992. {
  993. char *tmp = estrdup(function->functionName);
  994. int len = strlen(tmp);
  995. zend_str_tolower(tmp, len);
  996. if (zend_hash_str_add_ptr(&ctx.sdl->functions, tmp, len, function) == NULL) {
  997. zend_hash_next_index_insert_ptr(&ctx.sdl->functions, function);
  998. }
  999. efree(tmp);
  1000. if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
  1001. if (ctx.sdl->requests == NULL) {
  1002. ctx.sdl->requests = emalloc(sizeof(HashTable));
  1003. zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
  1004. }
  1005. tmp = estrdup(function->requestName);
  1006. len = strlen(tmp);
  1007. zend_str_tolower(tmp, len);
  1008. zend_hash_str_add_ptr(ctx.sdl->requests, tmp, len, function);
  1009. efree(tmp);
  1010. }
  1011. }
  1012. trav2 = trav2->next;
  1013. }
  1014. if (!ctx.sdl->bindings) {
  1015. ctx.sdl->bindings = emalloc(sizeof(HashTable));
  1016. zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
  1017. }
  1018. if (!zend_hash_str_add_ptr(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), tmpbinding)) {
  1019. zend_hash_next_index_insert_ptr(ctx.sdl->bindings, tmpbinding);
  1020. }
  1021. trav= trav->next;
  1022. }
  1023. zend_hash_move_forward(&ctx.services);
  1024. }
  1025. } else {
  1026. soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
  1027. }
  1028. if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
  1029. soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
  1030. }
  1031. } zend_catch {
  1032. /* Avoid persistent memory leak. */
  1033. zend_hash_destroy(&ctx.docs);
  1034. zend_bailout();
  1035. } zend_end_try();
  1036. zend_hash_destroy(&ctx.messages);
  1037. zend_hash_destroy(&ctx.bindings);
  1038. zend_hash_destroy(&ctx.portTypes);
  1039. zend_hash_destroy(&ctx.services);
  1040. zend_hash_destroy(&ctx.docs);
  1041. return ctx.sdl;
  1042. }
  1043. #define WSDL_CACHE_VERSION 0x10
  1044. #define WSDL_CACHE_GET(ret,type,buf) memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
  1045. #define WSDL_CACHE_GET_INT(ret,buf) ret = ((unsigned char)(*buf)[0])|((unsigned char)(*buf)[1]<<8)|((unsigned char)(*buf)[2]<<16)|((unsigned)(*buf)[3]<<24); *buf += 4;
  1046. #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
  1047. #define WSDL_CACHE_GET_N(ret,n,buf) memcpy(ret,*buf,n); *buf += n;
  1048. #define WSDL_CACHE_SKIP(n,buf) *buf += n;
  1049. #define WSDL_CACHE_PUT_INT(val,buf) smart_str_appendc(buf,(char)(val & 0xff)); \
  1050. smart_str_appendc(buf,(char)((val >> 8) & 0xff)); \
  1051. smart_str_appendc(buf,(char)((val >> 16) & 0xff)); \
  1052. smart_str_appendc(buf,(char)((val >> 24) & 0xff));
  1053. #define WSDL_CACHE_PUT_1(val,buf) smart_str_appendc(buf,val);
  1054. #define WSDL_CACHE_PUT_N(val,n,buf) smart_str_appendl(buf,(char*)val,n);
  1055. #define WSDL_NO_STRING_MARKER 0x7fffffff
  1056. static char* sdl_deserialize_string(char **in)
  1057. {
  1058. char *s;
  1059. int len;
  1060. WSDL_CACHE_GET_INT(len, in);
  1061. if (len == WSDL_NO_STRING_MARKER) {
  1062. return NULL;
  1063. } else {
  1064. s = emalloc(len+1);
  1065. WSDL_CACHE_GET_N(s, len, in);
  1066. s[len] = '\0';
  1067. return s;
  1068. }
  1069. }
  1070. static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
  1071. {
  1072. int len;
  1073. WSDL_CACHE_GET_INT(len, in);
  1074. if (len == WSDL_NO_STRING_MARKER) {
  1075. zend_hash_next_index_insert_ptr(ht, data);
  1076. } else {
  1077. zend_hash_str_add_ptr(ht, *in, len, data);
  1078. WSDL_CACHE_SKIP(len, in);
  1079. }
  1080. }
  1081. static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
  1082. {
  1083. int i;
  1084. attr->name = sdl_deserialize_string(in);
  1085. attr->namens = sdl_deserialize_string(in);
  1086. attr->ref = sdl_deserialize_string(in);
  1087. attr->def = sdl_deserialize_string(in);
  1088. attr->fixed = sdl_deserialize_string(in);
  1089. WSDL_CACHE_GET_1(attr->form, sdlForm, in);
  1090. WSDL_CACHE_GET_1(attr->use, sdlUse, in);
  1091. WSDL_CACHE_GET_INT(i, in);
  1092. attr->encode = encoders[i];
  1093. WSDL_CACHE_GET_INT(i, in);
  1094. if (i > 0) {
  1095. attr->extraAttributes = emalloc(sizeof(HashTable));
  1096. zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
  1097. while (i > 0) {
  1098. sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
  1099. sdl_deserialize_key(attr->extraAttributes, x, in);
  1100. x->ns = sdl_deserialize_string(in);
  1101. x->val = sdl_deserialize_string(in);
  1102. --i;
  1103. }
  1104. }
  1105. }
  1106. static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
  1107. {
  1108. if (**in == 1) {
  1109. sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
  1110. WSDL_CACHE_SKIP(1, in);
  1111. WSDL_CACHE_GET_INT(x->value, in);
  1112. WSDL_CACHE_GET_1(x->fixed, char, in);
  1113. return x;
  1114. } else {
  1115. WSDL_CACHE_SKIP(1, in);
  1116. return NULL;
  1117. }
  1118. }
  1119. static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
  1120. {
  1121. if (**in == 1) {
  1122. sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
  1123. WSDL_CACHE_SKIP(1, in);
  1124. x->value = sdl_deserialize_string(in);
  1125. WSDL_CACHE_GET_1(x->fixed, char, in);
  1126. return x;
  1127. } else {
  1128. WSDL_CACHE_SKIP(1, in);
  1129. return NULL;
  1130. }
  1131. }
  1132. static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
  1133. {
  1134. int i;
  1135. sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
  1136. WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
  1137. WSDL_CACHE_GET_INT(model->min_occurs, in);
  1138. WSDL_CACHE_GET_INT(model->max_occurs, in);
  1139. switch (model->kind) {
  1140. case XSD_CONTENT_ELEMENT:
  1141. WSDL_CACHE_GET_INT(i, in);
  1142. model->u.element = elements[i];
  1143. break;
  1144. case XSD_CONTENT_SEQUENCE:
  1145. case XSD_CONTENT_ALL:
  1146. case XSD_CONTENT_CHOICE:
  1147. WSDL_CACHE_GET_INT(i, in);
  1148. model->u.content = emalloc(sizeof(HashTable));
  1149. zend_hash_init(model->u.content, i, NULL, delete_model, 0);
  1150. while (i > 0) {
  1151. sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
  1152. zend_hash_next_index_insert_ptr(model->u.content, x);
  1153. i--;
  1154. }
  1155. break;
  1156. case XSD_CONTENT_GROUP_REF:
  1157. model->u.group_ref = sdl_deserialize_string(in);
  1158. break;
  1159. case XSD_CONTENT_GROUP:
  1160. WSDL_CACHE_GET_INT(i, in);
  1161. model->u.group = types[i];
  1162. break;
  1163. default:
  1164. break;
  1165. }
  1166. return model;
  1167. }
  1168. static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
  1169. {
  1170. int i;
  1171. sdlTypePtr *elements = NULL;
  1172. WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
  1173. type->name = sdl_deserialize_string(in);
  1174. type->namens = sdl_deserialize_string(in);
  1175. type->def = sdl_deserialize_string(in);
  1176. type->fixed = sdl_deserialize_string(in);
  1177. type->ref = sdl_deserialize_string(in);
  1178. WSDL_CACHE_GET_1(type->nillable, char, in);
  1179. WSDL_CACHE_GET_1(type->form, sdlForm, in);
  1180. WSDL_CACHE_GET_INT(i, in);
  1181. type->encode = encoders[i];
  1182. if (**in == 1) {
  1183. WSDL_CACHE_SKIP(1, in);
  1184. type->restrictions = emalloc(sizeof(sdlRestrictions));
  1185. /*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
  1186. type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
  1187. type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
  1188. type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
  1189. type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
  1190. type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
  1191. type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
  1192. type->restrictions->length = sdl_deserialize_resriction_int(in);
  1193. type->restrictions->minLength = sdl_deserialize_resriction_int(in);
  1194. type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
  1195. type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
  1196. type->restrictions->pattern = sdl_deserialize_resriction_char(in);
  1197. WSDL_CACHE_GET_INT(i, in);
  1198. if (i > 0) {
  1199. type->restrictions->enumeration = emalloc(sizeof(HashTable));
  1200. zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
  1201. while (i > 0) {
  1202. sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
  1203. sdl_deserialize_key(type->restrictions->enumeration, x, in);
  1204. --i;
  1205. }
  1206. } else {
  1207. type->restrictions->enumeration = NULL;
  1208. }
  1209. } else {
  1210. WSDL_CACHE_SKIP(1, in);
  1211. }
  1212. WSDL_CACHE_GET_INT(i, in);
  1213. if (i > 0) {
  1214. elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1215. elements[0] = NULL;
  1216. type->elements = emalloc(sizeof(HashTable));
  1217. zend_hash_init(type->elements, i, NULL, delete_type, 0);
  1218. while (i > 0) {
  1219. sdlTypePtr t = emalloc(sizeof(sdlType));
  1220. memset(t, 0, sizeof(sdlType));
  1221. sdl_deserialize_key(type->elements, t, in);
  1222. sdl_deserialize_type(t, types, encoders, in);
  1223. elements[i] = t;
  1224. --i;
  1225. }
  1226. }
  1227. WSDL_CACHE_GET_INT(i, in);
  1228. if (i > 0) {
  1229. type->attributes = emalloc(sizeof(HashTable));
  1230. zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
  1231. while (i > 0) {
  1232. sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
  1233. memset(attr, 0, sizeof(sdlAttribute));
  1234. sdl_deserialize_key(type->attributes, attr, in);
  1235. sdl_deserialize_attribute(attr, encoders, in);
  1236. --i;
  1237. }
  1238. }
  1239. if (**in != 0) {
  1240. WSDL_CACHE_SKIP(1, in);
  1241. type->model = sdl_deserialize_model(types, elements, in);
  1242. } else {
  1243. WSDL_CACHE_SKIP(1, in);
  1244. }
  1245. if (elements != NULL) {
  1246. efree(elements);
  1247. }
  1248. }
  1249. static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
  1250. {
  1251. int i;
  1252. WSDL_CACHE_GET_INT(enc->details.type, in);
  1253. enc->details.type_str = sdl_deserialize_string(in);
  1254. enc->details.ns = sdl_deserialize_string(in);
  1255. WSDL_CACHE_GET_INT(i, in);
  1256. enc->details.sdl_type = types[i];
  1257. enc->to_xml = sdl_guess_convert_xml;
  1258. enc->to_zval = sdl_guess_convert_zval;
  1259. if (enc->details.sdl_type == NULL) {
  1260. int ns_len = strlen(enc->details.ns);
  1261. int type_len = strlen(enc->details.type_str);
  1262. if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  1263. memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  1264. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  1265. memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  1266. char *enc_nscat;
  1267. int enc_ns_len;
  1268. int enc_len;
  1269. encodePtr real_enc;
  1270. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  1271. enc_len = enc_ns_len + type_len + 1;
  1272. enc_nscat = emalloc(enc_len + 1);
  1273. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  1274. enc_nscat[enc_ns_len] = ':';
  1275. memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
  1276. enc_nscat[enc_len] = '\0';
  1277. real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  1278. efree(enc_nscat);
  1279. if (real_enc) {
  1280. enc->to_zval = real_enc->to_zval;
  1281. enc->to_xml = real_enc->to_xml;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
  1287. {
  1288. int i, j, n;
  1289. WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
  1290. if (body->use == SOAP_ENCODED) {
  1291. WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
  1292. } else {
  1293. body->encodingStyle = SOAP_ENCODING_DEFAULT;
  1294. }
  1295. body->ns = sdl_deserialize_string(in);
  1296. WSDL_CACHE_GET_INT(i, in);
  1297. if (i > 0) {
  1298. body->headers = emalloc(sizeof(HashTable));
  1299. zend_hash_init(body->headers, i, NULL, delete_header, 0);
  1300. while (i > 0) {
  1301. sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1302. memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
  1303. sdl_deserialize_key(body->headers, tmp, in);
  1304. WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
  1305. if (tmp->use == SOAP_ENCODED) {
  1306. WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
  1307. } else {
  1308. tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
  1309. }
  1310. tmp->name = sdl_deserialize_string(in);
  1311. tmp->ns = sdl_deserialize_string(in);
  1312. WSDL_CACHE_GET_INT(n, in);
  1313. tmp->encode = encoders[n];
  1314. WSDL_CACHE_GET_INT(n, in);
  1315. tmp->element = types[n];
  1316. --i;
  1317. WSDL_CACHE_GET_INT(j, in);
  1318. if (j > 0) {
  1319. tmp->headerfaults = emalloc(sizeof(HashTable));
  1320. zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
  1321. while (j > 0) {
  1322. sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1323. memset(tmp2, 0, sizeof(sdlSoapBindingFunctionHeader));
  1324. sdl_deserialize_key(tmp->headerfaults, tmp2, in);
  1325. WSDL_CACHE_GET_1(tmp2->use, sdlEncodingUse, in);
  1326. if (tmp2->use == SOAP_ENCODED) {
  1327. WSDL_CACHE_GET_1(tmp2->encodingStyle, sdlRpcEncodingStyle, in);
  1328. } else {
  1329. tmp2->encodingStyle = SOAP_ENCODING_DEFAULT;
  1330. }
  1331. tmp2->name = sdl_deserialize_string(in);
  1332. tmp2->ns = sdl_deserialize_string(in);
  1333. WSDL_CACHE_GET_INT(n, in);
  1334. tmp2->encode = encoders[n];
  1335. WSDL_CACHE_GET_INT(n, in);
  1336. tmp2->element = types[n];
  1337. --j;
  1338. }
  1339. }
  1340. }
  1341. }
  1342. }
  1343. static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *types, char **in)
  1344. {
  1345. int i, n;
  1346. HashTable *ht;
  1347. WSDL_CACHE_GET_INT(i, in);
  1348. if (i == 0) {return NULL;}
  1349. ht = emalloc(sizeof(HashTable));
  1350. zend_hash_init(ht, i, NULL, delete_parameter, 0);
  1351. while (i > 0) {
  1352. sdlParamPtr param = emalloc(sizeof(sdlParam));
  1353. sdl_deserialize_key(ht, param, in);
  1354. param->paramName = sdl_deserialize_string(in);
  1355. WSDL_CACHE_GET_INT(param->order, in);
  1356. WSDL_CACHE_GET_INT(n, in);
  1357. param->encode = encoders[n];
  1358. WSDL_CACHE_GET_INT(n, in);
  1359. param->element = types[n];
  1360. --i;
  1361. }
  1362. return ht;
  1363. }
  1364. static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time_t *cached)
  1365. {
  1366. sdlPtr sdl;
  1367. time_t old_t;
  1368. int i, num_groups, num_types, num_elements, num_encoders, num_bindings, num_func;
  1369. sdlFunctionPtr *functions = NULL;
  1370. sdlBindingPtr *bindings;
  1371. sdlTypePtr *types;
  1372. encodePtr *encoders;
  1373. const encode *enc;
  1374. int f;
  1375. struct stat st;
  1376. char *in, *buf;
  1377. f = open(fn, O_RDONLY|O_BINARY);
  1378. if (f < 0) {
  1379. return NULL;
  1380. }
  1381. if (fstat(f, &st) != 0) {
  1382. close(f);
  1383. return NULL;
  1384. }
  1385. buf = in = emalloc(st.st_size);
  1386. if (read(f, in, st.st_size) != st.st_size) {
  1387. close(f);
  1388. efree(in);
  1389. return NULL;
  1390. }
  1391. close(f);
  1392. if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
  1393. unlink(fn);
  1394. efree(buf);
  1395. return NULL;
  1396. }
  1397. in += 6;
  1398. WSDL_CACHE_GET(old_t, time_t, &in);
  1399. if (old_t < t) {
  1400. unlink(fn);
  1401. efree(buf);
  1402. return NULL;
  1403. }
  1404. *cached = old_t;
  1405. WSDL_CACHE_GET_INT(i, &in);
  1406. if (i == 0 && strncmp(in, uri, i) != 0) {
  1407. unlink(fn);
  1408. efree(buf);
  1409. return NULL;
  1410. }
  1411. WSDL_CACHE_SKIP(i, &in);
  1412. sdl = emalloc(sizeof(*sdl));
  1413. memset(sdl, 0, sizeof(*sdl));
  1414. sdl->source = sdl_deserialize_string(&in);
  1415. sdl->target_ns = sdl_deserialize_string(&in);
  1416. WSDL_CACHE_GET_INT(num_groups, &in);
  1417. WSDL_CACHE_GET_INT(num_types, &in);
  1418. WSDL_CACHE_GET_INT(num_elements, &in);
  1419. WSDL_CACHE_GET_INT(num_encoders, &in);
  1420. i = num_groups+num_types+num_elements;
  1421. types = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1422. types[0] = NULL;
  1423. while (i > 0) {
  1424. types[i] = emalloc(sizeof(sdlType));
  1425. memset(types[i], 0, sizeof(sdlType));
  1426. i--;
  1427. }
  1428. i = num_encoders;
  1429. enc = defaultEncoding;
  1430. while (enc->details.type != END_KNOWN_TYPES) {
  1431. i++; enc++;
  1432. }
  1433. encoders = safe_emalloc((i+1), sizeof(encodePtr), 0);
  1434. i = num_encoders;
  1435. encoders[0] = NULL;
  1436. while (i > 0) {
  1437. encoders[i] = emalloc(sizeof(encode));
  1438. memset(encoders[i], 0, sizeof(encode));
  1439. i--;
  1440. }
  1441. i = num_encoders;
  1442. enc = defaultEncoding;
  1443. while (enc->details.type != END_KNOWN_TYPES) {
  1444. encoders[++i] = (encodePtr)enc++;
  1445. }
  1446. i = 1;
  1447. if (num_groups > 0) {
  1448. sdl->groups = emalloc(sizeof(HashTable));
  1449. zend_hash_init(sdl->groups, num_groups, NULL, delete_type, 0);
  1450. while (i < num_groups+1) {
  1451. sdl_deserialize_key(sdl->groups, types[i], &in);
  1452. sdl_deserialize_type(types[i], types, encoders, &in);
  1453. i++;
  1454. }
  1455. }
  1456. if (num_types > 0) {
  1457. sdl->types = emalloc(sizeof(HashTable));
  1458. zend_hash_init(sdl->types, num_types, NULL, delete_type, 0);
  1459. while (i < num_groups+num_types+1) {
  1460. sdl_deserialize_key(sdl->types, types[i], &in);
  1461. sdl_deserialize_type(types[i], types, encoders, &in);
  1462. i++;
  1463. }
  1464. }
  1465. if (num_elements > 0) {
  1466. sdl->elements = emalloc(sizeof(HashTable));
  1467. zend_hash_init(sdl->elements, num_elements, NULL, delete_type, 0);
  1468. while (i < num_groups+num_types+num_elements+1) {
  1469. sdl_deserialize_key(sdl->elements, types[i], &in);
  1470. sdl_deserialize_type(types[i], types, encoders, &in);
  1471. i++;
  1472. }
  1473. }
  1474. i = 1;
  1475. if (num_encoders > 0) {
  1476. sdl->encoders = emalloc(sizeof(HashTable));
  1477. zend_hash_init(sdl->encoders, num_encoders, NULL, delete_encoder, 0);
  1478. while (i < num_encoders+1) {
  1479. sdl_deserialize_key(sdl->encoders, encoders[i], &in);
  1480. sdl_deserialize_encoder(encoders[i], types, &in);
  1481. i++;
  1482. }
  1483. }
  1484. /* deserialize bindings */
  1485. WSDL_CACHE_GET_INT(num_bindings, &in);
  1486. bindings = safe_emalloc(num_bindings, sizeof(sdlBindingPtr), 0);
  1487. if (num_bindings > 0) {
  1488. sdl->bindings = emalloc(sizeof(HashTable));
  1489. zend_hash_init(sdl->bindings, num_bindings, NULL, delete_binding, 0);
  1490. for (i = 0; i < num_bindings; i++) {
  1491. sdlBindingPtr binding = emalloc(sizeof(sdlBinding));
  1492. memset(binding, 0, sizeof(sdlBinding));
  1493. sdl_deserialize_key(sdl->bindings, binding, &in);
  1494. binding->name = sdl_deserialize_string(&in);
  1495. binding->location = sdl_deserialize_string(&in);
  1496. WSDL_CACHE_GET_1(binding->bindingType,sdlBindingType,&in);
  1497. if (binding->bindingType == BINDING_SOAP && *in != 0) {
  1498. sdlSoapBindingPtr soap_binding = binding->bindingAttributes = emalloc(sizeof(sdlSoapBinding));
  1499. WSDL_CACHE_GET_1(soap_binding->style,sdlEncodingStyle,&in);
  1500. WSDL_CACHE_GET_1(soap_binding->transport,sdlTransport,&in);
  1501. } else {
  1502. WSDL_CACHE_SKIP(1,&in);
  1503. }
  1504. bindings[i] = binding;
  1505. }
  1506. }
  1507. /* deserialize functions */
  1508. WSDL_CACHE_GET_INT(num_func, &in);
  1509. zend_hash_init(&sdl->functions, num_func, NULL, delete_function, 0);
  1510. if (num_func > 0) {
  1511. functions = safe_emalloc(num_func, sizeof(sdlFunctionPtr), 0);
  1512. for (i = 0; i < num_func; i++) {
  1513. int binding_num, num_faults;
  1514. sdlFunctionPtr func = emalloc(sizeof(sdlFunction));
  1515. sdl_deserialize_key(&sdl->functions, func, &in);
  1516. func->functionName = sdl_deserialize_string(&in);
  1517. func->requestName = sdl_deserialize_string(&in);
  1518. func->responseName = sdl_deserialize_string(&in);
  1519. WSDL_CACHE_GET_INT(binding_num, &in);
  1520. if (binding_num == 0) {
  1521. func->binding = NULL;
  1522. } else {
  1523. func->binding = bindings[binding_num-1];
  1524. }
  1525. if (func->binding && func->binding->bindingType == BINDING_SOAP && *in != 0) {
  1526. sdlSoapBindingFunctionPtr binding = func->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunction));
  1527. memset(binding, 0, sizeof(sdlSoapBindingFunction));
  1528. WSDL_CACHE_GET_1(binding->style,sdlEncodingStyle,&in);
  1529. binding->soapAction = sdl_deserialize_string(&in);
  1530. sdl_deserialize_soap_body(&binding->input, encoders, types, &in);
  1531. sdl_deserialize_soap_body(&binding->output, encoders, types, &in);
  1532. } else {
  1533. WSDL_CACHE_SKIP(1, &in);
  1534. func->bindingAttributes = NULL;
  1535. }
  1536. func->requestParameters = sdl_deserialize_parameters(encoders, types, &in);
  1537. func->responseParameters = sdl_deserialize_parameters(encoders, types, &in);
  1538. WSDL_CACHE_GET_INT(num_faults, &in);
  1539. if (num_faults > 0) {
  1540. int j;
  1541. func->faults = emalloc(sizeof(HashTable));
  1542. zend_hash_init(func->faults, num_faults, NULL, delete_fault, 0);
  1543. for (j = 0; j < num_faults; j++) {
  1544. sdlFaultPtr fault = emalloc(sizeof(sdlFault));
  1545. sdl_deserialize_key(func->faults, fault, &in);
  1546. fault->name =sdl_deserialize_string(&in);
  1547. fault->details =sdl_deserialize_parameters(encoders, types, &in);
  1548. if (*in != 0) {
  1549. sdlSoapBindingFunctionFaultPtr binding = fault->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  1550. memset(binding, 0, sizeof(sdlSoapBindingFunctionFault));
  1551. WSDL_CACHE_GET_1(binding->use,sdlEncodingUse,&in);
  1552. if (binding->use == SOAP_ENCODED) {
  1553. WSDL_CACHE_GET_1(binding->encodingStyle, sdlRpcEncodingStyle, &in);
  1554. } else {
  1555. binding->encodingStyle = SOAP_ENCODING_DEFAULT;
  1556. }
  1557. binding->ns = sdl_deserialize_string(&in);
  1558. } else {
  1559. WSDL_CACHE_SKIP(1, &in);
  1560. fault->bindingAttributes = NULL;
  1561. }
  1562. }
  1563. } else {
  1564. func->faults = NULL;
  1565. }
  1566. functions[i] = func;
  1567. }
  1568. }
  1569. /* deserialize requests */
  1570. WSDL_CACHE_GET_INT(i, &in);
  1571. if (i > 0) {
  1572. sdl->requests = emalloc(sizeof(HashTable));
  1573. zend_hash_init(sdl->requests, i, NULL, NULL, 0);
  1574. while (i > 0) {
  1575. int function_num;
  1576. WSDL_CACHE_GET_INT(function_num, &in);
  1577. sdl_deserialize_key(sdl->requests, functions[function_num-1], &in);
  1578. i--;
  1579. }
  1580. }
  1581. if (functions) {
  1582. efree(functions);
  1583. }
  1584. efree(bindings);
  1585. efree(encoders);
  1586. efree(types);
  1587. efree(buf);
  1588. return sdl;
  1589. }
  1590. static void sdl_serialize_string(const char *str, smart_str *out)
  1591. {
  1592. if (str) {
  1593. int i = strlen(str);
  1594. WSDL_CACHE_PUT_INT(i, out);
  1595. if (i > 0) {
  1596. WSDL_CACHE_PUT_N(str, i, out);
  1597. }
  1598. } else {
  1599. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1600. }
  1601. }
  1602. // TODO: refactor it
  1603. static void sdl_serialize_key(zend_string *key, smart_str *out)
  1604. {
  1605. if (key) {
  1606. WSDL_CACHE_PUT_INT(ZSTR_LEN(key), out);
  1607. WSDL_CACHE_PUT_N(ZSTR_VAL(key), ZSTR_LEN(key), out);
  1608. } else {
  1609. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1610. }
  1611. }
  1612. static void sdl_serialize_encoder_ref(encodePtr enc, HashTable *tmp_encoders, smart_str *out) {
  1613. if (enc) {
  1614. zval *encoder_num;
  1615. if ((encoder_num = zend_hash_str_find(tmp_encoders, (char*)&enc, sizeof(enc))) != 0) {
  1616. WSDL_CACHE_PUT_INT(Z_LVAL_P(encoder_num), out);
  1617. } else {
  1618. WSDL_CACHE_PUT_INT(0, out);
  1619. }
  1620. } else {
  1621. WSDL_CACHE_PUT_INT(0, out);
  1622. }
  1623. }
  1624. static void sdl_serialize_type_ref(sdlTypePtr type, HashTable *tmp_types, smart_str *out) {
  1625. if (type) {
  1626. zval *type_num;
  1627. if ((type_num = zend_hash_str_find(tmp_types, (char*)&type, sizeof(type))) != NULL) {
  1628. WSDL_CACHE_PUT_INT(Z_LVAL_P(type_num), out);
  1629. } else {
  1630. WSDL_CACHE_PUT_INT(0, out);
  1631. }
  1632. } else {
  1633. WSDL_CACHE_PUT_INT(0,out);
  1634. }
  1635. }
  1636. static void sdl_serialize_attribute(sdlAttributePtr attr, HashTable *tmp_encoders, smart_str *out)
  1637. {
  1638. int i;
  1639. sdl_serialize_string(attr->name, out);
  1640. sdl_serialize_string(attr->namens, out);
  1641. sdl_serialize_string(attr->ref, out);
  1642. sdl_serialize_string(attr->def, out);
  1643. sdl_serialize_string(attr->fixed, out);
  1644. WSDL_CACHE_PUT_1(attr->form, out);
  1645. WSDL_CACHE_PUT_1(attr->use, out);
  1646. sdl_serialize_encoder_ref(attr->encode, tmp_encoders, out);
  1647. if (attr->extraAttributes) {
  1648. i = zend_hash_num_elements(attr->extraAttributes);
  1649. } else {
  1650. i = 0;
  1651. }
  1652. WSDL_CACHE_PUT_INT(i, out);
  1653. if (i > 0) {
  1654. sdlExtraAttributePtr tmp;
  1655. zend_string *key;
  1656. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  1657. sdl_serialize_key(key, out);
  1658. sdl_serialize_string(tmp->ns, out);
  1659. sdl_serialize_string(tmp->val, out);
  1660. } ZEND_HASH_FOREACH_END();
  1661. }
  1662. }
  1663. static void sdl_serialize_model(sdlContentModelPtr model, HashTable *tmp_types, HashTable *tmp_elements, smart_str *out)
  1664. {
  1665. WSDL_CACHE_PUT_1(model->kind, out);
  1666. WSDL_CACHE_PUT_INT(model->min_occurs, out);
  1667. WSDL_CACHE_PUT_INT(model->max_occurs, out);
  1668. switch (model->kind) {
  1669. case XSD_CONTENT_ELEMENT:
  1670. sdl_serialize_type_ref(model->u.element, tmp_elements, out);
  1671. break;
  1672. case XSD_CONTENT_SEQUENCE:
  1673. case XSD_CONTENT_ALL:
  1674. case XSD_CONTENT_CHOICE: {
  1675. sdlContentModelPtr tmp;
  1676. int i = zend_hash_num_elements(model->u.content);
  1677. WSDL_CACHE_PUT_INT(i, out);
  1678. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  1679. sdl_serialize_model(tmp, tmp_types, tmp_elements, out);
  1680. } ZEND_HASH_FOREACH_END();
  1681. }
  1682. break;
  1683. case XSD_CONTENT_GROUP_REF:
  1684. sdl_serialize_string(model->u.group_ref,out);
  1685. break;
  1686. case XSD_CONTENT_GROUP:
  1687. sdl_serialize_type_ref(model->u.group, tmp_types, out);
  1688. break;
  1689. default:
  1690. break;
  1691. }
  1692. }
  1693. static void sdl_serialize_resriction_int(sdlRestrictionIntPtr x, smart_str *out)
  1694. {
  1695. if (x) {
  1696. WSDL_CACHE_PUT_1(1, out);
  1697. WSDL_CACHE_PUT_INT(x->value, out);
  1698. WSDL_CACHE_PUT_1(x->fixed, out);
  1699. } else {
  1700. WSDL_CACHE_PUT_1(0, out);
  1701. }
  1702. }
  1703. static void sdl_serialize_resriction_char(sdlRestrictionCharPtr x, smart_str *out)
  1704. {
  1705. if (x) {
  1706. WSDL_CACHE_PUT_1(1, out);
  1707. sdl_serialize_string(x->value, out);
  1708. WSDL_CACHE_PUT_1(x->fixed, out);
  1709. } else {
  1710. WSDL_CACHE_PUT_1(0, out);
  1711. }
  1712. }
  1713. static void sdl_serialize_type(sdlTypePtr type, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1714. {
  1715. int i;
  1716. HashTable *tmp_elements = NULL;
  1717. WSDL_CACHE_PUT_1(type->kind, out);
  1718. sdl_serialize_string(type->name, out);
  1719. sdl_serialize_string(type->namens, out);
  1720. sdl_serialize_string(type->def, out);
  1721. sdl_serialize_string(type->fixed, out);
  1722. sdl_serialize_string(type->ref, out);
  1723. WSDL_CACHE_PUT_1(type->nillable, out);
  1724. WSDL_CACHE_PUT_1(type->form, out);
  1725. sdl_serialize_encoder_ref(type->encode, tmp_encoders, out);
  1726. if (type->restrictions) {
  1727. WSDL_CACHE_PUT_1(1, out);
  1728. sdl_serialize_resriction_int(type->restrictions->minExclusive,out);
  1729. sdl_serialize_resriction_int(type->restrictions->minInclusive,out);
  1730. sdl_serialize_resriction_int(type->restrictions->maxExclusive,out);
  1731. sdl_serialize_resriction_int(type->restrictions->maxInclusive,out);
  1732. sdl_serialize_resriction_int(type->restrictions->totalDigits,out);
  1733. sdl_serialize_resriction_int(type->restrictions->fractionDigits,out);
  1734. sdl_serialize_resriction_int(type->restrictions->length,out);
  1735. sdl_serialize_resriction_int(type->restrictions->minLength,out);
  1736. sdl_serialize_resriction_int(type->restrictions->maxLength,out);
  1737. sdl_serialize_resriction_char(type->restrictions->whiteSpace,out);
  1738. sdl_serialize_resriction_char(type->restrictions->pattern,out);
  1739. if (type->restrictions->enumeration) {
  1740. i = zend_hash_num_elements(type->restrictions->enumeration);
  1741. } else {
  1742. i = 0;
  1743. }
  1744. WSDL_CACHE_PUT_INT(i, out);
  1745. if (i > 0) {
  1746. sdlRestrictionCharPtr tmp;
  1747. zend_string *key;
  1748. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  1749. sdl_serialize_resriction_char(tmp, out);
  1750. sdl_serialize_key(key, out);
  1751. } ZEND_HASH_FOREACH_END();
  1752. }
  1753. } else {
  1754. WSDL_CACHE_PUT_1(0, out);
  1755. }
  1756. if (type->elements) {
  1757. i = zend_hash_num_elements(type->elements);
  1758. } else {
  1759. i = 0;
  1760. }
  1761. WSDL_CACHE_PUT_INT(i, out);
  1762. if (i > 0) {
  1763. sdlTypePtr tmp;
  1764. zend_string *key;
  1765. zval zv;
  1766. tmp_elements = emalloc(sizeof(HashTable));
  1767. zend_hash_init(tmp_elements, i, NULL, NULL, 0);
  1768. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  1769. sdl_serialize_key(key, out);
  1770. sdl_serialize_type(tmp, tmp_encoders, tmp_types, out);
  1771. ZVAL_LONG(&zv, i);
  1772. zend_hash_str_add(tmp_elements, (char*)&tmp, sizeof(tmp), &zv);
  1773. i--;
  1774. } ZEND_HASH_FOREACH_END();
  1775. }
  1776. if (type->attributes) {
  1777. i = zend_hash_num_elements(type->attributes);
  1778. } else {
  1779. i = 0;
  1780. }
  1781. WSDL_CACHE_PUT_INT(i, out);
  1782. if (i > 0) {
  1783. sdlAttributePtr tmp;
  1784. zend_string *key;
  1785. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  1786. sdl_serialize_key(key, out);
  1787. sdl_serialize_attribute(tmp, tmp_encoders, out);
  1788. } ZEND_HASH_FOREACH_END();
  1789. }
  1790. if (type->model) {
  1791. WSDL_CACHE_PUT_1(1, out);
  1792. sdl_serialize_model(type->model, tmp_types, tmp_elements, out);
  1793. } else {
  1794. WSDL_CACHE_PUT_1(0, out);
  1795. }
  1796. if (tmp_elements != NULL) {
  1797. zend_hash_destroy(tmp_elements);
  1798. efree(tmp_elements);
  1799. }
  1800. }
  1801. static void sdl_serialize_encoder(encodePtr enc, HashTable *tmp_types, smart_str *out)
  1802. {
  1803. WSDL_CACHE_PUT_INT(enc->details.type, out);
  1804. sdl_serialize_string(enc->details.type_str, out);
  1805. sdl_serialize_string(enc->details.ns, out);
  1806. sdl_serialize_type_ref(enc->details.sdl_type, tmp_types, out);
  1807. }
  1808. static void sdl_serialize_parameters(HashTable *ht, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1809. {
  1810. int i;
  1811. if (ht) {
  1812. i = zend_hash_num_elements(ht);
  1813. } else {
  1814. i = 0;
  1815. }
  1816. WSDL_CACHE_PUT_INT(i, out);
  1817. if (i > 0) {
  1818. sdlParamPtr tmp;
  1819. zend_string *key;
  1820. ZEND_HASH_FOREACH_STR_KEY_PTR(ht, key, tmp) {
  1821. sdl_serialize_key(key, out);
  1822. sdl_serialize_string(tmp->paramName, out);
  1823. WSDL_CACHE_PUT_INT(tmp->order, out);
  1824. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1825. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1826. } ZEND_HASH_FOREACH_END();
  1827. }
  1828. }
  1829. static void sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1830. {
  1831. int i, j;
  1832. WSDL_CACHE_PUT_1(body->use, out);
  1833. if (body->use == SOAP_ENCODED) {
  1834. WSDL_CACHE_PUT_1(body->encodingStyle, out);
  1835. }
  1836. sdl_serialize_string(body->ns, out);
  1837. if (body->headers) {
  1838. i = zend_hash_num_elements(body->headers);
  1839. } else {
  1840. i = 0;
  1841. }
  1842. WSDL_CACHE_PUT_INT(i, out);
  1843. if (i > 0) {
  1844. sdlSoapBindingFunctionHeaderPtr tmp;
  1845. zend_string *key;
  1846. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(body->headers, key, tmp) {
  1847. sdl_serialize_key(key, out);
  1848. WSDL_CACHE_PUT_1(tmp->use, out);
  1849. if (tmp->use == SOAP_ENCODED) {
  1850. WSDL_CACHE_PUT_1(tmp->encodingStyle, out);
  1851. }
  1852. sdl_serialize_string(tmp->name, out);
  1853. sdl_serialize_string(tmp->ns, out);
  1854. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1855. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1856. if (tmp->headerfaults) {
  1857. j = zend_hash_num_elements(tmp->headerfaults);
  1858. } else {
  1859. j = 0;
  1860. }
  1861. WSDL_CACHE_PUT_INT(j, out);
  1862. if (j > 0) {
  1863. sdlSoapBindingFunctionHeaderPtr tmp2;
  1864. zend_string *key;
  1865. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(body->headers, key, tmp2) {
  1866. sdl_serialize_key(key, out);
  1867. WSDL_CACHE_PUT_1(tmp2->use, out);
  1868. if (tmp2->use == SOAP_ENCODED) {
  1869. WSDL_CACHE_PUT_1(tmp2->encodingStyle, out);
  1870. }
  1871. sdl_serialize_string(tmp2->name, out);
  1872. sdl_serialize_string(tmp2->ns, out);
  1873. sdl_serialize_encoder_ref(tmp2->encode, tmp_encoders, out);
  1874. sdl_serialize_type_ref(tmp2->element, tmp_types, out);
  1875. } ZEND_HASH_FOREACH_END();
  1876. }
  1877. } ZEND_HASH_FOREACH_END();
  1878. }
  1879. }
  1880. static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl)
  1881. {
  1882. smart_str buf = {0};
  1883. smart_str *out = &buf;
  1884. int i;
  1885. int type_num = 1;
  1886. int encoder_num = 1;
  1887. int f;
  1888. const encode *enc;
  1889. HashTable tmp_types;
  1890. HashTable tmp_encoders;
  1891. HashTable tmp_bindings;
  1892. HashTable tmp_functions;
  1893. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1894. if (f < 0) {return;}
  1895. zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
  1896. zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
  1897. zend_hash_init(&tmp_bindings, 0, NULL, NULL, 0);
  1898. zend_hash_init(&tmp_functions, 0, NULL, NULL, 0);
  1899. WSDL_CACHE_PUT_N("wsdl", 4, out);
  1900. WSDL_CACHE_PUT_1(WSDL_CACHE_VERSION,out);
  1901. WSDL_CACHE_PUT_1(0,out);
  1902. WSDL_CACHE_PUT_N(&t, sizeof(t), out);
  1903. sdl_serialize_string(uri, out);
  1904. sdl_serialize_string(sdl->source, out);
  1905. sdl_serialize_string(sdl->target_ns, out);
  1906. if (sdl->groups) {
  1907. i = zend_hash_num_elements(sdl->groups);
  1908. } else {
  1909. i = 0;
  1910. }
  1911. WSDL_CACHE_PUT_INT(i, out);
  1912. if (i > 0) {
  1913. sdlTypePtr tmp;
  1914. zval zv;
  1915. ZEND_HASH_MAP_FOREACH_PTR(sdl->groups, tmp) {
  1916. ZVAL_LONG(&zv, type_num);
  1917. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1918. ++type_num;
  1919. } ZEND_HASH_FOREACH_END();
  1920. }
  1921. if (sdl->types) {
  1922. i = zend_hash_num_elements(sdl->types);
  1923. } else {
  1924. i = 0;
  1925. }
  1926. WSDL_CACHE_PUT_INT(i, out);
  1927. if (i > 0) {
  1928. sdlTypePtr tmp;
  1929. zval zv;
  1930. ZEND_HASH_FOREACH_PTR(sdl->types, tmp) {
  1931. ZVAL_LONG(&zv, type_num);
  1932. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1933. ++type_num;
  1934. } ZEND_HASH_FOREACH_END();
  1935. }
  1936. if (sdl->elements) {
  1937. i = zend_hash_num_elements(sdl->elements);
  1938. } else {
  1939. i = 0;
  1940. }
  1941. WSDL_CACHE_PUT_INT(i, out);
  1942. if (i > 0) {
  1943. sdlTypePtr tmp;
  1944. zval zv;
  1945. ZEND_HASH_MAP_FOREACH_PTR(sdl->elements, tmp) {
  1946. ZVAL_LONG(&zv, type_num);
  1947. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1948. ++type_num;
  1949. } ZEND_HASH_FOREACH_END();
  1950. }
  1951. if (sdl->encoders) {
  1952. i = zend_hash_num_elements(sdl->encoders);
  1953. } else {
  1954. i = 0;
  1955. }
  1956. WSDL_CACHE_PUT_INT(i, out);
  1957. if (i > 0) {
  1958. encodePtr tmp;
  1959. zval zv;
  1960. ZEND_HASH_FOREACH_PTR(sdl->encoders, tmp) {
  1961. ZVAL_LONG(&zv, encoder_num);
  1962. zend_hash_str_add(&tmp_encoders, (char*)&tmp, sizeof(tmp), &zv);
  1963. ++encoder_num;
  1964. } ZEND_HASH_FOREACH_END();
  1965. }
  1966. enc = defaultEncoding;
  1967. while (enc->details.type != END_KNOWN_TYPES) {
  1968. zval zv;
  1969. ZVAL_LONG(&zv, encoder_num);
  1970. zend_hash_str_add(&tmp_encoders, (char*)&enc, sizeof(encodePtr), &zv);
  1971. enc++;
  1972. ++encoder_num;
  1973. }
  1974. if (sdl->groups) {
  1975. sdlTypePtr tmp;
  1976. zend_string *key;
  1977. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  1978. sdl_serialize_key(key, out);
  1979. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1980. } ZEND_HASH_FOREACH_END();
  1981. }
  1982. if (sdl->types) {
  1983. sdlTypePtr tmp;
  1984. zend_string *key;
  1985. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  1986. sdl_serialize_key(key, out);
  1987. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1988. } ZEND_HASH_FOREACH_END();
  1989. }
  1990. if (sdl->elements) {
  1991. sdlTypePtr tmp;
  1992. zend_string *key;
  1993. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  1994. sdl_serialize_key(key, out);
  1995. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1996. } ZEND_HASH_FOREACH_END();
  1997. }
  1998. if (sdl->encoders) {
  1999. encodePtr tmp;
  2000. zend_string *key;
  2001. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  2002. sdl_serialize_key(key, out);
  2003. sdl_serialize_encoder(tmp, &tmp_types, out);
  2004. } ZEND_HASH_FOREACH_END();
  2005. }
  2006. /* serialize bindings */
  2007. if (sdl->bindings) {
  2008. i = zend_hash_num_elements(sdl->bindings);
  2009. } else {
  2010. i = 0;
  2011. }
  2012. WSDL_CACHE_PUT_INT(i, out);
  2013. if (i > 0) {
  2014. sdlBindingPtr tmp;
  2015. int binding_num = 1;
  2016. zval zv;
  2017. zend_string *key;
  2018. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2019. sdl_serialize_key(key, out);
  2020. sdl_serialize_string(tmp->name, out);
  2021. sdl_serialize_string(tmp->location, out);
  2022. WSDL_CACHE_PUT_1(tmp->bindingType,out);
  2023. if (tmp->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2024. sdlSoapBindingPtr binding = (sdlSoapBindingPtr)tmp->bindingAttributes;
  2025. WSDL_CACHE_PUT_1(binding->style, out);
  2026. WSDL_CACHE_PUT_1(binding->transport, out);
  2027. } else {
  2028. WSDL_CACHE_PUT_1(0,out);
  2029. }
  2030. ZVAL_LONG(&zv, binding_num);
  2031. zend_hash_str_add(&tmp_bindings, (char*)&tmp, sizeof(tmp), &zv);
  2032. binding_num++;
  2033. } ZEND_HASH_FOREACH_END();
  2034. }
  2035. /* serialize functions */
  2036. i = zend_hash_num_elements(&sdl->functions);
  2037. WSDL_CACHE_PUT_INT(i, out);
  2038. if (i > 0) {
  2039. sdlFunctionPtr tmp;
  2040. zval *binding_num, zv;
  2041. int function_num = 1;
  2042. zend_string *key;
  2043. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2044. sdl_serialize_key(key, out);
  2045. sdl_serialize_string(tmp->functionName, out);
  2046. sdl_serialize_string(tmp->requestName, out);
  2047. sdl_serialize_string(tmp->responseName, out);
  2048. if (tmp->binding) {
  2049. binding_num = zend_hash_str_find(&tmp_bindings,(char*)&tmp->binding, sizeof(tmp->binding));
  2050. if (binding_num) {
  2051. WSDL_CACHE_PUT_INT(Z_LVAL_P(binding_num), out);
  2052. if (Z_LVAL_P(binding_num) >= 0) {
  2053. if (tmp->binding->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2054. sdlSoapBindingFunctionPtr binding = (sdlSoapBindingFunctionPtr)tmp->bindingAttributes;
  2055. WSDL_CACHE_PUT_1(binding->style, out);
  2056. sdl_serialize_string(binding->soapAction, out);
  2057. sdl_serialize_soap_body(&binding->input, &tmp_encoders, &tmp_types, out);
  2058. sdl_serialize_soap_body(&binding->output, &tmp_encoders, &tmp_types, out);
  2059. } else {
  2060. WSDL_CACHE_PUT_1(0,out);
  2061. }
  2062. }
  2063. }
  2064. }
  2065. sdl_serialize_parameters(tmp->requestParameters, &tmp_encoders, &tmp_types, out);
  2066. sdl_serialize_parameters(tmp->responseParameters, &tmp_encoders, &tmp_types, out);
  2067. if (tmp->faults) {
  2068. sdlFaultPtr fault;
  2069. zend_string *key;
  2070. WSDL_CACHE_PUT_INT(zend_hash_num_elements(tmp->faults), out);
  2071. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(tmp->faults, key, fault) {
  2072. sdl_serialize_key(key, out);
  2073. sdl_serialize_string(fault->name, out);
  2074. sdl_serialize_parameters(fault->details, &tmp_encoders, &tmp_types, out);
  2075. if (tmp->binding->bindingType == BINDING_SOAP && fault->bindingAttributes != NULL) {
  2076. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  2077. WSDL_CACHE_PUT_1(binding->use, out);
  2078. if (binding->use == SOAP_ENCODED) {
  2079. WSDL_CACHE_PUT_1(binding->encodingStyle, out);
  2080. }
  2081. sdl_serialize_string(binding->ns, out);
  2082. } else {
  2083. WSDL_CACHE_PUT_1(0, out);
  2084. }
  2085. } ZEND_HASH_FOREACH_END();
  2086. } else {
  2087. WSDL_CACHE_PUT_INT(0, out);
  2088. }
  2089. ZVAL_LONG(&zv, function_num);
  2090. zend_hash_str_add(&tmp_functions, (char*)&tmp, sizeof(tmp), &zv);
  2091. function_num++;
  2092. } ZEND_HASH_FOREACH_END();
  2093. }
  2094. /* serialize requests */
  2095. if (sdl->requests) {
  2096. i = zend_hash_num_elements(sdl->requests);
  2097. } else {
  2098. i = 0;
  2099. }
  2100. WSDL_CACHE_PUT_INT(i, out);
  2101. if (i > 0) {
  2102. sdlFunctionPtr tmp;
  2103. zval *function_num;
  2104. zend_string *key;
  2105. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->requests, key, tmp) {
  2106. function_num = zend_hash_str_find(&tmp_functions, (char*)&tmp, sizeof(tmp));
  2107. WSDL_CACHE_PUT_INT(Z_LVAL_P(function_num), out);
  2108. sdl_serialize_key(key, out);
  2109. } ZEND_HASH_FOREACH_END();
  2110. }
  2111. php_ignore_value(write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)));
  2112. close(f);
  2113. smart_str_free(&buf);
  2114. zend_hash_destroy(&tmp_functions);
  2115. zend_hash_destroy(&tmp_bindings);
  2116. zend_hash_destroy(&tmp_encoders);
  2117. zend_hash_destroy(&tmp_types);
  2118. }
  2119. static void make_persistent_restriction_int(void *data)
  2120. {
  2121. sdlRestrictionIntPtr *rest = (sdlRestrictionIntPtr *)data;
  2122. sdlRestrictionIntPtr prest = NULL;
  2123. prest = malloc(sizeof(sdlRestrictionInt));
  2124. *prest = **rest;
  2125. *rest = prest;
  2126. }
  2127. static void make_persistent_restriction_char_int(sdlRestrictionCharPtr *rest)
  2128. {
  2129. sdlRestrictionCharPtr prest = NULL;
  2130. prest = malloc(sizeof(sdlRestrictionChar));
  2131. memset(prest, 0, sizeof(sdlRestrictionChar));
  2132. prest->value = strdup((*rest)->value);
  2133. prest->fixed = (*rest)->fixed;
  2134. *rest = prest;
  2135. }
  2136. static void make_persistent_sdl_type_ref(sdlTypePtr *type, HashTable *ptr_map, HashTable *bp_types)
  2137. {
  2138. sdlTypePtr tmp;
  2139. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)type, sizeof(sdlTypePtr))) != NULL) {
  2140. *type = tmp;
  2141. } else {
  2142. zend_hash_next_index_insert_ptr(bp_types, *type);
  2143. }
  2144. }
  2145. static void make_persistent_sdl_encoder_ref(encodePtr *enc, HashTable *ptr_map, HashTable *bp_encoders)
  2146. {
  2147. encodePtr tmp;
  2148. /* do not process defaultEncoding's here */
  2149. if ((*enc) >= defaultEncoding && (*enc) < defaultEncoding + numDefaultEncodings) {
  2150. return;
  2151. }
  2152. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)enc, sizeof(encodePtr))) != NULL) {
  2153. *enc = tmp;
  2154. } else {
  2155. zend_hash_next_index_insert_ptr(bp_encoders, enc);
  2156. }
  2157. }
  2158. static HashTable* make_persistent_sdl_function_headers(HashTable *headers, HashTable *ptr_map)
  2159. {
  2160. HashTable *pheaders;
  2161. sdlSoapBindingFunctionHeaderPtr tmp, pheader;
  2162. encodePtr penc;
  2163. sdlTypePtr ptype;
  2164. zend_string *key;
  2165. pheaders = malloc(sizeof(HashTable));
  2166. zend_hash_init(pheaders, zend_hash_num_elements(headers), NULL, delete_header_persistent, 1);
  2167. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(headers, key, tmp) {
  2168. pheader = malloc(sizeof(sdlSoapBindingFunctionHeader));
  2169. memset(pheader, 0, sizeof(sdlSoapBindingFunctionHeader));
  2170. *pheader = *tmp;
  2171. if (pheader->name) {
  2172. pheader->name = strdup(pheader->name);
  2173. }
  2174. if (pheader->ns) {
  2175. pheader->ns = strdup(pheader->ns);
  2176. }
  2177. if (pheader->encode && pheader->encode->details.sdl_type) {
  2178. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->encode, sizeof(encodePtr))) == NULL) {
  2179. assert(0);
  2180. }
  2181. pheader->encode = penc;
  2182. }
  2183. if (pheader->element) {
  2184. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->element, sizeof(sdlTypePtr))) == NULL) {
  2185. assert(0);
  2186. }
  2187. pheader->element = ptype;
  2188. }
  2189. if (pheader->headerfaults) {
  2190. pheader->headerfaults = make_persistent_sdl_function_headers(pheader->headerfaults, ptr_map);
  2191. }
  2192. if (key) {
  2193. /* We have to duplicate key emalloc->malloc */
  2194. zend_hash_str_add_ptr(pheaders, ZSTR_VAL(key), ZSTR_LEN(key), pheader);
  2195. } else {
  2196. zend_hash_next_index_insert_ptr(pheaders, pheader);
  2197. }
  2198. } ZEND_HASH_FOREACH_END();
  2199. return pheaders;
  2200. }
  2201. static void make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *ptr_map)
  2202. {
  2203. if (body->ns) {
  2204. body->ns = strdup(body->ns);
  2205. }
  2206. if (body->headers) {
  2207. body->headers = make_persistent_sdl_function_headers(body->headers, ptr_map);
  2208. }
  2209. }
  2210. static HashTable* make_persistent_sdl_parameters(HashTable *params, HashTable *ptr_map)
  2211. {
  2212. HashTable *pparams;
  2213. sdlParamPtr tmp, pparam;
  2214. sdlTypePtr ptype;
  2215. encodePtr penc;
  2216. zend_string *key;
  2217. pparams = malloc(sizeof(HashTable));
  2218. zend_hash_init(pparams, zend_hash_num_elements(params), NULL, delete_parameter_persistent, 1);
  2219. ZEND_HASH_FOREACH_STR_KEY_PTR(params, key, tmp) {
  2220. pparam = malloc(sizeof(sdlParam));
  2221. memset(pparam, 0, sizeof(sdlParam));
  2222. *pparam = *tmp;
  2223. if (pparam->paramName) {
  2224. pparam->paramName = strdup(pparam->paramName);
  2225. }
  2226. if (pparam->encode && pparam->encode->details.sdl_type) {
  2227. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->encode, sizeof(encodePtr))) == NULL) {
  2228. assert(0);
  2229. }
  2230. pparam->encode = penc;
  2231. }
  2232. if (pparam->element) {
  2233. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->element, sizeof(sdlTypePtr))) == NULL) {
  2234. assert(0);
  2235. }
  2236. pparam->element = ptype;
  2237. }
  2238. if (key) {
  2239. /* We have to duplicate key emalloc->malloc */
  2240. zend_hash_str_add_ptr(pparams, ZSTR_VAL(key), ZSTR_LEN(key), pparam);
  2241. } else {
  2242. zend_hash_next_index_insert_ptr(pparams, pparam);
  2243. }
  2244. } ZEND_HASH_FOREACH_END();
  2245. return pparams;
  2246. }
  2247. static HashTable* make_persistent_sdl_function_faults(sdlFunctionPtr func, HashTable *faults, HashTable *ptr_map)
  2248. {
  2249. HashTable *pfaults;
  2250. sdlFaultPtr tmp, pfault;
  2251. zend_string *key;
  2252. pfaults = malloc(sizeof(HashTable));
  2253. zend_hash_init(pfaults, zend_hash_num_elements(faults), NULL, delete_fault_persistent, 1);
  2254. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(faults, key, tmp) {
  2255. pfault = malloc(sizeof(sdlFault));
  2256. memset(pfault, 0, sizeof(sdlFault));
  2257. *pfault = *tmp;
  2258. if (pfault->name) {
  2259. pfault->name = strdup(pfault->name);
  2260. }
  2261. if (pfault->details) {
  2262. pfault->details = make_persistent_sdl_parameters(pfault->details, ptr_map);
  2263. }
  2264. if (func->binding->bindingType == BINDING_SOAP && pfault->bindingAttributes) {
  2265. sdlSoapBindingFunctionFaultPtr soap_binding;
  2266. soap_binding = malloc(sizeof(sdlSoapBindingFunctionFault));
  2267. memset(soap_binding, 0, sizeof(sdlSoapBindingFunctionFault));
  2268. *soap_binding = *(sdlSoapBindingFunctionFaultPtr)pfault->bindingAttributes;
  2269. if (soap_binding->ns) {
  2270. soap_binding->ns = strdup(soap_binding->ns);
  2271. }
  2272. pfault->bindingAttributes = soap_binding;
  2273. }
  2274. if (key) {
  2275. /* We have to duplicate key emalloc->malloc */
  2276. zend_hash_str_add_ptr(pfaults, ZSTR_VAL(key), ZSTR_LEN(key), pfault);
  2277. } else {
  2278. zend_hash_next_index_insert_ptr(pfaults, pfault);
  2279. }
  2280. } ZEND_HASH_FOREACH_END();
  2281. return pfaults;
  2282. }
  2283. static sdlAttributePtr make_persistent_sdl_attribute(sdlAttributePtr attr, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2284. {
  2285. sdlAttributePtr pattr;
  2286. zend_string *key;
  2287. pattr = malloc(sizeof(sdlAttribute));
  2288. memset(pattr, 0, sizeof(sdlAttribute));
  2289. *pattr = *attr;
  2290. if (pattr->name) {
  2291. pattr->name = strdup(pattr->name);
  2292. }
  2293. if (pattr->namens) {
  2294. pattr->namens = strdup(pattr->namens);
  2295. }
  2296. if (pattr->ref) {
  2297. pattr->ref = strdup(pattr->ref);
  2298. }
  2299. if (pattr->def) {
  2300. pattr->def = strdup(pattr->def);
  2301. }
  2302. if (pattr->fixed) {
  2303. pattr->fixed = strdup(pattr->fixed);
  2304. }
  2305. /* we do not want to process defaultEncoding's here */
  2306. if (pattr->encode) {
  2307. make_persistent_sdl_encoder_ref(&pattr->encode, ptr_map, bp_encoders);
  2308. }
  2309. if (pattr->extraAttributes) {
  2310. sdlExtraAttributePtr tmp, pextra;
  2311. pattr->extraAttributes = malloc(sizeof(HashTable));
  2312. zend_hash_init(pattr->extraAttributes, zend_hash_num_elements(attr->extraAttributes), NULL, delete_extra_attribute_persistent, 1);
  2313. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  2314. if (key) {
  2315. pextra = malloc(sizeof(sdlExtraAttribute));
  2316. memset(pextra, 0, sizeof(sdlExtraAttribute));
  2317. if (tmp->ns) {
  2318. pextra->ns = strdup(tmp->ns);
  2319. }
  2320. if (tmp->val) {
  2321. pextra->val = strdup(tmp->val);
  2322. }
  2323. /* We have to duplicate key emalloc->malloc */
  2324. zend_hash_str_add_ptr(pattr->extraAttributes, ZSTR_VAL(key), ZSTR_LEN(key), pextra);
  2325. }
  2326. } ZEND_HASH_FOREACH_END();
  2327. }
  2328. return pattr;
  2329. }
  2330. static sdlContentModelPtr make_persistent_sdl_model(sdlContentModelPtr model, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2331. {
  2332. sdlContentModelPtr pmodel;
  2333. sdlContentModelPtr tmp, pcontent;
  2334. pmodel = malloc(sizeof(sdlContentModel));
  2335. memset(pmodel, 0, sizeof(sdlContentModel));
  2336. *pmodel = *model;
  2337. switch (pmodel->kind) {
  2338. case XSD_CONTENT_ELEMENT:
  2339. if (pmodel->u.element) {
  2340. make_persistent_sdl_type_ref(&pmodel->u.element, ptr_map, bp_types);
  2341. }
  2342. break;
  2343. case XSD_CONTENT_SEQUENCE:
  2344. case XSD_CONTENT_ALL:
  2345. case XSD_CONTENT_CHOICE:
  2346. pmodel->u.content = malloc(sizeof(HashTable));
  2347. zend_hash_init(pmodel->u.content, zend_hash_num_elements(model->u.content), NULL, delete_model_persistent, 1);
  2348. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  2349. pcontent = make_persistent_sdl_model(tmp, ptr_map, bp_types, bp_encoders);
  2350. zend_hash_next_index_insert_ptr(pmodel->u.content, pcontent);
  2351. } ZEND_HASH_FOREACH_END();
  2352. break;
  2353. case XSD_CONTENT_GROUP_REF:
  2354. if (pmodel->u.group_ref) {
  2355. pmodel->u.group_ref = strdup(pmodel->u.group_ref);
  2356. }
  2357. break;
  2358. case XSD_CONTENT_GROUP:
  2359. if (pmodel->u.group) {
  2360. make_persistent_sdl_type_ref(&pmodel->u.group, ptr_map, bp_types);
  2361. }
  2362. break;
  2363. default:
  2364. break;
  2365. }
  2366. return pmodel;
  2367. }
  2368. static sdlTypePtr make_persistent_sdl_type(sdlTypePtr type, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2369. {
  2370. zend_string *key;
  2371. sdlTypePtr ptype = NULL;
  2372. ptype = malloc(sizeof(sdlType));
  2373. memset(ptype, 0, sizeof(sdlType));
  2374. *ptype = *type;
  2375. if (ptype->name) {
  2376. ptype->name = strdup(ptype->name);
  2377. }
  2378. if (ptype->namens) {
  2379. ptype->namens = strdup(ptype->namens);
  2380. }
  2381. if (ptype->def) {
  2382. ptype->def = strdup(ptype->def);
  2383. }
  2384. if (ptype->fixed) {
  2385. ptype->fixed = strdup(ptype->fixed);
  2386. }
  2387. if (ptype->ref) {
  2388. ptype->ref = strdup(ptype->ref);
  2389. }
  2390. /* we do not want to process defaultEncoding's here */
  2391. if (ptype->encode) {
  2392. make_persistent_sdl_encoder_ref(&ptype->encode, ptr_map, bp_encoders);
  2393. }
  2394. if (ptype->restrictions) {
  2395. ptype->restrictions = malloc(sizeof(sdlRestrictions));
  2396. memset(ptype->restrictions, 0, sizeof(sdlRestrictions));
  2397. *ptype->restrictions = *type->restrictions;
  2398. if (ptype->restrictions->minExclusive) {
  2399. make_persistent_restriction_int(&ptype->restrictions->minExclusive);
  2400. }
  2401. if (ptype->restrictions->maxExclusive) {
  2402. make_persistent_restriction_int(&ptype->restrictions->maxExclusive);
  2403. }
  2404. if (ptype->restrictions->minInclusive) {
  2405. make_persistent_restriction_int(&ptype->restrictions->minInclusive);
  2406. }
  2407. if (ptype->restrictions->maxInclusive) {
  2408. make_persistent_restriction_int(&ptype->restrictions->maxInclusive);
  2409. }
  2410. if (ptype->restrictions->totalDigits) {
  2411. make_persistent_restriction_int(&ptype->restrictions->totalDigits);
  2412. }
  2413. if (ptype->restrictions->fractionDigits) {
  2414. make_persistent_restriction_int(&ptype->restrictions->fractionDigits);
  2415. }
  2416. if (ptype->restrictions->length) {
  2417. make_persistent_restriction_int(&ptype->restrictions->length);
  2418. }
  2419. if (ptype->restrictions->minLength) {
  2420. make_persistent_restriction_int(&ptype->restrictions->minLength);
  2421. }
  2422. if (ptype->restrictions->maxLength) {
  2423. make_persistent_restriction_int(&ptype->restrictions->maxLength);
  2424. }
  2425. if (ptype->restrictions->whiteSpace) {
  2426. make_persistent_restriction_char_int(&ptype->restrictions->whiteSpace);
  2427. }
  2428. if (ptype->restrictions->pattern) {
  2429. make_persistent_restriction_char_int(&ptype->restrictions->pattern);
  2430. }
  2431. if (type->restrictions->enumeration) {
  2432. sdlRestrictionCharPtr tmp, penum;
  2433. ptype->restrictions->enumeration = malloc(sizeof(HashTable));
  2434. zend_hash_init(ptype->restrictions->enumeration, zend_hash_num_elements(type->restrictions->enumeration), NULL, delete_restriction_var_char_persistent, 1);
  2435. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  2436. penum = tmp;
  2437. make_persistent_restriction_char_int(&penum);
  2438. /* We have to duplicate key emalloc->malloc */
  2439. zend_hash_str_add_ptr(ptype->restrictions->enumeration, ZSTR_VAL(key), ZSTR_LEN(key), penum);
  2440. } ZEND_HASH_FOREACH_END();
  2441. }
  2442. }
  2443. if (ptype->elements) {
  2444. sdlTypePtr tmp, pelem;
  2445. ptype->elements = malloc(sizeof(HashTable));
  2446. zend_hash_init(ptype->elements, zend_hash_num_elements(type->elements), NULL, delete_type_persistent, 1);
  2447. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  2448. pelem = make_persistent_sdl_type(tmp, ptr_map, bp_types, bp_encoders);
  2449. if (key) {
  2450. /* We have to duplicate key emalloc->malloc */
  2451. zend_hash_str_add_ptr(ptype->elements, ZSTR_VAL(key), ZSTR_LEN(key), pelem);
  2452. } else {
  2453. zend_hash_next_index_insert_ptr(ptype->elements, pelem);
  2454. }
  2455. zend_hash_str_add_ptr(ptr_map, (char*)&tmp, sizeof(tmp), pelem);
  2456. } ZEND_HASH_FOREACH_END();
  2457. }
  2458. if (ptype->attributes) {
  2459. sdlAttributePtr tmp, pattr;
  2460. ptype->attributes = malloc(sizeof(HashTable));
  2461. zend_hash_init(ptype->attributes, zend_hash_num_elements(type->attributes), NULL, delete_attribute_persistent, 1);
  2462. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  2463. pattr = make_persistent_sdl_attribute(tmp, ptr_map, bp_types, bp_encoders);
  2464. if (key) {
  2465. /* We have to duplicate key emalloc->malloc */
  2466. zend_hash_str_add_ptr(ptype->attributes, ZSTR_VAL(key), ZSTR_LEN(key), pattr);
  2467. } else {
  2468. zend_hash_next_index_insert_ptr(ptype->attributes, pattr);
  2469. }
  2470. } ZEND_HASH_FOREACH_END();
  2471. }
  2472. if (type->model) {
  2473. ptype->model = make_persistent_sdl_model(ptype->model, ptr_map, bp_types, bp_encoders);
  2474. }
  2475. return ptype;
  2476. }
  2477. static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2478. {
  2479. encodePtr penc = NULL;
  2480. penc = malloc(sizeof(encode));
  2481. memset(penc, 0, sizeof(encode));
  2482. *penc = *enc;
  2483. if (penc->details.type_str) {
  2484. penc->details.type_str = strdup(penc->details.type_str);
  2485. }
  2486. if (penc->details.ns) {
  2487. penc->details.ns = strdup(penc->details.ns);
  2488. }
  2489. if (penc->details.sdl_type) {
  2490. make_persistent_sdl_type_ref(&penc->details.sdl_type, ptr_map, bp_types);
  2491. }
  2492. return penc;
  2493. }
  2494. static sdlBindingPtr make_persistent_sdl_binding(sdlBindingPtr bind, HashTable *ptr_map)
  2495. {
  2496. sdlBindingPtr pbind = NULL;
  2497. pbind = malloc(sizeof(sdlBinding));
  2498. memset(pbind, 0, sizeof(sdlBinding));
  2499. *pbind = *bind;
  2500. if (pbind->name) {
  2501. pbind->name = strdup(pbind->name);
  2502. }
  2503. if (pbind->location) {
  2504. pbind->location = strdup(pbind->location);
  2505. }
  2506. if (pbind->bindingType == BINDING_SOAP && pbind->bindingAttributes) {
  2507. sdlSoapBindingPtr soap_binding;
  2508. soap_binding = malloc(sizeof(sdlSoapBinding));
  2509. memset(soap_binding, 0, sizeof(sdlSoapBinding));
  2510. *soap_binding = *(sdlSoapBindingPtr)pbind->bindingAttributes;
  2511. pbind->bindingAttributes = soap_binding;
  2512. }
  2513. return pbind;
  2514. }
  2515. static sdlFunctionPtr make_persistent_sdl_function(sdlFunctionPtr func, HashTable *ptr_map)
  2516. {
  2517. sdlFunctionPtr pfunc = NULL;
  2518. pfunc = malloc(sizeof(sdlFunction));
  2519. memset(pfunc, 0, sizeof(sdlFunction));
  2520. *pfunc = *func;
  2521. if (pfunc->functionName) {
  2522. pfunc->functionName = strdup(pfunc->functionName);
  2523. }
  2524. if (pfunc->requestName) {
  2525. pfunc->requestName = strdup(pfunc->requestName);
  2526. }
  2527. if (pfunc->responseName) {
  2528. pfunc->responseName = strdup(pfunc->responseName);
  2529. }
  2530. if (pfunc->binding) {
  2531. sdlBindingPtr tmp;
  2532. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char*)&pfunc->binding, sizeof(pfunc->binding))) == NULL) {
  2533. assert(0);
  2534. }
  2535. pfunc->binding = tmp;
  2536. if (pfunc->binding->bindingType == BINDING_SOAP && pfunc->bindingAttributes) {
  2537. sdlSoapBindingFunctionPtr soap_binding;
  2538. soap_binding = malloc(sizeof(sdlSoapBindingFunction));
  2539. memset(soap_binding, 0, sizeof(sdlSoapBindingFunction));
  2540. *soap_binding = *(sdlSoapBindingFunctionPtr)pfunc->bindingAttributes;
  2541. if (soap_binding->soapAction) {
  2542. soap_binding->soapAction = strdup(soap_binding->soapAction);
  2543. }
  2544. make_persistent_sdl_soap_body(&soap_binding->input, ptr_map);
  2545. make_persistent_sdl_soap_body(&soap_binding->output, ptr_map);
  2546. pfunc->bindingAttributes = soap_binding;
  2547. }
  2548. if (pfunc->requestParameters) {
  2549. pfunc->requestParameters = make_persistent_sdl_parameters(pfunc->requestParameters, ptr_map);
  2550. }
  2551. if (pfunc->responseParameters) {
  2552. pfunc->responseParameters = make_persistent_sdl_parameters(pfunc->responseParameters, ptr_map);
  2553. }
  2554. if (pfunc->faults) {
  2555. pfunc->faults = make_persistent_sdl_function_faults(pfunc, pfunc->faults, ptr_map);
  2556. }
  2557. }
  2558. return pfunc;
  2559. }
  2560. static sdlPtr make_persistent_sdl(sdlPtr sdl)
  2561. {
  2562. sdlPtr psdl = NULL;
  2563. HashTable ptr_map;
  2564. HashTable bp_types, bp_encoders;
  2565. zend_string *key;
  2566. zend_hash_init(&bp_types, 0, NULL, NULL, 0);
  2567. zend_hash_init(&bp_encoders, 0, NULL, NULL, 0);
  2568. zend_hash_init(&ptr_map, 0, NULL, NULL, 0);
  2569. psdl = malloc(sizeof(*sdl));
  2570. memset(psdl, 0, sizeof(*sdl));
  2571. if (sdl->source) {
  2572. psdl->source = strdup(sdl->source);
  2573. }
  2574. if (sdl->target_ns) {
  2575. psdl->target_ns = strdup(sdl->target_ns);
  2576. }
  2577. if (sdl->groups) {
  2578. sdlTypePtr tmp;
  2579. sdlTypePtr ptype;
  2580. psdl->groups = malloc(sizeof(HashTable));
  2581. zend_hash_init(psdl->groups, zend_hash_num_elements(sdl->groups), NULL, delete_type_persistent, 1);
  2582. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  2583. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2584. if (key) {
  2585. /* We have to duplicate key emalloc->malloc */
  2586. zend_hash_str_add_ptr(psdl->groups, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2587. } else {
  2588. zend_hash_next_index_insert_ptr(psdl->groups, ptype);
  2589. }
  2590. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2591. } ZEND_HASH_FOREACH_END();
  2592. }
  2593. if (sdl->types) {
  2594. sdlTypePtr tmp;
  2595. sdlTypePtr ptype;
  2596. psdl->types = malloc(sizeof(HashTable));
  2597. zend_hash_init(psdl->types, zend_hash_num_elements(sdl->types), NULL, delete_type_persistent, 1);
  2598. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  2599. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2600. if (key) {
  2601. /* We have to duplicate key emalloc->malloc */
  2602. zend_hash_str_add_ptr(psdl->types, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2603. } else {
  2604. zend_hash_next_index_insert_ptr(psdl->types, ptype);
  2605. }
  2606. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2607. } ZEND_HASH_FOREACH_END();
  2608. }
  2609. if (sdl->elements) {
  2610. sdlTypePtr tmp;
  2611. sdlTypePtr ptype;
  2612. psdl->elements = malloc(sizeof(HashTable));
  2613. zend_hash_init(psdl->elements, zend_hash_num_elements(sdl->elements), NULL, delete_type_persistent, 1);
  2614. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  2615. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2616. if (key) {
  2617. /* We have to duplicate key emalloc->malloc */
  2618. zend_hash_str_add_ptr(psdl->elements, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2619. } else {
  2620. zend_hash_next_index_insert_ptr(psdl->elements, ptype);
  2621. }
  2622. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2623. } ZEND_HASH_FOREACH_END();
  2624. }
  2625. if (sdl->encoders) {
  2626. encodePtr tmp;
  2627. encodePtr penc;
  2628. psdl->encoders = malloc(sizeof(HashTable));
  2629. zend_hash_init(psdl->encoders, zend_hash_num_elements(sdl->encoders), NULL, delete_encoder_persistent, 1);
  2630. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  2631. penc = make_persistent_sdl_encoder(tmp, &ptr_map, &bp_types, &bp_encoders);
  2632. if (key) {
  2633. /* We have to duplicate key emalloc->malloc */
  2634. zend_hash_str_add_ptr(psdl->encoders, ZSTR_VAL(key), ZSTR_LEN(key), penc);
  2635. } else {
  2636. zend_hash_next_index_insert_ptr(psdl->encoders, penc);
  2637. }
  2638. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), penc);
  2639. } ZEND_HASH_FOREACH_END();
  2640. }
  2641. /* do backpatching here */
  2642. if (zend_hash_num_elements(&bp_types)) {
  2643. sdlTypePtr *tmp, ptype = NULL;
  2644. ZEND_HASH_FOREACH_PTR(&bp_types, tmp) {
  2645. if ((ptype = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2646. assert(0);
  2647. }
  2648. *tmp = ptype;
  2649. } ZEND_HASH_FOREACH_END();
  2650. }
  2651. if (zend_hash_num_elements(&bp_encoders)) {
  2652. encodePtr *tmp, penc = NULL;
  2653. ZEND_HASH_FOREACH_PTR(&bp_encoders, tmp) {
  2654. if ((penc = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2655. assert(0);
  2656. }
  2657. *tmp = penc;
  2658. } ZEND_HASH_FOREACH_END();
  2659. }
  2660. if (sdl->bindings) {
  2661. sdlBindingPtr tmp;
  2662. sdlBindingPtr pbind;
  2663. psdl->bindings = malloc(sizeof(HashTable));
  2664. zend_hash_init(psdl->bindings, zend_hash_num_elements(sdl->bindings), NULL, delete_binding_persistent, 1);
  2665. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2666. pbind = make_persistent_sdl_binding(tmp, &ptr_map);
  2667. if (key) {
  2668. /* We have to duplicate key emalloc->malloc */
  2669. zend_hash_str_add_ptr(psdl->bindings, ZSTR_VAL(key), ZSTR_LEN(key), pbind);
  2670. } else {
  2671. zend_hash_next_index_insert_ptr(psdl->bindings, pbind);
  2672. }
  2673. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pbind);
  2674. } ZEND_HASH_FOREACH_END();
  2675. }
  2676. zend_hash_init(&psdl->functions, zend_hash_num_elements(&sdl->functions), NULL, delete_function_persistent, 1);
  2677. if (zend_hash_num_elements(&sdl->functions)) {
  2678. sdlFunctionPtr tmp;
  2679. sdlFunctionPtr pfunc;
  2680. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2681. pfunc = make_persistent_sdl_function(tmp, &ptr_map);
  2682. if (key) {
  2683. /* We have to duplicate key emalloc->malloc */
  2684. zend_hash_str_add_ptr(&psdl->functions, ZSTR_VAL(key), ZSTR_LEN(key), pfunc);
  2685. } else {
  2686. zend_hash_next_index_insert_ptr(&psdl->functions, pfunc);
  2687. }
  2688. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pfunc);
  2689. } ZEND_HASH_FOREACH_END();
  2690. }
  2691. if (sdl->requests) {
  2692. zval *zv;
  2693. sdlFunctionPtr tmp;
  2694. sdlFunctionPtr preq;
  2695. psdl->requests = malloc(sizeof(HashTable));
  2696. zend_hash_init(psdl->requests, zend_hash_num_elements(sdl->requests), NULL, NULL, 1);
  2697. ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(sdl->requests, key, zv) {
  2698. tmp = Z_PTR_P(zv);
  2699. if ((preq = zend_hash_str_find_ptr(&ptr_map, (char*)&tmp, sizeof(tmp))) == NULL) {
  2700. assert(0);
  2701. }
  2702. Z_PTR_P(zv) = preq;
  2703. if (key) {
  2704. /* We have to duplicate key emalloc->malloc */
  2705. zend_hash_str_add_ptr(psdl->requests, ZSTR_VAL(key), ZSTR_LEN(key), preq);
  2706. }
  2707. } ZEND_HASH_FOREACH_END();
  2708. }
  2709. zend_hash_destroy(&ptr_map);
  2710. zend_hash_destroy(&bp_encoders);
  2711. zend_hash_destroy(&bp_types);
  2712. return psdl;
  2713. }
  2714. typedef struct _sdl_cache_bucket {
  2715. sdlPtr sdl;
  2716. time_t time;
  2717. } sdl_cache_bucket;
  2718. static void delete_psdl_int(sdl_cache_bucket *p)
  2719. {
  2720. sdlPtr tmp = p->sdl;
  2721. zend_hash_destroy(&tmp->functions);
  2722. if (tmp->source) {
  2723. free(tmp->source);
  2724. }
  2725. if (tmp->target_ns) {
  2726. free(tmp->target_ns);
  2727. }
  2728. if (tmp->elements) {
  2729. zend_hash_destroy(tmp->elements);
  2730. free(tmp->elements);
  2731. }
  2732. if (tmp->encoders) {
  2733. zend_hash_destroy(tmp->encoders);
  2734. free(tmp->encoders);
  2735. }
  2736. if (tmp->types) {
  2737. zend_hash_destroy(tmp->types);
  2738. free(tmp->types);
  2739. }
  2740. if (tmp->groups) {
  2741. zend_hash_destroy(tmp->groups);
  2742. free(tmp->groups);
  2743. }
  2744. if (tmp->bindings) {
  2745. zend_hash_destroy(tmp->bindings);
  2746. free(tmp->bindings);
  2747. }
  2748. if (tmp->requests) {
  2749. zend_hash_destroy(tmp->requests);
  2750. free(tmp->requests);
  2751. }
  2752. free(tmp);
  2753. }
  2754. static void delete_psdl(zval *zv)
  2755. {
  2756. delete_psdl_int(Z_PTR_P(zv));
  2757. free(Z_PTR_P(zv));
  2758. }
  2759. sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
  2760. {
  2761. char fn[MAXPATHLEN];
  2762. sdlPtr sdl = NULL;
  2763. char* old_error_code = SOAP_GLOBAL(error_code);
  2764. size_t uri_len = 0;
  2765. php_stream_context *context=NULL;
  2766. zval *tmp, orig_context, new_context;
  2767. smart_str headers = {0};
  2768. char* key = NULL;
  2769. time_t t = time(0);
  2770. bool has_proxy_authorization = 0;
  2771. bool has_authorization = 0;
  2772. ZVAL_UNDEF(&orig_context);
  2773. ZVAL_UNDEF(&new_context);
  2774. if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
  2775. uri_len = strlen(uri);
  2776. } else if (VCWD_REALPATH(uri, fn) == NULL) {
  2777. cache_wsdl = WSDL_CACHE_NONE;
  2778. } else {
  2779. uri = fn;
  2780. uri_len = strlen(uri);
  2781. }
  2782. if ((cache_wsdl & WSDL_CACHE_MEMORY) && SOAP_GLOBAL(mem_cache)) {
  2783. sdl_cache_bucket *p;
  2784. if (NULL != (p = zend_hash_str_find_ptr(SOAP_GLOBAL(mem_cache), uri, uri_len))) {
  2785. if (p->time < t - SOAP_GLOBAL(cache_ttl)) {
  2786. /* in-memory cache entry is expired */
  2787. zend_hash_str_del(&EG(persistent_list), uri, uri_len);
  2788. } else {
  2789. return p->sdl;
  2790. }
  2791. }
  2792. }
  2793. if ((cache_wsdl & WSDL_CACHE_DISK) && (uri_len < MAXPATHLEN)) {
  2794. time_t t = time(0);
  2795. char md5str[33];
  2796. PHP_MD5_CTX context;
  2797. unsigned char digest[16];
  2798. int len = strlen(SOAP_GLOBAL(cache_dir));
  2799. time_t cached;
  2800. char *user = php_get_current_user();
  2801. int user_len = user ? strlen(user) + 1 : 0;
  2802. md5str[0] = '\0';
  2803. PHP_MD5Init(&context);
  2804. PHP_MD5Update(&context, (unsigned char*)uri, uri_len);
  2805. PHP_MD5Final(digest, &context);
  2806. make_digest(md5str, digest);
  2807. key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
  2808. memcpy(key,SOAP_GLOBAL(cache_dir),len);
  2809. memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
  2810. len += sizeof("/wsdl-")-1;
  2811. if (user_len) {
  2812. memcpy(key+len, user, user_len-1);
  2813. len += user_len-1;
  2814. key[len++] = '-';
  2815. }
  2816. if (WSDL_CACHE_VERSION <= 0x9f) {
  2817. key[len++] = (WSDL_CACHE_VERSION >> 8) + '0';
  2818. } else {
  2819. key[len++] = (WSDL_CACHE_VERSION >> 8) - 10 + 'a';
  2820. }
  2821. if ((WSDL_CACHE_VERSION & 0xf) <= 0x9) {
  2822. key[len++] = (WSDL_CACHE_VERSION & 0xf) + '0';
  2823. } else {
  2824. key[len++] = (WSDL_CACHE_VERSION & 0xf) - 10 + 'a';
  2825. }
  2826. memcpy(key+len,md5str,sizeof(md5str));
  2827. if ((sdl = get_sdl_from_cache(key, uri, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
  2828. t = cached;
  2829. efree(key);
  2830. goto cache_in_memory;
  2831. }
  2832. }
  2833. if (instanceof_function(Z_OBJCE_P(this_ptr), soap_class_entry)) {
  2834. tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr);
  2835. if (Z_TYPE_P(tmp) == IS_RESOURCE) {
  2836. context = php_stream_context_from_zval(tmp, 0);
  2837. }
  2838. tmp = Z_CLIENT_USER_AGENT_P(this_ptr);
  2839. if (Z_TYPE_P(tmp) == IS_STRING && Z_STRLEN_P(tmp) > 0) {
  2840. smart_str_appends(&headers, "User-Agent: ");
  2841. smart_str_appends(&headers, Z_STRVAL_P(tmp));
  2842. smart_str_appends(&headers, "\r\n");
  2843. }
  2844. zval *proxy_host = Z_CLIENT_PROXY_HOST_P(this_ptr);
  2845. zval *proxy_port = Z_CLIENT_PROXY_PORT_P(this_ptr);
  2846. if (Z_TYPE_P(proxy_host) == IS_STRING && Z_TYPE_P(proxy_port) == IS_LONG) {
  2847. zval str_proxy;
  2848. smart_str proxy = {0};
  2849. smart_str_appends(&proxy,"tcp://");
  2850. smart_str_appends(&proxy,Z_STRVAL_P(proxy_host));
  2851. smart_str_appends(&proxy,":");
  2852. smart_str_append_long(&proxy,Z_LVAL_P(proxy_port));
  2853. smart_str_0(&proxy);
  2854. ZVAL_NEW_STR(&str_proxy, proxy.s);
  2855. if (!context) {
  2856. context = php_stream_context_alloc();
  2857. }
  2858. php_stream_context_set_option(context, "http", "proxy", &str_proxy);
  2859. zval_ptr_dtor(&str_proxy);
  2860. if (uri_len < sizeof("https://")-1 ||
  2861. strncasecmp(uri, "https://", sizeof("https://")-1) != 0) {
  2862. ZVAL_TRUE(&str_proxy);
  2863. php_stream_context_set_option(context, "http", "request_fulluri", &str_proxy);
  2864. }
  2865. has_proxy_authorization = proxy_authentication(this_ptr, &headers);
  2866. }
  2867. has_authorization = basic_authentication(this_ptr, &headers);
  2868. }
  2869. if (!context) {
  2870. context = php_stream_context_alloc();
  2871. }
  2872. /* Use HTTP/1.1 with "Connection: close" by default */
  2873. if ((tmp = php_stream_context_get_option(context, "http", "protocol_version")) == NULL) {
  2874. zval http_version;
  2875. ZVAL_DOUBLE(&http_version, 1.1);
  2876. php_stream_context_set_option(context, "http", "protocol_version", &http_version);
  2877. smart_str_appendl(&headers, "Connection: close\r\n", sizeof("Connection: close\r\n")-1);
  2878. }
  2879. if (headers.s && ZSTR_LEN(headers.s) > 0) {
  2880. zval str_headers;
  2881. if (!context) {
  2882. context = php_stream_context_alloc();
  2883. } else {
  2884. http_context_headers(context, has_authorization, has_proxy_authorization, 0, &headers);
  2885. }
  2886. smart_str_0(&headers);
  2887. ZVAL_NEW_STR(&str_headers, headers.s);
  2888. php_stream_context_set_option(context, "http", "header", &str_headers);
  2889. zval_ptr_dtor(&str_headers);
  2890. }
  2891. if (context) {
  2892. php_stream_context_to_zval(context, &new_context);
  2893. php_libxml_switch_context(&new_context, &orig_context);
  2894. }
  2895. SOAP_GLOBAL(error_code) = "WSDL";
  2896. sdl = load_wsdl(this_ptr, uri);
  2897. if (sdl) {
  2898. sdl->is_persistent = 0;
  2899. }
  2900. SOAP_GLOBAL(error_code) = old_error_code;
  2901. if (context) {
  2902. php_libxml_switch_context(&orig_context, NULL);
  2903. zval_ptr_dtor(&new_context);
  2904. }
  2905. if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
  2906. if (sdl) {
  2907. add_sdl_to_cache(key, uri, t, sdl);
  2908. }
  2909. efree(key);
  2910. }
  2911. cache_in_memory:
  2912. if (cache_wsdl & WSDL_CACHE_MEMORY) {
  2913. if (sdl) {
  2914. sdlPtr psdl;
  2915. sdl_cache_bucket p;
  2916. if (SOAP_GLOBAL(mem_cache) == NULL) {
  2917. SOAP_GLOBAL(mem_cache) = malloc(sizeof(HashTable));
  2918. zend_hash_init(SOAP_GLOBAL(mem_cache), 0, NULL, delete_psdl, 1);
  2919. } else if (SOAP_GLOBAL(cache_limit) > 0 &&
  2920. SOAP_GLOBAL(cache_limit) <= (zend_long)zend_hash_num_elements(SOAP_GLOBAL(mem_cache))) {
  2921. /* in-memory cache overflow */
  2922. sdl_cache_bucket *q;
  2923. time_t latest = t;
  2924. zend_string *latest_key = NULL, *key;
  2925. ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(SOAP_GLOBAL(mem_cache), key, q) {
  2926. if (q->time < latest) {
  2927. latest = q->time;
  2928. latest_key = key;
  2929. }
  2930. } ZEND_HASH_FOREACH_END();
  2931. if (latest_key) {
  2932. zend_hash_del(SOAP_GLOBAL(mem_cache), latest_key);
  2933. } else {
  2934. return sdl;
  2935. }
  2936. }
  2937. psdl = make_persistent_sdl(sdl);
  2938. psdl->is_persistent = 1;
  2939. p.time = t;
  2940. p.sdl = psdl;
  2941. zend_hash_str_update_mem(SOAP_GLOBAL(mem_cache), uri,
  2942. uri_len, &p, sizeof(sdl_cache_bucket));
  2943. /* remove non-persitent sdl structure */
  2944. delete_sdl_impl(sdl);
  2945. /* and replace it with persistent one */
  2946. sdl = psdl;
  2947. }
  2948. }
  2949. return sdl;
  2950. }
  2951. /* Deletes */
  2952. void delete_sdl_impl(void *handle)
  2953. {
  2954. sdlPtr tmp = (sdlPtr)handle;
  2955. zend_hash_destroy(&tmp->functions);
  2956. if (tmp->source) {
  2957. efree(tmp->source);
  2958. }
  2959. if (tmp->target_ns) {
  2960. efree(tmp->target_ns);
  2961. }
  2962. if (tmp->elements) {
  2963. zend_hash_destroy(tmp->elements);
  2964. efree(tmp->elements);
  2965. }
  2966. if (tmp->encoders) {
  2967. zend_hash_destroy(tmp->encoders);
  2968. efree(tmp->encoders);
  2969. }
  2970. if (tmp->types) {
  2971. zend_hash_destroy(tmp->types);
  2972. efree(tmp->types);
  2973. }
  2974. if (tmp->groups) {
  2975. zend_hash_destroy(tmp->groups);
  2976. efree(tmp->groups);
  2977. }
  2978. if (tmp->bindings) {
  2979. zend_hash_destroy(tmp->bindings);
  2980. efree(tmp->bindings);
  2981. }
  2982. if (tmp->requests) {
  2983. zend_hash_destroy(tmp->requests);
  2984. efree(tmp->requests);
  2985. }
  2986. efree(tmp);
  2987. }
  2988. void delete_sdl(void *handle)
  2989. {
  2990. sdlPtr tmp = (sdlPtr)handle;
  2991. if (!tmp->is_persistent) {
  2992. delete_sdl_impl(tmp);
  2993. }
  2994. }
  2995. static void delete_binding(zval *zv)
  2996. {
  2997. sdlBindingPtr binding = Z_PTR_P(zv);
  2998. if (binding->location) {
  2999. efree(binding->location);
  3000. }
  3001. if (binding->name) {
  3002. efree(binding->name);
  3003. }
  3004. if (binding->bindingType == BINDING_SOAP) {
  3005. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3006. if (soapBind) {
  3007. efree(soapBind);
  3008. }
  3009. }
  3010. efree(binding);
  3011. }
  3012. static void delete_binding_persistent(zval *zv)
  3013. {
  3014. sdlBindingPtr binding = Z_PTR_P(zv);
  3015. if (binding->location) {
  3016. free(binding->location);
  3017. }
  3018. if (binding->name) {
  3019. free(binding->name);
  3020. }
  3021. if (binding->bindingType == BINDING_SOAP) {
  3022. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3023. if (soapBind) {
  3024. free(soapBind);
  3025. }
  3026. }
  3027. free(binding);
  3028. }
  3029. static void delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)
  3030. {
  3031. if (body.ns) {
  3032. efree(body.ns);
  3033. }
  3034. if (body.headers) {
  3035. zend_hash_destroy(body.headers);
  3036. efree(body.headers);
  3037. }
  3038. }
  3039. static void delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)
  3040. {
  3041. if (body.ns) {
  3042. free(body.ns);
  3043. }
  3044. if (body.headers) {
  3045. zend_hash_destroy(body.headers);
  3046. free(body.headers);
  3047. }
  3048. }
  3049. static void delete_function(zval *zv)
  3050. {
  3051. sdlFunctionPtr function = Z_PTR_P(zv);
  3052. if (function->functionName) {
  3053. efree(function->functionName);
  3054. }
  3055. if (function->requestName) {
  3056. efree(function->requestName);
  3057. }
  3058. if (function->responseName) {
  3059. efree(function->responseName);
  3060. }
  3061. if (function->requestParameters) {
  3062. zend_hash_destroy(function->requestParameters);
  3063. efree(function->requestParameters);
  3064. }
  3065. if (function->responseParameters) {
  3066. zend_hash_destroy(function->responseParameters);
  3067. efree(function->responseParameters);
  3068. }
  3069. if (function->faults) {
  3070. zend_hash_destroy(function->faults);
  3071. efree(function->faults);
  3072. }
  3073. if (function->bindingAttributes &&
  3074. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3075. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3076. if (soapFunction->soapAction) {
  3077. efree(soapFunction->soapAction);
  3078. }
  3079. delete_sdl_soap_binding_function_body(soapFunction->input);
  3080. delete_sdl_soap_binding_function_body(soapFunction->output);
  3081. efree(soapFunction);
  3082. }
  3083. efree(function);
  3084. }
  3085. static void delete_function_persistent(zval *zv)
  3086. {
  3087. sdlFunctionPtr function = Z_PTR_P(zv);
  3088. if (function->functionName) {
  3089. free(function->functionName);
  3090. }
  3091. if (function->requestName) {
  3092. free(function->requestName);
  3093. }
  3094. if (function->responseName) {
  3095. free(function->responseName);
  3096. }
  3097. if (function->requestParameters) {
  3098. zend_hash_destroy(function->requestParameters);
  3099. free(function->requestParameters);
  3100. }
  3101. if (function->responseParameters) {
  3102. zend_hash_destroy(function->responseParameters);
  3103. free(function->responseParameters);
  3104. }
  3105. if (function->faults) {
  3106. zend_hash_destroy(function->faults);
  3107. free(function->faults);
  3108. }
  3109. if (function->bindingAttributes &&
  3110. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3111. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3112. if (soapFunction->soapAction) {
  3113. free(soapFunction->soapAction);
  3114. }
  3115. delete_sdl_soap_binding_function_body_persistent(soapFunction->input);
  3116. delete_sdl_soap_binding_function_body_persistent(soapFunction->output);
  3117. free(soapFunction);
  3118. }
  3119. free(function);
  3120. }
  3121. static void delete_parameter(zval *zv)
  3122. {
  3123. sdlParamPtr param = Z_PTR_P(zv);
  3124. if (param->paramName) {
  3125. efree(param->paramName);
  3126. }
  3127. efree(param);
  3128. }
  3129. static void delete_parameter_persistent(zval *zv)
  3130. {
  3131. sdlParamPtr param = Z_PTR_P(zv);
  3132. if (param->paramName) {
  3133. free(param->paramName);
  3134. }
  3135. free(param);
  3136. }
  3137. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr)
  3138. {
  3139. if (hdr->name) {
  3140. efree(hdr->name);
  3141. }
  3142. if (hdr->ns) {
  3143. efree(hdr->ns);
  3144. }
  3145. if (hdr->headerfaults) {
  3146. zend_hash_destroy(hdr->headerfaults);
  3147. efree(hdr->headerfaults);
  3148. }
  3149. efree(hdr);
  3150. }
  3151. static void delete_header(zval *zv)
  3152. {
  3153. delete_header_int(Z_PTR_P(zv));
  3154. }
  3155. static void delete_header_persistent(zval *zv)
  3156. {
  3157. sdlSoapBindingFunctionHeaderPtr hdr = Z_PTR_P(zv);
  3158. if (hdr->name) {
  3159. free(hdr->name);
  3160. }
  3161. if (hdr->ns) {
  3162. free(hdr->ns);
  3163. }
  3164. if (hdr->headerfaults) {
  3165. zend_hash_destroy(hdr->headerfaults);
  3166. free(hdr->headerfaults);
  3167. }
  3168. free(hdr);
  3169. }
  3170. static void delete_fault(zval *zv)
  3171. {
  3172. sdlFaultPtr fault = Z_PTR_P(zv);
  3173. if (fault->name) {
  3174. efree(fault->name);
  3175. }
  3176. if (fault->details) {
  3177. zend_hash_destroy(fault->details);
  3178. efree(fault->details);
  3179. }
  3180. if (fault->bindingAttributes) {
  3181. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3182. if (binding->ns) {
  3183. efree(binding->ns);
  3184. }
  3185. efree(fault->bindingAttributes);
  3186. }
  3187. efree(fault);
  3188. }
  3189. static void delete_fault_persistent(zval *zv)
  3190. {
  3191. sdlFaultPtr fault = Z_PTR_P(zv);
  3192. if (fault->name) {
  3193. free(fault->name);
  3194. }
  3195. if (fault->details) {
  3196. zend_hash_destroy(fault->details);
  3197. free(fault->details);
  3198. }
  3199. if (fault->bindingAttributes) {
  3200. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3201. if (binding->ns) {
  3202. free(binding->ns);
  3203. }
  3204. free(fault->bindingAttributes);
  3205. }
  3206. free(fault);
  3207. }
  3208. static void delete_document(zval *zv)
  3209. {
  3210. xmlDocPtr doc = Z_PTR_P(zv);
  3211. xmlFreeDoc(doc);
  3212. }