share.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Pierrick Charron <pierrick@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if HAVE_CURL
  24. #include "php_curl.h"
  25. #include <curl/curl.h>
  26. #define SAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
  27. /* {{{ proto void curl_share_init()
  28. Initialize a share curl handle */
  29. PHP_FUNCTION(curl_share_init)
  30. {
  31. php_curlsh *sh;
  32. if (zend_parse_parameters_none() == FAILURE) {
  33. return;
  34. }
  35. sh = ecalloc(1, sizeof(php_curlsh));
  36. sh->share = curl_share_init();
  37. RETURN_RES(zend_register_resource(sh, le_curl_share_handle));
  38. }
  39. /* }}} */
  40. /* {{{ proto void curl_share_close(resource sh)
  41. Close a set of cURL handles */
  42. PHP_FUNCTION(curl_share_close)
  43. {
  44. zval *z_sh;
  45. php_curlsh *sh;
  46. ZEND_PARSE_PARAMETERS_START(1,1)
  47. Z_PARAM_RESOURCE(z_sh)
  48. ZEND_PARSE_PARAMETERS_END();
  49. if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(z_sh), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
  50. RETURN_FALSE;
  51. }
  52. zend_list_close(Z_RES_P(z_sh));
  53. }
  54. /* }}} */
  55. static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
  56. {
  57. CURLSHcode error = CURLSHE_OK;
  58. switch (option) {
  59. case CURLSHOPT_SHARE:
  60. case CURLSHOPT_UNSHARE:
  61. error = curl_share_setopt(sh->share, option, zval_get_long(zvalue));
  62. break;
  63. default:
  64. php_error_docref(NULL, E_WARNING, "Invalid curl share configuration option");
  65. error = CURLSHE_BAD_OPTION;
  66. break;
  67. }
  68. SAVE_CURLSH_ERROR(sh, error);
  69. if (error != CURLSHE_OK) {
  70. return 1;
  71. } else {
  72. return 0;
  73. }
  74. }
  75. /* }}} */
  76. /* {{{ proto bool curl_share_setopt(resource sh, int option, mixed value)
  77. Set an option for a cURL transfer */
  78. PHP_FUNCTION(curl_share_setopt)
  79. {
  80. zval *zid, *zvalue;
  81. zend_long options;
  82. php_curlsh *sh;
  83. ZEND_PARSE_PARAMETERS_START(3,3)
  84. Z_PARAM_RESOURCE(zid)
  85. Z_PARAM_LONG(options)
  86. Z_PARAM_ZVAL(zvalue)
  87. ZEND_PARSE_PARAMETERS_END();
  88. if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(zid), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
  89. RETURN_FALSE;
  90. }
  91. if (!_php_curl_share_setopt(sh, options, zvalue, return_value)) {
  92. RETURN_TRUE;
  93. } else {
  94. RETURN_FALSE;
  95. }
  96. }
  97. /* }}} */
  98. void _php_curl_share_close(zend_resource *rsrc) /* {{{ */
  99. {
  100. php_curlsh *sh = (php_curlsh *)rsrc->ptr;
  101. if (sh) {
  102. curl_share_cleanup(sh->share);
  103. efree(sh);
  104. rsrc->ptr = NULL;
  105. }
  106. }
  107. /* }}} */
  108. /* {{{ proto int curl_share_errno(resource mh)
  109. Return an integer containing the last share curl error number */
  110. PHP_FUNCTION(curl_share_errno)
  111. {
  112. zval *z_sh;
  113. php_curlsh *sh;
  114. ZEND_PARSE_PARAMETERS_START(1,1)
  115. Z_PARAM_RESOURCE(z_sh)
  116. ZEND_PARSE_PARAMETERS_END();
  117. if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(z_sh), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
  118. RETURN_FALSE;
  119. }
  120. RETURN_LONG(sh->err.no);
  121. }
  122. /* }}} */
  123. /* {{{ proto bool curl_share_strerror(int code)
  124. return string describing error code */
  125. PHP_FUNCTION(curl_share_strerror)
  126. {
  127. zend_long code;
  128. const char *str;
  129. ZEND_PARSE_PARAMETERS_START(1,1)
  130. Z_PARAM_LONG(code)
  131. ZEND_PARSE_PARAMETERS_END();
  132. str = curl_share_strerror(code);
  133. if (str) {
  134. RETURN_STRING(str);
  135. } else {
  136. RETURN_NULL();
  137. }
  138. }
  139. /* }}} */
  140. #endif
  141. /*
  142. * Local variables:
  143. * tab-width: 4
  144. * c-basic-offset: 4
  145. * End:
  146. * vim600: noet sw=4 ts=4 fdm=marker
  147. * vim<600: noet sw=4 ts=4
  148. */