share.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. | Author: Pierrick Charron <pierrick@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #include "curl_private.h"
  22. #include <curl/curl.h>
  23. #define SAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
  24. /* {{{ Initialize a share curl handle */
  25. PHP_FUNCTION(curl_share_init)
  26. {
  27. php_curlsh *sh;
  28. ZEND_PARSE_PARAMETERS_NONE();
  29. object_init_ex(return_value, curl_share_ce);
  30. sh = Z_CURL_SHARE_P(return_value);
  31. sh->share = curl_share_init();
  32. }
  33. /* }}} */
  34. /* {{{ Close a set of cURL handles */
  35. PHP_FUNCTION(curl_share_close)
  36. {
  37. zval *z_sh;
  38. ZEND_PARSE_PARAMETERS_START(1,1)
  39. Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
  40. ZEND_PARSE_PARAMETERS_END();
  41. }
  42. /* }}} */
  43. static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
  44. {
  45. CURLSHcode error = CURLSHE_OK;
  46. switch (option) {
  47. case CURLSHOPT_SHARE:
  48. case CURLSHOPT_UNSHARE:
  49. error = curl_share_setopt(sh->share, option, zval_get_long(zvalue));
  50. break;
  51. default:
  52. zend_argument_value_error(2, "is not a valid cURL share option");
  53. error = CURLSHE_BAD_OPTION;
  54. break;
  55. }
  56. SAVE_CURLSH_ERROR(sh, error);
  57. return error != CURLSHE_OK;
  58. }
  59. /* }}} */
  60. /* {{{ Set an option for a cURL transfer */
  61. PHP_FUNCTION(curl_share_setopt)
  62. {
  63. zval *z_sh, *zvalue;
  64. zend_long options;
  65. php_curlsh *sh;
  66. ZEND_PARSE_PARAMETERS_START(3,3)
  67. Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
  68. Z_PARAM_LONG(options)
  69. Z_PARAM_ZVAL(zvalue)
  70. ZEND_PARSE_PARAMETERS_END();
  71. sh = Z_CURL_SHARE_P(z_sh);
  72. if (!_php_curl_share_setopt(sh, options, zvalue, return_value)) {
  73. RETURN_TRUE;
  74. } else {
  75. RETURN_FALSE;
  76. }
  77. }
  78. /* }}} */
  79. /* {{{ Return an integer containing the last share curl error number */
  80. PHP_FUNCTION(curl_share_errno)
  81. {
  82. zval *z_sh;
  83. php_curlsh *sh;
  84. ZEND_PARSE_PARAMETERS_START(1,1)
  85. Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
  86. ZEND_PARSE_PARAMETERS_END();
  87. sh = Z_CURL_SHARE_P(z_sh);
  88. RETURN_LONG(sh->err.no);
  89. }
  90. /* }}} */
  91. /* {{{ return string describing error code */
  92. PHP_FUNCTION(curl_share_strerror)
  93. {
  94. zend_long code;
  95. const char *str;
  96. ZEND_PARSE_PARAMETERS_START(1,1)
  97. Z_PARAM_LONG(code)
  98. ZEND_PARSE_PARAMETERS_END();
  99. str = curl_share_strerror(code);
  100. if (str) {
  101. RETURN_STRING(str);
  102. } else {
  103. RETURN_NULL();
  104. }
  105. }
  106. /* }}} */
  107. /* CurlShareHandle class */
  108. static zend_object_handlers curl_share_handlers;
  109. static zend_object *curl_share_create_object(zend_class_entry *class_type) {
  110. php_curlsh *intern = zend_object_alloc(sizeof(php_curlsh), class_type);
  111. zend_object_std_init(&intern->std, class_type);
  112. object_properties_init(&intern->std, class_type);
  113. intern->std.handlers = &curl_share_handlers;
  114. return &intern->std;
  115. }
  116. static zend_function *curl_share_get_constructor(zend_object *object) {
  117. zend_throw_error(NULL, "Cannot directly construct CurlShareHandle, use curl_share_init() instead");
  118. return NULL;
  119. }
  120. void curl_share_free_obj(zend_object *object)
  121. {
  122. php_curlsh *sh = curl_share_from_obj(object);
  123. curl_share_cleanup(sh->share);
  124. zend_object_std_dtor(&sh->std);
  125. }
  126. void curl_share_register_handlers(void) {
  127. curl_share_ce->create_object = curl_share_create_object;
  128. memcpy(&curl_share_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  129. curl_share_handlers.offset = XtOffsetOf(php_curlsh, std);
  130. curl_share_handlers.free_obj = curl_share_free_obj;
  131. curl_share_handlers.get_constructor = curl_share_get_constructor;
  132. curl_share_handlers.clone_obj = NULL;
  133. curl_share_handlers.compare = zend_objects_not_comparable;
  134. }