PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/php-pecl-solr-1.0.2/solr-1.0.2/php_solr_response.c

#
C | 432 lines | 223 code | 111 blank | 98 comment | 31 complexity | cda3f1ce675b4784278e550aae54e604 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Israel Ekpo <iekpo@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: php_solr_response.c 311799 2011-06-04 08:05:00Z iekpo $ */
  19. #include "php_solr.h"
  20. /* {{{ Macro for extracting property values using string constants */
  21. #define solr_read_response_object_property(objptr, name, silent) zend_read_property(Z_OBJCE_P(objptr), objptr, name, sizeof(name)-1, silent TSRMLS_CC)
  22. /* }}} */
  23. /* {{{ proto int SolrResponse::getHttpStatus(void)
  24. Returns the value of the http_status property. */
  25. PHP_METHOD(SolrResponse, getHttpStatus)
  26. {
  27. zend_bool silent = 1;
  28. zval *objptr = getThis();
  29. zval *http_status = NULL;
  30. if (!return_value_used) {
  31. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  32. return;
  33. }
  34. http_status = solr_read_response_object_property(objptr, "http_status", silent);
  35. RETURN_LONG(Z_LVAL_P(http_status));
  36. }
  37. /* }}} */
  38. /* {{{ proto string SolrResponse::getHttpStatusMessage(void)
  39. Returns the http_status_message property. */
  40. PHP_METHOD(SolrResponse, getHttpStatusMessage)
  41. {
  42. zend_bool silent = 1;
  43. zval *objptr = getThis();
  44. zval *http_status_message = NULL;
  45. if (!return_value_used) {
  46. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  47. return;
  48. }
  49. http_status_message = solr_read_response_object_property(objptr, "http_status_message", silent);
  50. RETURN_STRINGL(Z_STRVAL_P(http_status_message), Z_STRLEN_P(http_status_message), 1);
  51. }
  52. /* }}} */
  53. /* {{{ proto bool SolrResponse::success(void)
  54. Returns whether the request was successful or not. */
  55. PHP_METHOD(SolrResponse, success)
  56. {
  57. zend_bool silent = 1;
  58. zval *objptr = getThis();
  59. zval *success = NULL;
  60. if (!return_value_used) {
  61. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  62. return;
  63. }
  64. success = solr_read_response_object_property(objptr, "success", silent);
  65. RETURN_BOOL(Z_BVAL_P(success));
  66. }
  67. /* }}} */
  68. /* {{{ proto string SolrResponse::getRequestUrl(void)
  69. Returns the URL used for the request. */
  70. PHP_METHOD(SolrResponse, getRequestUrl)
  71. {
  72. zend_bool silent = 1;
  73. zval *objptr = getThis();
  74. zval *prop = NULL;
  75. if (!return_value_used) {
  76. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  77. return;
  78. }
  79. prop = solr_read_response_object_property(objptr, "http_request_url", silent);
  80. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  81. }
  82. /* }}} */
  83. /* {{{ proto string SolrResponse::getRawRequestHeaders(void)
  84. Returns the raw http request headers sent to the server. */
  85. PHP_METHOD(SolrResponse, getRawRequestHeaders)
  86. {
  87. zend_bool silent = 1;
  88. zval *objptr = getThis();
  89. zval *prop = NULL;
  90. if (!return_value_used) {
  91. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  92. return;
  93. }
  94. prop = solr_read_response_object_property(objptr, "http_raw_request_headers", silent);
  95. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  96. }
  97. /* }}} */
  98. /* {{{ proto string SolrResponse::getRawRequest(void)
  99. Returns the raw http request sent to the server. */
  100. PHP_METHOD(SolrResponse, getRawRequest)
  101. {
  102. zend_bool silent = 1;
  103. zval *objptr = getThis();
  104. zval *prop = NULL;
  105. if (!return_value_used) {
  106. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  107. return;
  108. }
  109. prop = solr_read_response_object_property(objptr, "http_raw_request", silent);
  110. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  111. }
  112. /* }}} */
  113. /* {{{ proto string SolrResponse::getRawResponseHeaders(void)
  114. Returns the raw http response headers from the server. */
  115. PHP_METHOD(SolrResponse, getRawResponseHeaders)
  116. {
  117. zend_bool silent = 1;
  118. zval *objptr = getThis();
  119. zval *prop = NULL;
  120. if (!return_value_used) {
  121. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  122. return;
  123. }
  124. prop = solr_read_response_object_property(objptr, "http_raw_response_headers", silent);
  125. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  126. }
  127. /* }}} */
  128. /* {{{ proto string SolrResponse::getRawResponse(void)
  129. Returns the raw http response from the server. */
  130. PHP_METHOD(SolrResponse, getRawResponse)
  131. {
  132. zend_bool silent = 1;
  133. zval *objptr = getThis();
  134. zval *prop = NULL;
  135. if (!return_value_used) {
  136. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  137. return;
  138. }
  139. prop = solr_read_response_object_property(objptr, "http_raw_response", silent);
  140. if (Z_STRLEN_P(prop))
  141. {
  142. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  143. }
  144. RETURN_NULL();
  145. }
  146. /* }}} */
  147. /* {{{ proto string SolrResponse::getDigestedResponse(void)
  148. Returns the serialized object string derived from the XML response. */
  149. PHP_METHOD(SolrResponse, getDigestedResponse)
  150. {
  151. zend_bool silent = 0;
  152. zval *objptr = getThis();
  153. zval *prop = NULL;
  154. if (!return_value_used) {
  155. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  156. return;
  157. }
  158. prop = solr_read_response_object_property(objptr, "http_digested_response", silent);
  159. if (Z_STRLEN_P(prop))
  160. {
  161. RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop), 1);
  162. }
  163. RETURN_NULL();
  164. }
  165. /* }}} */
  166. /* {{{ proto string SolrReponse::setParseMode([bool parse_mode])
  167. Sets the parsing mode. This determines whether documents will be parsed as SolrObjects or SolrDocuments. */
  168. PHP_METHOD(SolrResponse, setParseMode)
  169. {
  170. long int parse_mode = 0L;
  171. zval *objptr = getThis();
  172. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &parse_mode) == FAILURE) {
  173. RETURN_FALSE;
  174. }
  175. parse_mode = ((parse_mode < 0L) ? 0L : ((parse_mode > 1L) ? 1L : parse_mode));
  176. zend_update_property_long(Z_OBJCE_P(objptr), objptr, "parser_mode", sizeof("parser_mode")-1, parse_mode TSRMLS_CC);
  177. RETURN_TRUE;
  178. }
  179. /* }}} */
  180. /* {{{ proto SolrObject SolrResponse::getResponse(void)
  181. Returns the response object from the server. */
  182. PHP_METHOD(SolrResponse, getResponse)
  183. {
  184. zend_bool silent = 0;
  185. zval *objptr = getThis();
  186. if (return_value_used)
  187. {
  188. zval *response_writer = solr_read_response_object_property(objptr, "response_writer", silent);
  189. zval *raw_response = solr_read_response_object_property(objptr, "http_raw_response", silent);
  190. zval *success = solr_read_response_object_property(objptr, "success", silent);
  191. zval *parser_mode = solr_read_response_object_property(objptr, "parser_mode", silent);
  192. if (Z_BVAL_P(success) && Z_STRLEN_P(raw_response))
  193. {
  194. solr_string_t buffer;
  195. php_unserialize_data_t var_hash;
  196. const unsigned char *raw_resp;
  197. size_t raw_res_length;
  198. const unsigned char *str_end;
  199. int successful = 1;
  200. memset(&buffer, 0, sizeof(solr_string_t));
  201. /* At the moment only xml and phpnative response writers are supported */
  202. if (Z_STRLEN_P(response_writer))
  203. {
  204. if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_XML_RESPONSE_WRITER))
  205. {
  206. /* SOLR_XML_RESPONSE_WRITER */
  207. /* Convert from XML serialization to PHP serialization format */
  208. solr_encode_generic_xml_response(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response), Z_LVAL_P(parser_mode) TSRMLS_CC);
  209. } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_PHP_NATIVE_RESPONSE_WRITER)) {
  210. /* SOLR_PHP_NATIVE_RESPONSE_WRITER */
  211. /* Response string is already in Native PHP serialization format */
  212. solr_string_set(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response));
  213. } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_JSON_RESPONSE_WRITER)) {
  214. int json_translation_result = solr_json_to_php_native(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response) TSRMLS_CC);
  215. /* SOLR_JSON_RESPONSE_WRITER */
  216. /* Convert from JSON serialization to PHP serialization format */
  217. if (json_translation_result > 0)
  218. {
  219. solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, solr_get_json_error_msg(json_translation_result));
  220. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error in JSON->PHP conversion. JSON Error Code %d", json_translation_result);
  221. }
  222. }
  223. }
  224. if (buffer.len)
  225. {
  226. zend_update_property_stringl(Z_OBJCE_P(objptr), objptr, "http_digested_response", sizeof("http_digested_response")-1, buffer.str, buffer.len TSRMLS_CC);
  227. }
  228. memset(&var_hash, 0, sizeof(php_unserialize_data_t));
  229. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  230. raw_resp = (unsigned char *) buffer.str;
  231. raw_res_length = buffer.len;
  232. str_end = (unsigned char *) (raw_resp + raw_res_length);
  233. if (!php_var_unserialize(&return_value, &raw_resp, str_end, &var_hash TSRMLS_CC))
  234. {
  235. successful = 0;
  236. solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);
  237. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response.");
  238. }
  239. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  240. solr_string_free(&buffer);
  241. if (successful)
  242. {
  243. /* Overriding the default object handlers */
  244. Z_OBJ_HT_P(return_value) = &solr_object_handlers;
  245. }
  246. return ;
  247. }
  248. RETURN_NULL();
  249. } else {
  250. php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
  251. }
  252. }
  253. /* }}} */
  254. /* {{{ proto string SolrPingResponse::getResponse(void)
  255. Ping responses are always empty. Returns null. */
  256. PHP_METHOD(SolrPingResponse, getResponse)
  257. {
  258. /* Ping responses are not serialized */
  259. if (return_value_used)
  260. {
  261. RETURN_NULL();
  262. }
  263. }
  264. /* }}} */
  265. /* {{{ proto SolrPingResponse::__construct(void)
  266. Constructor */
  267. PHP_METHOD(SolrPingResponse, __construct)
  268. {
  269. }
  270. /* }}} */
  271. /* {{{ proto SolrPingResponse::__destruct(void)
  272. Destructor */
  273. PHP_METHOD(SolrPingResponse, __destruct)
  274. {
  275. }
  276. /* }}} */
  277. /* {{{ proto SolrQueryResponse::__construct(void)
  278. Constructor */
  279. PHP_METHOD(SolrQueryResponse, __construct)
  280. {
  281. }
  282. /* }}} */
  283. /* {{{ proto SolrQueryResponse::__destruct(void)
  284. Destructor */
  285. PHP_METHOD(SolrQueryResponse, __destruct)
  286. {
  287. }
  288. /* }}} */
  289. /* {{{ proto SolrUpdateResponse::__construct(void)
  290. Constructor */
  291. PHP_METHOD(SolrUpdateResponse, __construct)
  292. {
  293. }
  294. /* }}} */
  295. /* {{{ proto SolrUpdateResponse::__destruct(void)
  296. Destructor */
  297. PHP_METHOD(SolrUpdateResponse, __destruct)
  298. {
  299. }
  300. /* }}} */
  301. /* {{{ proto SolrGenericResponse::__construct(void)
  302. Constructor */
  303. PHP_METHOD(SolrGenericResponse, __construct)
  304. {
  305. }
  306. /* }}} */
  307. /* {{{ proto SolrGenericResponse::__destruct(void)
  308. Destructor */
  309. PHP_METHOD(SolrGenericResponse, __destruct)
  310. {
  311. }
  312. /* }}} */
  313. /*
  314. * Local variables:
  315. * tab-width: 4
  316. * c-basic-offset: 4
  317. * indent-tabs-mode: t
  318. * End:
  319. * vim600: fdm=marker
  320. * vim: noet sw=4 ts=4
  321. */