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