php_packet_soap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /* SOAP client calls this function to parse response from SOAP server */
  20. int parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctionPtr fn, char *fn_name, zval *return_value, zval *soap_headers)
  21. {
  22. char* envelope_ns = NULL;
  23. xmlDocPtr response;
  24. xmlNodePtr trav, env, head, body, resp, cur, fault;
  25. xmlAttrPtr attr;
  26. int param_count = 0;
  27. int soap_version = SOAP_1_1;
  28. HashTable *hdrs = NULL;
  29. ZVAL_NULL(return_value);
  30. /* Response for one-way opearation */
  31. if (buffer_size == 0) {
  32. return TRUE;
  33. }
  34. /* Parse XML packet */
  35. response = soap_xmlParseMemory(buffer, buffer_size);
  36. if (!response) {
  37. add_soap_fault(this_ptr, "Client", "looks like we got no XML document", NULL, NULL);
  38. return FALSE;
  39. }
  40. if (xmlGetIntSubset(response) != NULL) {
  41. add_soap_fault(this_ptr, "Client", "DTD are not supported by SOAP", NULL, NULL);
  42. xmlFreeDoc(response);
  43. return FALSE;
  44. }
  45. /* Get <Envelope> element */
  46. env = NULL;
  47. trav = response->children;
  48. while (trav != NULL) {
  49. if (trav->type == XML_ELEMENT_NODE) {
  50. if (env == NULL && node_is_equal_ex(trav,"Envelope",SOAP_1_1_ENV_NAMESPACE)) {
  51. env = trav;
  52. envelope_ns = SOAP_1_1_ENV_NAMESPACE;
  53. soap_version = SOAP_1_1;
  54. } else if (env == NULL && node_is_equal_ex(trav,"Envelope",SOAP_1_2_ENV_NAMESPACE)) {
  55. env = trav;
  56. envelope_ns = SOAP_1_2_ENV_NAMESPACE;
  57. soap_version = SOAP_1_2;
  58. } else {
  59. add_soap_fault(this_ptr, "VersionMismatch", "Wrong Version", NULL, NULL);
  60. xmlFreeDoc(response);
  61. return FALSE;
  62. }
  63. }
  64. trav = trav->next;
  65. }
  66. if (env == NULL) {
  67. add_soap_fault(this_ptr, "Client", "looks like we got XML without \"Envelope\" element", NULL, NULL);
  68. xmlFreeDoc(response);
  69. return FALSE;
  70. }
  71. attr = env->properties;
  72. while (attr != NULL) {
  73. if (attr->ns == NULL) {
  74. add_soap_fault(this_ptr, "Client", "A SOAP Envelope element cannot have non Namespace qualified attributes", NULL, NULL);
  75. xmlFreeDoc(response);
  76. return FALSE;
  77. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  78. if (soap_version == SOAP_1_2) {
  79. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Envelope", NULL, NULL);
  80. xmlFreeDoc(response);
  81. return FALSE;
  82. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  83. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  84. xmlFreeDoc(response);
  85. return FALSE;
  86. }
  87. }
  88. attr = attr->next;
  89. }
  90. /* Get <Header> element */
  91. head = NULL;
  92. trav = env->children;
  93. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  94. trav = trav->next;
  95. }
  96. if (trav != NULL && node_is_equal_ex(trav,"Header",envelope_ns)) {
  97. head = trav;
  98. trav = trav->next;
  99. }
  100. /* Get <Body> element */
  101. body = NULL;
  102. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  103. trav = trav->next;
  104. }
  105. if (trav != NULL && node_is_equal_ex(trav,"Body",envelope_ns)) {
  106. body = trav;
  107. trav = trav->next;
  108. }
  109. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  110. trav = trav->next;
  111. }
  112. if (body == NULL) {
  113. add_soap_fault(this_ptr, "Client", "Body must be present in a SOAP envelope", NULL, NULL);
  114. xmlFreeDoc(response);
  115. return FALSE;
  116. }
  117. attr = body->properties;
  118. while (attr != NULL) {
  119. if (attr->ns == NULL) {
  120. if (soap_version == SOAP_1_2) {
  121. add_soap_fault(this_ptr, "Client", "A SOAP Body element cannot have non Namespace qualified attributes", NULL, NULL);
  122. xmlFreeDoc(response);
  123. return FALSE;
  124. }
  125. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  126. if (soap_version == SOAP_1_2) {
  127. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Body", NULL, NULL);
  128. xmlFreeDoc(response);
  129. return FALSE;
  130. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  131. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  132. xmlFreeDoc(response);
  133. return FALSE;
  134. }
  135. }
  136. attr = attr->next;
  137. }
  138. if (trav != NULL && soap_version == SOAP_1_2) {
  139. add_soap_fault(this_ptr, "Client", "A SOAP 1.2 envelope can contain only Header and Body", NULL, NULL);
  140. xmlFreeDoc(response);
  141. return FALSE;
  142. }
  143. if (head != NULL) {
  144. attr = head->properties;
  145. while (attr != NULL) {
  146. if (attr->ns == NULL) {
  147. add_soap_fault(this_ptr, "Client", "A SOAP Header element cannot have non Namespace qualified attributes", NULL, NULL);
  148. xmlFreeDoc(response);
  149. return FALSE;
  150. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  151. if (soap_version == SOAP_1_2) {
  152. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Header", NULL, NULL);
  153. xmlFreeDoc(response);
  154. return FALSE;
  155. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  156. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  157. xmlFreeDoc(response);
  158. return FALSE;
  159. }
  160. }
  161. attr = attr->next;
  162. }
  163. }
  164. /* Check if <Body> contains <Fault> element */
  165. fault = get_node_ex(body->children,"Fault",envelope_ns);
  166. if (fault != NULL) {
  167. char *faultcode = NULL;
  168. zend_string *faultstring = NULL, *faultactor = NULL;
  169. zval details;
  170. xmlNodePtr tmp;
  171. ZVAL_UNDEF(&details);
  172. if (soap_version == SOAP_1_1) {
  173. tmp = get_node(fault->children, "faultcode");
  174. if (tmp != NULL && tmp->children != NULL) {
  175. faultcode = (char*)tmp->children->content;
  176. }
  177. tmp = get_node(fault->children, "faultstring");
  178. if (tmp != NULL && tmp->children != NULL) {
  179. zval zv;
  180. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  181. faultstring = Z_STR(zv);
  182. }
  183. tmp = get_node(fault->children, "faultactor");
  184. if (tmp != NULL && tmp->children != NULL) {
  185. zval zv;
  186. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  187. faultactor = Z_STR(zv);
  188. }
  189. tmp = get_node(fault->children, "detail");
  190. if (tmp != NULL) {
  191. master_to_zval(&details, NULL, tmp);
  192. }
  193. } else {
  194. tmp = get_node(fault->children, "Code");
  195. if (tmp != NULL && tmp->children != NULL) {
  196. tmp = get_node(tmp->children, "Value");
  197. if (tmp != NULL && tmp->children != NULL) {
  198. faultcode = (char*)tmp->children->content;
  199. }
  200. }
  201. tmp = get_node(fault->children,"Reason");
  202. if (tmp != NULL && tmp->children != NULL) {
  203. /* TODO: lang attribute */
  204. tmp = get_node(tmp->children,"Text");
  205. if (tmp != NULL && tmp->children != NULL) {
  206. zval zv;
  207. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  208. faultstring = Z_STR(zv);
  209. }
  210. }
  211. tmp = get_node(fault->children,"Detail");
  212. if (tmp != NULL) {
  213. master_to_zval(&details, NULL, tmp);
  214. }
  215. }
  216. add_soap_fault(this_ptr, faultcode, faultstring ? ZSTR_VAL(faultstring) : NULL, faultactor ? ZSTR_VAL(faultactor) : NULL, &details);
  217. if (faultstring) {
  218. zend_string_release_ex(faultstring, 0);
  219. }
  220. if (faultactor) {
  221. zend_string_release_ex(faultactor, 0);
  222. }
  223. if (Z_REFCOUNTED(details)) {
  224. Z_DELREF(details);
  225. }
  226. xmlFreeDoc(response);
  227. return FALSE;
  228. }
  229. /* Parse content of <Body> element */
  230. array_init(return_value);
  231. resp = body->children;
  232. while (resp != NULL && resp->type != XML_ELEMENT_NODE) {
  233. resp = resp->next;
  234. }
  235. if (resp != NULL) {
  236. if (fn != NULL && fn->binding && fn->binding->bindingType == BINDING_SOAP) {
  237. /* Function has WSDL description */
  238. sdlParamPtr param = NULL;
  239. xmlNodePtr val = NULL;
  240. char *name, *ns = NULL;
  241. zval tmp;
  242. sdlSoapBindingFunctionPtr fnb = (sdlSoapBindingFunctionPtr)fn->bindingAttributes;
  243. int res_count;
  244. hdrs = fnb->output.headers;
  245. if (fn->responseParameters) {
  246. res_count = zend_hash_num_elements(fn->responseParameters);
  247. ZEND_HASH_FOREACH_PTR(fn->responseParameters, param) {
  248. if (fnb->style == SOAP_DOCUMENT) {
  249. if (param->element) {
  250. name = param->element->name;
  251. ns = param->element->namens;
  252. /*
  253. name = param->encode->details.type_str;
  254. ns = param->encode->details.ns;
  255. */
  256. } else {
  257. name = param->paramName;
  258. }
  259. } else {
  260. name = fn->responseName;
  261. /* ns = ? */
  262. }
  263. /* Get value of parameter */
  264. cur = get_node_ex(resp, name, ns);
  265. if (!cur) {
  266. cur = get_node(resp, name);
  267. /* TODO: produce warning invalid ns */
  268. }
  269. if (!cur && fnb->style == SOAP_RPC) {
  270. cur = resp;
  271. }
  272. if (cur) {
  273. if (fnb->style == SOAP_DOCUMENT) {
  274. val = cur;
  275. } else {
  276. val = get_node(cur->children, param->paramName);
  277. if (res_count == 1) {
  278. if (val == NULL) {
  279. val = get_node(cur->children, "return");
  280. }
  281. if (val == NULL) {
  282. val = get_node(cur->children, "result");
  283. }
  284. if (val == NULL && cur->children && cur->children->next == NULL) {
  285. val = cur->children;
  286. }
  287. }
  288. }
  289. }
  290. if (!val) {
  291. /* TODO: may be "nil" is not OK? */
  292. ZVAL_NULL(&tmp);
  293. /*
  294. add_soap_fault(this_ptr, "Client", "Can't find response data", NULL, NULL);
  295. xmlFreeDoc(response);
  296. return FALSE;
  297. */
  298. } else {
  299. /* Decoding value of parameter */
  300. if (param != NULL) {
  301. master_to_zval(&tmp, param->encode, val);
  302. } else {
  303. master_to_zval(&tmp, NULL, val);
  304. }
  305. }
  306. add_assoc_zval(return_value, param->paramName, &tmp);
  307. param_count++;
  308. } ZEND_HASH_FOREACH_END();
  309. }
  310. } else {
  311. /* Function has no WSDL description */
  312. xmlNodePtr val;
  313. val = resp->children;
  314. while (val != NULL) {
  315. while (val && val->type != XML_ELEMENT_NODE) {
  316. val = val->next;
  317. }
  318. if (val != NULL) {
  319. if (!node_is_equal_ex(val,"result",RPC_SOAP12_NAMESPACE)) {
  320. zval tmp;
  321. zval *arr;
  322. master_to_zval(&tmp, NULL, val);
  323. if (val->name) {
  324. if ((arr = zend_hash_str_find(Z_ARRVAL_P(return_value), (char*)val->name, strlen((char*)val->name))) != NULL) {
  325. add_next_index_zval(arr, &tmp);
  326. } else if (val->next && get_node(val->next, (char*)val->name)) {
  327. zval arr;
  328. array_init(&arr);
  329. add_next_index_zval(&arr, &tmp);
  330. add_assoc_zval(return_value, (char*)val->name, &arr);
  331. } else {
  332. add_assoc_zval(return_value, (char*)val->name, &tmp);
  333. }
  334. } else {
  335. add_next_index_zval(return_value, &tmp);
  336. }
  337. ++param_count;
  338. }
  339. val = val->next;
  340. }
  341. }
  342. }
  343. }
  344. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  345. if (param_count == 0) {
  346. zend_array_destroy(Z_ARR_P(return_value));
  347. ZVAL_NULL(return_value);
  348. } else if (param_count == 1) {
  349. zval *tmp;
  350. zend_hash_internal_pointer_reset(Z_ARRVAL_P(return_value));
  351. tmp = zend_hash_get_current_data(Z_ARRVAL_P(return_value));
  352. if (!Z_REFCOUNTED_P(return_value)) {
  353. ZVAL_COPY(return_value, tmp);
  354. } else {
  355. zend_refcounted *garbage = Z_COUNTED_P(return_value);
  356. ZVAL_COPY(return_value, tmp);
  357. rc_dtor_func(garbage);
  358. }
  359. }
  360. }
  361. if (soap_headers && head) {
  362. trav = head->children;
  363. while (trav != NULL) {
  364. if (trav->type == XML_ELEMENT_NODE) {
  365. encodePtr enc = NULL;
  366. zval val;
  367. if (hdrs) {
  368. smart_str key = {0};
  369. sdlSoapBindingFunctionHeaderPtr hdr;
  370. if (trav->ns) {
  371. smart_str_appends(&key, (char*)trav->ns->href);
  372. smart_str_appendc(&key,':');
  373. }
  374. smart_str_appends(&key, (char*)trav->name);
  375. smart_str_0(&key);
  376. if ((hdr = zend_hash_find_ptr(hdrs, key.s)) != NULL) {
  377. enc = hdr->encode;
  378. }
  379. smart_str_free(&key);
  380. }
  381. master_to_zval(&val, enc, trav);
  382. add_assoc_zval(soap_headers, (char*)trav->name, &val);
  383. }
  384. trav = trav->next;
  385. }
  386. }
  387. xmlFreeDoc(response);
  388. return TRUE;
  389. }